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