blob: 2fb4df7a143a615072259a0d398ec33e82b4774f [file] [log] [blame]
David Bainbridge36d0b202019-10-23 18:33:34 +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 common
17
18import (
Matteo Scandolo2ba00d32020-01-16 17:33:03 -080019 "context"
serkant.uluderyab38671c2019-11-01 09:35:38 -070020 adapterIf "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif"
Matteo Scandolo2ba00d32020-01-16 17:33:03 -080021 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
khenaidoob6238b32020-04-07 12:07:36 -040022 mocks "github.com/opencord/voltha-lib-go/v3/pkg/mocks/kafka"
Matteo Scandolo2ba00d32020-01-16 17:33:03 -080023 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
24 "github.com/opencord/voltha-protos/v3/go/voltha"
25 "github.com/stretchr/testify/assert"
26 "google.golang.org/grpc/codes"
27 "google.golang.org/grpc/status"
28 "testing"
David Bainbridge36d0b202019-10-23 18:33:34 +000029)
30
31func TestCoreProxyImplementsAdapterIfCoreProxy(t *testing.T) {
32 proxy := &CoreProxy{}
33
34 if _, ok := interface{}(proxy).(adapterIf.CoreProxy); !ok {
35 t.Error("common CoreProxy does not implement adapterif.CoreProxy interface")
36 }
37
38}
Matteo Scandolo2ba00d32020-01-16 17:33:03 -080039
Matteo Scandolod58eaef2020-03-30 12:30:02 -070040func TestCoreProxy_RegisterAdapter_default(t *testing.T) {
41 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
42 InvokeRpcSpy: mocks.InvokeRpcSpy{
43 Calls: make(map[int]mocks.InvokeRpcArgs),
44 Response: &voltha.Device{Id: "testDevice"},
45 },
46 }
47
Neha Sharma3c425fb2020-06-08 16:42:32 +000048 proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
Matteo Scandolod58eaef2020-03-30 12:30:02 -070049
50 adapter := &voltha.Adapter{
51 Id: "testAdapter",
52 Vendor: "ONF",
53 Version: "1.0.0",
54 }
55 types := []*voltha.DeviceType{{
56 Id: "testolt",
57 Adapter: "testAdapter",
58 AcceptsBulkFlowUpdate: true,
59 }}
60 devices := &voltha.DeviceTypes{Items: types}
61
62 err := proxy.RegisterAdapter(context.TODO(), adapter, devices)
63
64 assert.Equal(t, mockKafkaIcProxy.InvokeRpcSpy.CallCount, 1)
65 assert.Equal(t, nil, err)
66
67 call := mockKafkaIcProxy.InvokeRpcSpy.Calls[1]
68 assert.Equal(t, call.Rpc, "Register")
69 assert.Equal(t, call.ToTopic, &kafka.Topic{Name: "testCoreTopic"})
70 assert.Equal(t, call.ReplyToTopic, &kafka.Topic{Name: "testAdapterTopic"})
71 assert.Equal(t, call.WaitForResponse, true)
72 assert.Equal(t, call.Key, "")
73 assert.Equal(t, call.KvArgs[0], &kafka.KVArg{Key: "adapter", Value: &voltha.Adapter{
74 Id: adapter.Id,
75 Vendor: adapter.Vendor,
76 Version: adapter.Version,
77 CurrentReplica: 1,
78 TotalReplicas: 1,
79 }})
80 assert.Equal(t, call.KvArgs[1], &kafka.KVArg{Key: "deviceTypes", Value: devices})
81}
82
83func TestCoreProxy_RegisterAdapter_multiple(t *testing.T) {
84 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
85 InvokeRpcSpy: mocks.InvokeRpcSpy{
86 Calls: make(map[int]mocks.InvokeRpcArgs),
87 Response: &voltha.Device{Id: "testDevice"},
88 },
89 }
90
Neha Sharma3c425fb2020-06-08 16:42:32 +000091 proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
Matteo Scandolod58eaef2020-03-30 12:30:02 -070092
93 adapter := &voltha.Adapter{
94 Id: "testAdapter",
95 Vendor: "ONF",
96 Version: "1.0.0",
97 CurrentReplica: 4,
98 TotalReplicas: 8,
99 }
100 types := []*voltha.DeviceType{{
101 Id: "testolt",
102 Adapter: "testAdapter",
103 AcceptsBulkFlowUpdate: true,
104 }}
105 devices := &voltha.DeviceTypes{Items: types}
106
107 err := proxy.RegisterAdapter(context.TODO(), adapter, devices)
108
109 assert.Equal(t, mockKafkaIcProxy.InvokeRpcSpy.CallCount, 1)
110 assert.Equal(t, nil, err)
111
112 call := mockKafkaIcProxy.InvokeRpcSpy.Calls[1]
113 assert.Equal(t, call.KvArgs[0], &kafka.KVArg{Key: "adapter", Value: &voltha.Adapter{
114 Id: adapter.Id,
115 Vendor: adapter.Vendor,
116 Version: adapter.Version,
117 CurrentReplica: 4,
118 TotalReplicas: 8,
119 }})
120}
121
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800122func TestCoreProxy_GetChildDevice_sn(t *testing.T) {
123
124 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
125 InvokeRpcSpy: mocks.InvokeRpcSpy{
Matteo Scandolob45cf592020-01-21 16:10:56 -0800126 Calls: make(map[int]mocks.InvokeRpcArgs),
127 Response: &voltha.Device{Id: "testDevice"},
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800128 },
129 }
130
Neha Sharma3c425fb2020-06-08 16:42:32 +0000131 proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800132
133 kwargs := make(map[string]interface{})
134 kwargs["serial_number"] = "TEST00000000001"
135
136 parentDeviceId := "aabbcc"
137 device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs)
138
139 assert.Equal(t, mockKafkaIcProxy.InvokeRpcSpy.CallCount, 1)
140 call := mockKafkaIcProxy.InvokeRpcSpy.Calls[1]
141 assert.Equal(t, call.Rpc, "GetChildDevice")
142 assert.Equal(t, call.ToTopic, &kafka.Topic{Name: "testCoreTopic"})
143 assert.Equal(t, call.ReplyToTopic, &kafka.Topic{Name: "testAdapterTopic"})
144 assert.Equal(t, call.WaitForResponse, true)
145 assert.Equal(t, call.Key, parentDeviceId)
146 assert.Equal(t, call.KvArgs[0], &kafka.KVArg{Key: "device_id", Value: &voltha.ID{Id: parentDeviceId}})
147 assert.Equal(t, call.KvArgs[1], &kafka.KVArg{Key: "serial_number", Value: &ic.StrType{Val: kwargs["serial_number"].(string)}})
148
149 assert.Equal(t, "testDevice", device.Id)
150 assert.Equal(t, nil, error)
151}
152
153func TestCoreProxy_GetChildDevice_id(t *testing.T) {
154
155 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
156 InvokeRpcSpy: mocks.InvokeRpcSpy{
Matteo Scandolob45cf592020-01-21 16:10:56 -0800157 Calls: make(map[int]mocks.InvokeRpcArgs),
158 Response: &voltha.Device{Id: "testDevice"},
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800159 },
160 }
161
Neha Sharma3c425fb2020-06-08 16:42:32 +0000162 proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800163
164 kwargs := make(map[string]interface{})
165 kwargs["onu_id"] = uint32(1234)
166
167 parentDeviceId := "aabbcc"
168 device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs)
169
170 assert.Equal(t, mockKafkaIcProxy.InvokeRpcSpy.CallCount, 1)
171 call := mockKafkaIcProxy.InvokeRpcSpy.Calls[1]
172 assert.Equal(t, call.Rpc, "GetChildDevice")
173 assert.Equal(t, call.ToTopic, &kafka.Topic{Name: "testCoreTopic"})
174 assert.Equal(t, call.ReplyToTopic, &kafka.Topic{Name: "testAdapterTopic"})
175 assert.Equal(t, call.WaitForResponse, true)
176 assert.Equal(t, call.Key, parentDeviceId)
177 assert.Equal(t, call.KvArgs[0], &kafka.KVArg{Key: "device_id", Value: &voltha.ID{Id: parentDeviceId}})
178 assert.Equal(t, call.KvArgs[1], &kafka.KVArg{Key: "onu_id", Value: &ic.IntType{Val: int64(kwargs["onu_id"].(uint32))}})
179
180 assert.Equal(t, "testDevice", device.Id)
181 assert.Equal(t, nil, error)
182}
183
184func TestCoreProxy_GetChildDevice_fail_timeout(t *testing.T) {
185
186 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
187 InvokeRpcSpy: mocks.InvokeRpcSpy{
Matteo Scandolob45cf592020-01-21 16:10:56 -0800188 Calls: make(map[int]mocks.InvokeRpcArgs),
189 Timeout: true,
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800190 },
191 }
192
Neha Sharma3c425fb2020-06-08 16:42:32 +0000193 proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800194
195 kwargs := make(map[string]interface{})
196 kwargs["onu_id"] = uint32(1234)
197
198 parentDeviceId := "aabbcc"
199 device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs)
200
201 assert.Nil(t, device)
202 parsedErr, _ := status.FromError(error)
203
Matteo Scandolob45cf592020-01-21 16:10:56 -0800204 assert.Equal(t, parsedErr.Code(), codes.DeadlineExceeded)
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800205}
206
207func TestCoreProxy_GetChildDevice_fail_unmarhsal(t *testing.T) {
208
209 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
210 InvokeRpcSpy: mocks.InvokeRpcSpy{
Matteo Scandolob45cf592020-01-21 16:10:56 -0800211 Calls: make(map[int]mocks.InvokeRpcArgs),
212 Response: &voltha.LogicalDevice{Id: "testDevice"},
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800213 },
214 }
215
Neha Sharma3c425fb2020-06-08 16:42:32 +0000216 proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800217
218 kwargs := make(map[string]interface{})
219 kwargs["onu_id"] = uint32(1234)
220
221 parentDeviceId := "aabbcc"
222 device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs)
223
224 assert.Nil(t, device)
225
226 parsedErr, _ := status.FromError(error)
227 assert.Equal(t, parsedErr.Code(), codes.InvalidArgument)
228}
Matteo Scandolob45cf592020-01-21 16:10:56 -0800229
230func TestCoreProxy_GetChildDevices_success(t *testing.T) {
231
232 devicesResponse := &voltha.Devices{}
233
234 devicesResponse.Items = append(devicesResponse.Items, &voltha.Device{Id: "testDevice1"})
235 devicesResponse.Items = append(devicesResponse.Items, &voltha.Device{Id: "testDevice2"})
236
237 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
238 InvokeRpcSpy: mocks.InvokeRpcSpy{
239 Calls: make(map[int]mocks.InvokeRpcArgs),
240 Response: devicesResponse,
241 },
242 }
243
Neha Sharma3c425fb2020-06-08 16:42:32 +0000244 proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
Matteo Scandolob45cf592020-01-21 16:10:56 -0800245
246 parentDeviceId := "aabbcc"
247 devices, error := proxy.GetChildDevices(context.TODO(), parentDeviceId)
248
249 assert.Equal(t, mockKafkaIcProxy.InvokeRpcSpy.CallCount, 1)
250 call := mockKafkaIcProxy.InvokeRpcSpy.Calls[1]
251 assert.Equal(t, call.Rpc, "GetChildDevices")
252 assert.Equal(t, call.ToTopic, &kafka.Topic{Name: "testCoreTopic"})
253 assert.Equal(t, call.ReplyToTopic, &kafka.Topic{Name: "testAdapterTopic"})
254 assert.Equal(t, call.WaitForResponse, true)
255 assert.Equal(t, call.Key, parentDeviceId)
256 assert.Equal(t, call.KvArgs[0], &kafka.KVArg{Key: "device_id", Value: &voltha.ID{Id: parentDeviceId}})
257
258 assert.Equal(t, nil, error)
259 assert.Equal(t, 2, len(devices.Items))
260}
261
262func TestCoreProxy_GetChildDevices_fail_unmarhsal(t *testing.T) {
263
264 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
265 InvokeRpcSpy: mocks.InvokeRpcSpy{
266 Calls: make(map[int]mocks.InvokeRpcArgs),
267 Response: &voltha.LogicalDevice{Id: "testDevice"},
268 },
269 }
270
Neha Sharma3c425fb2020-06-08 16:42:32 +0000271 proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
Matteo Scandolob45cf592020-01-21 16:10:56 -0800272
273 parentDeviceId := "aabbcc"
274 devices, error := proxy.GetChildDevices(context.TODO(), parentDeviceId)
275
276 assert.Nil(t, devices)
277
278 parsedErr, _ := status.FromError(error)
279 assert.Equal(t, parsedErr.Code(), codes.InvalidArgument)
280}
281
282func TestCoreProxy_GetChildDevices_fail_timeout(t *testing.T) {
283
284 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
285 InvokeRpcSpy: mocks.InvokeRpcSpy{
286 Calls: make(map[int]mocks.InvokeRpcArgs),
287 Timeout: true,
288 },
289 }
290
Neha Sharma3c425fb2020-06-08 16:42:32 +0000291 proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
Matteo Scandolob45cf592020-01-21 16:10:56 -0800292
293 parentDeviceId := "aabbcc"
294 devices, error := proxy.GetChildDevices(context.TODO(), parentDeviceId)
295
296 assert.Nil(t, devices)
297
298 parsedErr, _ := status.FromError(error)
299 assert.Equal(t, parsedErr.Code(), codes.DeadlineExceeded)
300}