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