blob: c33e5beb7de4ad260de644f8bfee0b6c7a80265f [file] [log] [blame]
sbarbari17d7e222019-11-05 10:02:29 -05001/*
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
17package model
18
19import (
20 "context"
21 "encoding/hex"
22 "github.com/google/uuid"
23 "github.com/opencord/voltha-protos/v2/go/common"
24 "github.com/opencord/voltha-protos/v2/go/voltha"
25 "strconv"
26 "testing"
27)
28
29var (
30 TestTransaction_Root *root
31 TestTransaction_RootProxy *Proxy
32 TestTransaction_TargetDeviceId string
33 TestTransaction_DeviceId string
34)
35
36func init() {
37 TestTransaction_Root = NewRoot(&voltha.Voltha{}, nil)
38 TestTransaction_RootProxy = TestTransaction_Root.node.CreateProxy(context.Background(), "/", false)
39}
40
41//func TestTransaction_1_GetDevices(t *testing.T) {
42// getTx := TestTransaction_RootProxy.OpenTransaction()
43//
44// devices := getTx.Get("/devices", 1, false)
45//
46// if len(devices.([]interface{})) == 0 {
47// t.Error("there are no available devices to retrieve")
48// } else {
49// // Save the target device id for later tests
50// TestTransaction_TargetDeviceId = devices.([]interface{})[0].(*voltha.Device).Id
51// t.Logf("retrieved devices: %+v", devices)
52// }
53//
54// getTx.Commit()
55//}
56
57func TestTransaction_2_AddDevice(t *testing.T) {
58 devIDBin, _ := uuid.New().MarshalBinary()
59 TestTransaction_DeviceId = "0001" + hex.EncodeToString(devIDBin)[:12]
60
61 ports := []*voltha.Port{
62 {
63 PortNo: 123,
64 Label: "test-port-0",
65 Type: voltha.Port_PON_OLT,
66 AdminState: common.AdminState_ENABLED,
67 OperStatus: common.OperStatus_ACTIVE,
68 DeviceId: "etcd_port-0-device-id",
69 Peers: []*voltha.Port_PeerPort{},
70 },
71 }
72
73 device := &voltha.Device{
74 Id: TestTransaction_DeviceId,
75 Type: "simulated_olt",
76 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
77 AdminState: voltha.AdminState_PREPROVISIONED,
78 Ports: ports,
79 }
80
81 addTx := TestTransaction_RootProxy.OpenTransaction()
82
83 if added := addTx.Add(context.Background(), "/devices", device); added == nil {
84 t.Error("Failed to add device")
85 } else {
86 TestTransaction_TargetDeviceId = added.(*voltha.Device).Id
87 t.Logf("Added device : %+v", added)
88 }
89 addTx.Commit()
90}
91
92func TestTransaction_3_GetDevice_PostAdd(t *testing.T) {
93
94 basePath := "/devices/" + TestTransaction_DeviceId
95
96 getDevWithPortsTx := TestTransaction_RootProxy.OpenTransaction()
97 device1 := getDevWithPortsTx.Get(context.Background(), basePath+"/ports", 1, false)
98 t.Logf("retrieved device with ports: %+v", device1)
99 getDevWithPortsTx.Commit()
100
101 getDevTx := TestTransaction_RootProxy.OpenTransaction()
102 device2 := getDevTx.Get(context.Background(), basePath, 0, false)
103 t.Logf("retrieved device: %+v", device2)
104
105 getDevTx.Commit()
106}
107
108func TestTransaction_4_UpdateDevice(t *testing.T) {
109 updateTx := TestTransaction_RootProxy.OpenTransaction()
110 if retrieved := updateTx.Get(context.Background(), "/devices/"+TestTransaction_TargetDeviceId, 1, false); retrieved == nil {
111 t.Error("Failed to get device")
112 } else {
113 var fwVersion int
114 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
115 fwVersion = 0
116 } else {
117 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
118 fwVersion++
119 }
120
121 //cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device)
122 retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
123 t.Logf("Before update : %+v", retrieved)
124
125 // FIXME: The makeBranch passed in function is nil or not being executed properly!!!!!
126 if afterUpdate := updateTx.Update(context.Background(), "/devices/"+TestTransaction_TargetDeviceId, retrieved, false); afterUpdate == nil {
127 t.Error("Failed to update device")
128 } else {
129 t.Logf("Updated device : %+v", afterUpdate)
130 }
131 }
132 updateTx.Commit()
133}
134
135func TestTransaction_5_GetDevice_PostUpdate(t *testing.T) {
136
137 basePath := "/devices/" + TestTransaction_DeviceId
138
139 getDevWithPortsTx := TestTransaction_RootProxy.OpenTransaction()
140 device1 := getDevWithPortsTx.Get(context.Background(), basePath+"/ports", 1, false)
141 t.Logf("retrieved device with ports: %+v", device1)
142 getDevWithPortsTx.Commit()
143
144 getDevTx := TestTransaction_RootProxy.OpenTransaction()
145 device2 := getDevTx.Get(context.Background(), basePath, 0, false)
146 t.Logf("retrieved device: %+v", device2)
147
148 getDevTx.Commit()
149}
150
151func TestTransaction_6_RemoveDevice(t *testing.T) {
152 removeTx := TestTransaction_RootProxy.OpenTransaction()
153 if removed := removeTx.Remove(context.Background(), "/devices/"+TestTransaction_DeviceId); removed == nil {
154 t.Error("Failed to remove device")
155 } else {
156 t.Logf("Removed device : %+v", removed)
157 }
158 removeTx.Commit()
159}
160
161func TestTransaction_7_GetDevice_PostRemove(t *testing.T) {
162
163 basePath := "/devices/" + TestTransaction_DeviceId
164
165 getDevTx := TestTransaction_RootProxy.OpenTransaction()
166 device := TestTransaction_RootProxy.Get(context.Background(), basePath, 0, false, "")
167 t.Logf("retrieved device: %+v", device)
168
169 getDevTx.Commit()
170}