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