Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 model |
| 17 | |
| 18 | import ( |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 19 | "encoding/hex" |
| 20 | "encoding/json" |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 21 | "fmt" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 22 | "github.com/google/uuid" |
| 23 | "github.com/opencord/voltha-go/common/log" |
| 24 | "github.com/opencord/voltha-go/protos/voltha" |
| 25 | "reflect" |
| 26 | "strconv" |
| 27 | "testing" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | type proxyTest struct { |
| 31 | Root *Root |
| 32 | Backend *Backend |
| 33 | Proxy *Proxy |
| 34 | DbPrefix string |
| 35 | DbType string |
| 36 | DbHost string |
| 37 | DbPort int |
| 38 | DbTimeout int |
| 39 | } |
| 40 | |
| 41 | var ( |
| 42 | pt = &proxyTest{ |
| 43 | DbPrefix: "service/voltha/data/core/0001", |
| 44 | DbType: "etcd", |
| 45 | //DbHost: "10.102.58.0", |
| 46 | DbHost: "localhost", |
| 47 | DbPort: 2379, |
| 48 | DbTimeout: 5, |
| 49 | } |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 50 | devId string |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 51 | targetDeviceId string |
| 52 | ) |
| 53 | |
| 54 | func init() { |
| 55 | if _, err := log.SetLogger(log.CONSOLE, 0, log.Fields{"instanceId": "proxy_test"}); err != nil { |
| 56 | log.With(log.Fields{"error": err}).Fatal("cannot setup logging") |
| 57 | } |
| 58 | defer log.CleanUp() |
| 59 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 60 | pt.Backend = NewBackend(pt.DbType, pt.DbHost, pt.DbPort, pt.DbTimeout, pt.DbPrefix) |
| 61 | |
| 62 | msgClass := &voltha.Voltha{} |
| 63 | root := NewRoot(msgClass, pt.Backend, nil) |
| 64 | pt.Root = root.Load(msgClass) |
| 65 | |
| 66 | GetProfiling().Report() |
| 67 | |
| 68 | pt.Proxy = pt.Root.Node.GetProxy("/", false) |
| 69 | } |
| 70 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 71 | //func Test_Proxy_0_GetRootProxy(t *testing.T) { |
| 72 | // pt.Backend = NewBackend(pt.DbType, pt.DbHost, pt.DbPort, pt.DbTimeout, pt.DbPrefix) |
| 73 | // |
| 74 | // msgClass := &voltha.Voltha{} |
| 75 | // root := NewRoot(msgClass, pt.Backend, nil) |
| 76 | // pt.Root = root.Load(msgClass) |
| 77 | // |
| 78 | // GetProfiling().Report() |
| 79 | // |
| 80 | // pt.Proxy = pt.Root.Node.GetProxy("/", false) |
| 81 | //} |
| 82 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 83 | func Test_Proxy_1_GetDevices(t *testing.T) { |
| 84 | devices := pt.Proxy.Get("/devices", 1, false, "") |
| 85 | |
| 86 | if len(devices.([]interface{})) == 0 { |
| 87 | t.Error("there are no available devices to retrieve") |
| 88 | } else { |
| 89 | // Save the target device id for later tests |
| 90 | targetDeviceId = devices.([]interface{})[0].(*voltha.Device).Id |
| 91 | t.Logf("retrieved devices: %+v", devices) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func Test_Proxy_2_GetDevice(t *testing.T) { |
| 96 | basePath := "/devices/" + targetDeviceId |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 97 | device1 := pt.Proxy.Get(basePath+"/ports", 1, false, "") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 98 | t.Logf("retrieved device with ports: %+v", device1) |
| 99 | |
| 100 | device2 := pt.Proxy.Get(basePath, 0, false, "") |
| 101 | |
| 102 | t.Logf("retrieved device: %+v", device2) |
| 103 | } |
| 104 | |
| 105 | //func Test_Proxy_3_AddDevice(t *testing.T) { |
| 106 | // //ports := []*voltha.Port{ |
| 107 | // // { |
| 108 | // // PortNo: 123, |
| 109 | // // Label: "test-port-0", |
| 110 | // // Type: voltha.Port_PON_OLT, |
| 111 | // // AdminState: common.AdminState_ENABLED, |
| 112 | // // OperStatus: common.OperStatus_ACTIVE, |
| 113 | // // DeviceId: "etcd_port-0-device-id", |
| 114 | // // Peers: []*voltha.Port_PeerPort{}, |
| 115 | // // }, |
| 116 | // //} |
| 117 | // devIdBin, _ := uuid.New().MarshalBinary() |
| 118 | // devId := hex.EncodeToString(devIdBin)[:12] |
| 119 | // |
| 120 | // device := &voltha.Device{ |
| 121 | // Id: devId, |
| 122 | // Type: "simulated_olt", |
| 123 | // //Root: true, |
| 124 | // //ParentId: "", |
| 125 | // //ParentPortNo: 0, |
| 126 | // //Vendor: "voltha-test", |
| 127 | // //Model: "latest-voltha-simulated-olt", |
| 128 | // //HardwareVersion: "1.0.0", |
| 129 | // //FirmwareVersion: "1.0.0", |
| 130 | // //Images: &voltha.Images{}, |
| 131 | // //SerialNumber: "abcdef-123456", |
| 132 | // //VendorId: "DEADBEEF-INC", |
| 133 | // //Adapter: "simulated_olt", |
| 134 | // //Vlan: 1234, |
| 135 | // Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"}, |
| 136 | // //ExtraArgs: "", |
| 137 | // //ProxyAddress: &voltha.Device_ProxyAddress{}, |
| 138 | // AdminState: voltha.AdminState_PREPROVISIONED, |
| 139 | // //OperStatus: common.OperStatus_ACTIVE, |
| 140 | // //Reason: "", |
| 141 | // //ConnectStatus: common.ConnectStatus_REACHABLE, |
| 142 | // //Custom: &any.Any{}, |
| 143 | // //Ports: ports, |
| 144 | // //Flows: &openflow_13.Flows{}, |
| 145 | // //FlowGroups: &openflow_13.FlowGroups{}, |
| 146 | // //PmConfigs: &voltha.PmConfigs{}, |
| 147 | // //ImageDownloads: []*voltha.ImageDownload{}, |
| 148 | // } |
| 149 | // |
| 150 | // //if retrieved := pt.Proxy.Get("/devices/00019b09a90bbe17", 0, false, ""); retrieved == nil { |
| 151 | // // t.Error("Failed to get device") |
| 152 | // //} else { |
| 153 | // // devIdBin, _ := uuid.New().MarshalBinary() |
| 154 | // // devId = "0001" + hex.EncodeToString(devIdBin)[:12] |
| 155 | // // newDevice := Clone(de\).(*voltha.Device) |
| 156 | // // newDevice.Id = devId |
| 157 | // |
| 158 | // if added := pt.Proxy.Add("/devices", device, ""); added == nil { |
| 159 | // t.Error("Failed to add device") |
| 160 | // } else { |
| 161 | // t.Logf("Added device : %+v", added) |
| 162 | // } |
| 163 | // //} |
| 164 | // |
| 165 | //} |
| 166 | func Test_Proxy_3_AddDevice(t *testing.T) { |
| 167 | devIdBin, _ := uuid.New().MarshalBinary() |
| 168 | devId = "0001" + hex.EncodeToString(devIdBin)[:12] |
| 169 | |
| 170 | device := &voltha.Device{ |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 171 | Id: devId, |
| 172 | Type: "simulated_olt", |
| 173 | Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"}, |
| 174 | AdminState: voltha.AdminState_PREPROVISIONED, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | if added := pt.Proxy.Add("/devices", device, ""); added == nil { |
| 178 | t.Error("Failed to add device") |
| 179 | } else { |
| 180 | t.Logf("Added device : %+v", added) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | func Test_Proxy_4_CheckAddedDevice(t *testing.T) { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 185 | if d := pt.Proxy.Get("/devices/"+devId, 0, false, ""); !reflect.ValueOf(d).IsValid() { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 186 | t.Error("Failed to find added device") |
| 187 | } else { |
| 188 | djson, _ := json.Marshal(d) |
| 189 | |
| 190 | t.Logf("Found device: count: %s", djson) |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | func Test_Proxy_5_UpdateDevice(t *testing.T) { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 195 | if retrieved := pt.Proxy.Get("/devices/"+targetDeviceId, 1, false, ""); retrieved == nil { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 196 | t.Error("Failed to get device") |
| 197 | } else { |
| 198 | var fwVersion int |
| 199 | if retrieved.(*voltha.Device).FirmwareVersion == "n/a" { |
| 200 | fwVersion = 0 |
| 201 | } else { |
| 202 | fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion) |
| 203 | fwVersion += 1 |
| 204 | } |
| 205 | |
| 206 | cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device) |
| 207 | cloned.FirmwareVersion = strconv.Itoa(fwVersion) |
| 208 | t.Logf("Before update : %+v", cloned) |
| 209 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 210 | if afterUpdate := pt.Proxy.Update("/devices/"+targetDeviceId, &cloned, false, ""); afterUpdate == nil { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 211 | t.Error("Failed to update device") |
| 212 | } else { |
| 213 | t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData()) |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | func Test_Proxy_6_CheckUpdatedDevice(t *testing.T) { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 219 | device := pt.Proxy.Get("/devices/"+targetDeviceId, 0, false, "") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 220 | |
| 221 | t.Logf("content of updated device: %+v", device) |
| 222 | } |
| 223 | |
| 224 | func Test_Proxy_7_RemoveDevice(t *testing.T) { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 225 | if removed := pt.Proxy.Remove("/devices/"+devId, ""); removed == nil { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 226 | t.Error("Failed to remove device") |
| 227 | } else { |
| 228 | t.Logf("Removed device : %+v", removed) |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | func Test_Proxy_8_CheckRemovedDevice(t *testing.T) { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 233 | if d := pt.Proxy.Get("/devices/"+devId, 0, false, ""); reflect.ValueOf(d).IsValid() { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 234 | djson, _ := json.Marshal(d) |
| 235 | t.Errorf("Device was not removed - %s", djson) |
| 236 | } else { |
| 237 | t.Logf("Device was removed: %s", devId) |
| 238 | } |
| 239 | } |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 240 | |
| 241 | // ----------------------------- |
| 242 | // Callback tests |
| 243 | // ----------------------------- |
| 244 | |
| 245 | func firstCallback(args ...interface{}) interface{} { |
| 246 | name := args[0] |
| 247 | id := args[1] |
| 248 | fmt.Printf("Running first callback - name: %s, id: %s\n", name, id) |
| 249 | return nil |
| 250 | } |
| 251 | func secondCallback(args ...interface{}) interface{} { |
| 252 | name := args[0].(map[string]string) |
| 253 | id := args[1] |
| 254 | fmt.Printf("Running second callback - name: %s, id: %f\n", name["name"], id) |
| 255 | panic("Generating a panic in second callback") |
| 256 | return nil |
| 257 | } |
| 258 | func thirdCallback(args ...interface{}) interface{} { |
| 259 | name := args[0] |
| 260 | id := args[1].(*voltha.Device) |
| 261 | fmt.Printf("Running third callback - name: %+v, id: %s\n", name, id.Id) |
| 262 | return nil |
| 263 | } |
| 264 | |
| 265 | func Test_Proxy_Callbacks_1_Register(t *testing.T) { |
| 266 | pt.Proxy.RegisterCallback(PRE_ADD, firstCallback, "abcde", "12345") |
| 267 | |
| 268 | m := make(map[string]string) |
| 269 | m["name"] = "fghij" |
| 270 | pt.Proxy.RegisterCallback(PRE_ADD, secondCallback, m, 1.2345) |
| 271 | |
| 272 | d := &voltha.Device{Id: "12345"} |
| 273 | pt.Proxy.RegisterCallback(PRE_ADD, thirdCallback, "klmno", d) |
| 274 | } |
| 275 | |
| 276 | func Test_Proxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) { |
| 277 | pt.Proxy.InvokeCallbacks(PRE_ADD, nil, true) |
| 278 | } |
| 279 | func Test_Proxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) { |
| 280 | pt.Proxy.InvokeCallbacks(PRE_ADD, nil, false) |
| 281 | } |
| 282 | |
| 283 | func Test_Proxy_Callbacks_4_Unregister(t *testing.T) { |
| 284 | pt.Proxy.UnregisterCallback(PRE_ADD, firstCallback) |
| 285 | pt.Proxy.UnregisterCallback(PRE_ADD, secondCallback) |
| 286 | pt.Proxy.UnregisterCallback(PRE_ADD, thirdCallback) |
| 287 | } |