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" |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 24 | "time" |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 25 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 26 | "github.com/google/uuid" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 27 | "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 S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 30 | "github.com/stretchr/testify/assert" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | var ( |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 34 | TestTransactionRoot Root |
| 35 | TestTransactionRootProxy *Proxy |
| 36 | TestTransactionTargetDeviceID string |
| 37 | TestTransactionDeviceID string |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 38 | ) |
| 39 | |
| 40 | func init() { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 41 | var err error |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 42 | TestTransactionRoot = NewRoot(&voltha.Voltha{}, nil) |
| 43 | if TestTransactionRootProxy, err = TestTransactionRoot.CreateProxy(context.Background(), "/", false); err != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 44 | log.With(log.Fields{"error": err}).Fatal("Cannot create proxy") |
| 45 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 46 | } |
| 47 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 48 | func TestTransaction_2_AddDevice(t *testing.T) { |
| 49 | devIDBin, _ := uuid.New().MarshalBinary() |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 50 | TestTransactionDeviceID = "0001" + hex.EncodeToString(devIDBin)[:12] |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 51 | |
| 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{ |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 65 | Id: TestTransactionDeviceID, |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 66 | Type: "simulated_olt", |
| 67 | Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"}, |
| 68 | AdminState: voltha.AdminState_PREPROVISIONED, |
| 69 | Ports: ports, |
| 70 | } |
| 71 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 72 | addTx := TestTransactionRootProxy.OpenTransaction() |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 73 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 74 | added, err := addTx.Add(context.Background(), "/devices", device) |
| 75 | if err != nil { |
| 76 | log.Errorf("Failed to add device due to error %v", err) |
| 77 | assert.NotNil(t, err) |
| 78 | } |
| 79 | if added == nil { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 80 | t.Error("Failed to add device") |
| 81 | } else { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 82 | TestTransactionTargetDeviceID = added.(*voltha.Device).Id |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 83 | t.Logf("Added device : %+v", added) |
| 84 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 85 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 86 | defer cancel() |
| 87 | addTx.Commit(ctx) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | func TestTransaction_3_GetDevice_PostAdd(t *testing.T) { |
| 91 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 92 | basePath := "/devices/" + TestTransactionDeviceID |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 93 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 94 | getDevWithPortsTx := TestTransactionRootProxy.OpenTransaction() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 95 | device1, err := getDevWithPortsTx.Get(context.Background(), basePath+"/ports", 1, false) |
| 96 | if err != nil { |
| 97 | log.Errorf("Failed to get device with ports due to error %v", err) |
| 98 | assert.NotNil(t, err) |
| 99 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 100 | t.Logf("retrieved device with ports: %+v", device1) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 101 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 102 | defer cancel() |
| 103 | getDevWithPortsTx.Commit(ctx) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 104 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 105 | getDevTx := TestTransactionRootProxy.OpenTransaction() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 106 | device2, err := getDevTx.Get(context.Background(), basePath, 0, false) |
| 107 | if err != nil { |
| 108 | log.Errorf("Failed to open transaction due to error %v", err) |
| 109 | assert.NotNil(t, err) |
| 110 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 111 | t.Logf("retrieved device: %+v", device2) |
| 112 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 113 | getDevTx.Commit(ctx) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | func TestTransaction_4_UpdateDevice(t *testing.T) { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 117 | updateTx := TestTransactionRootProxy.OpenTransaction() |
| 118 | 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] | 119 | log.Errorf("Failed to retrieve device info due to error %v", err) |
| 120 | assert.NotNil(t, err) |
| 121 | } else if retrieved == nil { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 122 | 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!!!!! |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 137 | afterUpdate, err := updateTx.Update(context.Background(), "/devices/"+TestTransactionTargetDeviceID, retrieved, false) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 138 | if err != nil { |
| 139 | log.Errorf("Failed to update device info due to error %v", err) |
| 140 | assert.NotNil(t, err) |
| 141 | } |
| 142 | if afterUpdate == nil { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 143 | t.Error("Failed to update device") |
| 144 | } else { |
| 145 | t.Logf("Updated device : %+v", afterUpdate) |
| 146 | } |
| 147 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 148 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 149 | defer cancel() |
| 150 | updateTx.Commit(ctx) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | func TestTransaction_5_GetDevice_PostUpdate(t *testing.T) { |
| 154 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 155 | basePath := "/devices/" + TestTransactionDeviceID |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 156 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 157 | getDevWithPortsTx := TestTransactionRootProxy.OpenTransaction() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 158 | device1, err := getDevWithPortsTx.Get(context.Background(), basePath+"/ports", 1, false) |
| 159 | if err != nil { |
| 160 | log.Errorf("Failed to device with ports info due to error %v", err) |
| 161 | assert.NotNil(t, err) |
| 162 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 163 | t.Logf("retrieved device with ports: %+v", device1) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 164 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 165 | defer cancel() |
| 166 | getDevWithPortsTx.Commit(ctx) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 167 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 168 | getDevTx := TestTransactionRootProxy.OpenTransaction() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 169 | device2, err := getDevTx.Get(context.Background(), basePath, 0, false) |
| 170 | if err != nil { |
| 171 | log.Errorf("Failed to get device info due to error %v", err) |
| 172 | assert.NotNil(t, err) |
| 173 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 174 | t.Logf("retrieved device: %+v", device2) |
| 175 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 176 | getDevTx.Commit(ctx) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | func TestTransaction_6_RemoveDevice(t *testing.T) { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 180 | removeTx := TestTransactionRootProxy.OpenTransaction() |
| 181 | removed, err := removeTx.Remove(context.Background(), "/devices/"+TestTransactionDeviceID) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 182 | if err != nil { |
| 183 | log.Errorf("Failed to remove device due to error %v", err) |
| 184 | assert.NotNil(t, err) |
| 185 | } |
| 186 | if removed == nil { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 187 | t.Error("Failed to remove device") |
| 188 | } else { |
| 189 | t.Logf("Removed device : %+v", removed) |
| 190 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 191 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 192 | defer cancel() |
| 193 | removeTx.Commit(ctx) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | func TestTransaction_7_GetDevice_PostRemove(t *testing.T) { |
| 197 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 198 | basePath := "/devices/" + TestTransactionDeviceID |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 199 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 200 | getDevTx := TestTransactionRootProxy.OpenTransaction() |
| 201 | device, err := TestTransactionRootProxy.Get(context.Background(), basePath, 0, false, "") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 202 | if err != nil { |
| 203 | log.Errorf("Failed to get device info post remove due to error %v", err) |
| 204 | assert.NotNil(t, err) |
| 205 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 206 | t.Logf("retrieved device: %+v", device) |
| 207 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 208 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 209 | defer cancel() |
| 210 | getDevTx.Commit(ctx) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 211 | } |