blob: bf80bb7e6050d7db8c17943e252a1d45547cf407 [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"
Stephane Barbarie694e2b92018-09-07 12:17:36 -040021 "fmt"
khenaidoob9203542018-09-17 22:56:37 -040022 "github.com/google/uuid"
23 "github.com/opencord/voltha-go/common/log"
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040024 "github.com/opencord/voltha-go/protos/common"
khenaidoob9203542018-09-17 22:56:37 -040025 "github.com/opencord/voltha-go/protos/voltha"
26 "reflect"
27 "strconv"
28 "testing"
Stephane Barbarieec0919b2018-09-05 14:14:29 -040029)
30
31type 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
42var (
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 Barbarie694e2b92018-09-07 12:17:36 -040051 devId string
Stephane Barbarieec0919b2018-09-05 14:14:29 -040052 targetDeviceId string
53)
54
55func init() {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040056 log.AddPackage(log.JSON, log.DebugLevel, nil)
khenaidoo2c6f1672018-09-20 23:14:41 -040057 log.UpdateAllLoggers(log.Fields{"instanceId": "proxy_test"})
58
Stephane Barbarieec0919b2018-09-05 14:14:29 -040059 defer log.CleanUp()
60
Stephane Barbarieec0919b2018-09-05 14:14:29 -040061 pt.Backend = NewBackend(pt.DbType, pt.DbHost, pt.DbPort, pt.DbTimeout, pt.DbPrefix)
62
63 msgClass := &voltha.Voltha{}
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040064 root := NewRoot(msgClass, pt.Backend)
Stephane Barbarieec0919b2018-09-05 14:14:29 -040065 pt.Root = root.Load(msgClass)
66
67 GetProfiling().Report()
68
69 pt.Proxy = pt.Root.Node.GetProxy("/", false)
70}
71
72func 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 Barbarie88fbe7f2018-09-25 12:25:23 -040084func Test_Proxy_2_AddDevice(t *testing.T) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -040085 devIdBin, _ := uuid.New().MarshalBinary()
86 devId = "0001" + hex.EncodeToString(devIdBin)[:12]
87
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040088 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 Barbarieec0919b2018-09-05 14:14:29 -0400100 device := &voltha.Device{
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400101 Id: devId,
102 Type: "simulated_olt",
103 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
104 AdminState: voltha.AdminState_PREPROVISIONED,
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400105 Ports: ports,
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400106 }
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 Barbarie88fbe7f2018-09-25 12:25:23 -0400115func Test_Proxy_3_GetDevice_PostAdd(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400116 if d := pt.Proxy.Get("/devices/"+devId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400117 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 Barbarie88fbe7f2018-09-25 12:25:23 -0400125func Test_Proxy_4_UpdateDevice(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400126 if retrieved := pt.Proxy.Get("/devices/"+targetDeviceId, 1, false, ""); retrieved == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400127 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 Barbarie694e2b92018-09-07 12:17:36 -0400141 if afterUpdate := pt.Proxy.Update("/devices/"+targetDeviceId, &cloned, false, ""); afterUpdate == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400142 t.Error("Failed to update device")
143 } else {
144 t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData())
145 }
146 }
147}
148
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400149func Test_Proxy_5_GetDevice_PostUpdate(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400150 device := pt.Proxy.Get("/devices/"+targetDeviceId, 0, false, "")
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400151
152 t.Logf("content of updated device: %+v", device)
153}
154
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400155func Test_Proxy_6_RemoveDevice(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400156 if removed := pt.Proxy.Remove("/devices/"+devId, ""); removed == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400157 t.Error("Failed to remove device")
158 } else {
159 t.Logf("Removed device : %+v", removed)
160 }
161}
162
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400163func Test_Proxy_7_GetDevice_PostRemove(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400164 if d := pt.Proxy.Get("/devices/"+devId, 0, false, ""); reflect.ValueOf(d).IsValid() {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400165 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 Barbarie694e2b92018-09-07 12:17:36 -0400171
172// -----------------------------
173// Callback tests
174// -----------------------------
175
176func 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}
182func 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 Barbarie88fbe7f2018-09-25 12:25:23 -0400186 // FIXME: the panic call seem to interfere with the logging mechanism
187 //panic("Generating a panic in second callback")
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400188 return nil
189}
190func 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
197func 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
208func Test_Proxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) {
209 pt.Proxy.InvokeCallbacks(PRE_ADD, nil, true)
210}
211func Test_Proxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) {
212 pt.Proxy.InvokeCallbacks(PRE_ADD, nil, false)
213}
214
215func 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}