blob: 97a88f5aed111b3f58fc0b7ab48fda81afd12dd9 [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"
Kent Hagerman143fea32020-07-10 15:28:55 -040020 "testing"
21
Girish Gowdra89c985b2020-10-14 15:02:09 -070022 adapterIf "github.com/opencord/voltha-lib-go/v4/pkg/adapters/adapterif"
23 "github.com/opencord/voltha-lib-go/v4/pkg/kafka"
24 mocks "github.com/opencord/voltha-lib-go/v4/pkg/mocks/kafka"
25 ic "github.com/opencord/voltha-protos/v4/go/inter_container"
26 "github.com/opencord/voltha-protos/v4/go/voltha"
Matteo Scandolo2ba00d32020-01-16 17:33:03 -080027 "github.com/stretchr/testify/assert"
28 "google.golang.org/grpc/codes"
29 "google.golang.org/grpc/status"
David Bainbridge36d0b202019-10-23 18:33:34 +000030)
31
32func TestCoreProxyImplementsAdapterIfCoreProxy(t *testing.T) {
33 proxy := &CoreProxy{}
34
35 if _, ok := interface{}(proxy).(adapterIf.CoreProxy); !ok {
36 t.Error("common CoreProxy does not implement adapterif.CoreProxy interface")
37 }
38
39}
Matteo Scandolo2ba00d32020-01-16 17:33:03 -080040
Matteo Scandolod58eaef2020-03-30 12:30:02 -070041func TestCoreProxy_RegisterAdapter_default(t *testing.T) {
42 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
43 InvokeRpcSpy: mocks.InvokeRpcSpy{
44 Calls: make(map[int]mocks.InvokeRpcArgs),
45 Response: &voltha.Device{Id: "testDevice"},
46 },
47 }
48
Neha Sharma94f16a92020-06-26 04:17:55 +000049 proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
Matteo Scandolod58eaef2020-03-30 12:30:02 -070050
51 adapter := &voltha.Adapter{
52 Id: "testAdapter",
53 Vendor: "ONF",
54 Version: "1.0.0",
55 }
56 types := []*voltha.DeviceType{{
57 Id: "testolt",
58 Adapter: "testAdapter",
59 AcceptsBulkFlowUpdate: true,
60 }}
61 devices := &voltha.DeviceTypes{Items: types}
62
63 err := proxy.RegisterAdapter(context.TODO(), adapter, devices)
64
65 assert.Equal(t, mockKafkaIcProxy.InvokeRpcSpy.CallCount, 1)
66 assert.Equal(t, nil, err)
67
68 call := mockKafkaIcProxy.InvokeRpcSpy.Calls[1]
69 assert.Equal(t, call.Rpc, "Register")
70 assert.Equal(t, call.ToTopic, &kafka.Topic{Name: "testCoreTopic"})
71 assert.Equal(t, call.ReplyToTopic, &kafka.Topic{Name: "testAdapterTopic"})
72 assert.Equal(t, call.WaitForResponse, true)
73 assert.Equal(t, call.Key, "")
74 assert.Equal(t, call.KvArgs[0], &kafka.KVArg{Key: "adapter", Value: &voltha.Adapter{
75 Id: adapter.Id,
76 Vendor: adapter.Vendor,
77 Version: adapter.Version,
78 CurrentReplica: 1,
79 TotalReplicas: 1,
80 }})
81 assert.Equal(t, call.KvArgs[1], &kafka.KVArg{Key: "deviceTypes", Value: devices})
82}
83
84func TestCoreProxy_RegisterAdapter_multiple(t *testing.T) {
85 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
86 InvokeRpcSpy: mocks.InvokeRpcSpy{
87 Calls: make(map[int]mocks.InvokeRpcArgs),
88 Response: &voltha.Device{Id: "testDevice"},
89 },
90 }
91
Neha Sharma94f16a92020-06-26 04:17:55 +000092 proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
Matteo Scandolod58eaef2020-03-30 12:30:02 -070093
94 adapter := &voltha.Adapter{
95 Id: "testAdapter",
96 Vendor: "ONF",
97 Version: "1.0.0",
98 CurrentReplica: 4,
99 TotalReplicas: 8,
100 }
101 types := []*voltha.DeviceType{{
102 Id: "testolt",
103 Adapter: "testAdapter",
104 AcceptsBulkFlowUpdate: true,
105 }}
106 devices := &voltha.DeviceTypes{Items: types}
107
108 err := proxy.RegisterAdapter(context.TODO(), adapter, devices)
109
110 assert.Equal(t, mockKafkaIcProxy.InvokeRpcSpy.CallCount, 1)
111 assert.Equal(t, nil, err)
112
113 call := mockKafkaIcProxy.InvokeRpcSpy.Calls[1]
114 assert.Equal(t, call.KvArgs[0], &kafka.KVArg{Key: "adapter", Value: &voltha.Adapter{
115 Id: adapter.Id,
116 Vendor: adapter.Vendor,
117 Version: adapter.Version,
118 CurrentReplica: 4,
119 TotalReplicas: 8,
120 }})
121}
122
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800123func TestCoreProxy_GetChildDevice_sn(t *testing.T) {
124
125 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
126 InvokeRpcSpy: mocks.InvokeRpcSpy{
Matteo Scandolob45cf592020-01-21 16:10:56 -0800127 Calls: make(map[int]mocks.InvokeRpcArgs),
128 Response: &voltha.Device{Id: "testDevice"},
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800129 },
130 }
131
Neha Sharma94f16a92020-06-26 04:17:55 +0000132 proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800133
134 kwargs := make(map[string]interface{})
135 kwargs["serial_number"] = "TEST00000000001"
136
137 parentDeviceId := "aabbcc"
138 device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs)
139
140 assert.Equal(t, mockKafkaIcProxy.InvokeRpcSpy.CallCount, 1)
141 call := mockKafkaIcProxy.InvokeRpcSpy.Calls[1]
142 assert.Equal(t, call.Rpc, "GetChildDevice")
143 assert.Equal(t, call.ToTopic, &kafka.Topic{Name: "testCoreTopic"})
144 assert.Equal(t, call.ReplyToTopic, &kafka.Topic{Name: "testAdapterTopic"})
145 assert.Equal(t, call.WaitForResponse, true)
146 assert.Equal(t, call.Key, parentDeviceId)
147 assert.Equal(t, call.KvArgs[0], &kafka.KVArg{Key: "device_id", Value: &voltha.ID{Id: parentDeviceId}})
148 assert.Equal(t, call.KvArgs[1], &kafka.KVArg{Key: "serial_number", Value: &ic.StrType{Val: kwargs["serial_number"].(string)}})
149
150 assert.Equal(t, "testDevice", device.Id)
151 assert.Equal(t, nil, error)
152}
153
154func TestCoreProxy_GetChildDevice_id(t *testing.T) {
155
156 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
157 InvokeRpcSpy: mocks.InvokeRpcSpy{
Matteo Scandolob45cf592020-01-21 16:10:56 -0800158 Calls: make(map[int]mocks.InvokeRpcArgs),
159 Response: &voltha.Device{Id: "testDevice"},
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800160 },
161 }
162
Neha Sharma94f16a92020-06-26 04:17:55 +0000163 proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800164
165 kwargs := make(map[string]interface{})
166 kwargs["onu_id"] = uint32(1234)
167
168 parentDeviceId := "aabbcc"
169 device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs)
170
171 assert.Equal(t, mockKafkaIcProxy.InvokeRpcSpy.CallCount, 1)
172 call := mockKafkaIcProxy.InvokeRpcSpy.Calls[1]
173 assert.Equal(t, call.Rpc, "GetChildDevice")
174 assert.Equal(t, call.ToTopic, &kafka.Topic{Name: "testCoreTopic"})
175 assert.Equal(t, call.ReplyToTopic, &kafka.Topic{Name: "testAdapterTopic"})
176 assert.Equal(t, call.WaitForResponse, true)
177 assert.Equal(t, call.Key, parentDeviceId)
178 assert.Equal(t, call.KvArgs[0], &kafka.KVArg{Key: "device_id", Value: &voltha.ID{Id: parentDeviceId}})
179 assert.Equal(t, call.KvArgs[1], &kafka.KVArg{Key: "onu_id", Value: &ic.IntType{Val: int64(kwargs["onu_id"].(uint32))}})
180
181 assert.Equal(t, "testDevice", device.Id)
182 assert.Equal(t, nil, error)
183}
184
185func TestCoreProxy_GetChildDevice_fail_timeout(t *testing.T) {
186
187 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
188 InvokeRpcSpy: mocks.InvokeRpcSpy{
Matteo Scandolob45cf592020-01-21 16:10:56 -0800189 Calls: make(map[int]mocks.InvokeRpcArgs),
190 Timeout: true,
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800191 },
192 }
193
Neha Sharma94f16a92020-06-26 04:17:55 +0000194 proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800195
196 kwargs := make(map[string]interface{})
197 kwargs["onu_id"] = uint32(1234)
198
199 parentDeviceId := "aabbcc"
200 device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs)
201
202 assert.Nil(t, device)
203 parsedErr, _ := status.FromError(error)
204
Matteo Scandolob45cf592020-01-21 16:10:56 -0800205 assert.Equal(t, parsedErr.Code(), codes.DeadlineExceeded)
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800206}
207
208func TestCoreProxy_GetChildDevice_fail_unmarhsal(t *testing.T) {
209
210 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
211 InvokeRpcSpy: mocks.InvokeRpcSpy{
Matteo Scandolob45cf592020-01-21 16:10:56 -0800212 Calls: make(map[int]mocks.InvokeRpcArgs),
213 Response: &voltha.LogicalDevice{Id: "testDevice"},
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800214 },
215 }
216
Neha Sharma94f16a92020-06-26 04:17:55 +0000217 proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800218
219 kwargs := make(map[string]interface{})
220 kwargs["onu_id"] = uint32(1234)
221
222 parentDeviceId := "aabbcc"
223 device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs)
224
225 assert.Nil(t, device)
226
227 parsedErr, _ := status.FromError(error)
228 assert.Equal(t, parsedErr.Code(), codes.InvalidArgument)
229}
Matteo Scandolob45cf592020-01-21 16:10:56 -0800230
231func TestCoreProxy_GetChildDevices_success(t *testing.T) {
Kent Hagerman143fea32020-07-10 15:28:55 -0400232 devicesResponse := &voltha.Devices{Items: []*voltha.Device{
233 {Id: "testDevice1"},
234 {Id: "testDevice2"},
235 }}
Matteo Scandolob45cf592020-01-21 16:10:56 -0800236
237 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
238 InvokeRpcSpy: mocks.InvokeRpcSpy{
239 Calls: make(map[int]mocks.InvokeRpcArgs),
240 Response: devicesResponse,
241 },
242 }
243
Neha Sharma94f16a92020-06-26 04:17:55 +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 Sharma94f16a92020-06-26 04:17:55 +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 Sharma94f16a92020-06-26 04:17:55 +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}