sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package model |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "encoding/hex" |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame^] | 22 | "strconv" |
| 23 | "testing" |
| 24 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 25 | "github.com/google/uuid" |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 26 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 27 | "github.com/opencord/voltha-protos/v2/go/common" |
| 28 | "github.com/opencord/voltha-protos/v2/go/voltha" |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 29 | "github.com/stretchr/testify/assert" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | var ( |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame^] | 33 | TestTransactionRoot Root |
| 34 | TestTransactionRootProxy *Proxy |
| 35 | TestTransactionTargetDeviceID string |
| 36 | TestTransactionDeviceID string |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 37 | ) |
| 38 | |
| 39 | func init() { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 40 | var err error |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame^] | 41 | TestTransactionRoot = NewRoot(&voltha.Voltha{}, nil) |
| 42 | if TestTransactionRootProxy, err = TestTransactionRoot.CreateProxy(context.Background(), "/", false); err != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 43 | log.With(log.Fields{"error": err}).Fatal("Cannot create proxy") |
| 44 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 45 | } |
| 46 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 47 | func TestTransaction_2_AddDevice(t *testing.T) { |
| 48 | devIDBin, _ := uuid.New().MarshalBinary() |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame^] | 49 | TestTransactionDeviceID = "0001" + hex.EncodeToString(devIDBin)[:12] |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 50 | |
| 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{ |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame^] | 64 | Id: TestTransactionDeviceID, |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 65 | Type: "simulated_olt", |
| 66 | Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"}, |
| 67 | AdminState: voltha.AdminState_PREPROVISIONED, |
| 68 | Ports: ports, |
| 69 | } |
| 70 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame^] | 71 | addTx := TestTransactionRootProxy.OpenTransaction() |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 72 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 73 | 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 { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 79 | t.Error("Failed to add device") |
| 80 | } else { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame^] | 81 | TestTransactionTargetDeviceID = added.(*voltha.Device).Id |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 82 | t.Logf("Added device : %+v", added) |
| 83 | } |
| 84 | addTx.Commit() |
| 85 | } |
| 86 | |
| 87 | func TestTransaction_3_GetDevice_PostAdd(t *testing.T) { |
| 88 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame^] | 89 | basePath := "/devices/" + TestTransactionDeviceID |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 90 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame^] | 91 | getDevWithPortsTx := TestTransactionRootProxy.OpenTransaction() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 92 | 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 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 97 | t.Logf("retrieved device with ports: %+v", device1) |
| 98 | getDevWithPortsTx.Commit() |
| 99 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame^] | 100 | getDevTx := TestTransactionRootProxy.OpenTransaction() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 101 | 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 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 106 | t.Logf("retrieved device: %+v", device2) |
| 107 | |
| 108 | getDevTx.Commit() |
| 109 | } |
| 110 | |
| 111 | func TestTransaction_4_UpdateDevice(t *testing.T) { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame^] | 112 | updateTx := TestTransactionRootProxy.OpenTransaction() |
| 113 | if retrieved, err := updateTx.Get(context.Background(), "/devices/"+TestTransactionTargetDeviceID, 1, false); err != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 114 | log.Errorf("Failed to retrieve device info due to error %v", err) |
| 115 | assert.NotNil(t, err) |
| 116 | } else if retrieved == nil { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 117 | 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!!!!! |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame^] | 132 | afterUpdate, err := updateTx.Update(context.Background(), "/devices/"+TestTransactionTargetDeviceID, retrieved, false) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 133 | 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 { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 138 | t.Error("Failed to update device") |
| 139 | } else { |
| 140 | t.Logf("Updated device : %+v", afterUpdate) |
| 141 | } |
| 142 | } |
| 143 | updateTx.Commit() |
| 144 | } |
| 145 | |
| 146 | func TestTransaction_5_GetDevice_PostUpdate(t *testing.T) { |
| 147 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame^] | 148 | basePath := "/devices/" + TestTransactionDeviceID |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 149 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame^] | 150 | getDevWithPortsTx := TestTransactionRootProxy.OpenTransaction() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 151 | 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 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 156 | t.Logf("retrieved device with ports: %+v", device1) |
| 157 | getDevWithPortsTx.Commit() |
| 158 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame^] | 159 | getDevTx := TestTransactionRootProxy.OpenTransaction() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 160 | 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 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 165 | t.Logf("retrieved device: %+v", device2) |
| 166 | |
| 167 | getDevTx.Commit() |
| 168 | } |
| 169 | |
| 170 | func TestTransaction_6_RemoveDevice(t *testing.T) { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame^] | 171 | removeTx := TestTransactionRootProxy.OpenTransaction() |
| 172 | removed, err := removeTx.Remove(context.Background(), "/devices/"+TestTransactionDeviceID) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 173 | 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 { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 178 | t.Error("Failed to remove device") |
| 179 | } else { |
| 180 | t.Logf("Removed device : %+v", removed) |
| 181 | } |
| 182 | removeTx.Commit() |
| 183 | } |
| 184 | |
| 185 | func TestTransaction_7_GetDevice_PostRemove(t *testing.T) { |
| 186 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame^] | 187 | basePath := "/devices/" + TestTransactionDeviceID |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 188 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame^] | 189 | getDevTx := TestTransactionRootProxy.OpenTransaction() |
| 190 | device, err := TestTransactionRootProxy.Get(context.Background(), basePath, 0, false, "") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 191 | if err != nil { |
| 192 | log.Errorf("Failed to get device info post remove due to error %v", err) |
| 193 | assert.NotNil(t, err) |
| 194 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 195 | t.Logf("retrieved device: %+v", device) |
| 196 | |
| 197 | getDevTx.Commit() |
| 198 | } |