Skip to content

Commit b08a922

Browse files
committed
sonic-swss-common: port to libyang3
Port from libyang1 to libyang3. This depends on changes to sonic-buildimage. Signed-off-by: Brad House (@bradh352)
1 parent eb30bb7 commit b08a922

File tree

2 files changed

+57
-39
lines changed

2 files changed

+57
-39
lines changed

common/defaultvalueprovider.cpp

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ bool TableInfoMultipleList::FoundFieldMappingByKey(const string &key, FieldDefau
127127
return keySchema != m_defaultValueMapping.end();
128128
}
129129

130-
shared_ptr<KeySchema> DefaultValueHelper::GetKeySchema(struct lys_node* tableChildNode)
130+
shared_ptr<KeySchema> DefaultValueHelper::GetKeySchema(const struct lysc_node* tableChildNode)
131131
{
132132
SWSS_LOG_DEBUG("DefaultValueHelper::GetKeySchema %s\n",tableChildNode->name);
133133

@@ -138,15 +138,20 @@ shared_ptr<KeySchema> DefaultValueHelper::GetKeySchema(struct lys_node* tableChi
138138
SWSS_LOG_DEBUG("Child list: %s\n",tableChildNode->name);
139139

140140
// when a top level container contains list, the key defined by the 'keys' field.
141-
struct lys_node_list *listNode = (struct lys_node_list*)tableChildNode;
142-
if (listNode->keys_str == nullptr)
141+
const struct lysc_node_list *listNode = (const struct lysc_node_list*)tableChildNode;
142+
143+
for (const struct lysc_node *node = listNode->child; node != NULL; node = node->next)
143144
{
144-
SWSS_LOG_ERROR("Ignore empty key string on list: %s\n",tableChildNode->name);
145-
return nullptr;
145+
if (!lysc_is_key(node)) {
146+
continue;
147+
}
148+
if (keyValue.length())
149+
{
150+
keyValue += " ";
151+
}
152+
keyValue += node->name;
153+
keyFieldCount++;
146154
}
147-
148-
string key(listNode->keys_str);
149-
keyFieldCount = (int)count(key.begin(), key.end(), ' ') + 1;
150155
}
151156
else if (tableChildNode->nodetype == LYS_CONTAINER)
152157
{
@@ -164,16 +169,16 @@ shared_ptr<KeySchema> DefaultValueHelper::GetKeySchema(struct lys_node* tableChi
164169
return make_shared<KeySchema>(keyValue, keyFieldCount);
165170
}
166171

167-
void DefaultValueHelper::GetDefaultValueInfoForLeaf(struct lys_node_leaf* leafNode, shared_ptr<FieldDefaultValueMapping> fieldMapping)
172+
void DefaultValueHelper::GetDefaultValueInfoForLeaf(const struct lysc_node_leaf* leafNode, shared_ptr<FieldDefaultValueMapping> fieldMapping)
168173
{
169174
if (leafNode->dflt)
170175
{
171-
SWSS_LOG_DEBUG("field: %s, default: %s\n",leafNode->name, leafNode->dflt);
172-
fieldMapping->emplace(string(leafNode->name), string(leafNode->dflt));
176+
SWSS_LOG_DEBUG("field: %s, default: %s\n",leafNode->name, lyd_value_get_canonical(leafNode->module->ctx, leafNode->dflt));
177+
fieldMapping->emplace(string(leafNode->name), string(lyd_value_get_canonical(leafNode->module->ctx, leafNode->dflt)));
173178
}
174179
}
175180

176-
void DefaultValueHelper::GetDefaultValueInfoForChoice(struct lys_node_choice* choiceNode, shared_ptr<FieldDefaultValueMapping> fieldMapping)
181+
void DefaultValueHelper::GetDefaultValueInfoForChoice(const struct lysc_node_choice* choiceNode, shared_ptr<FieldDefaultValueMapping> fieldMapping)
177182
{
178183
if (choiceNode->dflt == nullptr)
179184
{
@@ -193,61 +198,69 @@ void DefaultValueHelper::GetDefaultValueInfoForChoice(struct lys_node_choice* ch
193198

194199
SWSS_LOG_DEBUG("default choice leaf field: %s\n",fieldInChoice->name);
195200
WARNINGS_NO_CAST_ALIGN
196-
struct lys_node_leaf *dfltLeafNode = reinterpret_cast<struct lys_node_leaf*>(fieldInChoice);
201+
struct lysc_node_leaf *dfltLeafNode = reinterpret_cast<struct lysc_node_leaf*>(fieldInChoice);
197202
WARNINGS_RESET
198203
if (dfltLeafNode->dflt)
199204
{
200-
SWSS_LOG_DEBUG("default choice leaf field: %s, default: %s\n",dfltLeafNode->name, dfltLeafNode->dflt);
201-
fieldMapping->emplace(string(fieldInChoice->name), string(dfltLeafNode->dflt));
205+
SWSS_LOG_DEBUG("default choice leaf field: %s, default: %s\n",dfltLeafNode->name, lyd_value_get_canonical(dfltLeafNode->module->ctx, dfltLeafNode->dflt));
206+
fieldMapping->emplace(string(fieldInChoice->name), string(lyd_value_get_canonical(dfltLeafNode->module->ctx, dfltLeafNode->dflt)));
202207
}
203208

204209
fieldInChoice = fieldInChoice->next;
205210
}
206211
}
207212

208-
void DefaultValueHelper::GetDefaultValueInfoForLeaflist(struct lys_node_leaflist *listNode, shared_ptr<FieldDefaultValueMapping> fieldMapping)
213+
void DefaultValueHelper::GetDefaultValueInfoForLeaflist(const struct lysc_node_leaflist *listNode, shared_ptr<FieldDefaultValueMapping> fieldMapping)
209214
{
215+
size_t i;
210216
// Get leaf-list default value according to:https://www.rfc-editor.org/rfc/rfc7950.html#section-7.7
211-
if (listNode->dflt == nullptr)
217+
if (listNode->dflts == nullptr)
212218
{
213219
return;
214220
}
215221

216-
const char** dfltValues = listNode->dflt;
222+
/* Convert to null-terminated array of const char * */
223+
const char **dfltValues = (const char **)malloc((LY_ARRAY_COUNT(listNode->dflts)+1) * sizeof(*dfltValues));
224+
for (i=0; i<LY_ARRAY_COUNT(listNode->dflts); i++) {
225+
dfltValues[i] = lyd_value_get_canonical(listNode->module->ctx, listNode->dflts[i]);
226+
}
227+
dfltValues[LY_ARRAY_COUNT(listNode->dflts)] = NULL;
228+
217229
//convert list default value to json string
218230
string dfltValueJson = JSon::buildJson(dfltValues);
231+
free(dfltValues);
219232
SWSS_LOG_DEBUG("list field: %s, default: %s\n",listNode->name, dfltValueJson.c_str());
220233
fieldMapping->emplace(string(listNode->name), dfltValueJson);
221234
}
222235

223-
FieldDefaultValueMappingPtr DefaultValueHelper::GetDefaultValueInfo(struct lys_node* tableChildNode)
236+
FieldDefaultValueMappingPtr DefaultValueHelper::GetDefaultValueInfo(const struct lysc_node* tableChildNode)
224237
{
225238
SWSS_LOG_DEBUG("DefaultValueHelper::GetDefaultValueInfo %s\n",tableChildNode->name);
226239

227-
auto field = tableChildNode->child;
240+
auto field = lysc_node_child(tableChildNode);
228241
auto fieldMapping = make_shared<FieldDefaultValueMapping>();
229242
while (field)
230243
{
231244
if (field->nodetype == LYS_LEAF)
232245
{
233246
WARNINGS_NO_CAST_ALIGN
234-
struct lys_node_leaf *leafNode = reinterpret_cast<struct lys_node_leaf*>(field);
247+
const struct lysc_node_leaf *leafNode = reinterpret_cast<const struct lysc_node_leaf*>(field);
235248
WARNINGS_RESET
236249

237250
SWSS_LOG_DEBUG("leaf field: %s\n",leafNode->name);
238251
GetDefaultValueInfoForLeaf(leafNode, fieldMapping);
239252
}
240253
else if (field->nodetype == LYS_CHOICE)
241254
{
242-
struct lys_node_choice *choiceNode = reinterpret_cast<struct lys_node_choice*>(field);
255+
const struct lysc_node_choice *choiceNode = reinterpret_cast<const struct lysc_node_choice*>(field);
243256

244257
SWSS_LOG_DEBUG("choice field: %s\n",choiceNode->name);
245258
GetDefaultValueInfoForChoice(choiceNode, fieldMapping);
246259
}
247260
else if (field->nodetype == LYS_LEAFLIST)
248261
{
249262
WARNINGS_NO_CAST_ALIGN
250-
struct lys_node_leaflist *listNode = reinterpret_cast<struct lys_node_leaflist*>(field);
263+
const struct lysc_node_leaflist *listNode = reinterpret_cast<const struct lysc_node_leaflist*>(field);
251264
WARNINGS_RESET
252265

253266
SWSS_LOG_DEBUG("list field: %s\n",listNode->name);
@@ -260,10 +273,10 @@ FieldDefaultValueMappingPtr DefaultValueHelper::GetDefaultValueInfo(struct lys_n
260273
return fieldMapping;
261274
}
262275

263-
int DefaultValueHelper::BuildTableDefaultValueMapping(struct lys_node* table, TableDefaultValueMapping &tableDefaultValueMapping)
276+
int DefaultValueHelper::BuildTableDefaultValueMapping(const struct lysc_node* table, TableDefaultValueMapping &tableDefaultValueMapping)
264277
{
265278
int childListCount = 0;
266-
auto nextChild = table->child;
279+
auto nextChild = lysc_node_child(table);
267280
while (nextChild)
268281
{
269282
// get key from schema
@@ -289,7 +302,7 @@ int DefaultValueHelper::BuildTableDefaultValueMapping(struct lys_node* table, Ta
289302
}
290303

291304
// Load default value info from yang model and append to default value mapping
292-
void DefaultValueProvider::AppendTableInfoToMapping(struct lys_node* table)
305+
void DefaultValueProvider::AppendTableInfoToMapping(const struct lysc_node* table)
293306
{
294307
SWSS_LOG_DEBUG("DefaultValueProvider::AppendTableInfoToMapping table name: %s\n",table->name);
295308
TableDefaultValueMapping tableDefaultValueMapping;
@@ -401,7 +414,7 @@ DefaultValueProvider::~DefaultValueProvider()
401414
if (m_context)
402415
{
403416
// set private_destructor to NULL because no any private data
404-
ly_ctx_destroy(m_context, NULL);
417+
ly_ctx_destroy(m_context);
405418
}
406419
}
407420

@@ -418,7 +431,11 @@ void DefaultValueProvider::Initialize(const char* modulePath)
418431
ThrowRunTimeError("Open Yang model path " + string(modulePath) + " failed");
419432
}
420433

421-
m_context = ly_ctx_new(modulePath, LY_CTX_ALLIMPLEMENTED);
434+
if (ly_ctx_new(modulePath, LY_CTX_ALL_IMPLEMENTED, &m_context) != LY_SUCCESS)
435+
{
436+
ThrowRunTimeError("ly_ctx_new() failed");
437+
}
438+
422439
struct dirent *subDir;
423440
while ((subDir = readdir(moduleDir)) != nullptr)
424441
{
@@ -442,22 +459,23 @@ void DefaultValueProvider::LoadModule(const string &name, const string &path, st
442459
const struct lys_module *module = ly_ctx_load_module(
443460
context,
444461
name.c_str(),
445-
EMPTY_STR); // Use EMPTY_STR to revision to load the latest revision
462+
EMPTY_STR, // Use EMPTY_STR to revision to load the latest revision
463+
NULL);
446464
if (module == nullptr)
447465
{
448466
const char* err = ly_errmsg(context);
449467
SWSS_LOG_ERROR("Load Yang file %s failed: %s.\n", path.c_str(), err);
450468
return;
451469
}
452470

453-
if (module->data == nullptr)
471+
if (module->compiled == nullptr || module->compiled->data == nullptr)
454472
{
455473
// Not every yang file should contains yang model
456474
SWSS_LOG_WARN("Yang file %s does not contains model %s.\n", path.c_str(), name.c_str());
457475
return;
458476
}
459477

460-
struct lys_node *topLevelNode = module->data;
478+
struct lysc_node *topLevelNode = module->compiled->data;
461479
while (topLevelNode)
462480
{
463481
if (topLevelNode->nodetype != LYS_CONTAINER)
@@ -469,7 +487,7 @@ void DefaultValueProvider::LoadModule(const string &name, const string &path, st
469487
}
470488

471489
SWSS_LOG_DEBUG("top level container: %s\n",topLevelNode->name);
472-
auto container = topLevelNode->child;
490+
auto container = lysc_node_child(topLevelNode);
473491
while (container)
474492
{
475493
SWSS_LOG_DEBUG("container name: %s\n",container->name);

common/defaultvalueprovider.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,17 @@ struct TableInfoMultipleList : public TableInfoBase
8686
class DefaultValueHelper
8787
{
8888
public:
89-
static int BuildTableDefaultValueMapping(struct lys_node* table, TableDefaultValueMapping& tableDefaultValueMapping);
89+
static int BuildTableDefaultValueMapping(const struct lysc_node* table, TableDefaultValueMapping& tableDefaultValueMapping);
9090

91-
static std::shared_ptr<KeySchema> GetKeySchema(struct lys_node* table_child_node);
91+
static std::shared_ptr<KeySchema> GetKeySchema(const struct lysc_node* table_child_node);
9292

93-
static FieldDefaultValueMappingPtr GetDefaultValueInfo(struct lys_node* tableChildNode);
93+
static FieldDefaultValueMappingPtr GetDefaultValueInfo(const struct lysc_node* tableChildNode);
9494

95-
static void GetDefaultValueInfoForChoice(struct lys_node_choice* choiceNode, std::shared_ptr<FieldDefaultValueMapping> fieldMapping);
95+
static void GetDefaultValueInfoForChoice(const struct lysc_node_choice* choiceNode, std::shared_ptr<FieldDefaultValueMapping> fieldMapping);
9696

97-
static void GetDefaultValueInfoForLeaf(struct lys_node_leaf* leafNode, std::shared_ptr<FieldDefaultValueMapping> fieldMapping);
97+
static void GetDefaultValueInfoForLeaf(const struct lysc_node_leaf* leafNode, std::shared_ptr<FieldDefaultValueMapping> fieldMapping);
9898

99-
static void GetDefaultValueInfoForLeaflist(struct lys_node_leaflist *listNode, std::shared_ptr<FieldDefaultValueMapping> fieldMapping);
99+
static void GetDefaultValueInfoForLeaflist(const struct lysc_node_leaflist *listNode, std::shared_ptr<FieldDefaultValueMapping> fieldMapping);
100100
};
101101

102102
class DefaultValueProvider
@@ -125,7 +125,7 @@ class DefaultValueProvider
125125
void LoadModule(const std::string &name, const std::string &path, struct ly_ctx *context);
126126

127127
// Load default value info from yang model and append to default value mapping
128-
void AppendTableInfoToMapping(struct lys_node* table);
128+
void AppendTableInfoToMapping(const struct lysc_node* table);
129129

130130
std::shared_ptr<TableInfoBase> FindDefaultValueInfo(const std::string &table);
131131

0 commit comments

Comments
 (0)