blob: 0e49f33f612108ff5b314fe75186f8e5f601d353 [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"
Stephane Barbariee16186c2018-09-11 10:46:34 -040024 "reflect"
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 Barbariedc5022d2018-11-19 15:21:44 -050030 txTargetDevID string
31 txDevID string
Stephane Barbariee16186c2018-09-11 10:46:34 -040032)
33
Stephane Barbariee16186c2018-09-11 10:46:34 -040034func Test_Transaction_1_GetDevices(t *testing.T) {
Stephane Barbariea188d942018-10-16 16:43:04 -040035 getTx := modelTestConfig.RootProxy.OpenTransaction()
Stephane Barbariee16186c2018-09-11 10:46:34 -040036
37 devices := getTx.Get("/devices", 1, false)
38
39 if len(devices.([]interface{})) == 0 {
40 t.Error("there are no available devices to retrieve")
41 } else {
42 // Save the target device id for later tests
Stephane Barbariedc5022d2018-11-19 15:21:44 -050043 txTargetDevID = devices.([]interface{})[0].(*voltha.Device).Id
Stephane Barbariee16186c2018-09-11 10:46:34 -040044 t.Logf("retrieved devices: %+v", devices)
45 }
46
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040047 getTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -040048}
49
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040050func Test_Transaction_2_AddDevice(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050051 devIDBin, _ := uuid.New().MarshalBinary()
52 txDevID = "0001" + hex.EncodeToString(devIDBin)[:12]
Stephane Barbariee16186c2018-09-11 10:46:34 -040053
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040054 ports := []*voltha.Port{
55 {
56 PortNo: 123,
57 Label: "test-port-0",
58 Type: voltha.Port_PON_OLT,
59 AdminState: common.AdminState_ENABLED,
60 OperStatus: common.OperStatus_ACTIVE,
61 DeviceId: "etcd_port-0-device-id",
62 Peers: []*voltha.Port_PeerPort{},
63 },
64 }
65
Stephane Barbariee16186c2018-09-11 10:46:34 -040066 device := &voltha.Device{
Stephane Barbariedc5022d2018-11-19 15:21:44 -050067 Id: txDevID,
Stephane Barbariee16186c2018-09-11 10:46:34 -040068 Type: "simulated_olt",
69 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
70 AdminState: voltha.AdminState_PREPROVISIONED,
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040071 Ports: ports,
Stephane Barbariee16186c2018-09-11 10:46:34 -040072 }
73
Stephane Barbariea188d942018-10-16 16:43:04 -040074 addTx := modelTestConfig.RootProxy.OpenTransaction()
Stephane Barbariee16186c2018-09-11 10:46:34 -040075
76 if added := addTx.Add("/devices", device); added == nil {
77 t.Error("Failed to add device")
78 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050079 txTargetDevID = added.(*voltha.Device).Id
Stephane Barbariee16186c2018-09-11 10:46:34 -040080 t.Logf("Added device : %+v", added)
81 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040082 addTx.Commit()
83}
84
85func Test_Transaction_3_GetDevice_PostAdd(t *testing.T) {
86
Stephane Barbariedc5022d2018-11-19 15:21:44 -050087 basePath := "/devices/" + txDevID
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040088
Stephane Barbariea188d942018-10-16 16:43:04 -040089 getDevWithPortsTx := modelTestConfig.RootProxy.OpenTransaction()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040090 device1 := getDevWithPortsTx.Get(basePath+"/ports", 1, false)
91 t.Logf("retrieved device with ports: %+v", device1)
92 getDevWithPortsTx.Commit()
93
Stephane Barbariea188d942018-10-16 16:43:04 -040094 getDevTx := modelTestConfig.RootProxy.OpenTransaction()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040095 device2 := getDevTx.Get(basePath, 0, false)
96 t.Logf("retrieved device: %+v", device2)
97
98 getDevTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -040099}
100
101func Test_Transaction_4_UpdateDevice(t *testing.T) {
Stephane Barbariea188d942018-10-16 16:43:04 -0400102 updateTx := modelTestConfig.RootProxy.OpenTransaction()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500103 if retrieved := updateTx.Get("/devices/"+txTargetDevID, 1, false); retrieved == nil {
Stephane Barbariee16186c2018-09-11 10:46:34 -0400104 t.Error("Failed to get device")
105 } else {
106 var fwVersion int
107 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
108 fwVersion = 0
109 } else {
110 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500111 fwVersion++
Stephane Barbariee16186c2018-09-11 10:46:34 -0400112 }
113
114 cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device)
115 cloned.FirmwareVersion = strconv.Itoa(fwVersion)
116 t.Logf("Before update : %+v", cloned)
117
118 // FIXME: The makeBranch passed in function is nil or not being executed properly!!!!!
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500119 if afterUpdate := updateTx.Update("/devices/"+txTargetDevID, &cloned, false); afterUpdate == nil {
Stephane Barbariee16186c2018-09-11 10:46:34 -0400120 t.Error("Failed to update device")
121 } else {
122 t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData())
123 }
124 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400125 updateTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -0400126}
127
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400128func Test_Transaction_5_GetDevice_PostUpdate(t *testing.T) {
129
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500130 basePath := "/devices/" + txDevID
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400131
Stephane Barbariea188d942018-10-16 16:43:04 -0400132 getDevWithPortsTx := modelTestConfig.RootProxy.OpenTransaction()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400133 device1 := getDevWithPortsTx.Get(basePath+"/ports", 1, false)
134 t.Logf("retrieved device with ports: %+v", device1)
135 getDevWithPortsTx.Commit()
136
Stephane Barbariea188d942018-10-16 16:43:04 -0400137 getDevTx := modelTestConfig.RootProxy.OpenTransaction()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400138 device2 := getDevTx.Get(basePath, 0, false)
139 t.Logf("retrieved device: %+v", device2)
140
141 getDevTx.Commit()
142}
143
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400144func Test_Transaction_6_RemoveDevice(t *testing.T) {
Stephane Barbariea188d942018-10-16 16:43:04 -0400145 removeTx := modelTestConfig.RootProxy.OpenTransaction()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500146 if removed := removeTx.Remove("/devices/" + txDevID); removed == nil {
Stephane Barbariee16186c2018-09-11 10:46:34 -0400147 t.Error("Failed to remove device")
148 } else {
149 t.Logf("Removed device : %+v", removed)
150 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400151 removeTx.Commit()
152}
153
154func Test_Transaction_7_GetDevice_PostRemove(t *testing.T) {
155
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500156 basePath := "/devices/" + txDevID
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400157
Stephane Barbariea188d942018-10-16 16:43:04 -0400158 getDevTx := modelTestConfig.RootProxy.OpenTransaction()
159 device := modelTestConfig.RootProxy.Get(basePath, 0, false, "")
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400160 t.Logf("retrieved device: %+v", device)
161
162 getDevTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -0400163}