blob: a490acdea839029cb8072d7d39482bb64429f6be [file] [log] [blame]
Stephane Barbariee16186c2018-09-11 10:46:34 -04001/*
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 */
16package model
17
18import (
Stephane Barbariee16186c2018-09-11 10:46:34 -040019 "encoding/hex"
khenaidoob9203542018-09-17 22:56:37 -040020 "github.com/google/uuid"
21 "github.com/opencord/voltha-go/common/log"
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040022 "github.com/opencord/voltha-go/protos/common"
khenaidoob9203542018-09-17 22:56:37 -040023 "github.com/opencord/voltha-go/protos/voltha"
Stephane Barbariee16186c2018-09-11 10:46:34 -040024 "reflect"
khenaidoob9203542018-09-17 22:56:37 -040025 "strconv"
26 "testing"
Stephane Barbariee16186c2018-09-11 10:46:34 -040027)
28
29type transactionTest struct {
Stephane Barbarie06c4a742018-10-01 11:09:32 -040030 Root *root
khenaidoob9203542018-09-17 22:56:37 -040031 Backend *Backend
32 Proxy *Proxy
33 DbPrefix string
34 DbType string
35 DbHost string
36 DbPort int
37 DbTimeout int
Stephane Barbariee16186c2018-09-11 10:46:34 -040038}
39
40var (
41 tx = &transactionTest{
42 DbPrefix: "service/voltha/data/core/0001",
43 DbType: "etcd",
44 //DbHost: "10.102.58.0",
45 DbHost: "localhost",
46 DbPort: 2379,
47 DbTimeout: 5,
48 }
49 txTargetDevId string
khenaidoob9203542018-09-17 22:56:37 -040050 txDevId string
Stephane Barbariee16186c2018-09-11 10:46:34 -040051)
52
53func init() {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040054 log.AddPackage(log.JSON, log.DebugLevel, nil)
khenaidoo2c6f1672018-09-20 23:14:41 -040055 log.UpdateAllLoggers(log.Fields{"instanceId": "transaction_test"})
56
Stephane Barbariee16186c2018-09-11 10:46:34 -040057 defer log.CleanUp()
58
59 tx.Backend = NewBackend(tx.DbType, tx.DbHost, tx.DbPort, tx.DbTimeout, tx.DbPrefix)
60
61 msgClass := &voltha.Voltha{}
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040062 root := NewRoot(msgClass, tx.Backend)
63
64 if tx.Backend != nil {
65 tx.Root = root.Load(msgClass)
66 } else {
67 tx.Root = root
68 }
Stephane Barbariee16186c2018-09-11 10:46:34 -040069
70 GetProfiling().Report()
71
Stephane Barbarie06c4a742018-10-01 11:09:32 -040072 tx.Proxy = tx.Root.GetProxy("/", false)
Stephane Barbariee16186c2018-09-11 10:46:34 -040073}
74
75func Test_Transaction_1_GetDevices(t *testing.T) {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040076 getTx := tx.Proxy.OpenTransaction()
Stephane Barbariee16186c2018-09-11 10:46:34 -040077
78 devices := getTx.Get("/devices", 1, false)
79
80 if len(devices.([]interface{})) == 0 {
81 t.Error("there are no available devices to retrieve")
82 } else {
83 // Save the target device id for later tests
84 txTargetDevId = devices.([]interface{})[0].(*voltha.Device).Id
85 t.Logf("retrieved devices: %+v", devices)
86 }
87
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040088 getTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -040089}
90
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040091func Test_Transaction_2_AddDevice(t *testing.T) {
Stephane Barbariee16186c2018-09-11 10:46:34 -040092 devIdBin, _ := uuid.New().MarshalBinary()
93 txDevId = "0001" + hex.EncodeToString(devIdBin)[:12]
94
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040095 ports := []*voltha.Port{
96 {
97 PortNo: 123,
98 Label: "test-port-0",
99 Type: voltha.Port_PON_OLT,
100 AdminState: common.AdminState_ENABLED,
101 OperStatus: common.OperStatus_ACTIVE,
102 DeviceId: "etcd_port-0-device-id",
103 Peers: []*voltha.Port_PeerPort{},
104 },
105 }
106
Stephane Barbariee16186c2018-09-11 10:46:34 -0400107 device := &voltha.Device{
108 Id: txDevId,
109 Type: "simulated_olt",
110 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
111 AdminState: voltha.AdminState_PREPROVISIONED,
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400112 Ports: ports,
Stephane Barbariee16186c2018-09-11 10:46:34 -0400113 }
114
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400115 addTx := tx.Proxy.OpenTransaction()
Stephane Barbariee16186c2018-09-11 10:46:34 -0400116
117 if added := addTx.Add("/devices", device); added == nil {
118 t.Error("Failed to add device")
119 } else {
120 t.Logf("Added device : %+v", added)
121 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400122 addTx.Commit()
123}
124
125func Test_Transaction_3_GetDevice_PostAdd(t *testing.T) {
126
127 basePath := "/devices/" + txDevId
128
129 getDevWithPortsTx := tx.Proxy.OpenTransaction()
130 device1 := getDevWithPortsTx.Get(basePath+"/ports", 1, false)
131 t.Logf("retrieved device with ports: %+v", device1)
132 getDevWithPortsTx.Commit()
133
134 getDevTx := tx.Proxy.OpenTransaction()
135 device2 := getDevTx.Get(basePath, 0, false)
136 t.Logf("retrieved device: %+v", device2)
137
138 getDevTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -0400139}
140
141func Test_Transaction_4_UpdateDevice(t *testing.T) {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400142 updateTx := tx.Proxy.OpenTransaction()
Stephane Barbariee16186c2018-09-11 10:46:34 -0400143 if retrieved := updateTx.Get("/devices/"+txTargetDevId, 1, false); retrieved == nil {
144 t.Error("Failed to get device")
145 } else {
146 var fwVersion int
147 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
148 fwVersion = 0
149 } else {
150 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
151 fwVersion += 1
152 }
153
154 cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device)
155 cloned.FirmwareVersion = strconv.Itoa(fwVersion)
156 t.Logf("Before update : %+v", cloned)
157
158 // FIXME: The makeBranch passed in function is nil or not being executed properly!!!!!
159 if afterUpdate := updateTx.Update("/devices/"+txTargetDevId, &cloned, false); afterUpdate == nil {
160 t.Error("Failed to update device")
161 } else {
162 t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData())
163 }
164 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400165 updateTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -0400166}
167
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400168func Test_Transaction_5_GetDevice_PostUpdate(t *testing.T) {
169
170 basePath := "/devices/" + txDevId
171
172 getDevWithPortsTx := tx.Proxy.OpenTransaction()
173 device1 := getDevWithPortsTx.Get(basePath+"/ports", 1, false)
174 t.Logf("retrieved device with ports: %+v", device1)
175 getDevWithPortsTx.Commit()
176
177 getDevTx := tx.Proxy.OpenTransaction()
178 device2 := getDevTx.Get(basePath, 0, false)
179 t.Logf("retrieved device: %+v", device2)
180
181 getDevTx.Commit()
182}
183
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400184func Test_Transaction_6_RemoveDevice(t *testing.T) {
185 removeTx := tx.Proxy.OpenTransaction()
khenaidoob9203542018-09-17 22:56:37 -0400186 if removed := removeTx.Remove("/devices/" + txDevId); removed == nil {
Stephane Barbariee16186c2018-09-11 10:46:34 -0400187 t.Error("Failed to remove device")
188 } else {
189 t.Logf("Removed device : %+v", removed)
190 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400191 removeTx.Commit()
192}
193
194func Test_Transaction_7_GetDevice_PostRemove(t *testing.T) {
195
196 basePath := "/devices/" + txDevId
197
198 getDevTx := tx.Proxy.OpenTransaction()
199 device := tx.Proxy.Get(basePath, 0, false, "")
200 t.Logf("retrieved device: %+v", device)
201
202 getDevTx.Commit()
Stephane Barbariee16186c2018-09-11 10:46:34 -0400203}