blob: 4a0cc2915abf956d716a0e35f4f443ff58579439 [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 (
19 "github.com/opencord/voltha-go/protos/voltha"
20 "github.com/opencord/voltha-go/common/log"
21 "testing"
22 "github.com/google/uuid"
23 "encoding/hex"
24 "strconv"
25 "reflect"
26)
27
28type transactionTest struct {
29 Root *Root
30 Backend *Backend
31 Proxy *Proxy
32 DbPrefix string
33 DbType string
34 DbHost string
35 DbPort int
36 DbTimeout int
37}
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
49 txDevId string
50)
51
52func init() {
53 if _, err := log.SetLogger(log.CONSOLE, 0, log.Fields{"instanceId": "transaction_test"}); err != nil {
54 log.With(log.Fields{"error": err}).Fatal("cannot setup logging")
55 }
56 defer log.CleanUp()
57
58 tx.Backend = NewBackend(tx.DbType, tx.DbHost, tx.DbPort, tx.DbTimeout, tx.DbPrefix)
59
60 msgClass := &voltha.Voltha{}
61 root := NewRoot(msgClass, tx.Backend, nil)
62 tx.Root = root.Load(msgClass)
63
64 GetProfiling().Report()
65
66 tx.Proxy = tx.Root.Node.GetProxy("/", false)
67
68}
69
70func Test_Transaction_1_GetDevices(t *testing.T) {
71 getTx := tx.Proxy.openTransaction()
72
73 devices := getTx.Get("/devices", 1, false)
74
75 if len(devices.([]interface{})) == 0 {
76 t.Error("there are no available devices to retrieve")
77 } else {
78 // Save the target device id for later tests
79 txTargetDevId = devices.([]interface{})[0].(*voltha.Device).Id
80 t.Logf("retrieved devices: %+v", devices)
81 }
82
83 tx.Proxy.commitTransaction(getTx.txid)
84}
85
86func Test_Transaction_2_GetDevice(t *testing.T) {
87
88 basePath := "/devices/" + txTargetDevId
89
90 getDevWithPortsTx := tx.Proxy.openTransaction()
91 device1 := getDevWithPortsTx.Get(basePath+"/ports", 1, false)
92 t.Logf("retrieved device with ports: %+v", device1)
93 tx.Proxy.commitTransaction(getDevWithPortsTx.txid)
94
95 getDevTx := tx.Proxy.openTransaction()
96 device2 := getDevTx.Get(basePath, 0, false)
97 t.Logf("retrieved device: %+v", device2)
98 tx.Proxy.commitTransaction(getDevTx.txid)
99}
100
101func Test_Transaction_3_AddDevice(t *testing.T) {
102 devIdBin, _ := uuid.New().MarshalBinary()
103 txDevId = "0001" + hex.EncodeToString(devIdBin)[:12]
104
105 device := &voltha.Device{
106 Id: txDevId,
107 Type: "simulated_olt",
108 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
109 AdminState: voltha.AdminState_PREPROVISIONED,
110 }
111
112 addTx := tx.Proxy.openTransaction()
113
114 if added := addTx.Add("/devices", device); added == nil {
115 t.Error("Failed to add device")
116 } else {
117 t.Logf("Added device : %+v", added)
118 }
119 tx.Proxy.commitTransaction(addTx.txid)
120}
121
122func Test_Transaction_4_UpdateDevice(t *testing.T) {
123 updateTx := tx.Proxy.openTransaction()
124 if retrieved := updateTx.Get("/devices/"+txTargetDevId, 1, false); retrieved == nil {
125 t.Error("Failed to get device")
126 } else {
127 var fwVersion int
128 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
129 fwVersion = 0
130 } else {
131 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
132 fwVersion += 1
133 }
134
135 cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device)
136 cloned.FirmwareVersion = strconv.Itoa(fwVersion)
137 t.Logf("Before update : %+v", cloned)
138
139 // FIXME: The makeBranch passed in function is nil or not being executed properly!!!!!
140 if afterUpdate := updateTx.Update("/devices/"+txTargetDevId, &cloned, false); afterUpdate == nil {
141 t.Error("Failed to update device")
142 } else {
143 t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData())
144 }
145 }
146 tx.Proxy.commitTransaction(updateTx.txid)
147}
148
149func Test_Transaction_5_RemoveDevice(t *testing.T) {
150 removeTx := tx.Proxy.openTransaction()
151 if removed := removeTx.Remove("/devices/"+txDevId); removed == nil {
152 t.Error("Failed to remove device")
153 } else {
154 t.Logf("Removed device : %+v", removed)
155 }
156 tx.Proxy.commitTransaction(removeTx.txid)
157}