blob: 10ca520f78a690fcef7f6e7f16067fc81b409d76 [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"
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040022 "github.com/opencord/voltha-go/protos/common"
khenaidoob9203542018-09-17 22:56:37 -040023 "github.com/opencord/voltha-go/protos/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}
Stephane Barbarie1039ec42019-02-04 10:43:16 -050039//func TestTransaction_1_GetDevices(t *testing.T) {
40// getTx := TestTransaction_RootProxy.OpenTransaction()
41//
42// devices := getTx.Get("/devices", 1, false)
43//
44// if len(devices.([]interface{})) == 0 {
45// t.Error("there are no available devices to retrieve")
46// } else {
47// // Save the target device id for later tests
48// TestTransaction_TargetDeviceId = devices.([]interface{})[0].(*voltha.Device).Id
49// t.Logf("retrieved devices: %+v", devices)
50// }
51//
52// getTx.Commit()
53//}
Stephane Barbariee16186c2018-09-11 10:46:34 -040054
Stephane Barbarie1039ec42019-02-04 10:43:16 -050055func TestTransaction_2_AddDevice(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050056 devIDBin, _ := uuid.New().MarshalBinary()
Stephane Barbarie1039ec42019-02-04 10:43:16 -050057 TestTransaction_DeviceId = "0001" + hex.EncodeToString(devIDBin)[:12]
Stephane Barbariee16186c2018-09-11 10:46:34 -040058
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040059 ports := []*voltha.Port{
60 {
61 PortNo: 123,
62 Label: "test-port-0",
63 Type: voltha.Port_PON_OLT,
64 AdminState: common.AdminState_ENABLED,
65 OperStatus: common.OperStatus_ACTIVE,
66 DeviceId: "etcd_port-0-device-id",
67 Peers: []*voltha.Port_PeerPort{},
68 },
69 }
70
Stephane Barbariee16186c2018-09-11 10:46:34 -040071 device := &voltha.Device{
Stephane Barbarie1039ec42019-02-04 10:43:16 -050072 Id: TestTransaction_DeviceId,
Stephane Barbariee16186c2018-09-11 10:46:34 -040073 Type: "simulated_olt",
74 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
75 AdminState: voltha.AdminState_PREPROVISIONED,
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040076 Ports: ports,
Stephane Barbariee16186c2018-09-11 10:46:34 -040077 }
78
Stephane Barbarie1039ec42019-02-04 10:43:16 -050079 addTx := TestTransaction_RootProxy.OpenTransaction()
Stephane Barbariee16186c2018-09-11 10:46:34 -040080
81 if added := addTx.Add("/devices", device); added == nil {
82 t.Error("Failed to add device")
83 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -050084 TestTransaction_TargetDeviceId = added.(*voltha.Device).Id
Stephane Barbariee16186c2018-09-11 10:46:34 -040085 t.Logf("Added device : %+v", added)
86 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040087 addTx.Commit()
88}
89
Stephane Barbarie1039ec42019-02-04 10:43:16 -050090func TestTransaction_3_GetDevice_PostAdd(t *testing.T) {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040091
Stephane Barbarie1039ec42019-02-04 10:43:16 -050092 basePath := "/devices/" + TestTransaction_DeviceId
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040093
Stephane Barbarie1039ec42019-02-04 10:43:16 -050094 getDevWithPortsTx := TestTransaction_RootProxy.OpenTransaction()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040095 device1 := getDevWithPortsTx.Get(basePath+"/ports", 1, false)
96 t.Logf("retrieved device with ports: %+v", device1)
97 getDevWithPortsTx.Commit()
98
Stephane Barbarie1039ec42019-02-04 10:43:16 -050099 getDevTx := TestTransaction_RootProxy.OpenTransaction()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400100 device2 := getDevTx.Get(basePath, 0, false)
101 t.Logf("retrieved device: %+v", device2)
102
103 getDevTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -0400104}
105
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500106func TestTransaction_4_UpdateDevice(t *testing.T) {
107 updateTx := TestTransaction_RootProxy.OpenTransaction()
108 if retrieved := updateTx.Get("/devices/"+TestTransaction_TargetDeviceId, 1, false); retrieved == nil {
Stephane Barbariee16186c2018-09-11 10:46:34 -0400109 t.Error("Failed to get device")
110 } else {
111 var fwVersion int
112 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
113 fwVersion = 0
114 } else {
115 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500116 fwVersion++
Stephane Barbariee16186c2018-09-11 10:46:34 -0400117 }
118
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500119 //cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device)
120 retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
121 t.Logf("Before update : %+v", retrieved)
Stephane Barbariee16186c2018-09-11 10:46:34 -0400122
123 // FIXME: The makeBranch passed in function is nil or not being executed properly!!!!!
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500124 if afterUpdate := updateTx.Update("/devices/"+TestTransaction_TargetDeviceId, retrieved, false); afterUpdate == nil {
Stephane Barbariee16186c2018-09-11 10:46:34 -0400125 t.Error("Failed to update device")
126 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500127 t.Logf("Updated device : %+v", afterUpdate)
Stephane Barbariee16186c2018-09-11 10:46:34 -0400128 }
129 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400130 updateTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -0400131}
132
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500133func TestTransaction_5_GetDevice_PostUpdate(t *testing.T) {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400134
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500135 basePath := "/devices/" + TestTransaction_DeviceId
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400136
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500137 getDevWithPortsTx := TestTransaction_RootProxy.OpenTransaction()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400138 device1 := getDevWithPortsTx.Get(basePath+"/ports", 1, false)
139 t.Logf("retrieved device with ports: %+v", device1)
140 getDevWithPortsTx.Commit()
141
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500142 getDevTx := TestTransaction_RootProxy.OpenTransaction()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400143 device2 := getDevTx.Get(basePath, 0, false)
144 t.Logf("retrieved device: %+v", device2)
145
146 getDevTx.Commit()
147}
148
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500149func TestTransaction_6_RemoveDevice(t *testing.T) {
150 removeTx := TestTransaction_RootProxy.OpenTransaction()
151 if removed := removeTx.Remove("/devices/" + TestTransaction_DeviceId); removed == nil {
Stephane Barbariee16186c2018-09-11 10:46:34 -0400152 t.Error("Failed to remove device")
153 } else {
154 t.Logf("Removed device : %+v", removed)
155 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400156 removeTx.Commit()
157}
158
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500159func TestTransaction_7_GetDevice_PostRemove(t *testing.T) {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400160
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500161 basePath := "/devices/" + TestTransaction_DeviceId
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400162
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500163 getDevTx := TestTransaction_RootProxy.OpenTransaction()
164 device := TestTransaction_RootProxy.Get(basePath, 0, false, "")
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400165 t.Logf("retrieved device: %+v", device)
166
167 getDevTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -0400168}