Add VES stndDefined PM and subscription for O-DU.
[sim/o1-interface.git] / ntsimulator / ntsim-ng / core / datastore / populate_aux.c
1 /*************************************************************************
2 *
3 * Copyright 2021 highstreet technologies GmbH and others
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 ***************************************************************************/
17 #define _GNU_SOURCE
18
19 #include "populate_internal.h"
20
21 #include "core/session.h"
22 #include "core/framework.h"
23 #include "utils/sys_utils.h"
24 #include "utils/log_utils.h"
25 #include "utils/type_utils.h"
26 #include "utils/rand_utils.h"
27
28 #include <assert.h>
29 #include <stdlib.h>
30
31 struct lyd_node *datastore_load_external(const char *filename, bool operational) {
32     struct lyd_node *data_tree = 0;
33     if(filename) {
34         if(file_exists(filename)) {
35             LYD_FORMAT format = LYD_JSON;
36             if(strstr(filename, ".xml") != 0) {
37                 format = LYD_XML;
38             }
39
40             int flags = LYD_OPT_TRUSTED | LYD_OPT_NOSIBLINGS;
41             if(operational) {
42                 flags |= LYD_OPT_DATA;
43             }
44             else {
45                 flags |= LYD_OPT_CONFIG;
46             }
47
48             data_tree = lyd_parse_path(session_context, filename, format, flags);
49             if(data_tree == 0) {
50                 log_error("lyd_parse_path failed\n");
51             }
52         }
53     }
54
55     return data_tree;
56 }
57
58 char populate_info_get_mandatory(const struct lys_node *schema) {
59     assert(schema);
60
61     char mandatory = ' ';
62     if((schema->flags & LYS_MAND_TRUE) != 0) {
63         mandatory = 'M';
64     }
65     if((schema->parent) && (schema->parent->nodetype == LYS_CASE)) {
66         if((schema->parent->flags & LYS_MAND_TRUE) != 0) {
67             mandatory = 'M';
68         }
69     }
70
71     return mandatory;
72 }
73
74 const char* populate_leafref_test_val(int index) {
75     switch(index) {
76         case 0:
77             return "1";
78             break;
79
80         case 1:
81             return "1.1.1.1";
82             break;
83
84         case 2:
85             return "Fd:4D:63:A5:21:C5";
86             break;
87
88         case 3:
89             return "";
90             break;
91
92         case 4:
93             return "::1";
94             break;
95
96         case 5:
97             return "false";
98             break;
99
100         case 6:
101             return "TDD";
102             break;
103
104         case 7:
105             return "NR";
106             break;
107
108         case 8:
109             return "best-effort";
110             break;
111
112         case 9:
113             return "yes-fault:o-ran-sc-alarm-type";
114             break;
115
116         case 10:
117             return "";
118             break;
119
120         default:
121             log_error("index out of bounds\n");
122             return 0;
123             break;
124     }
125 }
126
127 int populate_instance_add_module(populate_instance_t *instance, const struct lys_module *module) {
128     assert(module);
129     assert(instance);
130
131     for(int i = 0; i < instance->mod_count; i++) {
132         if(instance->modules[i] == module) {
133             return NTS_ERR_OK;
134         }
135     }
136
137     instance->modules = (const struct lys_module **)realloc(instance->modules, sizeof(const struct lys_module *) * (instance->mod_count + 1));
138     if(!instance->modules) {
139         log_error("bad realloc\n");
140         return NTS_ERR_FAILED;
141     }
142     instance->modules[instance->mod_count] = module;
143     instance->mod_count++;
144
145     return NTS_ERR_OK;
146 }
147
148 int populate_instance_get_count(const char *path) {
149     assert(path);
150
151     for(int i = 0; i < framework_config.datastore_generate.custom_list_instances_count; i++) {
152         if(strcmp(path, framework_config.datastore_generate.custom_list_instances[i].path) == 0) {
153             return framework_config.datastore_generate.custom_list_instances[i].count;
154         }
155     }
156     return framework_config.datastore_generate.default_list_instances;
157 }
158
159 char *populate_get_restrict_schema(const char *path) {
160     assert(path);
161     char *ret = 0;
162
163     for(int i = 0; i < framework_config.datastore_generate.restrict_schema_count; i++) {
164         if(strcmp(path, framework_config.datastore_generate.restrict_schema[i].path) == 0) {
165             ret = strdup(framework_config.datastore_generate.restrict_schema[i].values[framework_config.datastore_generate.restrict_schema[i].index]);
166             framework_config.datastore_generate.restrict_schema[i].index++;
167             if(framework_config.datastore_generate.restrict_schema[i].index >= framework_config.datastore_generate.restrict_schema[i].values_count) {
168                 framework_config.datastore_generate.restrict_schema[i].index = 0;
169             }
170             break;
171         }
172     }
173
174     return ret;
175 }