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 { |
| 32 | Root *Root |
| 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 | |
| 69 | pt.Proxy = pt.Root.Node.GetProxy("/", false) |
| 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 | } |
| 124 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame^] | 125 | func Test_Proxy_4_UpdateDevice(t *testing.T) { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 126 | if retrieved := pt.Proxy.Get("/devices/"+targetDeviceId, 1, false, ""); retrieved == nil { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 127 | t.Error("Failed to get device") |
| 128 | } else { |
| 129 | var fwVersion int |
| 130 | if retrieved.(*voltha.Device).FirmwareVersion == "n/a" { |
| 131 | fwVersion = 0 |
| 132 | } else { |
| 133 | fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion) |
| 134 | fwVersion += 1 |
| 135 | } |
| 136 | |
| 137 | cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device) |
| 138 | cloned.FirmwareVersion = strconv.Itoa(fwVersion) |
| 139 | t.Logf("Before update : %+v", cloned) |
| 140 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 141 | if afterUpdate := pt.Proxy.Update("/devices/"+targetDeviceId, &cloned, false, ""); afterUpdate == nil { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 142 | t.Error("Failed to update device") |
| 143 | } else { |
| 144 | t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData()) |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame^] | 149 | func Test_Proxy_5_GetDevice_PostUpdate(t *testing.T) { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 150 | device := pt.Proxy.Get("/devices/"+targetDeviceId, 0, false, "") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 151 | |
| 152 | t.Logf("content of updated device: %+v", device) |
| 153 | } |
| 154 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame^] | 155 | func Test_Proxy_6_RemoveDevice(t *testing.T) { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 156 | if removed := pt.Proxy.Remove("/devices/"+devId, ""); removed == nil { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 157 | t.Error("Failed to remove device") |
| 158 | } else { |
| 159 | t.Logf("Removed device : %+v", removed) |
| 160 | } |
| 161 | } |
| 162 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame^] | 163 | func Test_Proxy_7_GetDevice_PostRemove(t *testing.T) { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 164 | 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] | 165 | djson, _ := json.Marshal(d) |
| 166 | t.Errorf("Device was not removed - %s", djson) |
| 167 | } else { |
| 168 | t.Logf("Device was removed: %s", devId) |
| 169 | } |
| 170 | } |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 171 | |
| 172 | // ----------------------------- |
| 173 | // Callback tests |
| 174 | // ----------------------------- |
| 175 | |
| 176 | func firstCallback(args ...interface{}) interface{} { |
| 177 | name := args[0] |
| 178 | id := args[1] |
| 179 | fmt.Printf("Running first callback - name: %s, id: %s\n", name, id) |
| 180 | return nil |
| 181 | } |
| 182 | func secondCallback(args ...interface{}) interface{} { |
| 183 | name := args[0].(map[string]string) |
| 184 | id := args[1] |
| 185 | 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^] | 186 | // FIXME: the panic call seem to interfere with the logging mechanism |
| 187 | //panic("Generating a panic in second callback") |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 188 | return nil |
| 189 | } |
| 190 | func thirdCallback(args ...interface{}) interface{} { |
| 191 | name := args[0] |
| 192 | id := args[1].(*voltha.Device) |
| 193 | fmt.Printf("Running third callback - name: %+v, id: %s\n", name, id.Id) |
| 194 | return nil |
| 195 | } |
| 196 | |
| 197 | func Test_Proxy_Callbacks_1_Register(t *testing.T) { |
| 198 | pt.Proxy.RegisterCallback(PRE_ADD, firstCallback, "abcde", "12345") |
| 199 | |
| 200 | m := make(map[string]string) |
| 201 | m["name"] = "fghij" |
| 202 | pt.Proxy.RegisterCallback(PRE_ADD, secondCallback, m, 1.2345) |
| 203 | |
| 204 | d := &voltha.Device{Id: "12345"} |
| 205 | pt.Proxy.RegisterCallback(PRE_ADD, thirdCallback, "klmno", d) |
| 206 | } |
| 207 | |
| 208 | func Test_Proxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) { |
| 209 | pt.Proxy.InvokeCallbacks(PRE_ADD, nil, true) |
| 210 | } |
| 211 | func Test_Proxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) { |
| 212 | pt.Proxy.InvokeCallbacks(PRE_ADD, nil, false) |
| 213 | } |
| 214 | |
| 215 | func Test_Proxy_Callbacks_4_Unregister(t *testing.T) { |
| 216 | pt.Proxy.UnregisterCallback(PRE_ADD, firstCallback) |
| 217 | pt.Proxy.UnregisterCallback(PRE_ADD, secondCallback) |
| 218 | pt.Proxy.UnregisterCallback(PRE_ADD, thirdCallback) |
| 219 | } |