blob: 45329fd239c6cd5121740fd14668323ed8d787e1 [file] [log] [blame]
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001/*
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 */
16package model
17
18import (
Stephane Barbarieec0919b2018-09-05 14:14:29 -040019 "encoding/hex"
20 "encoding/json"
khenaidoob9203542018-09-17 22:56:37 -040021 "github.com/google/uuid"
22 "github.com/opencord/voltha-go/common/log"
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040023 "github.com/opencord/voltha-go/protos/common"
khenaidoob9203542018-09-17 22:56:37 -040024 "github.com/opencord/voltha-go/protos/voltha"
25 "reflect"
26 "strconv"
27 "testing"
Stephane Barbarieec0919b2018-09-05 14:14:29 -040028)
29
30type proxyTest struct {
Stephane Barbarie06c4a742018-10-01 11:09:32 -040031 Root *root
Stephane Barbarieec0919b2018-09-05 14:14:29 -040032 Backend *Backend
33 Proxy *Proxy
34 DbPrefix string
35 DbType string
36 DbHost string
37 DbPort int
38 DbTimeout int
39}
40
41var (
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 Barbarie694e2b92018-09-07 12:17:36 -040050 devId string
Stephane Barbarieec0919b2018-09-05 14:14:29 -040051 targetDeviceId string
52)
53
54func init() {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040055 log.AddPackage(log.JSON, log.DebugLevel, nil)
khenaidoo2c6f1672018-09-20 23:14:41 -040056 log.UpdateAllLoggers(log.Fields{"instanceId": "proxy_test"})
57
Stephane Barbarieec0919b2018-09-05 14:14:29 -040058 defer log.CleanUp()
59
Stephane Barbarieec0919b2018-09-05 14:14:29 -040060 pt.Backend = NewBackend(pt.DbType, pt.DbHost, pt.DbPort, pt.DbTimeout, pt.DbPrefix)
61
62 msgClass := &voltha.Voltha{}
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040063 root := NewRoot(msgClass, pt.Backend)
Stephane Barbarieec0919b2018-09-05 14:14:29 -040064 pt.Root = root.Load(msgClass)
65
66 GetProfiling().Report()
67
Stephane Barbarie06c4a742018-10-01 11:09:32 -040068 pt.Proxy = pt.Root.GetProxy("/", false)
Stephane Barbarieec0919b2018-09-05 14:14:29 -040069}
70
71func 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 Barbarie88fbe7f2018-09-25 12:25:23 -040083func Test_Proxy_2_AddDevice(t *testing.T) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -040084 devIdBin, _ := uuid.New().MarshalBinary()
85 devId = "0001" + hex.EncodeToString(devIdBin)[:12]
86
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040087 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 Barbarieec0919b2018-09-05 14:14:29 -040099 device := &voltha.Device{
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400100 Id: devId,
101 Type: "simulated_olt",
102 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
103 AdminState: voltha.AdminState_PREPROVISIONED,
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400104 Ports: ports,
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400105 }
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 Barbarie88fbe7f2018-09-25 12:25:23 -0400114func Test_Proxy_3_GetDevice_PostAdd(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400115 if d := pt.Proxy.Get("/devices/"+devId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400116 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 Barbarie06c4a742018-10-01 11:09:32 -0400123func 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 Barbarie06c4a742018-10-01 11:09:32 -0400152 //
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
168func 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 Barbarieec0919b2018-09-05 14:14:29 -0400177
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400178func Test_Proxy_4_UpdateDevice(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400179 if retrieved := pt.Proxy.Get("/devices/"+targetDeviceId, 1, false, ""); retrieved == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400180 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 Barbarie694e2b92018-09-07 12:17:36 -0400194 if afterUpdate := pt.Proxy.Update("/devices/"+targetDeviceId, &cloned, false, ""); afterUpdate == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400195 t.Error("Failed to update device")
196 } else {
197 t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData())
198 }
199 }
200}
201
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400202func Test_Proxy_5_GetDevice_PostUpdate(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400203 device := pt.Proxy.Get("/devices/"+targetDeviceId, 0, false, "")
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400204
205 t.Logf("content of updated device: %+v", device)
206}
207
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400208func Test_Proxy_6_RemoveDevice(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400209 if removed := pt.Proxy.Remove("/devices/"+devId, ""); removed == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400210 t.Error("Failed to remove device")
211 } else {
212 t.Logf("Removed device : %+v", removed)
213 }
214}
215
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400216func Test_Proxy_7_GetDevice_PostRemove(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400217 if d := pt.Proxy.Get("/devices/"+devId, 0, false, ""); reflect.ValueOf(d).IsValid() {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400218 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 Barbarie694e2b92018-09-07 12:17:36 -0400224
225// -----------------------------
226// Callback tests
227// -----------------------------
228
229func firstCallback(args ...interface{}) interface{} {
230 name := args[0]
231 id := args[1]
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -0400232 log.Infof("Running first callback - name: %s, id: %s\n", name, id)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400233 return nil
234}
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400235func deviceCallback(args ...interface{}) interface{} {
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -0400236 log.Infof("Running device callback\n")
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400237 return nil
238}
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400239func secondCallback(args ...interface{}) interface{} {
240 name := args[0].(map[string]string)
241 id := args[1]
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -0400242 log.Infof("Running second callback - name: %s, id: %f\n", name["name"], id)
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400243 // FIXME: the panic call seem to interfere with the logging mechanism
244 //panic("Generating a panic in second callback")
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400245 return nil
246}
247func thirdCallback(args ...interface{}) interface{} {
248 name := args[0]
249 id := args[1].(*voltha.Device)
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -0400250 log.Infof("Running third callback - name: %+v, id: %s\n", name, id.Id)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400251 return nil
252}
253
254func 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
265func Test_Proxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) {
266 pt.Proxy.InvokeCallbacks(PRE_ADD, nil, true)
267}
268func Test_Proxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) {
269 pt.Proxy.InvokeCallbacks(PRE_ADD, nil, false)
270}
271
272func 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}