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