Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [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 | package model |
| 17 | |
| 18 | import ( |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 19 | "encoding/hex" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 20 | "github.com/google/uuid" |
| 21 | "github.com/opencord/voltha-go/common/log" |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 22 | "github.com/opencord/voltha-go/protos/common" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/protos/voltha" |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 24 | "reflect" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 25 | "strconv" |
| 26 | "testing" |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | type transactionTest struct { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 30 | Root *root |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 31 | Backend *Backend |
| 32 | Proxy *Proxy |
| 33 | DbPrefix string |
| 34 | DbType string |
| 35 | DbHost string |
| 36 | DbPort int |
| 37 | DbTimeout int |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | var ( |
| 41 | tx = &transactionTest{ |
| 42 | DbPrefix: "service/voltha/data/core/0001", |
| 43 | DbType: "etcd", |
| 44 | //DbHost: "10.102.58.0", |
| 45 | DbHost: "localhost", |
| 46 | DbPort: 2379, |
| 47 | DbTimeout: 5, |
| 48 | } |
| 49 | txTargetDevId string |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 50 | txDevId string |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 51 | ) |
| 52 | |
| 53 | func init() { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 54 | log.AddPackage(log.JSON, log.DebugLevel, nil) |
khenaidoo | 2c6f167 | 2018-09-20 23:14:41 -0400 | [diff] [blame] | 55 | log.UpdateAllLoggers(log.Fields{"instanceId": "transaction_test"}) |
| 56 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 57 | defer log.CleanUp() |
| 58 | |
| 59 | tx.Backend = NewBackend(tx.DbType, tx.DbHost, tx.DbPort, tx.DbTimeout, tx.DbPrefix) |
| 60 | |
| 61 | msgClass := &voltha.Voltha{} |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 62 | root := NewRoot(msgClass, tx.Backend) |
| 63 | |
| 64 | if tx.Backend != nil { |
| 65 | tx.Root = root.Load(msgClass) |
| 66 | } else { |
| 67 | tx.Root = root |
| 68 | } |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 69 | |
| 70 | GetProfiling().Report() |
| 71 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 72 | tx.Proxy = tx.Root.GetProxy("/", false) |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | func Test_Transaction_1_GetDevices(t *testing.T) { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 76 | getTx := tx.Proxy.OpenTransaction() |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 77 | |
| 78 | devices := getTx.Get("/devices", 1, false) |
| 79 | |
| 80 | if len(devices.([]interface{})) == 0 { |
| 81 | t.Error("there are no available devices to retrieve") |
| 82 | } else { |
| 83 | // Save the target device id for later tests |
| 84 | txTargetDevId = devices.([]interface{})[0].(*voltha.Device).Id |
| 85 | t.Logf("retrieved devices: %+v", devices) |
| 86 | } |
| 87 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 88 | getTx.Commit() |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 91 | func Test_Transaction_2_AddDevice(t *testing.T) { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 92 | devIdBin, _ := uuid.New().MarshalBinary() |
| 93 | txDevId = "0001" + hex.EncodeToString(devIdBin)[:12] |
| 94 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 95 | ports := []*voltha.Port{ |
| 96 | { |
| 97 | PortNo: 123, |
| 98 | Label: "test-port-0", |
| 99 | Type: voltha.Port_PON_OLT, |
| 100 | AdminState: common.AdminState_ENABLED, |
| 101 | OperStatus: common.OperStatus_ACTIVE, |
| 102 | DeviceId: "etcd_port-0-device-id", |
| 103 | Peers: []*voltha.Port_PeerPort{}, |
| 104 | }, |
| 105 | } |
| 106 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 107 | device := &voltha.Device{ |
| 108 | Id: txDevId, |
| 109 | Type: "simulated_olt", |
| 110 | Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"}, |
| 111 | AdminState: voltha.AdminState_PREPROVISIONED, |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 112 | Ports: ports, |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 113 | } |
| 114 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 115 | addTx := tx.Proxy.OpenTransaction() |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 116 | |
| 117 | if added := addTx.Add("/devices", device); added == nil { |
| 118 | t.Error("Failed to add device") |
| 119 | } else { |
| 120 | t.Logf("Added device : %+v", added) |
| 121 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 122 | addTx.Commit() |
| 123 | } |
| 124 | |
| 125 | func Test_Transaction_3_GetDevice_PostAdd(t *testing.T) { |
| 126 | |
| 127 | basePath := "/devices/" + txDevId |
| 128 | |
| 129 | getDevWithPortsTx := tx.Proxy.OpenTransaction() |
| 130 | device1 := getDevWithPortsTx.Get(basePath+"/ports", 1, false) |
| 131 | t.Logf("retrieved device with ports: %+v", device1) |
| 132 | getDevWithPortsTx.Commit() |
| 133 | |
| 134 | getDevTx := tx.Proxy.OpenTransaction() |
| 135 | device2 := getDevTx.Get(basePath, 0, false) |
| 136 | t.Logf("retrieved device: %+v", device2) |
| 137 | |
| 138 | getDevTx.Commit() |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | func Test_Transaction_4_UpdateDevice(t *testing.T) { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 142 | updateTx := tx.Proxy.OpenTransaction() |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 143 | if retrieved := updateTx.Get("/devices/"+txTargetDevId, 1, false); retrieved == nil { |
| 144 | t.Error("Failed to get device") |
| 145 | } else { |
| 146 | var fwVersion int |
| 147 | if retrieved.(*voltha.Device).FirmwareVersion == "n/a" { |
| 148 | fwVersion = 0 |
| 149 | } else { |
| 150 | fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion) |
| 151 | fwVersion += 1 |
| 152 | } |
| 153 | |
| 154 | cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device) |
| 155 | cloned.FirmwareVersion = strconv.Itoa(fwVersion) |
| 156 | t.Logf("Before update : %+v", cloned) |
| 157 | |
| 158 | // FIXME: The makeBranch passed in function is nil or not being executed properly!!!!! |
| 159 | if afterUpdate := updateTx.Update("/devices/"+txTargetDevId, &cloned, false); afterUpdate == nil { |
| 160 | t.Error("Failed to update device") |
| 161 | } else { |
| 162 | t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData()) |
| 163 | } |
| 164 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 165 | updateTx.Commit() |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 166 | } |
| 167 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 168 | func Test_Transaction_5_GetDevice_PostUpdate(t *testing.T) { |
| 169 | |
| 170 | basePath := "/devices/" + txDevId |
| 171 | |
| 172 | getDevWithPortsTx := tx.Proxy.OpenTransaction() |
| 173 | device1 := getDevWithPortsTx.Get(basePath+"/ports", 1, false) |
| 174 | t.Logf("retrieved device with ports: %+v", device1) |
| 175 | getDevWithPortsTx.Commit() |
| 176 | |
| 177 | getDevTx := tx.Proxy.OpenTransaction() |
| 178 | device2 := getDevTx.Get(basePath, 0, false) |
| 179 | t.Logf("retrieved device: %+v", device2) |
| 180 | |
| 181 | getDevTx.Commit() |
| 182 | } |
| 183 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 184 | func Test_Transaction_6_RemoveDevice(t *testing.T) { |
| 185 | removeTx := tx.Proxy.OpenTransaction() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 186 | if removed := removeTx.Remove("/devices/" + txDevId); removed == nil { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 187 | t.Error("Failed to remove device") |
| 188 | } else { |
| 189 | t.Logf("Removed device : %+v", removed) |
| 190 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 191 | removeTx.Commit() |
| 192 | } |
| 193 | |
| 194 | func Test_Transaction_7_GetDevice_PostRemove(t *testing.T) { |
| 195 | |
| 196 | basePath := "/devices/" + txDevId |
| 197 | |
| 198 | getDevTx := tx.Proxy.OpenTransaction() |
| 199 | device := tx.Proxy.Get(basePath, 0, false, "") |
| 200 | t.Logf("retrieved device: %+v", device) |
| 201 | |
| 202 | getDevTx.Commit() |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 203 | } |