-
Notifications
You must be signed in to change notification settings - Fork 1
/
yang.c
182 lines (167 loc) · 5.47 KB
/
yang.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
/**
* @file yang.c
* Yang based database schema support for Apteryx.
*
* Copyright 2019, Allied Telesis Labs New Zealand, Ltd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>
*/
#include "internal.h"
#include <libyang/libyang.h>
/* Convert an YANG node to apteryx_schema_node */
static struct apteryx_schema_node *
yang_to_node (const struct lys_node *yang, int depth)
{
struct apteryx_schema_node *node;
struct apteryx_schema_node *rnode;
/* NULL */
if (!yang)
{
ERROR ("YANG: Node NULL\n");
return NULL;
}
/* Create a new node */
node = node_create (yang->name);
/* Description */
if (yang->dsc)
{
node->description = g_strdup (yang->dsc);
}
/* Parse node specific values */
switch (yang->nodetype)
{
case LYS_LEAF:
{
struct lys_node_leaf *leaf = (struct lys_node_leaf *) yang;
node->flags |= NODE_FLAGS_LEAF;
if (leaf->dflt)
node->defvalue = g_strdup (leaf->dflt);
switch (leaf->type.base)
{
case LY_TYPE_STRING:
break;
case LY_TYPE_ENUM:
{
for (int i = 0; i < leaf->type.info.enums.count; i++)
{
struct lys_type_enum *enm = &leaf->type.info.enums.enm[i];
struct apteryx_schema_node *child = node_create (enm->name);
child->flags |= NODE_FLAGS_ENUM;
if (enm->dsc)
{
child->description = g_strdup (enm->dsc);
}
child->value = g_strdup_printf ("%d", (enm->value));
node->children = g_list_append (node->children, child);
}
break;
}
default:
ERROR ("YANG: Node \"%s\" data type \"%d\" not supported\n",
yang->name, leaf->type.base);
node_destroy (node);
return NULL;
}
break;
}
case LYS_CONTAINER:
case LYS_LEAFLIST:
case LYS_LIST:
break;
default:
ERROR ("YANG: Node \"%s\" type \"%d\" not supported\n",
yang->name, yang->nodetype);
node_destroy (node);
return NULL;
}
/* Pattern */
//TODO node->pattern
/* Flags */
if (yang->nodetype == LYS_LEAF)
{
if (yang->flags & LYS_CONFIG_W)
node->flags |= (NODE_FLAGS_READ | NODE_FLAGS_WRITE);
if (yang->flags & LYS_CONFIG_R)
node->flags |= NODE_FLAGS_READ;
//TODO write-only
//TODO hidden
//TODO config
}
/* Check for lists */
rnode = node;
if (yang->nodetype == LYS_LIST || yang->nodetype == LYS_LEAFLIST)
{
node = node_create ("*");
node->description = g_strdup ("List entry");
if (yang->nodetype == LYS_LEAFLIST)
{
node->flags |= NODE_FLAGS_LEAF;
if (yang->flags & LYS_CONFIG_W)
node->flags |= (NODE_FLAGS_READ | NODE_FLAGS_WRITE);
if (yang->flags & LYS_CONFIG_R)
node->flags |= NODE_FLAGS_READ;
}
rnode->children = g_list_append (rnode->children, node);
depth += 1;
}
/* Process children */
for (struct lys_node *child = yang->child; child; child = child->next)
{
struct apteryx_schema_node *cn = yang_to_node (child, depth + 1);
if (cn)
node->children = g_list_append (node->children, cn);
}
return rnode;
}
/* Load an Apteryx schema in XML format */
struct apteryx_schema_node *
yang_schema_load (const char *filename, char **name, char **organization, char **version)
{
struct apteryx_schema_node *root = NULL;
struct ly_ctx *ctx;
const struct lys_module *mod;
const struct lys_node *node;
/* Create a new context to load this module with */
ctx = ly_ctx_new (NULL, 0);
if (!ctx)
{
ERROR ("YANG: Failed to create context.\n");
return NULL;
}
/* Parse */
mod = lys_parse_path (ctx, (const char *) filename, LYS_IN_YANG);
if (!mod)
{
ERROR ("YANG: Failed to parse file\n");
ly_ctx_destroy (ctx, NULL);
return NULL;
}
/* Model attributes */
*name = g_strdup (mod->name);
*organization = g_strdup (mod->org);
if (mod->rev_size)
*version = g_strdup (mod->rev[0].date);
/* Get root node */
node = lys_getnext (NULL, NULL, mod, LYS_GETNEXT_NOSTATECHECK);
if (!node || !node->name)
{
ERROR ("YANG: No root node\n");
ly_ctx_destroy (ctx, NULL);
return NULL;
}
/* Convert to apteryx_schema_node */
root = yang_to_node (node, 0);
ly_ctx_destroy (ctx, NULL);
return root;
}