diff options
Diffstat (limited to 'platform/src/policy.cpp')
| -rw-r--r-- | platform/src/policy.cpp | 117 |
1 files changed, 103 insertions, 14 deletions
diff --git a/platform/src/policy.cpp b/platform/src/policy.cpp index 3bd756b..776e8d5 100644 --- a/platform/src/policy.cpp +++ b/platform/src/policy.cpp @@ -16,6 +16,8 @@ * Struct policy_enforcer ******************************************************************************/ +#define EFFECTIVE_RANGE_MAX_SIZE 128 + enum input_mode { MAAT_INPUT_JSON = 0, @@ -34,7 +36,8 @@ struct policy_config int deferred_load; int effect_interval_ms; - char device_group[32]; + char data_center[EFFECTIVE_RANGE_MAX_SIZE]; + char device_group[EFFECTIVE_RANGE_MAX_SIZE]; char stat_file[2048]; char table_info[2048]; char accept_tags[2048]; @@ -134,16 +137,28 @@ enum admin_status ADMMIN_STATUS_INACTIVE = 2, }; +enum effective_type +{ + EFFECTIVE_TYPE_DEVICE_GROUP = 0x1, + EFFECTIVE_TYPE_DATA_CENTER = 0x2, +}; + +struct effective_range +{ + enum effective_type type; + char value[EFFECTIVE_RANGE_MAX_SIZE]; +}; + struct sf_param { int sf_vsys_id; int sf_profile_id; int sf_ref_cnt; - char sf_device_group[32]; enum admin_status sf_admin_status; struct connectivity sf_connectivity; struct health_check sf_health_check; + struct effective_range sf_effective_range; uint64_t health_check_session_id; }; @@ -152,6 +167,19 @@ struct sf_param * Private API ******************************************************************************/ +static const char *effective_type_to_string(enum effective_type type) +{ + switch (type) + { + case EFFECTIVE_TYPE_DEVICE_GROUP: + return "device_group"; + case EFFECTIVE_TYPE_DATA_CENTER: + return "data_center"; + default: + return "unknown"; + } +} + static const char *admin_status_to_string(enum admin_status admin_status) { switch (admin_status) @@ -166,7 +194,7 @@ static const char *admin_status_to_string(enum admin_status admin_status) } // {"tags":[{"tag":"device_group","value":"group-xxg-9140"},{"tag":"data_center","value":"center-xxg-9140"}]} -static void parser_device_group(const char *accept_tags, char *buffer) +static void parser_effective_range(const char *accept_tags, char *data_center, char *device_group) { cJSON *json; cJSON *tags; @@ -206,8 +234,14 @@ static void parser_device_group(const char *accept_tags, char *buffer) if (strcasecmp(item_key->valuestring, "device_group") == 0) { - memcpy(buffer, item_val->valuestring, strlen(item_val->valuestring)); - break; + memset(device_group, 0, EFFECTIVE_RANGE_MAX_SIZE); + memcpy(device_group, item_val->valuestring, MIN(strlen(item_val->valuestring), EFFECTIVE_RANGE_MAX_SIZE)); + } + + if (strcasecmp(item_key->valuestring, "data_center") == 0) + { + memset(data_center, 0, EFFECTIVE_RANGE_MAX_SIZE); + memcpy(data_center, item_val->valuestring, MIN(strlen(item_val->valuestring), EFFECTIVE_RANGE_MAX_SIZE)); } } @@ -247,7 +281,7 @@ static void policy_enforcer_config(const char *profile, struct policy_config *co if (strlen(config->accept_path)) { MESA_load_profile_string_def(config->accept_path, "MAAT", "accept_tags", config->accept_tags, sizeof(config->accept_tags), "{\"tags\":[{\"tag\":\"device_id\",\"value\":\"device_1\"}]}"); - parser_device_group(config->accept_tags, config->device_group); + parser_effective_range(config->accept_tags, config->data_center, config->device_group); } LOG_DEBUG("%s: MAAT->input_mode : %s", LOG_TAG_POLICY, (config->input_mode == MAAT_INPUT_REDIS ? "redis" : (config->input_mode == MAAT_INPUT_JSON ? "json" : (config->input_mode == MAAT_INPUT_FILE ? "file" : "unknown")))); @@ -263,6 +297,7 @@ static void policy_enforcer_config(const char *profile, struct policy_config *co LOG_DEBUG("%s: MAAT->accept_path : %s", LOG_TAG_POLICY, config->accept_path); LOG_DEBUG("%s: MAAT->accept_tags : %s", LOG_TAG_POLICY, config->accept_tags); LOG_DEBUG("%s: MAAT->device_group : %s", LOG_TAG_POLICY, config->device_group); + LOG_DEBUG("%s: MAAT->data_center : %s", LOG_TAG_POLICY, config->data_center); LOG_DEBUG("%s: MAAT->inc_cfg_dir : %s", LOG_TAG_POLICY, config->inc_cfg_dir); LOG_DEBUG("%s: MAAT->ful_cfg_dir : %s", LOG_TAG_POLICY, config->ful_cfg_dir); LOG_DEBUG("%s: MAAT->json_cfg_file : %s", LOG_TAG_POLICY, config->json_cfg_file); @@ -673,17 +708,18 @@ static void sff_param_free(struct sff_param *param) static void sf_param_new_cb(const char *table_name, int table_id, const char *key, const char *table_line, void **ad, long argl, void *argp) { struct sf_param *param = NULL; + cJSON *root0 = NULL; cJSON *root1 = NULL; cJSON *root2 = NULL; cJSON *item = NULL; + int vsys_id = 0; + int is_valid = 0; int profile_id = 0; - char device_group[32] = {0}; int admin_status = 0; char connectivity[128] = {0}; char health_check[128] = {0}; - int vsys_id = 0; - int is_valid = 0; + char device_group[EFFECTIVE_RANGE_MAX_SIZE] = {0}; if (sscanf(table_line, "%d\t%s\t%d\t%s\t%s\t%d\t%d", &profile_id, device_group, &admin_status, connectivity, health_check, &vsys_id, &is_valid) != 7) @@ -696,8 +732,41 @@ static void sf_param_new_cb(const char *table_name, int table_id, const char *ke param->sf_vsys_id = vsys_id; param->sf_profile_id = atoi(key); param->sf_ref_cnt = 1; - memcpy(param->sf_device_group, device_group, strlen(device_group)); - LOG_DEBUG("%s: parse sf profile: %d, device_group: %s", LOG_TAG_POLICY, param->sf_profile_id, param->sf_device_group); + + // device_group + root0 = cJSON_Parse(device_group); + if (root0 == NULL) + { + LOG_ERROR("%s: unexpected sf profile: (invalid device_group param) %s", LOG_TAG_POLICY, table_line); + goto error_out; + } + item = cJSON_GetObjectItem(root0, "tag"); + if (!item || !cJSON_IsString(item)) + { + LOG_ERROR("%s: unexpected sf profile: (invalid device_group->tag param) %s", LOG_TAG_POLICY, table_line); + goto error_out; + } + if (0 == strcasecmp(item->valuestring, "device_group")) + { + param->sf_effective_range.type = EFFECTIVE_TYPE_DEVICE_GROUP; + } + else if (0 == strcasecmp(item->valuestring, "data_center")) + { + param->sf_effective_range.type = EFFECTIVE_TYPE_DATA_CENTER; + } + else + { + LOG_ERROR("%s: unexpected sf profile: (invalid device_group->tag param) %s", LOG_TAG_POLICY, table_line); + goto error_out; + } + item = cJSON_GetObjectItem(root0, "value"); + if (!item || !cJSON_IsString(item)) + { + LOG_ERROR("%s: unexpected sf profile: (invalid device_group->value param) %s", LOG_TAG_POLICY, table_line); + goto error_out; + } + memcpy(param->sf_effective_range.value, item->valuestring, MIN(strlen(item->valuestring), EFFECTIVE_RANGE_MAX_SIZE)); + LOG_DEBUG("%s: parse sf profile: %d, device_group->tag: %s, device_group->value: %s", LOG_TAG_POLICY, param->sf_profile_id, effective_type_to_string(param->sf_effective_range.type), param->sf_effective_range.value); // admin_status switch (admin_status) @@ -858,11 +927,18 @@ static void sf_param_new_cb(const char *table_name, int table_id, const char *ke *ad = param; LOG_INFO("%s: Add sf profile: %d", LOG_TAG_POLICY, param->sf_profile_id); + cJSON_Delete(root0); cJSON_Delete(root1); cJSON_Delete(root2); return; error_out: + if (root0) + { + cJSON_Delete(root0); + root0 = NULL; + } + if (root1) { cJSON_Delete(root1); @@ -939,11 +1015,24 @@ static void select_sf_by_nearby_and_adminstatus(struct policy_enforcer *enforcer if (sff_param->sff_ldbc.localiza == LDBC_LOCALIZATION_NEARBY) { - if (strcasecmp(enforcer->config.device_group, sf->sf_device_group) == 0) + if (sf->sf_effective_range.type == EFFECTIVE_TYPE_DEVICE_GROUP) { - if (sf->sf_admin_status == ADMMIN_STATUS_ACTIVE) + if (strcasecmp(enforcer->config.device_group, sf->sf_effective_range.value) == 0) { - fixed_num_array_add_elem(array, sff_param->sf_profile_ids[i]); + if (sf->sf_admin_status == ADMMIN_STATUS_ACTIVE) + { + fixed_num_array_add_elem(array, sff_param->sf_profile_ids[i]); + } + } + } + else + { + if (strcasecmp(enforcer->config.data_center, sf->sf_effective_range.value) == 0) + { + if (sf->sf_admin_status == ADMMIN_STATUS_ACTIVE) + { + fixed_num_array_add_elem(array, sff_param->sf_profile_ids[i]); + } } } } |
