blob: b9fb2a186db1869422559d8001ffe8fe8b1bb020 [file] [log] [blame]
Hardik Windlass84861682019-11-10 05:44:33 +00001/*
2 * Copyright 2019-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package core
17
18import (
19 "context"
20 "github.com/opencord/voltha-go/db/model"
21 "github.com/opencord/voltha-go/ro_core/config"
22 "github.com/opencord/voltha-protos/v2/go/openflow_13"
23 "github.com/opencord/voltha-protos/v2/go/voltha"
24 "github.com/stretchr/testify/assert"
25 "reflect"
26 "testing"
27)
28
29func MakeTestNewCoreConfig() *Core {
30 var core Core
31 core.instanceId = "ro_core"
32 core.config = config.NewROCoreFlags()
33 core.clusterDataRoot = model.NewRoot(&voltha.Voltha{}, nil)
34 core.clusterDataProxy = core.clusterDataRoot.CreateProxy(context.Background(), "/", false)
35 core.genericMgr = newModelProxyManager(core.clusterDataProxy)
36 core.deviceMgr = newDeviceManager(core.clusterDataProxy, core.instanceId)
37
38 return &core
39}
40
41func TestNewLogicalDeviceManager(t *testing.T) {
42 core := MakeTestNewCoreConfig()
43
44 logicalDevMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
45 assert.NotNil(t, logicalDevMgr)
46}
47
48func TestAddLogicalDeviceAgentToMap(t *testing.T) {
49 core := MakeTestNewCoreConfig()
50 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
51 assert.NotNil(t, ldMgr)
52
53 // Before ADD
54 ldAgentNil := ldMgr.getLogicalDeviceAgent("id")
55 assert.Nil(t, ldAgentNil)
56
57 /*** Case: addLogicalDeviceAgentToMap() is Success ***/
58 ldAgent := newLogicalDeviceAgent("id", "", ldMgr, core.deviceMgr, core.clusterDataProxy)
59 assert.NotNil(t, ldAgent)
60 ldMgr.addLogicalDeviceAgentToMap(ldAgent)
61
62 // Verify ADD is successful
63 ldAgentNotNil := ldMgr.getLogicalDeviceAgent("id")
64 assert.NotNil(t, ldAgentNotNil)
65 assert.Equal(t, "id", ldAgentNotNil.logicalDeviceId)
66}
67
68func TestGetLogicalDeviceAgent(t *testing.T) {
69 core := MakeTestNewCoreConfig()
70 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
71 assert.NotNil(t, ldMgr)
72
73 /*** Case: getLogicalDeviceAgent() is NIL ***/
74 ldAgentNil := ldMgr.getLogicalDeviceAgent("id")
75 assert.Nil(t, ldAgentNil)
76
77 /*** Case: getLogicalDeviceAgent() is Success ***/
78 ldAgent := newLogicalDeviceAgent("id", "", ldMgr, core.deviceMgr, core.clusterDataProxy)
79 assert.NotNil(t, ldAgent)
80 ldMgr.addLogicalDeviceAgentToMap(ldAgent)
81 ldAgentNotNil := ldMgr.getLogicalDeviceAgent("id")
82 assert.NotNil(t, ldAgentNotNil)
83 assert.Equal(t, "id", ldAgentNotNil.logicalDeviceId)
84}
85
86func TestDeleteLogicalDeviceAgent(t *testing.T) {
87 core := MakeTestNewCoreConfig()
88 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
89 assert.NotNil(t, ldMgr)
90
91 /*** Case: deleteLogicalDeviceAgent() with Invalid Value ***/
92 ldAgentNilChk := ldMgr.getLogicalDeviceAgent("invalid_id")
93 assert.Nil(t, ldAgentNilChk)
94 ldMgr.deleteLogicalDeviceAgent("invalid_id")
95
96 /*** Case: deleteLogicalDeviceAgent() is Success ***/
97 // Initialize and Add Logical Device Agent
98 ldAgent := newLogicalDeviceAgent("id", "", ldMgr, core.deviceMgr, core.clusterDataProxy)
99 assert.NotNil(t, ldAgent)
100 ldMgr.addLogicalDeviceAgentToMap(ldAgent)
101
102 // Verify ADD is successful
103 ldAgentNotNil := ldMgr.getLogicalDeviceAgent("id")
104 assert.NotNil(t, ldAgentNotNil)
105 assert.Equal(t, "id", ldAgentNotNil.logicalDeviceId)
106
107 // Method under Test
108 ldMgr.deleteLogicalDeviceAgent("id")
109
110 // Verify DEL is successful
111 ldAgentNil := ldMgr.getLogicalDeviceAgent("id")
112 assert.Nil(t, ldAgentNil)
113}
114
115func TestLdMgrGetLogicalDevice(t *testing.T) {
116 wantResult := &voltha.LogicalDevice{}
117
118 core := MakeTestNewCoreConfig()
119 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
120 assert.NotNil(t, ldMgr)
121
122 /*** Case: getLogicalDevice() is NIL ***/
123 logicalDevNil, errNotNil := ldMgr.getLogicalDevice("id")
124 assert.Nil(t, logicalDevNil)
125 assert.NotNil(t, errNotNil)
126
127 /*** Case: getLogicalDevice() is Success ***/
128 // Add Data
129 if added := core.clusterDataProxy.Add(
130 context.Background(),
131 "/logical_devices",
132 &voltha.LogicalDevice{
133 Id: "id",
134 },
135 ""); added == nil {
136 t.Error("Failed to add logical device")
137 }
138 ldAgent := newLogicalDeviceAgent("id", "device_id", ldMgr, core.deviceMgr, core.clusterDataProxy)
139 assert.NotNil(t, ldAgent)
140 ldMgr.addLogicalDeviceAgentToMap(ldAgent)
141
142 // Verify Add is successful
143 ldAgentNotNil := ldMgr.getLogicalDeviceAgent("id")
144 assert.NotNil(t, ldAgentNotNil)
145 assert.Equal(t, "id", ldAgentNotNil.logicalDeviceId)
146
147 // Verify getLogicalDevice() is NOT NIL
148 logicalDevNotNil, errNil := ldMgr.getLogicalDevice("id")
149 assert.NotNil(t, logicalDevNotNil)
150 assert.Nil(t, errNil)
151 if reflect.TypeOf(logicalDevNotNil) != reflect.TypeOf(wantResult) {
152 t.Errorf("GetLogicalDevice() = %v, want %v", logicalDevNotNil, wantResult)
153 }
154 assert.Equal(t, "id", logicalDevNotNil.Id)
155}
156
157func TestListLogicalDevices(t *testing.T) {
158 wantResult := &voltha.LogicalDevices{}
159
160 core := MakeTestNewCoreConfig()
161 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
162 assert.NotNil(t, ldMgr)
163
164 /*** Case: listLogicalDevices() is Empty ***/
165 result, error := ldMgr.listLogicalDevices()
166 assert.NotNil(t, result)
167 assert.Nil(t, error)
168 if reflect.TypeOf(result) != reflect.TypeOf(wantResult) {
169 t.Errorf("ListLogicalDevices() = %v, want %v", result, wantResult)
170 }
171 assert.Empty(t, result)
172}
173
174func TestLoad(t *testing.T) {
175 core := MakeTestNewCoreConfig()
176 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
177 assert.NotNil(t, ldMgr)
178
179 /*** Case: Error Scenario ***/
180 error := ldMgr.load("id")
181 assert.NotNil(t, error)
182}
183
184func TestGetLogicalDeviceId(t *testing.T) {
185 core := MakeTestNewCoreConfig()
186 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
187 assert.NotNil(t, ldMgr)
188
189 /*** Case: Logical Device Id Found ***/
190 result0, error0 := ldMgr.getLogicalDeviceId(&voltha.Device{Id: "id", Root: true, ParentId: "parent_id"})
191 assert.NotNil(t, result0)
192 assert.Nil(t, error0)
193
194 /*** Case: Logical Device Id Not Found ***/
195 result1, error1 := ldMgr.getLogicalDeviceId(&voltha.Device{Id: "id", ParentId: "parent_id"})
196 assert.Nil(t, result1)
197 assert.NotNil(t, error1)
198}
199
200func TestGetLogicalPortId(t *testing.T) {
201 wantResult := &voltha.LogicalPortId{}
202
203 core := MakeTestNewCoreConfig()
204 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
205 assert.NotNil(t, ldMgr)
206
207 /*** Case: Logical Port Id Not Found: getLogicalDeviceId() Error ***/
208 result0, error0 := ldMgr.getLogicalPortId(&voltha.Device{Id: "id", ParentId: "parent_id"})
209 assert.Nil(t, result0)
210 assert.NotNil(t, error0)
211
212 /*** Case: Logical Port Id Not Found: getLogicalDevice() Error ***/
213 result1, error1 := ldMgr.getLogicalPortId(&voltha.Device{Id: "id", Root: true, ParentId: "parent_id"})
214 assert.Nil(t, result1)
215 assert.NotNil(t, error1)
216
217 /*** Case: Logical Port Id Found ***/
218 device := &voltha.Device{Id: "id", Root: true, ParentId: "parent_id"}
219
220 // Add Data
221 if added := core.clusterDataProxy.Add(
222 context.Background(),
223 "/logical_devices",
224 &voltha.LogicalDevice{
225 Id: "parent_id",
226 Ports: []*voltha.LogicalPort{
227 {
228 Id: "123",
229 DeviceId: "id",
230 },
231 },
232 },
233 ""); added == nil {
234 t.Error("Failed to add logical device")
235 }
236 ldAgent := newLogicalDeviceAgent("parent_id", "device_id", ldMgr, core.deviceMgr, core.clusterDataProxy)
237 assert.NotNil(t, ldAgent)
238 ldMgr.addLogicalDeviceAgentToMap(ldAgent)
239
240 // Verify Add is successful
241 ldAgentNotNil := ldMgr.getLogicalDeviceAgent("parent_id")
242 assert.NotNil(t, ldAgentNotNil)
243 assert.Equal(t, "parent_id", ldAgentNotNil.logicalDeviceId)
244
245 // Verify getLogicalPortId() is Success
246 result2, error2 := ldMgr.getLogicalPortId(device)
247 assert.NotNil(t, result2)
248 assert.Nil(t, error2)
249 if reflect.TypeOf(result2) != reflect.TypeOf(wantResult) {
250 t.Errorf("GetLogicalPortId() = %v, want %v", result2, wantResult)
251 }
252 assert.Equal(t, "parent_id", result2.Id)
253}
254
255func TestListLogicalDevicePorts(t *testing.T) {
256 core := MakeTestNewCoreConfig()
257 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
258 assert.NotNil(t, ldMgr)
259
260 /*** Case: Logical Device Ports Not Found ***/
261 result0, error0 := ldMgr.ListLogicalDevicePorts(context.Background(), "id")
262 assert.Nil(t, result0)
263 assert.NotNil(t, error0)
264
265 /*** Case: Logical Device Ports Found ***/
266 wantResult := &voltha.LogicalPorts{
267 Items: []*voltha.LogicalPort{
268 {
269 Id: "123",
270 },
271 },
272 }
273
274 // Add Data
275 if added := core.clusterDataProxy.Add(
276 context.Background(),
277 "/logical_devices",
278 &voltha.LogicalDevice{
279 Id: "id",
280 Ports: []*voltha.LogicalPort{
281 {
282 Id: "123",
283 },
284 },
285 },
286 ""); added == nil {
287 t.Error("Failed to add logical device")
288 }
289 ldAgent := newLogicalDeviceAgent("id", "", ldMgr, core.deviceMgr, core.clusterDataProxy)
290 ldMgr.addLogicalDeviceAgentToMap(ldAgent)
291
292 // Verify Add is successful
293 ldAgentNotNil := ldMgr.getLogicalDeviceAgent("id")
294 assert.NotNil(t, ldAgentNotNil)
295 assert.Equal(t, "id", ldAgentNotNil.logicalDeviceId)
296
297 // Verify ListLogicalDevicePorts() is Success
298 result1, error1 := ldMgr.ListLogicalDevicePorts(context.Background(), "id")
299 assert.NotNil(t, result1)
300 assert.Nil(t, error1)
301 if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) {
302 t.Errorf("ListLogicalDevicePorts() = %v, want %v", result1, wantResult)
303 }
304 assert.Equal(t, wantResult, result1)
305}
306
307func TestListLogicalDeviceFlows(t *testing.T) {
308 core := MakeTestNewCoreConfig()
309 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
310 assert.NotNil(t, ldMgr)
311
312 /*** Case: Logical Device Flows Not Found ***/
313 result0, error0 := ldMgr.ListLogicalDeviceFlows(context.Background(), "id")
314 assert.Nil(t, result0)
315 assert.NotNil(t, error0)
316
317 /*** Case: Logical Device Flows Found ***/
318 wantResult := &voltha.Flows{}
319
320 // Add Data
321 if added := core.clusterDataProxy.Add(
322 context.Background(),
323 "/logical_devices",
324 &voltha.LogicalDevice{
325 Id: "id",
326 Flows: &openflow_13.Flows{
327 Items: []*openflow_13.OfpFlowStats{
328 &openflow_13.OfpFlowStats{
329 Id: 1111,
330 },
331 },
332 },
333 },
334 ""); added == nil {
335 t.Error("Failed to add logical device")
336 }
337 ldAgent := newLogicalDeviceAgent("id", "", ldMgr, core.deviceMgr, core.clusterDataProxy)
338 ldMgr.addLogicalDeviceAgentToMap(ldAgent)
339
340 // Verify Add is successful
341 ldAgentNotNil := ldMgr.getLogicalDeviceAgent("id")
342 assert.NotNil(t, ldAgentNotNil)
343 assert.Equal(t, "id", ldAgentNotNil.logicalDeviceId)
344
345 // Verify ListLogicalDeviceFlows() is Success
346 result1, error1 := ldMgr.ListLogicalDeviceFlows(context.Background(), "id")
347 assert.NotNil(t, result1)
348 assert.Nil(t, error1)
349 if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) {
350 t.Errorf("ListLogicalDeviceFlows() = %v, want %v", result1, wantResult)
351 }
352 assert.NotEmpty(t, result1)
353}
354
355func TestListLogicalDeviceFlowGroups(t *testing.T) {
356 core := MakeTestNewCoreConfig()
357 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
358 assert.NotNil(t, ldMgr)
359
360 /*** Case: Logical Device Flow Groups Not Found ***/
361 result0, error0 := ldMgr.ListLogicalDeviceFlowGroups(context.Background(), "id")
362 assert.Nil(t, result0)
363 assert.NotNil(t, error0)
364
365 /*** Case: Logical Device Flow Groups Found ***/
366 wantResult := &voltha.FlowGroups{}
367
368 // Add Data
369 if added := core.clusterDataProxy.Add(
370 context.Background(),
371 "/logical_devices",
372 &voltha.LogicalDevice{
373 Id: "id",
374 FlowGroups: &openflow_13.FlowGroups{
375 Items: []*openflow_13.OfpGroupEntry{
376 {
377 Stats: &openflow_13.OfpGroupStats{
378 GroupId: 1,
379 },
380 },
381 },
382 },
383 },
384 ""); added == nil {
385 t.Error("Failed to add logical device")
386 }
387 ldAgent := newLogicalDeviceAgent("id", "", ldMgr, core.deviceMgr, core.clusterDataProxy)
388 ldMgr.addLogicalDeviceAgentToMap(ldAgent)
389
390 // Verify Add is successful
391 ldAgentNotNil := ldMgr.getLogicalDeviceAgent("id")
392 assert.NotNil(t, ldAgentNotNil)
393 assert.Equal(t, "id", ldAgentNotNil.logicalDeviceId)
394
395 // Verify ListLogicalDeviceFlowGroups() is Success
396 result1, error1 := ldMgr.ListLogicalDeviceFlowGroups(context.Background(), "id")
397 assert.NotNil(t, result1)
398 assert.Nil(t, error1)
399 if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) {
400 t.Errorf("ListLogicalDeviceFlowGroups() = %v, want %v", result1, wantResult)
401 }
402 assert.NotEmpty(t, result1)
403}
404
405func TestGetLogicalPort(t *testing.T) {
406 core := MakeTestNewCoreConfig()
407 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
408 assert.NotNil(t, ldMgr)
409
410 /*** Case: Logical Port Not Found: getLogicalDevice() Error ***/
411 result0, error0 := ldMgr.getLogicalPort(&voltha.LogicalPortId{Id: "id", PortId: "123"})
412 assert.Nil(t, result0)
413 assert.NotNil(t, error0)
414
415 /*** Case: Logical Port Found ***/
416 wantResult := &voltha.LogicalPort{Id: "123"}
417
418 // Add Data
419 if added := core.clusterDataProxy.Add(
420 context.Background(),
421 "/logical_devices",
422 &voltha.LogicalDevice{
423 Id: "id",
424 Ports: []*voltha.LogicalPort{
425 {
426 Id: "123",
427 },
428 },
429 },
430 ""); added == nil {
431 t.Error("Failed to add logical device")
432 }
433 ldAgent := newLogicalDeviceAgent("id", "", ldMgr, core.deviceMgr, core.clusterDataProxy)
434 ldMgr.addLogicalDeviceAgentToMap(ldAgent)
435
436 // Verify Add is successful
437 ldAgentNotNil := ldMgr.getLogicalDeviceAgent("id")
438 assert.NotNil(t, ldAgentNotNil)
439 assert.Equal(t, "id", ldAgentNotNil.logicalDeviceId)
440
441 // Verify getLogicalPort() is Success
442 result1, error1 := ldMgr.getLogicalPort(&voltha.LogicalPortId{Id: "id", PortId: "123"})
443 assert.NotNil(t, result1)
444 assert.Nil(t, error1)
445 if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) {
446 t.Errorf("getLogicalPort() = %v, want %v", result1, wantResult)
447 }
448 assert.Equal(t, wantResult, result1)
449}