Skip to content

Commit fb3c520

Browse files
committed
local changes
1 parent fd1508e commit fb3c520

File tree

5 files changed

+149
-55
lines changed

5 files changed

+149
-55
lines changed

BUILD

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,37 @@ cc_library(
1111
hdrs = glob([
1212
"common/*.h",
1313
"common/*.hpp",
14-
]),
14+
], allow_empty = True),
1515
copts = [
1616
"-std=c++14",
17-
"-I/usr/include/libnl3", # Expected location in the SONiC build container"
17+
# TODO: this is not required with apt.installed debs.
18+
# "-I/usr/include/libnl3", # Expected location in the SONiC build container"
1819
],
20+
# Not needed with apt.install
21+
# linkopts = ["-lpthread -lhiredis -lnl-genl-3 -lnl-nf-3 -lnl-route-3 -lnl-3 -lzmq -luuid -lyang"],
1922
includes = [
2023
"common",
2124
],
25+
# Approach 1:
26+
deps = [
27+
"@bookworm//libhiredis-dev:libhiredis",
28+
"@bookworm//nlohmann-json3-dev:nlohmann-json3",
29+
"@bookworm//libnl-3-dev:libnl-3",
30+
"@bookworm//libnl-route-3-dev:libnl-route-3",
31+
"@bookworm//libnl-nf-3-dev:libnl-nf-3",
32+
"@bookworm//libyang2-dev:libyang2",
33+
"@bookworm//libzmq3-dev:libzmq3",
34+
"@bookworm//uuid-dev:uuid",
35+
"@bookworm//libboost-dev:libboost"
36+
],
37+
# Approach 2: BCR entries compiled from source
2238
# deps = [
2339
# "@boost.algorithm",
2440
# "@boost.serialization",
2541
# "@nlohmann_json//:json",
2642
# "@libuuid//:libuuid",
2743
# "@swig//:swig",
2844
# ],
29-
linkopts = ["-lpthread -lhiredis -lnl-genl-3 -lnl-nf-3 -lnl-route-3 -lnl-3 -lzmq -luuid -lyang"],
3045
visibility = ["//visibility:public"],
3146
)
3247

@@ -35,7 +50,7 @@ cc_library(
3550
hdrs = glob([
3651
"common/*.h",
3752
"common/*.hpp",
38-
]),
53+
], allow_empty = True),
3954
include_prefix = "swss",
4055
strip_include_prefix = "common",
4156
deps = [":common"],

MODULE.bazel

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,66 @@
11
module(name = "sonic-swss-common")
22

3-
bazel_dep(name = "googletest", version = "1.11.0", repo_name = "com_google_googletest")
4-
bazel_dep(name = "glog", version = "0.5.0", repo_name = "com_github_google_glog")
3+
bazel_dep(name = "rules_distroless", version = "0.0.0")
4+
local_path_override(
5+
module_name = "rules_distroless",
6+
path = "../rules_distroless"
7+
)
58

9+
10+
apt = use_extension("@rules_distroless//apt:extensions.bzl", "apt")
11+
12+
apt.sources_list(
13+
architectures = ["amd64"],
14+
components = ["main"],
15+
suites = ["bookworm", "bookworm-updates"],
16+
uris = ["https://snapshot.debian.org/archive/debian/20251001T023456Z"],
17+
)
18+
19+
apt.sources_list(
20+
architectures = ["amd64"],
21+
components = ["main"],
22+
suites = ["bookworm-security"],
23+
uris = ["https://snapshot.debian.org/archive/debian-security/20251001T023456Z"],
24+
)
25+
26+
27+
28+
# sudo apt-get install -y
29+
# make libtool m4 autoconf dh-exec debhelper cmake pkg-config
30+
# nlohmann-json3-dev libhiredis-dev libnl-3-dev libnl-genl-3-dev
31+
# libnl-route-3-dev libnl-nf-3-dev swig3.0 libpython2.7-dev libboost-dev
32+
# libboost-serialization-dev uuid-dev libzmq3-dev
33+
34+
apt.install(
35+
dependency_set = "bookworm",
36+
target_release = "bookworm",
37+
packages = [
38+
"nlohmann-json3-dev",
39+
"libhiredis-dev",
40+
"libnl-3-dev",
41+
"libnl-genl-3-dev",
42+
"libnl-route-3-dev",
43+
"libnl-nf-3-dev",
44+
"libboost-dev",
45+
"libboost-serialization-dev",
46+
"uuid-dev",
47+
"libzmq3-dev",
48+
"libyang2-dev",
49+
50+
# These seem to be unused
51+
# "swig3.0",
52+
# "libpython2.7-dev",
53+
# "libgtest-dev",
54+
# "libgmock-dev",
55+
]
56+
)
57+
58+
apt.lock(into = ":packages.lock")
59+
60+
use_repo(apt, "bookworm")
61+
62+
# bazel_dep(name = "googletest", version = "1.11.0", repo_name = "com_google_googletest")
63+
# bazel_dep(name = "glog", version = "0.5.0", repo_name = "com_github_google_glog")
664
# bazel_dep(name = "nlohmann_json", version = "3.12.0")
765
# bazel_dep(name = "boost.algorithm", version = "1.87.0")
866
# bazel_dep(name = "boost.serialization", version = "1.87.0")

common/defaultvalueprovider.cpp

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include <string>
55
#include <vector>
66

7-
#include <dirent.h>
8-
#include <stdio.h>
7+
#include <dirent.h>
8+
#include <stdio.h>
99

1010
#include "defaultvalueprovider.h"
1111
#include "logger.h"
@@ -119,15 +119,15 @@ bool TableInfoMultipleList::FoundFieldMappingByKey(const string &key, FieldDefau
119119
SWSS_LOG_DEBUG("TableInfoMultipleList::FoundFieldMappingByKey %s\n", key.c_str());
120120
int fieldCount = (int)count(key.begin(), key.end(), '|') + 1;
121121
auto keySchema = m_defaultValueMapping.find(fieldCount);
122-
122+
123123
// when not found, key_info still a valid iterator
124124
*foundMappingPtr = keySchema->second.get();
125-
125+
126126
// return false when not found
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
{
@@ -160,20 +165,20 @@ shared_ptr<KeySchema> DefaultValueHelper::GetKeySchema(struct lys_node* tableChi
160165
SWSS_LOG_DEBUG("Ignore child element: %s\n",tableChildNode->name);
161166
return nullptr;
162167
}
163-
168+
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
{
@@ -190,64 +195,72 @@ void DefaultValueHelper::GetDefaultValueInfoForChoice(struct lys_node_choice* ch
190195
SWSS_LOG_ERROR("choice case %s is not a leaf node\n",fieldInChoice->name);
191196
continue;
192197
}
193-
198+
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);

0 commit comments

Comments
 (0)