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