blob: ff2385ae7034ccec4bc346d81548667fc62ba92d [file] [log] [blame]
sbarbari17d7e222019-11-05 10:02:29 -05001/*
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 */
16
17package model
18
19import (
20 "context"
21 "encoding/hex"
npujar9a30c702019-11-14 17:06:39 +053022 "strconv"
23 "testing"
24
sbarbari17d7e222019-11-05 10:02:29 -050025 "github.com/google/uuid"
Thomas Lee Se5a44012019-11-07 20:32:24 +053026 "github.com/opencord/voltha-lib-go/v2/pkg/log"
sbarbari17d7e222019-11-05 10:02:29 -050027 "github.com/opencord/voltha-protos/v2/go/common"
28 "github.com/opencord/voltha-protos/v2/go/voltha"
Thomas Lee Se5a44012019-11-07 20:32:24 +053029 "github.com/stretchr/testify/assert"
sbarbari17d7e222019-11-05 10:02:29 -050030)
31
32var (
npujar9a30c702019-11-14 17:06:39 +053033 TestTransactionRoot Root
34 TestTransactionRootProxy *Proxy
35 TestTransactionTargetDeviceID string
36 TestTransactionDeviceID string
sbarbari17d7e222019-11-05 10:02:29 -050037)
38
39func init() {
Thomas Lee Se5a44012019-11-07 20:32:24 +053040 var err error
npujar9a30c702019-11-14 17:06:39 +053041 TestTransactionRoot = NewRoot(&voltha.Voltha{}, nil)
42 if TestTransactionRootProxy, err = TestTransactionRoot.CreateProxy(context.Background(), "/", false); err != nil {
Thomas Lee Se5a44012019-11-07 20:32:24 +053043 log.With(log.Fields{"error": err}).Fatal("Cannot create proxy")
44 }
sbarbari17d7e222019-11-05 10:02:29 -050045}
46
sbarbari17d7e222019-11-05 10:02:29 -050047func TestTransaction_2_AddDevice(t *testing.T) {
48 devIDBin, _ := uuid.New().MarshalBinary()
npujar9a30c702019-11-14 17:06:39 +053049 TestTransactionDeviceID = "0001" + hex.EncodeToString(devIDBin)[:12]
sbarbari17d7e222019-11-05 10:02:29 -050050
51 ports := []*voltha.Port{
52 {
53 PortNo: 123,
54 Label: "test-port-0",
55 Type: voltha.Port_PON_OLT,
56 AdminState: common.AdminState_ENABLED,
57 OperStatus: common.OperStatus_ACTIVE,
58 DeviceId: "etcd_port-0-device-id",
59 Peers: []*voltha.Port_PeerPort{},
60 },
61 }
62
63 device := &voltha.Device{
npujar9a30c702019-11-14 17:06:39 +053064 Id: TestTransactionDeviceID,
sbarbari17d7e222019-11-05 10:02:29 -050065 Type: "simulated_olt",
66 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
67 AdminState: voltha.AdminState_PREPROVISIONED,
68 Ports: ports,
69 }
70
npujar9a30c702019-11-14 17:06:39 +053071 addTx := TestTransactionRootProxy.OpenTransaction()
sbarbari17d7e222019-11-05 10:02:29 -050072
Thomas Lee Se5a44012019-11-07 20:32:24 +053073 added, err := addTx.Add(context.Background(), "/devices", device)
74 if err != nil {
75 log.Errorf("Failed to add device due to error %v", err)
76 assert.NotNil(t, err)
77 }
78 if added == nil {
sbarbari17d7e222019-11-05 10:02:29 -050079 t.Error("Failed to add device")
80 } else {
npujar9a30c702019-11-14 17:06:39 +053081 TestTransactionTargetDeviceID = added.(*voltha.Device).Id
sbarbari17d7e222019-11-05 10:02:29 -050082 t.Logf("Added device : %+v", added)
83 }
84 addTx.Commit()
85}
86
87func TestTransaction_3_GetDevice_PostAdd(t *testing.T) {
88
npujar9a30c702019-11-14 17:06:39 +053089 basePath := "/devices/" + TestTransactionDeviceID
sbarbari17d7e222019-11-05 10:02:29 -050090
npujar9a30c702019-11-14 17:06:39 +053091 getDevWithPortsTx := TestTransactionRootProxy.OpenTransaction()
Thomas Lee Se5a44012019-11-07 20:32:24 +053092 device1, err := getDevWithPortsTx.Get(context.Background(), basePath+"/ports", 1, false)
93 if err != nil {
94 log.Errorf("Failed to get device with ports due to error %v", err)
95 assert.NotNil(t, err)
96 }
sbarbari17d7e222019-11-05 10:02:29 -050097 t.Logf("retrieved device with ports: %+v", device1)
98 getDevWithPortsTx.Commit()
99
npujar9a30c702019-11-14 17:06:39 +0530100 getDevTx := TestTransactionRootProxy.OpenTransaction()
Thomas Lee Se5a44012019-11-07 20:32:24 +0530101 device2, err := getDevTx.Get(context.Background(), basePath, 0, false)
102 if err != nil {
103 log.Errorf("Failed to open transaction due to error %v", err)
104 assert.NotNil(t, err)
105 }
sbarbari17d7e222019-11-05 10:02:29 -0500106 t.Logf("retrieved device: %+v", device2)
107
108 getDevTx.Commit()
109}
110
111func TestTransaction_4_UpdateDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530112 updateTx := TestTransactionRootProxy.OpenTransaction()
113 if retrieved, err := updateTx.Get(context.Background(), "/devices/"+TestTransactionTargetDeviceID, 1, false); err != nil {
Thomas Lee Se5a44012019-11-07 20:32:24 +0530114 log.Errorf("Failed to retrieve device info due to error %v", err)
115 assert.NotNil(t, err)
116 } else if retrieved == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500117 t.Error("Failed to get device")
118 } else {
119 var fwVersion int
120 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
121 fwVersion = 0
122 } else {
123 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
124 fwVersion++
125 }
126
127 //cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device)
128 retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
129 t.Logf("Before update : %+v", retrieved)
130
131 // FIXME: The makeBranch passed in function is nil or not being executed properly!!!!!
npujar9a30c702019-11-14 17:06:39 +0530132 afterUpdate, err := updateTx.Update(context.Background(), "/devices/"+TestTransactionTargetDeviceID, retrieved, false)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530133 if err != nil {
134 log.Errorf("Failed to update device info due to error %v", err)
135 assert.NotNil(t, err)
136 }
137 if afterUpdate == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500138 t.Error("Failed to update device")
139 } else {
140 t.Logf("Updated device : %+v", afterUpdate)
141 }
142 }
143 updateTx.Commit()
144}
145
146func TestTransaction_5_GetDevice_PostUpdate(t *testing.T) {
147
npujar9a30c702019-11-14 17:06:39 +0530148 basePath := "/devices/" + TestTransactionDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500149
npujar9a30c702019-11-14 17:06:39 +0530150 getDevWithPortsTx := TestTransactionRootProxy.OpenTransaction()
Thomas Lee Se5a44012019-11-07 20:32:24 +0530151 device1, err := getDevWithPortsTx.Get(context.Background(), basePath+"/ports", 1, false)
152 if err != nil {
153 log.Errorf("Failed to device with ports info due to error %v", err)
154 assert.NotNil(t, err)
155 }
sbarbari17d7e222019-11-05 10:02:29 -0500156 t.Logf("retrieved device with ports: %+v", device1)
157 getDevWithPortsTx.Commit()
158
npujar9a30c702019-11-14 17:06:39 +0530159 getDevTx := TestTransactionRootProxy.OpenTransaction()
Thomas Lee Se5a44012019-11-07 20:32:24 +0530160 device2, err := getDevTx.Get(context.Background(), basePath, 0, false)
161 if err != nil {
162 log.Errorf("Failed to get device info due to error %v", err)
163 assert.NotNil(t, err)
164 }
sbarbari17d7e222019-11-05 10:02:29 -0500165 t.Logf("retrieved device: %+v", device2)
166
167 getDevTx.Commit()
168}
169
170func TestTransaction_6_RemoveDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530171 removeTx := TestTransactionRootProxy.OpenTransaction()
172 removed, err := removeTx.Remove(context.Background(), "/devices/"+TestTransactionDeviceID)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530173 if err != nil {
174 log.Errorf("Failed to remove device due to error %v", err)
175 assert.NotNil(t, err)
176 }
177 if removed == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500178 t.Error("Failed to remove device")
179 } else {
180 t.Logf("Removed device : %+v", removed)
181 }
182 removeTx.Commit()
183}
184
185func TestTransaction_7_GetDevice_PostRemove(t *testing.T) {
186
npujar9a30c702019-11-14 17:06:39 +0530187 basePath := "/devices/" + TestTransactionDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500188
npujar9a30c702019-11-14 17:06:39 +0530189 getDevTx := TestTransactionRootProxy.OpenTransaction()
190 device, err := TestTransactionRootProxy.Get(context.Background(), basePath, 0, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530191 if err != nil {
192 log.Errorf("Failed to get device info post remove due to error %v", err)
193 assert.NotNil(t, err)
194 }
sbarbari17d7e222019-11-05 10:02:29 -0500195 t.Logf("retrieved device: %+v", device)
196
197 getDevTx.Commit()
198}