blob: a52688a724ed253676c42c2c20951aefd9593ef9 [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"
22 "github.com/google/uuid"
Thomas Lee Se5a44012019-11-07 20:32:24 +053023 "github.com/opencord/voltha-lib-go/v2/pkg/log"
sbarbari17d7e222019-11-05 10:02:29 -050024 "github.com/opencord/voltha-protos/v2/go/common"
25 "github.com/opencord/voltha-protos/v2/go/voltha"
Thomas Lee Se5a44012019-11-07 20:32:24 +053026 "github.com/stretchr/testify/assert"
sbarbari17d7e222019-11-05 10:02:29 -050027 "strconv"
28 "testing"
29)
30
31var (
32 TestTransaction_Root *root
33 TestTransaction_RootProxy *Proxy
34 TestTransaction_TargetDeviceId string
35 TestTransaction_DeviceId string
36)
37
38func init() {
Thomas Lee Se5a44012019-11-07 20:32:24 +053039 var err error
sbarbari17d7e222019-11-05 10:02:29 -050040 TestTransaction_Root = NewRoot(&voltha.Voltha{}, nil)
Thomas Lee Se5a44012019-11-07 20:32:24 +053041 if TestTransaction_RootProxy, err = TestTransaction_Root.node.CreateProxy(context.Background(), "/", false); err != nil {
42 log.With(log.Fields{"error": err}).Fatal("Cannot create proxy")
43 }
sbarbari17d7e222019-11-05 10:02:29 -050044}
45
sbarbari17d7e222019-11-05 10:02:29 -050046func TestTransaction_2_AddDevice(t *testing.T) {
47 devIDBin, _ := uuid.New().MarshalBinary()
48 TestTransaction_DeviceId = "0001" + hex.EncodeToString(devIDBin)[:12]
49
50 ports := []*voltha.Port{
51 {
52 PortNo: 123,
53 Label: "test-port-0",
54 Type: voltha.Port_PON_OLT,
55 AdminState: common.AdminState_ENABLED,
56 OperStatus: common.OperStatus_ACTIVE,
57 DeviceId: "etcd_port-0-device-id",
58 Peers: []*voltha.Port_PeerPort{},
59 },
60 }
61
62 device := &voltha.Device{
63 Id: TestTransaction_DeviceId,
64 Type: "simulated_olt",
65 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
66 AdminState: voltha.AdminState_PREPROVISIONED,
67 Ports: ports,
68 }
69
70 addTx := TestTransaction_RootProxy.OpenTransaction()
71
Thomas Lee Se5a44012019-11-07 20:32:24 +053072 added, err := addTx.Add(context.Background(), "/devices", device)
73 if err != nil {
74 log.Errorf("Failed to add device due to error %v", err)
75 assert.NotNil(t, err)
76 }
77 if added == nil {
sbarbari17d7e222019-11-05 10:02:29 -050078 t.Error("Failed to add device")
79 } else {
80 TestTransaction_TargetDeviceId = added.(*voltha.Device).Id
81 t.Logf("Added device : %+v", added)
82 }
83 addTx.Commit()
84}
85
86func TestTransaction_3_GetDevice_PostAdd(t *testing.T) {
87
88 basePath := "/devices/" + TestTransaction_DeviceId
89
90 getDevWithPortsTx := TestTransaction_RootProxy.OpenTransaction()
Thomas Lee Se5a44012019-11-07 20:32:24 +053091 device1, err := getDevWithPortsTx.Get(context.Background(), basePath+"/ports", 1, false)
92 if err != nil {
93 log.Errorf("Failed to get device with ports due to error %v", err)
94 assert.NotNil(t, err)
95 }
sbarbari17d7e222019-11-05 10:02:29 -050096 t.Logf("retrieved device with ports: %+v", device1)
97 getDevWithPortsTx.Commit()
98
99 getDevTx := TestTransaction_RootProxy.OpenTransaction()
Thomas Lee Se5a44012019-11-07 20:32:24 +0530100 device2, err := getDevTx.Get(context.Background(), basePath, 0, false)
101 if err != nil {
102 log.Errorf("Failed to open transaction due to error %v", err)
103 assert.NotNil(t, err)
104 }
sbarbari17d7e222019-11-05 10:02:29 -0500105 t.Logf("retrieved device: %+v", device2)
106
107 getDevTx.Commit()
108}
109
110func TestTransaction_4_UpdateDevice(t *testing.T) {
111 updateTx := TestTransaction_RootProxy.OpenTransaction()
Thomas Lee Se5a44012019-11-07 20:32:24 +0530112 if retrieved, err := updateTx.Get(context.Background(), "/devices/"+TestTransaction_TargetDeviceId, 1, false); err != nil {
113 log.Errorf("Failed to retrieve device info due to error %v", err)
114 assert.NotNil(t, err)
115 } else if retrieved == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500116 t.Error("Failed to get device")
117 } else {
118 var fwVersion int
119 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
120 fwVersion = 0
121 } else {
122 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
123 fwVersion++
124 }
125
126 //cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device)
127 retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
128 t.Logf("Before update : %+v", retrieved)
129
130 // FIXME: The makeBranch passed in function is nil or not being executed properly!!!!!
Thomas Lee Se5a44012019-11-07 20:32:24 +0530131 afterUpdate, err := updateTx.Update(context.Background(), "/devices/"+TestTransaction_TargetDeviceId, retrieved, false)
132 if err != nil {
133 log.Errorf("Failed to update device info due to error %v", err)
134 assert.NotNil(t, err)
135 }
136 if afterUpdate == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500137 t.Error("Failed to update device")
138 } else {
139 t.Logf("Updated device : %+v", afterUpdate)
140 }
141 }
142 updateTx.Commit()
143}
144
145func TestTransaction_5_GetDevice_PostUpdate(t *testing.T) {
146
147 basePath := "/devices/" + TestTransaction_DeviceId
148
149 getDevWithPortsTx := TestTransaction_RootProxy.OpenTransaction()
Thomas Lee Se5a44012019-11-07 20:32:24 +0530150 device1, err := getDevWithPortsTx.Get(context.Background(), basePath+"/ports", 1, false)
151 if err != nil {
152 log.Errorf("Failed to device with ports info due to error %v", err)
153 assert.NotNil(t, err)
154 }
sbarbari17d7e222019-11-05 10:02:29 -0500155 t.Logf("retrieved device with ports: %+v", device1)
156 getDevWithPortsTx.Commit()
157
158 getDevTx := TestTransaction_RootProxy.OpenTransaction()
Thomas Lee Se5a44012019-11-07 20:32:24 +0530159 device2, err := getDevTx.Get(context.Background(), basePath, 0, false)
160 if err != nil {
161 log.Errorf("Failed to get device info due to error %v", err)
162 assert.NotNil(t, err)
163 }
sbarbari17d7e222019-11-05 10:02:29 -0500164 t.Logf("retrieved device: %+v", device2)
165
166 getDevTx.Commit()
167}
168
169func TestTransaction_6_RemoveDevice(t *testing.T) {
170 removeTx := TestTransaction_RootProxy.OpenTransaction()
Thomas Lee Se5a44012019-11-07 20:32:24 +0530171 removed, err := removeTx.Remove(context.Background(), "/devices/"+TestTransaction_DeviceId)
172 if err != nil {
173 log.Errorf("Failed to remove device due to error %v", err)
174 assert.NotNil(t, err)
175 }
176 if removed == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500177 t.Error("Failed to remove device")
178 } else {
179 t.Logf("Removed device : %+v", removed)
180 }
181 removeTx.Commit()
182}
183
184func TestTransaction_7_GetDevice_PostRemove(t *testing.T) {
185
186 basePath := "/devices/" + TestTransaction_DeviceId
187
188 getDevTx := TestTransaction_RootProxy.OpenTransaction()
Thomas Lee Se5a44012019-11-07 20:32:24 +0530189 device, err := TestTransaction_RootProxy.Get(context.Background(), basePath, 0, false, "")
190 if err != nil {
191 log.Errorf("Failed to get device info post remove due to error %v", err)
192 assert.NotNil(t, err)
193 }
sbarbari17d7e222019-11-05 10:02:29 -0500194 t.Logf("retrieved device: %+v", device)
195
196 getDevTx.Commit()
197}