@@ -5,19 +5,50 @@ extern "C" {
5
5
}
6
6
7
7
#include " swss/logger.h"
8
+ #include " swss/dbconnector.h"
9
+ #include " swss/schema.h"
10
+ #include " swss/redisreply.h"
8
11
#include " sairedis.h"
9
12
#include " sai_redis.h"
10
13
#include " meta/saiserialize.h"
14
+ #include " syncd.h"
11
15
12
16
#include < map>
13
17
#include < unordered_map>
14
18
#include < vector>
19
+ #include < thread>
20
+ #include < tuple>
15
21
16
22
#define ASSERT_SUCCESS (format,...) \
17
23
if ((status)!=SAI_STATUS_SUCCESS) \
18
24
SWSS_LOG_THROW (format " : %s" , ##__VA_ARGS__, sai_serialize_status(status).c_str());
19
25
20
- const char * profile_get_value (
26
+ static sai_next_hop_group_api_t test_next_hop_group_api;
27
+ static std::vector<std::tuple<sai_object_id_t , sai_object_id_t , std::vector<sai_attribute_t >>> created_next_hop_group_member;
28
+
29
+ sai_status_t test_create_next_hop_group_member (
30
+ _Out_ sai_object_id_t *next_hop_group_member_id,
31
+ _In_ sai_object_id_t switch_id,
32
+ _In_ uint32_t attr_count,
33
+ _In_ const sai_attribute_t *attr_list)
34
+ {
35
+ created_next_hop_group_member.emplace_back ();
36
+ auto & back = created_next_hop_group_member.back ();
37
+ std::get<0 >(back) = *next_hop_group_member_id;
38
+ std::get<1 >(back) = switch_id;
39
+ auto & attrs = std::get<2 >(back);
40
+ attrs.insert (attrs.end (), attr_list, attr_list + attr_count);
41
+ return 0 ;
42
+ }
43
+
44
+ void clearDB ()
45
+ {
46
+ swss::DBConnector db (ASIC_DB, " localhost" , 6379 , 0 );
47
+ swss::RedisReply r (&db, " FLUSHALL" , REDIS_REPLY_STATUS);
48
+ r.checkStatusOK ();
49
+ }
50
+
51
+ static const char * profile_get_value (
21
52
_In_ sai_switch_profile_id_t profile_id,
22
53
_In_ const char * variable)
23
54
{
@@ -26,7 +57,7 @@ const char* profile_get_value(
26
57
return NULL ;
27
58
}
28
59
29
- int profile_get_next_value (
60
+ static int profile_get_next_value (
30
61
_In_ sai_switch_profile_id_t profile_id,
31
62
_Out_ const char ** variable,
32
63
_Out_ const char ** value)
@@ -50,7 +81,7 @@ int profile_get_next_value(
50
81
return -1 ;
51
82
}
52
83
53
- service_method_table_t test_services = {
84
+ static service_method_table_t test_services = {
54
85
profile_get_value,
55
86
profile_get_next_value
56
87
};
@@ -64,6 +95,11 @@ void test_sai_initialize()
64
95
// with enabled unix socket
65
96
sai_status_t status = sai_api_initialize (0 , (service_method_table_t *)&test_services);
66
97
98
+ // Mock the SAI api
99
+ test_next_hop_group_api.create_next_hop_group_member = test_create_next_hop_group_member;
100
+ sai_metadata_sai_next_hop_group_api = &test_next_hop_group_api;
101
+ created_next_hop_group_member.clear ();
102
+
67
103
ASSERT_SUCCESS (" Failed to initialize api" );
68
104
}
69
105
@@ -100,15 +136,55 @@ sai_object_id_t create_dummy_object_id(
100
136
return (((sai_object_id_t )objecttype) << 48 ) | ++index ;
101
137
}
102
138
139
+ bool starts_with (const std::string& str, const std::string& substr)
140
+ {
141
+ return strncmp (str.c_str (), substr.c_str (), substr.size ()) == 0 ;
142
+ }
143
+
144
+ void bulk_nhgm_consumer_worker ()
145
+ {
146
+ std::string tableName = ASIC_STATE_TABLE;
147
+ swss::DBConnector db (ASIC_DB, " localhost" , 6379 , 0 );
148
+ swss::ConsumerTable c (&db, tableName);
149
+ swss::Select cs;
150
+ swss::Selectable *selectcs;
151
+ int tmpfd;
152
+ int ret = 0 ;
153
+
154
+ cs.addSelectable (&c);
155
+ while ((ret = cs.select (&selectcs, &tmpfd)) == swss::Select::OBJECT)
156
+ {
157
+ swss::KeyOpFieldsValuesTuple kco;
158
+ c.pop (kco);
159
+
160
+ auto & key = kfvKey (kco);
161
+ auto & op = kfvOp (kco);
162
+ auto & values = kfvFieldsValues (kco);
163
+
164
+ if (starts_with (key, " SAI_OBJECT_TYPE_SWITCH" )) continue ;
165
+
166
+ if (op == " bulkcreate" )
167
+ {
168
+ sai_status_t status = processBulkEvent ((sai_common_api_t )SAI_COMMON_API_BULK_CREATE, kco);
169
+ ASSERT_SUCCESS (" Failed to processBulkEvent" );
170
+ break ;
171
+ }
172
+
173
+ }
174
+ }
175
+
103
176
void test_bulk_next_hop_group_member_create ()
104
177
{
105
178
SWSS_LOG_ENTER ();
106
179
107
180
swss::Logger::getInstance ().setMinPrio (swss::Logger::SWSS_NOTICE);
108
181
182
+ clearDB ();
109
183
meta_init_db ();
110
184
redis_clear_switch_ids ();
111
185
186
+ auto consumerThreads = new std::thread (bulk_nhgm_consumer_worker);
187
+
112
188
swss::Logger::getInstance ().setMinPrio (swss::Logger::SWSS_DEBUG);
113
189
114
190
sai_status_t status;
@@ -144,6 +220,7 @@ void test_bulk_next_hop_group_member_create()
144
220
sai_object_meta_key_t meta_key_hopgruop = { .objecttype = SAI_OBJECT_TYPE_NEXT_HOP_GROUP, .objectkey = { .key = { .object_id = hopgroup } } };
145
221
std::string hopgroup_key = sai_serialize_object_meta_key (meta_key_hopgruop);
146
222
ObjectAttrHash[hopgroup_key] = { };
223
+ sai_object_id_t hopgroup_vid = translate_rid_to_vid (hopgroup, switch_id);
147
224
148
225
for (uint32_t i = 0 ; i < count; ++i)
149
226
{
@@ -153,15 +230,16 @@ void test_bulk_next_hop_group_member_create()
153
230
sai_object_meta_key_t meta_key_hop = { .objecttype = SAI_OBJECT_TYPE_NEXT_HOP, .objectkey = { .key = { .object_id = hop } } };
154
231
std::string hop_key = sai_serialize_object_meta_key (meta_key_hop);
155
232
ObjectAttrHash[hop_key] = { };
233
+ sai_object_id_t hop_vid = translate_rid_to_vid (hop, switch_id);
156
234
157
235
std::vector<sai_attribute_t > list (2 );
158
236
sai_attribute_t &attr1 = list[0 ];
159
237
sai_attribute_t &attr2 = list[1 ];
160
238
161
239
attr1.id = SAI_NEXT_HOP_GROUP_MEMBER_ATTR_NEXT_HOP_GROUP_ID;
162
- attr1.value .oid = hopgroup ;
240
+ attr1.value .oid = hopgroup_vid ;
163
241
attr2.id = SAI_NEXT_HOP_GROUP_MEMBER_ATTR_NEXT_HOP_ID;
164
- attr2.value .oid = hop ;
242
+ attr2.value .oid = hop_vid ;
165
243
nhgm_attrs.push_back (list);
166
244
nhgm_attrs_count.push_back (2 );
167
245
}
@@ -181,6 +259,18 @@ void test_bulk_next_hop_group_member_create()
181
259
status = statuses[j];
182
260
ASSERT_SUCCESS (" Failed to create nhgm # %zu" , j);
183
261
}
262
+
263
+ consumerThreads->join ();
264
+ delete consumerThreads;
265
+
266
+ // check the SAI api calling
267
+ for (size_t i = 0 ; i < created_next_hop_group_member.size (); i++)
268
+ {
269
+ auto & created = created_next_hop_group_member[i];
270
+ auto & created_attrs = std::get<2 >(created);
271
+ assert (created_attrs.size () == 2 );
272
+ assert (created_attrs[1 ].value .oid == nhgm_attrs[i][1 ].value .oid );
273
+ }
184
274
}
185
275
186
276
void test_bulk_route_set ()
@@ -189,6 +279,7 @@ void test_bulk_route_set()
189
279
190
280
swss::Logger::getInstance ().setMinPrio (swss::Logger::SWSS_NOTICE);
191
281
282
+ clearDB ();
192
283
meta_init_db ();
193
284
redis_clear_switch_ids ();
194
285
0 commit comments