blob: c281f033c275d22ea77b184ef3047762c7a7d489 [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001/*
2 * Copyright 2019-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 core
17
18import (
19 "context"
20 "crypto/rand"
npujar1d86a522019-11-14 17:11:16 +053021 "testing"
22 "time"
23
khenaidooab1f7bd2019-11-14 14:00:27 -050024 cm "github.com/opencord/voltha-go/rw_core/mocks"
25 com "github.com/opencord/voltha-lib-go/v2/pkg/adapters/common"
26 "github.com/opencord/voltha-lib-go/v2/pkg/kafka"
27 "github.com/opencord/voltha-lib-go/v2/pkg/log"
28 lm "github.com/opencord/voltha-lib-go/v2/pkg/mocks"
29 of "github.com/opencord/voltha-protos/v2/go/openflow_13"
30 "github.com/opencord/voltha-protos/v2/go/voltha"
31 "github.com/stretchr/testify/assert"
32 "google.golang.org/grpc/codes"
33 "google.golang.org/grpc/status"
khenaidooab1f7bd2019-11-14 14:00:27 -050034)
35
36const (
37 coreName = "rw_core"
38 adapterName = "adapter_mock"
npujar1d86a522019-11-14 17:11:16 +053039 coreInstanceID = "1000"
khenaidooab1f7bd2019-11-14 14:00:27 -050040)
41
42var (
43 coreKafkaICProxy *kafka.InterContainerProxy
44 adapterKafkaICProxy *kafka.InterContainerProxy
45 kc kafka.Client
46 adapterReqHandler *com.RequestHandlerProxy
47 adapter *cm.Adapter
48)
49
50func init() {
npujar1d86a522019-11-14 17:11:16 +053051 if _, err := log.SetDefaultLogger(log.JSON, 0, log.Fields{"instanceId": coreInstanceID}); err != nil {
khenaidooab1f7bd2019-11-14 14:00:27 -050052 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
53 }
54 // Set the log level to Warning
55 log.SetAllLogLevel(2)
56
57 var err error
58
59 // Create the KV client
60 kc = lm.NewKafkaClient()
61
62 // Setup core inter-container proxy and core request handler
63 if coreKafkaICProxy, err = kafka.NewInterContainerProxy(
64 kafka.MsgClient(kc),
65 kafka.DefaultTopic(&kafka.Topic{Name: coreName})); err != nil || coreKafkaICProxy == nil {
66 log.Fatalw("Failure-creating-core-intercontainerProxy", log.Fields{"error": err})
67
68 }
npujar1d86a522019-11-14 17:11:16 +053069 if err = coreKafkaICProxy.Start(); err != nil {
khenaidooab1f7bd2019-11-14 14:00:27 -050070 log.Fatalw("Failure-starting-core-kafka-intercontainerProxy", log.Fields{"error": err})
71 }
npujar1d86a522019-11-14 17:11:16 +053072 if err = coreKafkaICProxy.SubscribeWithDefaultRequestHandler(kafka.Topic{Name: coreName}, 0); err != nil {
khenaidooab1f7bd2019-11-14 14:00:27 -050073 log.Fatalw("Failure-subscribing-core-request-handler", log.Fields{"error": err})
74 }
75
76 // Setup adapter inter-container proxy and adapter request handler
77 adapterCoreProxy := com.NewCoreProxy(nil, adapterName, coreName)
78 adapter = cm.NewAdapter(adapterCoreProxy)
npujar1d86a522019-11-14 17:11:16 +053079 adapterReqHandler = com.NewRequestHandlerProxy(coreInstanceID, adapter, adapterCoreProxy)
khenaidooab1f7bd2019-11-14 14:00:27 -050080 if adapterKafkaICProxy, err = kafka.NewInterContainerProxy(
81 kafka.MsgClient(kc),
82 kafka.DefaultTopic(&kafka.Topic{Name: adapterName}),
83 kafka.RequestHandlerInterface(adapterReqHandler)); err != nil || adapterKafkaICProxy == nil {
84 log.Fatalw("Failure-creating-adapter-intercontainerProxy", log.Fields{"error": err})
85 }
86 if err = adapterKafkaICProxy.Start(); err != nil {
87 log.Fatalw("Failure-starting-adapter-kafka-intercontainerProxy", log.Fields{"error": err})
88 }
89 if err = adapterKafkaICProxy.SubscribeWithDefaultRequestHandler(kafka.Topic{Name: adapterName}, 0); err != nil {
90 log.Fatalw("Failure-subscribing-adapter-request-handler", log.Fields{"error": err})
91 }
92}
93
94func getRandomBytes(size int) (bytes []byte, err error) {
95 bytes = make([]byte, size)
96 _, err = rand.Read(bytes)
97 return
98}
99
100func TestCreateAdapterProxy(t *testing.T) {
101 ap := NewAdapterProxy(coreKafkaICProxy, coreName)
102 assert.NotNil(t, ap)
103}
104
105func testSimpleRequests(t *testing.T) {
106 type simpleRequest func(context.Context, *voltha.Device) error
107 ap := NewAdapterProxy(coreKafkaICProxy, coreName)
108 simpleRequests := []simpleRequest{
109 ap.AdoptDevice,
110 ap.DisableDevice,
111 ap.RebootDevice,
112 ap.DeleteDevice,
113 ap.ReconcileDevice,
114 ap.ReEnableDevice,
115 }
116 for _, f := range simpleRequests {
117 //Success
118 d := &voltha.Device{Id: "deviceId", Adapter: adapterName}
119 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
120 err := f(ctx, d)
121 cancel()
122 assert.Nil(t, err)
123
124 // Failure - invalid adapter
125 expectedError := status.Error(codes.Canceled, "context deadline exceeded")
126 d = &voltha.Device{Id: "deviceId", Adapter: "adapter_mock_1"}
127 ctx, cancel = context.WithTimeout(context.Background(), 20*time.Millisecond)
128 err = f(ctx, d)
129 cancel()
130 assert.NotNil(t, err)
131 assert.Equal(t, expectedError.Error(), err.Error())
132
133 // Failure - short timeout
134 expectedError = status.Error(codes.Canceled, "context deadline exceeded")
135 d = &voltha.Device{Id: "deviceId", Adapter: adapterName}
136 ctx, cancel = context.WithTimeout(context.Background(), 100*time.Nanosecond)
137 err = f(ctx, d)
138 cancel()
139 assert.NotNil(t, err)
140 assert.Equal(t, expectedError.Error(), err.Error())
141 }
142}
143
144func testGetSwitchCapabilityFromAdapter(t *testing.T) {
145 ap := NewAdapterProxy(coreKafkaICProxy, coreName)
146 d := &voltha.Device{Id: "deviceId", Adapter: adapterName}
147 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
148 switchCap, err := ap.GetOfpDeviceInfo(ctx, d)
149 cancel()
150 assert.Nil(t, err)
151 assert.NotNil(t, switchCap)
152 expectedCap, _ := adapter.Get_ofp_device_info(d)
153 assert.Equal(t, switchCap.String(), expectedCap.String())
154}
155
156func testGetPortInfoFromAdapter(t *testing.T) {
157 ap := NewAdapterProxy(coreKafkaICProxy, coreName)
158 d := &voltha.Device{Id: "deviceId", Adapter: adapterName}
159 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
160 portNo := uint32(1)
161 portInfo, err := ap.GetOfpPortInfo(ctx, d, portNo)
162 cancel()
163 assert.Nil(t, err)
164 assert.NotNil(t, portInfo)
165 expectedPortInfo, _ := adapter.Get_ofp_port_info(d, int64(portNo))
166 assert.Equal(t, portInfo.String(), expectedPortInfo.String())
167}
168
169func testPacketOut(t *testing.T) {
170 ap := NewAdapterProxy(coreKafkaICProxy, coreName)
171 d := &voltha.Device{Id: "deviceId", Adapter: adapterName}
172 outPort := uint32(1)
173 packet, err := getRandomBytes(50)
174 assert.Nil(t, err)
175 err = ap.packetOut(adapterName, d.Id, outPort, &of.OfpPacketOut{Data: packet})
176 assert.Nil(t, err)
177}
178
179func testFlowUpdates(t *testing.T) {
180 ap := NewAdapterProxy(coreKafkaICProxy, coreName)
181 d := &voltha.Device{Id: "deviceId", Adapter: adapterName}
182 err := ap.UpdateFlowsBulk(d, &voltha.Flows{}, &voltha.FlowGroups{}, &voltha.FlowMetadata{})
183 assert.Nil(t, err)
184 flowChanges := &voltha.FlowChanges{ToAdd: &voltha.Flows{Items: nil}, ToRemove: &voltha.Flows{Items: nil}}
185 groupChanges := &voltha.FlowGroupChanges{ToAdd: &voltha.FlowGroups{Items: nil}, ToRemove: &voltha.FlowGroups{Items: nil}, ToUpdate: &voltha.FlowGroups{Items: nil}}
186 err = ap.UpdateFlowsIncremental(d, flowChanges, groupChanges, &voltha.FlowMetadata{})
187 assert.Nil(t, err)
188}
189
190func testPmUpdates(t *testing.T) {
191 ap := NewAdapterProxy(coreKafkaICProxy, coreName)
192 d := &voltha.Device{Id: "deviceId", Adapter: adapterName}
193 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
194 err := ap.UpdatePmConfigs(ctx, d, &voltha.PmConfigs{})
195 cancel()
196 assert.Nil(t, err)
197}
198
199func TestSuite(t *testing.T) {
200 //1. Test the simple requests first
201 testSimpleRequests(t)
202
203 //2. Test get switch capability
204 testGetSwitchCapabilityFromAdapter(t)
205
206 //3. Test get port info
207 testGetPortInfoFromAdapter(t)
208
209 //4. Test PacketOut
210 testPacketOut(t)
211
212 // 5. Test flow updates
213 testFlowUpdates(t)
214
215 //6. Pm configs
216 testPmUpdates(t)
217}