David Bainbridge | 36d0b20 | 2019-10-23 18:33:34 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package common |
| 17 | |
| 18 | import ( |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 19 | "context" |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 20 | adapterIf "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif" |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 21 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 22 | mocks "github.com/opencord/voltha-lib-go/v3/pkg/mocks/kafka" |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 23 | 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 Bainbridge | 36d0b20 | 2019-10-23 18:33:34 +0000 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | func 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 Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 39 | |
Matteo Scandolo | d58eaef | 2020-03-30 12:30:02 -0700 | [diff] [blame] | 40 | func 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 | |
| 48 | proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic") |
| 49 | |
| 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 | |
| 83 | func 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 | |
| 91 | proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic") |
| 92 | |
| 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 Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 122 | func TestCoreProxy_GetChildDevice_sn(t *testing.T) { |
| 123 | |
| 124 | var mockKafkaIcProxy = mocks.MockKafkaICProxy{ |
| 125 | InvokeRpcSpy: mocks.InvokeRpcSpy{ |
Matteo Scandolo | b45cf59 | 2020-01-21 16:10:56 -0800 | [diff] [blame] | 126 | Calls: make(map[int]mocks.InvokeRpcArgs), |
| 127 | Response: &voltha.Device{Id: "testDevice"}, |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 128 | }, |
| 129 | } |
| 130 | |
| 131 | proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic") |
| 132 | |
| 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 | |
| 153 | func TestCoreProxy_GetChildDevice_id(t *testing.T) { |
| 154 | |
| 155 | var mockKafkaIcProxy = mocks.MockKafkaICProxy{ |
| 156 | InvokeRpcSpy: mocks.InvokeRpcSpy{ |
Matteo Scandolo | b45cf59 | 2020-01-21 16:10:56 -0800 | [diff] [blame] | 157 | Calls: make(map[int]mocks.InvokeRpcArgs), |
| 158 | Response: &voltha.Device{Id: "testDevice"}, |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 159 | }, |
| 160 | } |
| 161 | |
| 162 | proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic") |
| 163 | |
| 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 | |
| 184 | func TestCoreProxy_GetChildDevice_fail_timeout(t *testing.T) { |
| 185 | |
| 186 | var mockKafkaIcProxy = mocks.MockKafkaICProxy{ |
| 187 | InvokeRpcSpy: mocks.InvokeRpcSpy{ |
Matteo Scandolo | b45cf59 | 2020-01-21 16:10:56 -0800 | [diff] [blame] | 188 | Calls: make(map[int]mocks.InvokeRpcArgs), |
| 189 | Timeout: true, |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 190 | }, |
| 191 | } |
| 192 | |
| 193 | proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic") |
| 194 | |
| 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 Scandolo | b45cf59 | 2020-01-21 16:10:56 -0800 | [diff] [blame] | 204 | assert.Equal(t, parsedErr.Code(), codes.DeadlineExceeded) |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | func TestCoreProxy_GetChildDevice_fail_unmarhsal(t *testing.T) { |
| 208 | |
| 209 | var mockKafkaIcProxy = mocks.MockKafkaICProxy{ |
| 210 | InvokeRpcSpy: mocks.InvokeRpcSpy{ |
Matteo Scandolo | b45cf59 | 2020-01-21 16:10:56 -0800 | [diff] [blame] | 211 | Calls: make(map[int]mocks.InvokeRpcArgs), |
| 212 | Response: &voltha.LogicalDevice{Id: "testDevice"}, |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 213 | }, |
| 214 | } |
| 215 | |
| 216 | proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic") |
| 217 | |
| 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 Scandolo | b45cf59 | 2020-01-21 16:10:56 -0800 | [diff] [blame] | 229 | |
| 230 | func 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 | |
| 244 | proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic") |
| 245 | |
| 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 | |
| 262 | func 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 | |
| 271 | proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic") |
| 272 | |
| 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 | |
| 282 | func 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 | |
| 291 | proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic") |
| 292 | |
| 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 | } |