blob: 130a9923b332aa92c65622c4d1ceb105920add8c [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"
22 "github.com/opencord/voltha-go/protos/voltha"
Stephane Barbariee16186c2018-09-11 10:46:34 -040023 "reflect"
khenaidoob9203542018-09-17 22:56:37 -040024 "strconv"
25 "testing"
Stephane Barbariee16186c2018-09-11 10:46:34 -040026)
27
28type transactionTest struct {
khenaidoob9203542018-09-17 22:56:37 -040029 Root *Root
30 Backend *Backend
31 Proxy *Proxy
32 DbPrefix string
33 DbType string
34 DbHost string
35 DbPort int
36 DbTimeout int
Stephane Barbariee16186c2018-09-11 10:46:34 -040037}
38
39var (
40 tx = &transactionTest{
41 DbPrefix: "service/voltha/data/core/0001",
42 DbType: "etcd",
43 //DbHost: "10.102.58.0",
44 DbHost: "localhost",
45 DbPort: 2379,
46 DbTimeout: 5,
47 }
48 txTargetDevId string
khenaidoob9203542018-09-17 22:56:37 -040049 txDevId string
Stephane Barbariee16186c2018-09-11 10:46:34 -040050)
51
52func init() {
khenaidoo2c6f1672018-09-20 23:14:41 -040053
54 log.AddPackage(log.JSON, log.ErrorLevel, nil)
55 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{}
62 root := NewRoot(msgClass, tx.Backend, nil)
63 tx.Root = root.Load(msgClass)
64
65 GetProfiling().Report()
66
67 tx.Proxy = tx.Root.Node.GetProxy("/", false)
68
69}
70
71func Test_Transaction_1_GetDevices(t *testing.T) {
72 getTx := tx.Proxy.openTransaction()
73
74 devices := getTx.Get("/devices", 1, false)
75
76 if len(devices.([]interface{})) == 0 {
77 t.Error("there are no available devices to retrieve")
78 } else {
79 // Save the target device id for later tests
80 txTargetDevId = devices.([]interface{})[0].(*voltha.Device).Id
81 t.Logf("retrieved devices: %+v", devices)
82 }
83
84 tx.Proxy.commitTransaction(getTx.txid)
85}
86
87func Test_Transaction_2_GetDevice(t *testing.T) {
88
89 basePath := "/devices/" + txTargetDevId
90
91 getDevWithPortsTx := tx.Proxy.openTransaction()
92 device1 := getDevWithPortsTx.Get(basePath+"/ports", 1, false)
93 t.Logf("retrieved device with ports: %+v", device1)
94 tx.Proxy.commitTransaction(getDevWithPortsTx.txid)
95
96 getDevTx := tx.Proxy.openTransaction()
97 device2 := getDevTx.Get(basePath, 0, false)
98 t.Logf("retrieved device: %+v", device2)
99 tx.Proxy.commitTransaction(getDevTx.txid)
100}
101
102func Test_Transaction_3_AddDevice(t *testing.T) {
103 devIdBin, _ := uuid.New().MarshalBinary()
104 txDevId = "0001" + hex.EncodeToString(devIdBin)[:12]
105
106 device := &voltha.Device{
107 Id: txDevId,
108 Type: "simulated_olt",
109 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
110 AdminState: voltha.AdminState_PREPROVISIONED,
111 }
112
113 addTx := tx.Proxy.openTransaction()
114
115 if added := addTx.Add("/devices", device); added == nil {
116 t.Error("Failed to add device")
117 } else {
118 t.Logf("Added device : %+v", added)
119 }
120 tx.Proxy.commitTransaction(addTx.txid)
121}
122
123func Test_Transaction_4_UpdateDevice(t *testing.T) {
124 updateTx := tx.Proxy.openTransaction()
125 if retrieved := updateTx.Get("/devices/"+txTargetDevId, 1, false); retrieved == nil {
126 t.Error("Failed to get device")
127 } else {
128 var fwVersion int
129 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
130 fwVersion = 0
131 } else {
132 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
133 fwVersion += 1
134 }
135
136 cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device)
137 cloned.FirmwareVersion = strconv.Itoa(fwVersion)
138 t.Logf("Before update : %+v", cloned)
139
140 // FIXME: The makeBranch passed in function is nil or not being executed properly!!!!!
141 if afterUpdate := updateTx.Update("/devices/"+txTargetDevId, &cloned, false); afterUpdate == nil {
142 t.Error("Failed to update device")
143 } else {
144 t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData())
145 }
146 }
147 tx.Proxy.commitTransaction(updateTx.txid)
148}
149
150func Test_Transaction_5_RemoveDevice(t *testing.T) {
151 removeTx := tx.Proxy.openTransaction()
khenaidoob9203542018-09-17 22:56:37 -0400152 if removed := removeTx.Remove("/devices/" + txDevId); removed == nil {
Stephane Barbariee16186c2018-09-11 10:46:34 -0400153 t.Error("Failed to remove device")
154 } else {
155 t.Logf("Removed device : %+v", removed)
156 }
157 tx.Proxy.commitTransaction(removeTx.txid)
158}