blob: bc53791bdeaf69f094f43ecc668432f17829a19e [file] [log] [blame]
Stephane Barbariee16186c2018-09-11 10:46:34 -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 */
Stephane Barbariedc5022d2018-11-19 15:21:44 -050016
Stephane Barbariee16186c2018-09-11 10:46:34 -040017package model
18
19import (
Stephane Barbariee16186c2018-09-11 10:46:34 -040020 "encoding/hex"
khenaidoob9203542018-09-17 22:56:37 -040021 "github.com/google/uuid"
William Kurkiandaa6bb22019-03-07 12:26:28 -050022 "github.com/opencord/voltha-protos/go/common"
23 "github.com/opencord/voltha-protos/go/voltha"
khenaidoob9203542018-09-17 22:56:37 -040024 "strconv"
25 "testing"
Stephane Barbariee16186c2018-09-11 10:46:34 -040026)
27
Stephane Barbariee16186c2018-09-11 10:46:34 -040028var (
Stephane Barbarie1039ec42019-02-04 10:43:16 -050029 TestTransaction_Root *root
30 TestTransaction_RootProxy *Proxy
31 TestTransaction_TargetDeviceId string
32 TestTransaction_DeviceId string
Stephane Barbariee16186c2018-09-11 10:46:34 -040033)
34
Stephane Barbarie1039ec42019-02-04 10:43:16 -050035func init() {
36 TestTransaction_Root = NewRoot(&voltha.Voltha{}, nil)
37 TestTransaction_RootProxy = TestTransaction_Root.node.CreateProxy("/", false)
Stephane Barbariee16186c2018-09-11 10:46:34 -040038}
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040039
Stephane Barbarie1039ec42019-02-04 10:43:16 -050040//func TestTransaction_1_GetDevices(t *testing.T) {
41// getTx := TestTransaction_RootProxy.OpenTransaction()
42//
43// devices := getTx.Get("/devices", 1, false)
44//
45// if len(devices.([]interface{})) == 0 {
46// t.Error("there are no available devices to retrieve")
47// } else {
48// // Save the target device id for later tests
49// TestTransaction_TargetDeviceId = devices.([]interface{})[0].(*voltha.Device).Id
50// t.Logf("retrieved devices: %+v", devices)
51// }
52//
53// getTx.Commit()
54//}
Stephane Barbariee16186c2018-09-11 10:46:34 -040055
Stephane Barbarie1039ec42019-02-04 10:43:16 -050056func TestTransaction_2_AddDevice(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050057 devIDBin, _ := uuid.New().MarshalBinary()
Stephane Barbarie1039ec42019-02-04 10:43:16 -050058 TestTransaction_DeviceId = "0001" + hex.EncodeToString(devIDBin)[:12]
Stephane Barbariee16186c2018-09-11 10:46:34 -040059
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040060 ports := []*voltha.Port{
61 {
62 PortNo: 123,
63 Label: "test-port-0",
64 Type: voltha.Port_PON_OLT,
65 AdminState: common.AdminState_ENABLED,
66 OperStatus: common.OperStatus_ACTIVE,
67 DeviceId: "etcd_port-0-device-id",
68 Peers: []*voltha.Port_PeerPort{},
69 },
70 }
71
Stephane Barbariee16186c2018-09-11 10:46:34 -040072 device := &voltha.Device{
Stephane Barbarie1039ec42019-02-04 10:43:16 -050073 Id: TestTransaction_DeviceId,
Stephane Barbariee16186c2018-09-11 10:46:34 -040074 Type: "simulated_olt",
75 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
76 AdminState: voltha.AdminState_PREPROVISIONED,
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040077 Ports: ports,
Stephane Barbariee16186c2018-09-11 10:46:34 -040078 }
79
Stephane Barbarie1039ec42019-02-04 10:43:16 -050080 addTx := TestTransaction_RootProxy.OpenTransaction()
Stephane Barbariee16186c2018-09-11 10:46:34 -040081
82 if added := addTx.Add("/devices", device); added == nil {
83 t.Error("Failed to add device")
84 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -050085 TestTransaction_TargetDeviceId = added.(*voltha.Device).Id
Stephane Barbariee16186c2018-09-11 10:46:34 -040086 t.Logf("Added device : %+v", added)
87 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040088 addTx.Commit()
89}
90
Stephane Barbarie1039ec42019-02-04 10:43:16 -050091func TestTransaction_3_GetDevice_PostAdd(t *testing.T) {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040092
Stephane Barbarie1039ec42019-02-04 10:43:16 -050093 basePath := "/devices/" + TestTransaction_DeviceId
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040094
Stephane Barbarie1039ec42019-02-04 10:43:16 -050095 getDevWithPortsTx := TestTransaction_RootProxy.OpenTransaction()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040096 device1 := getDevWithPortsTx.Get(basePath+"/ports", 1, false)
97 t.Logf("retrieved device with ports: %+v", device1)
98 getDevWithPortsTx.Commit()
99
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500100 getDevTx := TestTransaction_RootProxy.OpenTransaction()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400101 device2 := getDevTx.Get(basePath, 0, false)
102 t.Logf("retrieved device: %+v", device2)
103
104 getDevTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -0400105}
106
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500107func TestTransaction_4_UpdateDevice(t *testing.T) {
108 updateTx := TestTransaction_RootProxy.OpenTransaction()
109 if retrieved := updateTx.Get("/devices/"+TestTransaction_TargetDeviceId, 1, false); retrieved == nil {
Stephane Barbariee16186c2018-09-11 10:46:34 -0400110 t.Error("Failed to get device")
111 } else {
112 var fwVersion int
113 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
114 fwVersion = 0
115 } else {
116 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500117 fwVersion++
Stephane Barbariee16186c2018-09-11 10:46:34 -0400118 }
119
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500120 //cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device)
121 retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
122 t.Logf("Before update : %+v", retrieved)
Stephane Barbariee16186c2018-09-11 10:46:34 -0400123
124 // FIXME: The makeBranch passed in function is nil or not being executed properly!!!!!
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500125 if afterUpdate := updateTx.Update("/devices/"+TestTransaction_TargetDeviceId, retrieved, false); afterUpdate == nil {
Stephane Barbariee16186c2018-09-11 10:46:34 -0400126 t.Error("Failed to update device")
127 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500128 t.Logf("Updated device : %+v", afterUpdate)
Stephane Barbariee16186c2018-09-11 10:46:34 -0400129 }
130 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400131 updateTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -0400132}
133
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500134func TestTransaction_5_GetDevice_PostUpdate(t *testing.T) {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400135
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500136 basePath := "/devices/" + TestTransaction_DeviceId
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400137
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500138 getDevWithPortsTx := TestTransaction_RootProxy.OpenTransaction()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400139 device1 := getDevWithPortsTx.Get(basePath+"/ports", 1, false)
140 t.Logf("retrieved device with ports: %+v", device1)
141 getDevWithPortsTx.Commit()
142
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500143 getDevTx := TestTransaction_RootProxy.OpenTransaction()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400144 device2 := getDevTx.Get(basePath, 0, false)
145 t.Logf("retrieved device: %+v", device2)
146
147 getDevTx.Commit()
148}
149
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500150func TestTransaction_6_RemoveDevice(t *testing.T) {
151 removeTx := TestTransaction_RootProxy.OpenTransaction()
152 if removed := removeTx.Remove("/devices/" + TestTransaction_DeviceId); removed == nil {
Stephane Barbariee16186c2018-09-11 10:46:34 -0400153 t.Error("Failed to remove device")
154 } else {
155 t.Logf("Removed device : %+v", removed)
156 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400157 removeTx.Commit()
158}
159
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500160func TestTransaction_7_GetDevice_PostRemove(t *testing.T) {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400161
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500162 basePath := "/devices/" + TestTransaction_DeviceId
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400163
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500164 getDevTx := TestTransaction_RootProxy.OpenTransaction()
165 device := TestTransaction_RootProxy.Get(basePath, 0, false, "")
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400166 t.Logf("retrieved device: %+v", device)
167
168 getDevTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -0400169}