-
Notifications
You must be signed in to change notification settings - Fork 6
/
yang-library.c
224 lines (199 loc) · 7.12 KB
/
yang-library.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
* @file yang-library.c
*
* Copyright 2023, Allied Telesis Labs New Zealand, Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "internal.h"
#include "models/ietf-yang-library.h"
#include "models/ietf-restconf-monitoring.h"
/* Name for the set of modules */
#define MODULES_STR "modules"
#define SCHEMA_STR "schema"
#define DATASTORE_STR "datastore"
#define COMMON_STR "common"
/* List of supported capabilities. This is hard-coded for now, if we add a
* capability in the code, we need to update this table. Null terminate it
* for ease of traversal.
* Ref: http://www.iana.org/assignments/restconf-capability-urns/restconf-capability-urns.xhtml
* Commented capabilities are those defined but not yet supported.
*/
char *restconf_capabilities[] =
{
"urn:ietf:params:restconf:capability:defaults:1.0?basic-mode=explicit",
"urn:ietf:params:restconf:capability:depth:1.0",
"urn:ietf:params:restconf:capability:fields:1.0",
// "urn:ietf:params:restconf:capability:filter:1.0",
// "urn:ietf:params:restconf:capability:replay:1.0",
"urn:ietf:params:restconf:capability:with-defaults:1.0",
// "urn:ietf:params:restconf:capability:yang-patch:1.0", /* [RFC8072] */
// "urn:ietf:params:restconf:capability:with-origin:1.0", /* [RFC8527] */
// "urn:ietf:params:restconf:capability:with-operational-defaults:1.0", /* [RFC8527] */
NULL
};
/*
* A wrapper for APTERYX_LEAF.
* @param root - The node the leaf will be added to
* @param node_name - Name of the new node
* @param value - Value to set the node to, or NULL to delete the leaf
*/
static GNode *
add_leaf (GNode *root, char *node_name, char *value)
{
GNode *child_node = NULL;
assert (root);
if (value == NULL)
{
value = g_strdup ("");
}
child_node = APTERYX_LEAF (root, (gpointer) node_name, (gpointer) value);
return child_node;
}
/**
* Add a leaf to a node
*
* @param root - The node the leaf will be added to
* @param node_name - Name of the new node
* @param value - Value to set the node to, or NULL to delete the leaf
*
* @return a pointer to the leaf, or NULL for failure
*/
static GNode *
add_leaf_strdup (GNode *root, const char *node_name, const char *value)
{
if (root && node_name)
{
return add_leaf (root, g_strdup (node_name), g_strdup (value));
}
return NULL;
}
/**
* Add an entry to the apteryx database for each model known to resconf
*
* @param root - The node the leaf will be added to
*/
static void
schema_set_model_information (sch_instance *schema, GNode *root)
{
GNode *gnode;
sch_loaded_model *loaded;
GList *list;
GList *loaded_models = sch_get_loaded_models (schema);
for (list = g_list_first (loaded_models); list; list = g_list_next (list))
{
loaded = list->data;
if (loaded->model && strlen (loaded->model))
{
gnode = add_leaf_strdup (root, YANG_LIBRARY_MODULE_SET_MODULE_PATH,
loaded->model);
add_leaf_strdup (gnode, MODULES_STATE_MODULE_NAME, loaded->model);
if (loaded->version)
{
add_leaf_strdup (gnode, MODULES_STATE_MODULE_REVISION,
loaded->version);
}
if (loaded->ns_href)
{
add_leaf_strdup (gnode, MODULES_STATE_MODULE_NAMESPACE, loaded->ns_href);
}
if (loaded->features)
{
gchar **split;
int count;
int i;
split = g_strsplit (loaded->features, ",", 0);
count = g_strv_length (split);
for (i = 0; i < count; i++)
{
char *feature_path;
feature_path =
g_strdup_printf ("%s/%s", YANG_LIBRARY_MODULE_SET_MODULE_FEATURE, split[i]);
add_leaf_strdup (gnode, feature_path, split[i]);
g_free (feature_path);
}
g_strfreev (split);
}
if (loaded->deviations)
{
gchar **split;
int count;
int i;
split = g_strsplit (loaded->deviations, ",", 0);
count = g_strv_length (split);
for (i = 0; i < count; i++)
{
char *deviation_path;
deviation_path =
g_strdup_printf ("%s/%s", YANG_LIBRARY_MODULE_SET_MODULE_DEVIATION, split[i]);
add_leaf_strdup (gnode, deviation_path, split[i]);
g_free (deviation_path);
}
g_strfreev (split);
}
}
}
}
/**
* Given a schema create the Apteryx data for the ietf-yang-library model required
* by restconf.
*
* @param g_schema - The root schema xml node
*/
void
yang_library_create (sch_instance *schema)
{
GNode *root;
GNode *modules;
GNode *datastore;
GNode *tmp;
GNode *sch_tmp;
time_t now = time (NULL);
char set_id[24];
root = APTERYX_NODE (NULL, g_strdup (YANG_LIBRARY_PATH));
modules = add_leaf_strdup (root, YANG_LIBRARY_SCHEMA_MODULE_SET, COMMON_STR);
add_leaf_strdup (modules, YANG_LIBRARY_MODULE_SET_NAME, COMMON_STR);
schema_set_model_information (schema, modules);
tmp = add_leaf_strdup (root, SCHEMA_STR, SCHEMA_STR);
add_leaf_strdup (tmp, YANG_LIBRARY_SCHEMA_NAME, COMMON_STR);
sch_tmp = add_leaf_strdup (tmp, YANG_LIBRARY_SCHEMA_MODULE_SET, COMMON_STR);
add_leaf_strdup (sch_tmp, COMMON_STR, COMMON_STR);
datastore = add_leaf_strdup (root, DATASTORE_STR, DATASTORE_STR);
add_leaf_strdup (datastore, YANG_LIBRARY_DATASTORE_NAME, "ietf-datastores:running");
add_leaf_strdup (datastore, YANG_LIBRARY_DATASTORE_SCHEMA, COMMON_STR);
apteryx_set_tree (root);
apteryx_free_tree (root);
/* Each time this routine is run the content-id will be set to a unique id based
* on the clock */
snprintf (set_id, sizeof (set_id), "%" PRIx64 "", now);
apteryx_set (YANG_LIBRARY_CONTENT_ID, set_id);
}
/**
* Create the Apteryx data for the ietf-restconf-monitoring model required by restconf.
*
* @param g_schema - The root schema xml node
*/
void
restconf_monitoring_create (sch_instance *schema)
{
GNode *root;
char **cap = restconf_capabilities;
root = APTERYX_NODE (NULL, g_strdup (RESTCONF_STATE_CAPABILITIES_CAPABILITY));
for (; *cap != NULL; cap++)
{
add_leaf_strdup (root, *cap, *cap);
}
apteryx_set_tree (root);
apteryx_free_tree (root);
}