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