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" |
| 22 | "github.com/google/uuid" |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame^] | 23 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 24 | "github.com/opencord/voltha-protos/v2/go/common" |
| 25 | "github.com/opencord/voltha-protos/v2/go/voltha" |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame^] | 26 | "github.com/stretchr/testify/assert" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 27 | "strconv" |
| 28 | "testing" |
| 29 | ) |
| 30 | |
| 31 | var ( |
| 32 | TestTransaction_Root *root |
| 33 | TestTransaction_RootProxy *Proxy |
| 34 | TestTransaction_TargetDeviceId string |
| 35 | TestTransaction_DeviceId string |
| 36 | ) |
| 37 | |
| 38 | func init() { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame^] | 39 | var err error |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 40 | TestTransaction_Root = NewRoot(&voltha.Voltha{}, nil) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame^] | 41 | 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 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 44 | } |
| 45 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 46 | func 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 S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame^] | 72 | 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 { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 78 | 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 | |
| 86 | func TestTransaction_3_GetDevice_PostAdd(t *testing.T) { |
| 87 | |
| 88 | basePath := "/devices/" + TestTransaction_DeviceId |
| 89 | |
| 90 | getDevWithPortsTx := TestTransaction_RootProxy.OpenTransaction() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame^] | 91 | 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 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 96 | t.Logf("retrieved device with ports: %+v", device1) |
| 97 | getDevWithPortsTx.Commit() |
| 98 | |
| 99 | getDevTx := TestTransaction_RootProxy.OpenTransaction() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame^] | 100 | 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 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 105 | t.Logf("retrieved device: %+v", device2) |
| 106 | |
| 107 | getDevTx.Commit() |
| 108 | } |
| 109 | |
| 110 | func TestTransaction_4_UpdateDevice(t *testing.T) { |
| 111 | updateTx := TestTransaction_RootProxy.OpenTransaction() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame^] | 112 | 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 { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 116 | 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 S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame^] | 131 | 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 { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 137 | t.Error("Failed to update device") |
| 138 | } else { |
| 139 | t.Logf("Updated device : %+v", afterUpdate) |
| 140 | } |
| 141 | } |
| 142 | updateTx.Commit() |
| 143 | } |
| 144 | |
| 145 | func TestTransaction_5_GetDevice_PostUpdate(t *testing.T) { |
| 146 | |
| 147 | basePath := "/devices/" + TestTransaction_DeviceId |
| 148 | |
| 149 | getDevWithPortsTx := TestTransaction_RootProxy.OpenTransaction() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame^] | 150 | 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 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 155 | t.Logf("retrieved device with ports: %+v", device1) |
| 156 | getDevWithPortsTx.Commit() |
| 157 | |
| 158 | getDevTx := TestTransaction_RootProxy.OpenTransaction() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame^] | 159 | 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 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 164 | t.Logf("retrieved device: %+v", device2) |
| 165 | |
| 166 | getDevTx.Commit() |
| 167 | } |
| 168 | |
| 169 | func TestTransaction_6_RemoveDevice(t *testing.T) { |
| 170 | removeTx := TestTransaction_RootProxy.OpenTransaction() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame^] | 171 | 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 { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 177 | t.Error("Failed to remove device") |
| 178 | } else { |
| 179 | t.Logf("Removed device : %+v", removed) |
| 180 | } |
| 181 | removeTx.Commit() |
| 182 | } |
| 183 | |
| 184 | func TestTransaction_7_GetDevice_PostRemove(t *testing.T) { |
| 185 | |
| 186 | basePath := "/devices/" + TestTransaction_DeviceId |
| 187 | |
| 188 | getDevTx := TestTransaction_RootProxy.OpenTransaction() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame^] | 189 | 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 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 194 | t.Logf("retrieved device: %+v", device) |
| 195 | |
| 196 | getDevTx.Commit() |
| 197 | } |