2023-01-30 21:59:35 +08:00
|
|
|
/*
|
|
|
|
|
**********************************************************************************************
|
|
|
|
|
* File: maat_virtual.cpp
|
|
|
|
|
* Description:
|
|
|
|
|
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
|
|
|
|
|
* Date: 2022-10-31
|
|
|
|
|
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
|
|
|
|
|
***********************************************************************************************
|
|
|
|
|
*/
|
|
|
|
|
|
2023-02-07 11:25:31 +08:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
2023-01-30 21:59:35 +08:00
|
|
|
#include "maat_kv.h"
|
|
|
|
|
#include "maat_utils.h"
|
|
|
|
|
#include "log/log.h"
|
|
|
|
|
#include "maat_virtual.h"
|
2023-02-07 11:25:31 +08:00
|
|
|
#include "maat_rule.h"
|
|
|
|
|
#include "maat_table.h"
|
2023-01-30 21:59:35 +08:00
|
|
|
|
|
|
|
|
#define MODULE_VIRTUAL module_name_str("maat.virtual")
|
|
|
|
|
|
|
|
|
|
struct virtual_schema {
|
|
|
|
|
int physical_table_id[SCAN_TYPE_MAX];
|
2023-02-03 17:28:14 +08:00
|
|
|
int table_id;
|
2023-02-09 22:13:15 +08:00
|
|
|
struct table_manager *ref_tbl_mgr;
|
2023-01-30 21:59:35 +08:00
|
|
|
};
|
|
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
void *virtual_schema_new(cJSON *json, struct table_manager *tbl_mgr,
|
|
|
|
|
const char *table_name, struct log_handle *logger)
|
2023-01-30 21:59:35 +08:00
|
|
|
{
|
|
|
|
|
cJSON *item = cJSON_GetObjectItem(json, "physical_table");
|
|
|
|
|
if (NULL == item || item->type != cJSON_Array) {
|
2023-02-03 17:28:14 +08:00
|
|
|
log_error(logger, MODULE_VIRTUAL,
|
|
|
|
|
"virtual table %s has no physical_table column", table_name);
|
2023-01-30 21:59:35 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2023-01-31 20:39:53 +08:00
|
|
|
|
|
|
|
|
struct virtual_schema *vt_schema = ALLOC(struct virtual_schema, 1);
|
2023-02-09 22:13:15 +08:00
|
|
|
vt_schema->ref_tbl_mgr = tbl_mgr;
|
2023-02-07 11:25:31 +08:00
|
|
|
|
2023-01-30 21:59:35 +08:00
|
|
|
int cnt = cJSON_GetArraySize(item);
|
|
|
|
|
for (int i = 0; i < cnt; i++) {
|
|
|
|
|
cJSON *tmp_item = cJSON_GetArrayItem(item, i);
|
|
|
|
|
if (tmp_item != NULL && tmp_item->type == cJSON_String) {
|
2023-02-07 11:25:31 +08:00
|
|
|
int table_id = table_manager_get_table_id(tbl_mgr, tmp_item->valuestring);
|
2023-01-30 21:59:35 +08:00
|
|
|
/* physical table should already exist */
|
2023-02-07 11:25:31 +08:00
|
|
|
if (table_id < 0) {
|
|
|
|
|
log_error(logger, MODULE_VIRTUAL, "table:%s is not registered",
|
|
|
|
|
tmp_item->valuestring);
|
|
|
|
|
FREE(vt_schema);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2023-01-30 21:59:35 +08:00
|
|
|
|
2023-02-07 11:25:31 +08:00
|
|
|
enum table_type table_type = table_manager_get_table_type(tbl_mgr, table_id);
|
|
|
|
|
assert(table_type != TABLE_TYPE_INVALID);
|
|
|
|
|
enum scan_type scan_type = maat_table_get_scan_type(table_type);
|
|
|
|
|
assert(scan_type != SCAN_TYPE_INVALID);
|
|
|
|
|
vt_schema->physical_table_id[scan_type]= table_id;
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return vt_schema;
|
|
|
|
|
}
|
2023-01-31 20:39:53 +08:00
|
|
|
|
|
|
|
|
void virtual_schema_free(void *virtual_schema)
|
|
|
|
|
{
|
|
|
|
|
FREE(virtual_schema);
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int virtual_table_get_physical_table_id(void *virtual_schema, enum scan_type type)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == virtual_schema) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct virtual_schema *schema = (struct virtual_schema *)virtual_schema;
|
|
|
|
|
return schema->physical_table_id[type];
|
2023-01-31 20:39:53 +08:00
|
|
|
}
|