blob: e7b33bcdbefd65be52aaeb8adbc9e48f2379cad1 [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"
npujar03b018e2019-11-13 15:29:36 +053020 "reflect"
21 "testing"
22
Hardik Windlass84861682019-11-10 05:44:33 +000023 "github.com/opencord/voltha-go/db/model"
24 "github.com/opencord/voltha-go/ro_core/config"
25 "github.com/opencord/voltha-protos/v2/go/openflow_13"
26 "github.com/opencord/voltha-protos/v2/go/voltha"
27 "github.com/stretchr/testify/assert"
Hardik Windlass84861682019-11-10 05:44:33 +000028)
29
30func MakeTestNewCoreConfig() *Core {
31 var core Core
npujar03b018e2019-11-13 15:29:36 +053032 core.instanceID = "ro_core"
Hardik Windlass84861682019-11-10 05:44:33 +000033 core.config = config.NewROCoreFlags()
34 core.clusterDataRoot = model.NewRoot(&voltha.Voltha{}, nil)
35 core.clusterDataProxy = core.clusterDataRoot.CreateProxy(context.Background(), "/", false)
36 core.genericMgr = newModelProxyManager(core.clusterDataProxy)
npujar03b018e2019-11-13 15:29:36 +053037 core.deviceMgr = newDeviceManager(core.clusterDataProxy, core.instanceID)
Hardik Windlass84861682019-11-10 05:44:33 +000038
39 return &core
40}
41
42func TestNewLogicalDeviceManager(t *testing.T) {
43 core := MakeTestNewCoreConfig()
44
45 logicalDevMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
46 assert.NotNil(t, logicalDevMgr)
47}
48
49func TestAddLogicalDeviceAgentToMap(t *testing.T) {
50 core := MakeTestNewCoreConfig()
51 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
52 assert.NotNil(t, ldMgr)
53
54 // Before ADD
55 ldAgentNil := ldMgr.getLogicalDeviceAgent("id")
56 assert.Nil(t, ldAgentNil)
57
58 /*** Case: addLogicalDeviceAgentToMap() is Success ***/
59 ldAgent := newLogicalDeviceAgent("id", "", ldMgr, core.deviceMgr, core.clusterDataProxy)
60 assert.NotNil(t, ldAgent)
61 ldMgr.addLogicalDeviceAgentToMap(ldAgent)
62
63 // Verify ADD is successful
64 ldAgentNotNil := ldMgr.getLogicalDeviceAgent("id")
65 assert.NotNil(t, ldAgentNotNil)
npujar03b018e2019-11-13 15:29:36 +053066 assert.Equal(t, "id", ldAgentNotNil.logicalDeviceID)
Hardik Windlass84861682019-11-10 05:44:33 +000067}
68
69func TestGetLogicalDeviceAgent(t *testing.T) {
70 core := MakeTestNewCoreConfig()
71 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
72 assert.NotNil(t, ldMgr)
73
74 /*** Case: getLogicalDeviceAgent() is NIL ***/
75 ldAgentNil := ldMgr.getLogicalDeviceAgent("id")
76 assert.Nil(t, ldAgentNil)
77
78 /*** Case: getLogicalDeviceAgent() is Success ***/
79 ldAgent := newLogicalDeviceAgent("id", "", ldMgr, core.deviceMgr, core.clusterDataProxy)
80 assert.NotNil(t, ldAgent)
81 ldMgr.addLogicalDeviceAgentToMap(ldAgent)
82 ldAgentNotNil := ldMgr.getLogicalDeviceAgent("id")
83 assert.NotNil(t, ldAgentNotNil)
npujar03b018e2019-11-13 15:29:36 +053084 assert.Equal(t, "id", ldAgentNotNil.logicalDeviceID)
Hardik Windlass84861682019-11-10 05:44:33 +000085}
86
87func TestDeleteLogicalDeviceAgent(t *testing.T) {
88 core := MakeTestNewCoreConfig()
89 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
90 assert.NotNil(t, ldMgr)
91
92 /*** Case: deleteLogicalDeviceAgent() with Invalid Value ***/
93 ldAgentNilChk := ldMgr.getLogicalDeviceAgent("invalid_id")
94 assert.Nil(t, ldAgentNilChk)
95 ldMgr.deleteLogicalDeviceAgent("invalid_id")
96
97 /*** Case: deleteLogicalDeviceAgent() is Success ***/
98 // Initialize and Add Logical Device Agent
99 ldAgent := newLogicalDeviceAgent("id", "", ldMgr, core.deviceMgr, core.clusterDataProxy)
100 assert.NotNil(t, ldAgent)
101 ldMgr.addLogicalDeviceAgentToMap(ldAgent)
102
103 // Verify ADD is successful
104 ldAgentNotNil := ldMgr.getLogicalDeviceAgent("id")
105 assert.NotNil(t, ldAgentNotNil)
npujar03b018e2019-11-13 15:29:36 +0530106 assert.Equal(t, "id", ldAgentNotNil.logicalDeviceID)
Hardik Windlass84861682019-11-10 05:44:33 +0000107
108 // Method under Test
109 ldMgr.deleteLogicalDeviceAgent("id")
110
111 // Verify DEL is successful
112 ldAgentNil := ldMgr.getLogicalDeviceAgent("id")
113 assert.Nil(t, ldAgentNil)
114}
115
116func TestLdMgrGetLogicalDevice(t *testing.T) {
117 wantResult := &voltha.LogicalDevice{}
118
119 core := MakeTestNewCoreConfig()
120 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
121 assert.NotNil(t, ldMgr)
122
123 /*** Case: getLogicalDevice() is NIL ***/
124 logicalDevNil, errNotNil := ldMgr.getLogicalDevice("id")
125 assert.Nil(t, logicalDevNil)
126 assert.NotNil(t, errNotNil)
127
128 /*** Case: getLogicalDevice() is Success ***/
129 // Add Data
130 if added := core.clusterDataProxy.Add(
131 context.Background(),
132 "/logical_devices",
133 &voltha.LogicalDevice{
134 Id: "id",
135 },
136 ""); added == nil {
137 t.Error("Failed to add logical device")
138 }
139 ldAgent := newLogicalDeviceAgent("id", "device_id", ldMgr, core.deviceMgr, core.clusterDataProxy)
140 assert.NotNil(t, ldAgent)
141 ldMgr.addLogicalDeviceAgentToMap(ldAgent)
142
143 // Verify Add is successful
144 ldAgentNotNil := ldMgr.getLogicalDeviceAgent("id")
145 assert.NotNil(t, ldAgentNotNil)
npujar03b018e2019-11-13 15:29:36 +0530146 assert.Equal(t, "id", ldAgentNotNil.logicalDeviceID)
Hardik Windlass84861682019-11-10 05:44:33 +0000147
148 // Verify getLogicalDevice() is NOT NIL
149 logicalDevNotNil, errNil := ldMgr.getLogicalDevice("id")
150 assert.NotNil(t, logicalDevNotNil)
151 assert.Nil(t, errNil)
152 if reflect.TypeOf(logicalDevNotNil) != reflect.TypeOf(wantResult) {
153 t.Errorf("GetLogicalDevice() = %v, want %v", logicalDevNotNil, wantResult)
154 }
155 assert.Equal(t, "id", logicalDevNotNil.Id)
156}
157
158func TestListLogicalDevices(t *testing.T) {
159 wantResult := &voltha.LogicalDevices{}
160
161 core := MakeTestNewCoreConfig()
162 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
163 assert.NotNil(t, ldMgr)
164
165 /*** Case: listLogicalDevices() is Empty ***/
166 result, error := ldMgr.listLogicalDevices()
167 assert.NotNil(t, result)
168 assert.Nil(t, error)
169 if reflect.TypeOf(result) != reflect.TypeOf(wantResult) {
170 t.Errorf("ListLogicalDevices() = %v, want %v", result, wantResult)
171 }
172 assert.Empty(t, result)
173}
174
175func TestLoad(t *testing.T) {
176 core := MakeTestNewCoreConfig()
177 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
178 assert.NotNil(t, ldMgr)
179
180 /*** Case: Error Scenario ***/
181 error := ldMgr.load("id")
182 assert.NotNil(t, error)
183}
184
185func TestGetLogicalDeviceId(t *testing.T) {
186 core := MakeTestNewCoreConfig()
187 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
188 assert.NotNil(t, ldMgr)
189
190 /*** Case: Logical Device Id Found ***/
npujar03b018e2019-11-13 15:29:36 +0530191 result0, error0 := ldMgr.getLogicalDeviceID(&voltha.Device{Id: "id", Root: true, ParentId: "parent_id"})
Hardik Windlass84861682019-11-10 05:44:33 +0000192 assert.NotNil(t, result0)
193 assert.Nil(t, error0)
194
195 /*** Case: Logical Device Id Not Found ***/
npujar03b018e2019-11-13 15:29:36 +0530196 result1, error1 := ldMgr.getLogicalDeviceID(&voltha.Device{Id: "id", ParentId: "parent_id"})
Hardik Windlass84861682019-11-10 05:44:33 +0000197 assert.Nil(t, result1)
198 assert.NotNil(t, error1)
199}
200
201func TestGetLogicalPortId(t *testing.T) {
202 wantResult := &voltha.LogicalPortId{}
203
204 core := MakeTestNewCoreConfig()
205 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
206 assert.NotNil(t, ldMgr)
207
208 /*** Case: Logical Port Id Not Found: getLogicalDeviceId() Error ***/
npujar03b018e2019-11-13 15:29:36 +0530209 result0, error0 := ldMgr.getLogicalPortID(&voltha.Device{Id: "id", ParentId: "parent_id"})
Hardik Windlass84861682019-11-10 05:44:33 +0000210 assert.Nil(t, result0)
211 assert.NotNil(t, error0)
212
213 /*** Case: Logical Port Id Not Found: getLogicalDevice() Error ***/
npujar03b018e2019-11-13 15:29:36 +0530214 result1, error1 := ldMgr.getLogicalPortID(&voltha.Device{Id: "id", Root: true, ParentId: "parent_id"})
Hardik Windlass84861682019-11-10 05:44:33 +0000215 assert.Nil(t, result1)
216 assert.NotNil(t, error1)
217
218 /*** Case: Logical Port Id Found ***/
219 device := &voltha.Device{Id: "id", Root: true, ParentId: "parent_id"}
220
221 // Add Data
222 if added := core.clusterDataProxy.Add(
223 context.Background(),
224 "/logical_devices",
225 &voltha.LogicalDevice{
226 Id: "parent_id",
227 Ports: []*voltha.LogicalPort{
228 {
229 Id: "123",
230 DeviceId: "id",
231 },
232 },
233 },
234 ""); added == nil {
235 t.Error("Failed to add logical device")
236 }
237 ldAgent := newLogicalDeviceAgent("parent_id", "device_id", ldMgr, core.deviceMgr, core.clusterDataProxy)
238 assert.NotNil(t, ldAgent)
239 ldMgr.addLogicalDeviceAgentToMap(ldAgent)
240
241 // Verify Add is successful
242 ldAgentNotNil := ldMgr.getLogicalDeviceAgent("parent_id")
243 assert.NotNil(t, ldAgentNotNil)
npujar03b018e2019-11-13 15:29:36 +0530244 assert.Equal(t, "parent_id", ldAgentNotNil.logicalDeviceID)
Hardik Windlass84861682019-11-10 05:44:33 +0000245
246 // Verify getLogicalPortId() is Success
npujar03b018e2019-11-13 15:29:36 +0530247 result2, error2 := ldMgr.getLogicalPortID(device)
Hardik Windlass84861682019-11-10 05:44:33 +0000248 assert.NotNil(t, result2)
249 assert.Nil(t, error2)
250 if reflect.TypeOf(result2) != reflect.TypeOf(wantResult) {
251 t.Errorf("GetLogicalPortId() = %v, want %v", result2, wantResult)
252 }
253 assert.Equal(t, "parent_id", result2.Id)
254}
255
256func TestListLogicalDevicePorts(t *testing.T) {
257 core := MakeTestNewCoreConfig()
258 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
259 assert.NotNil(t, ldMgr)
260
261 /*** Case: Logical Device Ports Not Found ***/
262 result0, error0 := ldMgr.ListLogicalDevicePorts(context.Background(), "id")
263 assert.Nil(t, result0)
264 assert.NotNil(t, error0)
265
266 /*** Case: Logical Device Ports Found ***/
267 wantResult := &voltha.LogicalPorts{
268 Items: []*voltha.LogicalPort{
269 {
270 Id: "123",
271 },
272 },
273 }
274
275 // Add Data
276 if added := core.clusterDataProxy.Add(
277 context.Background(),
278 "/logical_devices",
279 &voltha.LogicalDevice{
280 Id: "id",
281 Ports: []*voltha.LogicalPort{
282 {
283 Id: "123",
284 },
285 },
286 },
287 ""); added == nil {
288 t.Error("Failed to add logical device")
289 }
290 ldAgent := newLogicalDeviceAgent("id", "", ldMgr, core.deviceMgr, core.clusterDataProxy)
291 ldMgr.addLogicalDeviceAgentToMap(ldAgent)
292
293 // Verify Add is successful
294 ldAgentNotNil := ldMgr.getLogicalDeviceAgent("id")
295 assert.NotNil(t, ldAgentNotNil)
npujar03b018e2019-11-13 15:29:36 +0530296 assert.Equal(t, "id", ldAgentNotNil.logicalDeviceID)
Hardik Windlass84861682019-11-10 05:44:33 +0000297
298 // Verify ListLogicalDevicePorts() is Success
299 result1, error1 := ldMgr.ListLogicalDevicePorts(context.Background(), "id")
300 assert.NotNil(t, result1)
301 assert.Nil(t, error1)
302 if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) {
303 t.Errorf("ListLogicalDevicePorts() = %v, want %v", result1, wantResult)
304 }
305 assert.Equal(t, wantResult, result1)
306}
307
308func TestListLogicalDeviceFlows(t *testing.T) {
309 core := MakeTestNewCoreConfig()
310 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
311 assert.NotNil(t, ldMgr)
312
313 /*** Case: Logical Device Flows Not Found ***/
314 result0, error0 := ldMgr.ListLogicalDeviceFlows(context.Background(), "id")
315 assert.Nil(t, result0)
316 assert.NotNil(t, error0)
317
318 /*** Case: Logical Device Flows Found ***/
319 wantResult := &voltha.Flows{}
320
321 // Add Data
322 if added := core.clusterDataProxy.Add(
323 context.Background(),
324 "/logical_devices",
325 &voltha.LogicalDevice{
326 Id: "id",
327 Flows: &openflow_13.Flows{
328 Items: []*openflow_13.OfpFlowStats{
329 &openflow_13.OfpFlowStats{
330 Id: 1111,
331 },
332 },
333 },
334 },
335 ""); added == nil {
336 t.Error("Failed to add logical device")
337 }
338 ldAgent := newLogicalDeviceAgent("id", "", ldMgr, core.deviceMgr, core.clusterDataProxy)
339 ldMgr.addLogicalDeviceAgentToMap(ldAgent)
340
341 // Verify Add is successful
342 ldAgentNotNil := ldMgr.getLogicalDeviceAgent("id")
343 assert.NotNil(t, ldAgentNotNil)
npujar03b018e2019-11-13 15:29:36 +0530344 assert.Equal(t, "id", ldAgentNotNil.logicalDeviceID)
Hardik Windlass84861682019-11-10 05:44:33 +0000345
346 // Verify ListLogicalDeviceFlows() is Success
347 result1, error1 := ldMgr.ListLogicalDeviceFlows(context.Background(), "id")
348 assert.NotNil(t, result1)
349 assert.Nil(t, error1)
350 if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) {
351 t.Errorf("ListLogicalDeviceFlows() = %v, want %v", result1, wantResult)
352 }
353 assert.NotEmpty(t, result1)
354}
355
356func TestListLogicalDeviceFlowGroups(t *testing.T) {
357 core := MakeTestNewCoreConfig()
358 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
359 assert.NotNil(t, ldMgr)
360
361 /*** Case: Logical Device Flow Groups Not Found ***/
362 result0, error0 := ldMgr.ListLogicalDeviceFlowGroups(context.Background(), "id")
363 assert.Nil(t, result0)
364 assert.NotNil(t, error0)
365
366 /*** Case: Logical Device Flow Groups Found ***/
367 wantResult := &voltha.FlowGroups{}
368
369 // Add Data
370 if added := core.clusterDataProxy.Add(
371 context.Background(),
372 "/logical_devices",
373 &voltha.LogicalDevice{
374 Id: "id",
375 FlowGroups: &openflow_13.FlowGroups{
376 Items: []*openflow_13.OfpGroupEntry{
377 {
378 Stats: &openflow_13.OfpGroupStats{
379 GroupId: 1,
380 },
381 },
382 },
383 },
384 },
385 ""); added == nil {
386 t.Error("Failed to add logical device")
387 }
388 ldAgent := newLogicalDeviceAgent("id", "", ldMgr, core.deviceMgr, core.clusterDataProxy)
389 ldMgr.addLogicalDeviceAgentToMap(ldAgent)
390
391 // Verify Add is successful
392 ldAgentNotNil := ldMgr.getLogicalDeviceAgent("id")
393 assert.NotNil(t, ldAgentNotNil)
npujar03b018e2019-11-13 15:29:36 +0530394 assert.Equal(t, "id", ldAgentNotNil.logicalDeviceID)
Hardik Windlass84861682019-11-10 05:44:33 +0000395
396 // Verify ListLogicalDeviceFlowGroups() is Success
397 result1, error1 := ldMgr.ListLogicalDeviceFlowGroups(context.Background(), "id")
398 assert.NotNil(t, result1)
399 assert.Nil(t, error1)
400 if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) {
401 t.Errorf("ListLogicalDeviceFlowGroups() = %v, want %v", result1, wantResult)
402 }
403 assert.NotEmpty(t, result1)
404}
405
406func TestGetLogicalPort(t *testing.T) {
407 core := MakeTestNewCoreConfig()
408 ldMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
409 assert.NotNil(t, ldMgr)
410
411 /*** Case: Logical Port Not Found: getLogicalDevice() Error ***/
412 result0, error0 := ldMgr.getLogicalPort(&voltha.LogicalPortId{Id: "id", PortId: "123"})
413 assert.Nil(t, result0)
414 assert.NotNil(t, error0)
415
416 /*** Case: Logical Port Found ***/
417 wantResult := &voltha.LogicalPort{Id: "123"}
418
419 // Add Data
420 if added := core.clusterDataProxy.Add(
421 context.Background(),
422 "/logical_devices",
423 &voltha.LogicalDevice{
424 Id: "id",
425 Ports: []*voltha.LogicalPort{
426 {
427 Id: "123",
428 },
429 },
430 },
431 ""); added == nil {
432 t.Error("Failed to add logical device")
433 }
434 ldAgent := newLogicalDeviceAgent("id", "", ldMgr, core.deviceMgr, core.clusterDataProxy)
435 ldMgr.addLogicalDeviceAgentToMap(ldAgent)
436
437 // Verify Add is successful
438 ldAgentNotNil := ldMgr.getLogicalDeviceAgent("id")
439 assert.NotNil(t, ldAgentNotNil)
npujar03b018e2019-11-13 15:29:36 +0530440 assert.Equal(t, "id", ldAgentNotNil.logicalDeviceID)
Hardik Windlass84861682019-11-10 05:44:33 +0000441
442 // Verify getLogicalPort() is Success
443 result1, error1 := ldMgr.getLogicalPort(&voltha.LogicalPortId{Id: "id", PortId: "123"})
444 assert.NotNil(t, result1)
445 assert.Nil(t, error1)
446 if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) {
447 t.Errorf("getLogicalPort() = %v, want %v", result1, wantResult)
448 }
449 assert.Equal(t, wantResult, result1)
450}