blob: 3660a86e4111f0026b49deaaf6cc87671cd247b5 [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 Barbarieef6650d2019-07-18 12:15:09 -040020 "context"
Stephane Barbariee16186c2018-09-11 10:46:34 -040021 "encoding/hex"
khenaidoob9203542018-09-17 22:56:37 -040022 "github.com/google/uuid"
William Kurkiandaa6bb22019-03-07 12:26:28 -050023 "github.com/opencord/voltha-protos/go/common"
24 "github.com/opencord/voltha-protos/go/voltha"
khenaidoob9203542018-09-17 22:56:37 -040025 "strconv"
26 "testing"
Stephane Barbariee16186c2018-09-11 10:46:34 -040027)
28
Stephane Barbariee16186c2018-09-11 10:46:34 -040029var (
Stephane Barbarie1039ec42019-02-04 10:43:16 -050030 TestTransaction_Root *root
31 TestTransaction_RootProxy *Proxy
32 TestTransaction_TargetDeviceId string
33 TestTransaction_DeviceId string
Stephane Barbariee16186c2018-09-11 10:46:34 -040034)
35
Stephane Barbarie1039ec42019-02-04 10:43:16 -050036func init() {
37 TestTransaction_Root = NewRoot(&voltha.Voltha{}, nil)
Stephane Barbarieef6650d2019-07-18 12:15:09 -040038 TestTransaction_RootProxy = TestTransaction_Root.node.CreateProxy(context.Background(), "/", false)
Stephane Barbariee16186c2018-09-11 10:46:34 -040039}
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040040
Stephane Barbarie1039ec42019-02-04 10:43:16 -050041//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//}
Stephane Barbariee16186c2018-09-11 10:46:34 -040056
Stephane Barbarie1039ec42019-02-04 10:43:16 -050057func TestTransaction_2_AddDevice(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050058 devIDBin, _ := uuid.New().MarshalBinary()
Stephane Barbarie1039ec42019-02-04 10:43:16 -050059 TestTransaction_DeviceId = "0001" + hex.EncodeToString(devIDBin)[:12]
Stephane Barbariee16186c2018-09-11 10:46:34 -040060
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040061 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
Stephane Barbariee16186c2018-09-11 10:46:34 -040073 device := &voltha.Device{
Stephane Barbarie1039ec42019-02-04 10:43:16 -050074 Id: TestTransaction_DeviceId,
Stephane Barbariee16186c2018-09-11 10:46:34 -040075 Type: "simulated_olt",
76 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
77 AdminState: voltha.AdminState_PREPROVISIONED,
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040078 Ports: ports,
Stephane Barbariee16186c2018-09-11 10:46:34 -040079 }
80
Stephane Barbarie1039ec42019-02-04 10:43:16 -050081 addTx := TestTransaction_RootProxy.OpenTransaction()
Stephane Barbariee16186c2018-09-11 10:46:34 -040082
Stephane Barbarieef6650d2019-07-18 12:15:09 -040083 if added := addTx.Add(context.Background(), "/devices", device); added == nil {
Stephane Barbariee16186c2018-09-11 10:46:34 -040084 t.Error("Failed to add device")
85 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -050086 TestTransaction_TargetDeviceId = added.(*voltha.Device).Id
Stephane Barbariee16186c2018-09-11 10:46:34 -040087 t.Logf("Added device : %+v", added)
88 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040089 addTx.Commit()
90}
91
Stephane Barbarie1039ec42019-02-04 10:43:16 -050092func TestTransaction_3_GetDevice_PostAdd(t *testing.T) {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040093
Stephane Barbarie1039ec42019-02-04 10:43:16 -050094 basePath := "/devices/" + TestTransaction_DeviceId
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040095
Stephane Barbarie1039ec42019-02-04 10:43:16 -050096 getDevWithPortsTx := TestTransaction_RootProxy.OpenTransaction()
Stephane Barbarieef6650d2019-07-18 12:15:09 -040097 device1 := getDevWithPortsTx.Get(context.Background(), basePath+"/ports", 1, false)
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040098 t.Logf("retrieved device with ports: %+v", device1)
99 getDevWithPortsTx.Commit()
100
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500101 getDevTx := TestTransaction_RootProxy.OpenTransaction()
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400102 device2 := getDevTx.Get(context.Background(), basePath, 0, false)
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400103 t.Logf("retrieved device: %+v", device2)
104
105 getDevTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -0400106}
107
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500108func TestTransaction_4_UpdateDevice(t *testing.T) {
109 updateTx := TestTransaction_RootProxy.OpenTransaction()
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400110 if retrieved := updateTx.Get(context.Background(), "/devices/"+TestTransaction_TargetDeviceId, 1, false); retrieved == nil {
Stephane Barbariee16186c2018-09-11 10:46:34 -0400111 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)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500118 fwVersion++
Stephane Barbariee16186c2018-09-11 10:46:34 -0400119 }
120
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500121 //cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device)
122 retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
123 t.Logf("Before update : %+v", retrieved)
Stephane Barbariee16186c2018-09-11 10:46:34 -0400124
125 // FIXME: The makeBranch passed in function is nil or not being executed properly!!!!!
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400126 if afterUpdate := updateTx.Update(context.Background(), "/devices/"+TestTransaction_TargetDeviceId, retrieved, false); afterUpdate == nil {
Stephane Barbariee16186c2018-09-11 10:46:34 -0400127 t.Error("Failed to update device")
128 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500129 t.Logf("Updated device : %+v", afterUpdate)
Stephane Barbariee16186c2018-09-11 10:46:34 -0400130 }
131 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400132 updateTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -0400133}
134
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500135func TestTransaction_5_GetDevice_PostUpdate(t *testing.T) {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400136
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500137 basePath := "/devices/" + TestTransaction_DeviceId
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400138
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500139 getDevWithPortsTx := TestTransaction_RootProxy.OpenTransaction()
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400140 device1 := getDevWithPortsTx.Get(context.Background(), basePath+"/ports", 1, false)
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400141 t.Logf("retrieved device with ports: %+v", device1)
142 getDevWithPortsTx.Commit()
143
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500144 getDevTx := TestTransaction_RootProxy.OpenTransaction()
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400145 device2 := getDevTx.Get(context.Background(), basePath, 0, false)
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400146 t.Logf("retrieved device: %+v", device2)
147
148 getDevTx.Commit()
149}
150
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500151func TestTransaction_6_RemoveDevice(t *testing.T) {
152 removeTx := TestTransaction_RootProxy.OpenTransaction()
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400153 if removed := removeTx.Remove(context.Background(), "/devices/"+TestTransaction_DeviceId); removed == nil {
Stephane Barbariee16186c2018-09-11 10:46:34 -0400154 t.Error("Failed to remove device")
155 } else {
156 t.Logf("Removed device : %+v", removed)
157 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400158 removeTx.Commit()
159}
160
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500161func TestTransaction_7_GetDevice_PostRemove(t *testing.T) {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400162
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500163 basePath := "/devices/" + TestTransaction_DeviceId
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400164
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500165 getDevTx := TestTransaction_RootProxy.OpenTransaction()
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400166 device := TestTransaction_RootProxy.Get(context.Background(), basePath, 0, false, "")
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400167 t.Logf("retrieved device: %+v", device)
168
169 getDevTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -0400170}