blob: 7b438b29ba8814f0e5dcae1e8be8d5f76749eee0 [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 */
16package model
17
18import (
Stephane Barbariee16186c2018-09-11 10:46:34 -040019 "encoding/hex"
khenaidoob9203542018-09-17 22:56:37 -040020 "github.com/google/uuid"
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040021 "github.com/opencord/voltha-go/protos/common"
khenaidoob9203542018-09-17 22:56:37 -040022 "github.com/opencord/voltha-go/protos/voltha"
Stephane Barbariee16186c2018-09-11 10:46:34 -040023 "reflect"
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 Barbariee16186c2018-09-11 10:46:34 -040029 txTargetDevId string
khenaidoob9203542018-09-17 22:56:37 -040030 txDevId string
Stephane Barbariee16186c2018-09-11 10:46:34 -040031)
32
Stephane Barbariee16186c2018-09-11 10:46:34 -040033func Test_Transaction_1_GetDevices(t *testing.T) {
Stephane Barbariea188d942018-10-16 16:43:04 -040034 getTx := modelTestConfig.RootProxy.OpenTransaction()
Stephane Barbariee16186c2018-09-11 10:46:34 -040035
36 devices := getTx.Get("/devices", 1, false)
37
38 if len(devices.([]interface{})) == 0 {
39 t.Error("there are no available devices to retrieve")
40 } else {
41 // Save the target device id for later tests
42 txTargetDevId = devices.([]interface{})[0].(*voltha.Device).Id
43 t.Logf("retrieved devices: %+v", devices)
44 }
45
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040046 getTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -040047}
48
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040049func Test_Transaction_2_AddDevice(t *testing.T) {
Stephane Barbariee16186c2018-09-11 10:46:34 -040050 devIdBin, _ := uuid.New().MarshalBinary()
51 txDevId = "0001" + hex.EncodeToString(devIdBin)[:12]
52
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040053 ports := []*voltha.Port{
54 {
55 PortNo: 123,
56 Label: "test-port-0",
57 Type: voltha.Port_PON_OLT,
58 AdminState: common.AdminState_ENABLED,
59 OperStatus: common.OperStatus_ACTIVE,
60 DeviceId: "etcd_port-0-device-id",
61 Peers: []*voltha.Port_PeerPort{},
62 },
63 }
64
Stephane Barbariee16186c2018-09-11 10:46:34 -040065 device := &voltha.Device{
66 Id: txDevId,
67 Type: "simulated_olt",
68 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
69 AdminState: voltha.AdminState_PREPROVISIONED,
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040070 Ports: ports,
Stephane Barbariee16186c2018-09-11 10:46:34 -040071 }
72
Stephane Barbariea188d942018-10-16 16:43:04 -040073 addTx := modelTestConfig.RootProxy.OpenTransaction()
Stephane Barbariee16186c2018-09-11 10:46:34 -040074
75 if added := addTx.Add("/devices", device); added == nil {
76 t.Error("Failed to add device")
77 } else {
78 t.Logf("Added device : %+v", added)
79 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040080 addTx.Commit()
81}
82
83func Test_Transaction_3_GetDevice_PostAdd(t *testing.T) {
84
85 basePath := "/devices/" + txDevId
86
Stephane Barbariea188d942018-10-16 16:43:04 -040087 getDevWithPortsTx := modelTestConfig.RootProxy.OpenTransaction()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040088 device1 := getDevWithPortsTx.Get(basePath+"/ports", 1, false)
89 t.Logf("retrieved device with ports: %+v", device1)
90 getDevWithPortsTx.Commit()
91
Stephane Barbariea188d942018-10-16 16:43:04 -040092 getDevTx := modelTestConfig.RootProxy.OpenTransaction()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040093 device2 := getDevTx.Get(basePath, 0, false)
94 t.Logf("retrieved device: %+v", device2)
95
96 getDevTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -040097}
98
99func Test_Transaction_4_UpdateDevice(t *testing.T) {
Stephane Barbariea188d942018-10-16 16:43:04 -0400100 updateTx := modelTestConfig.RootProxy.OpenTransaction()
Stephane Barbariee16186c2018-09-11 10:46:34 -0400101 if retrieved := updateTx.Get("/devices/"+txTargetDevId, 1, false); retrieved == nil {
102 t.Error("Failed to get device")
103 } else {
104 var fwVersion int
105 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
106 fwVersion = 0
107 } else {
108 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
109 fwVersion += 1
110 }
111
112 cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device)
113 cloned.FirmwareVersion = strconv.Itoa(fwVersion)
114 t.Logf("Before update : %+v", cloned)
115
116 // FIXME: The makeBranch passed in function is nil or not being executed properly!!!!!
117 if afterUpdate := updateTx.Update("/devices/"+txTargetDevId, &cloned, false); afterUpdate == nil {
118 t.Error("Failed to update device")
119 } else {
120 t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData())
121 }
122 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400123 updateTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -0400124}
125
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400126func Test_Transaction_5_GetDevice_PostUpdate(t *testing.T) {
127
128 basePath := "/devices/" + txDevId
129
Stephane Barbariea188d942018-10-16 16:43:04 -0400130 getDevWithPortsTx := modelTestConfig.RootProxy.OpenTransaction()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400131 device1 := getDevWithPortsTx.Get(basePath+"/ports", 1, false)
132 t.Logf("retrieved device with ports: %+v", device1)
133 getDevWithPortsTx.Commit()
134
Stephane Barbariea188d942018-10-16 16:43:04 -0400135 getDevTx := modelTestConfig.RootProxy.OpenTransaction()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400136 device2 := getDevTx.Get(basePath, 0, false)
137 t.Logf("retrieved device: %+v", device2)
138
139 getDevTx.Commit()
140}
141
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400142func Test_Transaction_6_RemoveDevice(t *testing.T) {
Stephane Barbariea188d942018-10-16 16:43:04 -0400143 removeTx := modelTestConfig.RootProxy.OpenTransaction()
khenaidoob9203542018-09-17 22:56:37 -0400144 if removed := removeTx.Remove("/devices/" + txDevId); removed == nil {
Stephane Barbariee16186c2018-09-11 10:46:34 -0400145 t.Error("Failed to remove device")
146 } else {
147 t.Logf("Removed device : %+v", removed)
148 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400149 removeTx.Commit()
150}
151
152func Test_Transaction_7_GetDevice_PostRemove(t *testing.T) {
153
154 basePath := "/devices/" + txDevId
155
Stephane Barbariea188d942018-10-16 16:43:04 -0400156 getDevTx := modelTestConfig.RootProxy.OpenTransaction()
157 device := modelTestConfig.RootProxy.Get(basePath, 0, false, "")
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400158 t.Logf("retrieved device: %+v", device)
159
160 getDevTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -0400161}