blob: 4e1346b6baa55418f8836ca610dc9eca68ebaf14 [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"
npujar467fe752020-01-16 20:17:45 +053024 "time"
npujar9a30c702019-11-14 17:06:39 +053025
sbarbari17d7e222019-11-05 10:02:29 -050026 "github.com/google/uuid"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080027 "github.com/opencord/voltha-lib-go/v3/pkg/log"
28 "github.com/opencord/voltha-protos/v3/go/common"
29 "github.com/opencord/voltha-protos/v3/go/voltha"
Thomas Lee Se5a44012019-11-07 20:32:24 +053030 "github.com/stretchr/testify/assert"
sbarbari17d7e222019-11-05 10:02:29 -050031)
32
33var (
npujar9a30c702019-11-14 17:06:39 +053034 TestTransactionRoot Root
35 TestTransactionRootProxy *Proxy
36 TestTransactionTargetDeviceID string
37 TestTransactionDeviceID string
sbarbari17d7e222019-11-05 10:02:29 -050038)
39
40func init() {
Thomas Lee Se5a44012019-11-07 20:32:24 +053041 var err error
npujar9a30c702019-11-14 17:06:39 +053042 TestTransactionRoot = NewRoot(&voltha.Voltha{}, nil)
43 if TestTransactionRootProxy, err = TestTransactionRoot.CreateProxy(context.Background(), "/", false); err != nil {
Thomas Lee Se5a44012019-11-07 20:32:24 +053044 log.With(log.Fields{"error": err}).Fatal("Cannot create proxy")
45 }
sbarbari17d7e222019-11-05 10:02:29 -050046}
47
sbarbari17d7e222019-11-05 10:02:29 -050048func TestTransaction_2_AddDevice(t *testing.T) {
49 devIDBin, _ := uuid.New().MarshalBinary()
npujar9a30c702019-11-14 17:06:39 +053050 TestTransactionDeviceID = "0001" + hex.EncodeToString(devIDBin)[:12]
sbarbari17d7e222019-11-05 10:02:29 -050051
52 ports := []*voltha.Port{
53 {
54 PortNo: 123,
55 Label: "test-port-0",
56 Type: voltha.Port_PON_OLT,
57 AdminState: common.AdminState_ENABLED,
58 OperStatus: common.OperStatus_ACTIVE,
59 DeviceId: "etcd_port-0-device-id",
60 Peers: []*voltha.Port_PeerPort{},
61 },
62 }
63
64 device := &voltha.Device{
npujar9a30c702019-11-14 17:06:39 +053065 Id: TestTransactionDeviceID,
sbarbari17d7e222019-11-05 10:02:29 -050066 Type: "simulated_olt",
67 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
68 AdminState: voltha.AdminState_PREPROVISIONED,
69 Ports: ports,
70 }
71
npujar9a30c702019-11-14 17:06:39 +053072 addTx := TestTransactionRootProxy.OpenTransaction()
sbarbari17d7e222019-11-05 10:02:29 -050073
Thomas Lee Se5a44012019-11-07 20:32:24 +053074 added, err := addTx.Add(context.Background(), "/devices", device)
75 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000076 logger.Errorf("Failed to add device due to error %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +053077 assert.NotNil(t, err)
78 }
79 if added == nil {
sbarbari17d7e222019-11-05 10:02:29 -050080 t.Error("Failed to add device")
81 } else {
npujar9a30c702019-11-14 17:06:39 +053082 TestTransactionTargetDeviceID = added.(*voltha.Device).Id
sbarbari17d7e222019-11-05 10:02:29 -050083 t.Logf("Added device : %+v", added)
84 }
npujar467fe752020-01-16 20:17:45 +053085 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
86 defer cancel()
87 addTx.Commit(ctx)
sbarbari17d7e222019-11-05 10:02:29 -050088}
89
90func TestTransaction_3_GetDevice_PostAdd(t *testing.T) {
91
npujar9a30c702019-11-14 17:06:39 +053092 basePath := "/devices/" + TestTransactionDeviceID
sbarbari17d7e222019-11-05 10:02:29 -050093
npujar9a30c702019-11-14 17:06:39 +053094 getDevWithPortsTx := TestTransactionRootProxy.OpenTransaction()
Thomas Lee Se5a44012019-11-07 20:32:24 +053095 device1, err := getDevWithPortsTx.Get(context.Background(), basePath+"/ports", 1, false)
96 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000097 logger.Errorf("Failed to get device with ports due to error %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +053098 assert.NotNil(t, err)
99 }
sbarbari17d7e222019-11-05 10:02:29 -0500100 t.Logf("retrieved device with ports: %+v", device1)
npujar467fe752020-01-16 20:17:45 +0530101 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
102 defer cancel()
103 getDevWithPortsTx.Commit(ctx)
sbarbari17d7e222019-11-05 10:02:29 -0500104
npujar9a30c702019-11-14 17:06:39 +0530105 getDevTx := TestTransactionRootProxy.OpenTransaction()
Thomas Lee Se5a44012019-11-07 20:32:24 +0530106 device2, err := getDevTx.Get(context.Background(), basePath, 0, false)
107 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000108 logger.Errorf("Failed to open transaction due to error %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530109 assert.NotNil(t, err)
110 }
sbarbari17d7e222019-11-05 10:02:29 -0500111 t.Logf("retrieved device: %+v", device2)
112
npujar467fe752020-01-16 20:17:45 +0530113 getDevTx.Commit(ctx)
sbarbari17d7e222019-11-05 10:02:29 -0500114}
115
116func TestTransaction_4_UpdateDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530117 updateTx := TestTransactionRootProxy.OpenTransaction()
118 if retrieved, err := updateTx.Get(context.Background(), "/devices/"+TestTransactionTargetDeviceID, 1, false); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000119 logger.Errorf("Failed to retrieve device info due to error %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530120 assert.NotNil(t, err)
121 } else if retrieved == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500122 t.Error("Failed to get device")
123 } else {
124 var fwVersion int
125 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
126 fwVersion = 0
127 } else {
128 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
129 fwVersion++
130 }
131
132 //cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device)
133 retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
134 t.Logf("Before update : %+v", retrieved)
135
136 // FIXME: The makeBranch passed in function is nil or not being executed properly!!!!!
npujar9a30c702019-11-14 17:06:39 +0530137 afterUpdate, err := updateTx.Update(context.Background(), "/devices/"+TestTransactionTargetDeviceID, retrieved, false)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530138 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000139 logger.Errorf("Failed to update device info due to error %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530140 assert.NotNil(t, err)
141 }
142 if afterUpdate == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500143 t.Error("Failed to update device")
144 } else {
145 t.Logf("Updated device : %+v", afterUpdate)
146 }
147 }
npujar467fe752020-01-16 20:17:45 +0530148 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
149 defer cancel()
150 updateTx.Commit(ctx)
sbarbari17d7e222019-11-05 10:02:29 -0500151}
152
153func TestTransaction_5_GetDevice_PostUpdate(t *testing.T) {
154
npujar9a30c702019-11-14 17:06:39 +0530155 basePath := "/devices/" + TestTransactionDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500156
npujar9a30c702019-11-14 17:06:39 +0530157 getDevWithPortsTx := TestTransactionRootProxy.OpenTransaction()
Thomas Lee Se5a44012019-11-07 20:32:24 +0530158 device1, err := getDevWithPortsTx.Get(context.Background(), basePath+"/ports", 1, false)
159 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000160 logger.Errorf("Failed to device with ports info due to error %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530161 assert.NotNil(t, err)
162 }
sbarbari17d7e222019-11-05 10:02:29 -0500163 t.Logf("retrieved device with ports: %+v", device1)
npujar467fe752020-01-16 20:17:45 +0530164 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
165 defer cancel()
166 getDevWithPortsTx.Commit(ctx)
sbarbari17d7e222019-11-05 10:02:29 -0500167
npujar9a30c702019-11-14 17:06:39 +0530168 getDevTx := TestTransactionRootProxy.OpenTransaction()
Thomas Lee Se5a44012019-11-07 20:32:24 +0530169 device2, err := getDevTx.Get(context.Background(), basePath, 0, false)
170 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000171 logger.Errorf("Failed to get device info due to error %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530172 assert.NotNil(t, err)
173 }
sbarbari17d7e222019-11-05 10:02:29 -0500174 t.Logf("retrieved device: %+v", device2)
175
npujar467fe752020-01-16 20:17:45 +0530176 getDevTx.Commit(ctx)
sbarbari17d7e222019-11-05 10:02:29 -0500177}
178
179func TestTransaction_6_RemoveDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530180 removeTx := TestTransactionRootProxy.OpenTransaction()
181 removed, err := removeTx.Remove(context.Background(), "/devices/"+TestTransactionDeviceID)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530182 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000183 logger.Errorf("Failed to remove device due to error %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530184 assert.NotNil(t, err)
185 }
186 if removed == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500187 t.Error("Failed to remove device")
188 } else {
189 t.Logf("Removed device : %+v", removed)
190 }
npujar467fe752020-01-16 20:17:45 +0530191 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
192 defer cancel()
193 removeTx.Commit(ctx)
sbarbari17d7e222019-11-05 10:02:29 -0500194}
195
196func TestTransaction_7_GetDevice_PostRemove(t *testing.T) {
197
npujar9a30c702019-11-14 17:06:39 +0530198 basePath := "/devices/" + TestTransactionDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500199
npujar9a30c702019-11-14 17:06:39 +0530200 getDevTx := TestTransactionRootProxy.OpenTransaction()
201 device, err := TestTransactionRootProxy.Get(context.Background(), basePath, 0, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530202 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000203 logger.Errorf("Failed to get device info post remove due to error %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530204 assert.NotNil(t, err)
205 }
sbarbari17d7e222019-11-05 10:02:29 -0500206 t.Logf("retrieved device: %+v", device)
207
npujar467fe752020-01-16 20:17:45 +0530208 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
209 defer cancel()
210 getDevTx.Commit(ctx)
sbarbari17d7e222019-11-05 10:02:29 -0500211}