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 | */ |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 16 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 17 | package model |
| 18 | |
| 19 | import ( |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 20 | "encoding/hex" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 21 | "github.com/google/uuid" |
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 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 29 | var ( |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 30 | txTargetDevID string |
| 31 | txDevID string |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 32 | ) |
| 33 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 34 | func Test_Transaction_1_GetDevices(t *testing.T) { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 35 | getTx := modelTestConfig.RootProxy.OpenTransaction() |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 36 | |
| 37 | devices := getTx.Get("/devices", 1, false) |
| 38 | |
| 39 | if len(devices.([]interface{})) == 0 { |
| 40 | t.Error("there are no available devices to retrieve") |
| 41 | } else { |
| 42 | // Save the target device id for later tests |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 43 | txTargetDevID = devices.([]interface{})[0].(*voltha.Device).Id |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 44 | t.Logf("retrieved devices: %+v", devices) |
| 45 | } |
| 46 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 47 | getTx.Commit() |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 48 | } |
| 49 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 50 | func Test_Transaction_2_AddDevice(t *testing.T) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 51 | devIDBin, _ := uuid.New().MarshalBinary() |
| 52 | txDevID = "0001" + hex.EncodeToString(devIDBin)[:12] |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 53 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 54 | ports := []*voltha.Port{ |
| 55 | { |
| 56 | PortNo: 123, |
| 57 | Label: "test-port-0", |
| 58 | Type: voltha.Port_PON_OLT, |
| 59 | AdminState: common.AdminState_ENABLED, |
| 60 | OperStatus: common.OperStatus_ACTIVE, |
| 61 | DeviceId: "etcd_port-0-device-id", |
| 62 | Peers: []*voltha.Port_PeerPort{}, |
| 63 | }, |
| 64 | } |
| 65 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 66 | device := &voltha.Device{ |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 67 | Id: txDevID, |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 68 | Type: "simulated_olt", |
| 69 | Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"}, |
| 70 | AdminState: voltha.AdminState_PREPROVISIONED, |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 71 | Ports: ports, |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 74 | addTx := modelTestConfig.RootProxy.OpenTransaction() |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 75 | |
| 76 | if added := addTx.Add("/devices", device); added == nil { |
| 77 | t.Error("Failed to add device") |
| 78 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 79 | txTargetDevID = added.(*voltha.Device).Id |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 80 | t.Logf("Added device : %+v", added) |
| 81 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 82 | addTx.Commit() |
| 83 | } |
| 84 | |
| 85 | func Test_Transaction_3_GetDevice_PostAdd(t *testing.T) { |
| 86 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 87 | basePath := "/devices/" + txDevID |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 88 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 89 | getDevWithPortsTx := modelTestConfig.RootProxy.OpenTransaction() |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 90 | device1 := getDevWithPortsTx.Get(basePath+"/ports", 1, false) |
| 91 | t.Logf("retrieved device with ports: %+v", device1) |
| 92 | getDevWithPortsTx.Commit() |
| 93 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 94 | getDevTx := modelTestConfig.RootProxy.OpenTransaction() |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 95 | device2 := getDevTx.Get(basePath, 0, false) |
| 96 | t.Logf("retrieved device: %+v", device2) |
| 97 | |
| 98 | getDevTx.Commit() |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | func Test_Transaction_4_UpdateDevice(t *testing.T) { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 102 | updateTx := modelTestConfig.RootProxy.OpenTransaction() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 103 | if retrieved := updateTx.Get("/devices/"+txTargetDevID, 1, false); retrieved == nil { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 104 | t.Error("Failed to get device") |
| 105 | } else { |
| 106 | var fwVersion int |
| 107 | if retrieved.(*voltha.Device).FirmwareVersion == "n/a" { |
| 108 | fwVersion = 0 |
| 109 | } else { |
| 110 | fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 111 | fwVersion++ |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device) |
| 115 | cloned.FirmwareVersion = strconv.Itoa(fwVersion) |
| 116 | t.Logf("Before update : %+v", cloned) |
| 117 | |
| 118 | // FIXME: The makeBranch passed in function is nil or not being executed properly!!!!! |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 119 | if afterUpdate := updateTx.Update("/devices/"+txTargetDevID, &cloned, false); afterUpdate == nil { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 120 | t.Error("Failed to update device") |
| 121 | } else { |
| 122 | t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData()) |
| 123 | } |
| 124 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 125 | updateTx.Commit() |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 126 | } |
| 127 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 128 | func Test_Transaction_5_GetDevice_PostUpdate(t *testing.T) { |
| 129 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 130 | basePath := "/devices/" + txDevID |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 131 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 132 | getDevWithPortsTx := modelTestConfig.RootProxy.OpenTransaction() |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 133 | device1 := getDevWithPortsTx.Get(basePath+"/ports", 1, false) |
| 134 | t.Logf("retrieved device with ports: %+v", device1) |
| 135 | getDevWithPortsTx.Commit() |
| 136 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 137 | getDevTx := modelTestConfig.RootProxy.OpenTransaction() |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 138 | device2 := getDevTx.Get(basePath, 0, false) |
| 139 | t.Logf("retrieved device: %+v", device2) |
| 140 | |
| 141 | getDevTx.Commit() |
| 142 | } |
| 143 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 144 | func Test_Transaction_6_RemoveDevice(t *testing.T) { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 145 | removeTx := modelTestConfig.RootProxy.OpenTransaction() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 146 | if removed := removeTx.Remove("/devices/" + txDevID); removed == nil { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 147 | t.Error("Failed to remove device") |
| 148 | } else { |
| 149 | t.Logf("Removed device : %+v", removed) |
| 150 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 151 | removeTx.Commit() |
| 152 | } |
| 153 | |
| 154 | func Test_Transaction_7_GetDevice_PostRemove(t *testing.T) { |
| 155 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 156 | basePath := "/devices/" + txDevID |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 157 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 158 | getDevTx := modelTestConfig.RootProxy.OpenTransaction() |
| 159 | device := modelTestConfig.RootProxy.Get(basePath, 0, false, "") |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 160 | t.Logf("retrieved device: %+v", device) |
| 161 | |
| 162 | getDevTx.Commit() |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 163 | } |