blob: 12ff83e7239cac09517cd008e0c4682932051c18 [file] [log] [blame]
kdarapu3248f9a2019-10-03 13:54:52 +05301/*
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
17//Package adaptercore provides the utility for olt devices, flows and statistics
18package adaptercore
19
20import (
Gamze Abakafee36392019-10-03 11:17:24 +000021 "fmt"
kdarapu3248f9a2019-10-03 13:54:52 +053022 "testing"
23
Scott Bakerc6e54cb2019-11-04 09:31:25 -080024 "github.com/opencord/voltha-protos/v2/go/voltha"
kdarapub26b4502019-10-05 03:02:33 +053025
Scott Baker51290152019-10-24 14:23:20 -070026 "github.com/opencord/voltha-lib-go/v2/pkg/db/model"
27 fu "github.com/opencord/voltha-lib-go/v2/pkg/flows"
28 "github.com/opencord/voltha-lib-go/v2/pkg/log"
29 tp "github.com/opencord/voltha-lib-go/v2/pkg/techprofile"
kdarapu3248f9a2019-10-03 13:54:52 +053030 "github.com/opencord/voltha-openolt-adapter/adaptercore/resourcemanager"
kdarapub26b4502019-10-05 03:02:33 +053031 rsrcMgr "github.com/opencord/voltha-openolt-adapter/adaptercore/resourcemanager"
kdarapu3248f9a2019-10-03 13:54:52 +053032 "github.com/opencord/voltha-openolt-adapter/mocks"
Scott Bakerc6e54cb2019-11-04 09:31:25 -080033 ofp "github.com/opencord/voltha-protos/v2/go/openflow_13"
34 "github.com/opencord/voltha-protos/v2/go/openolt"
35 openoltpb2 "github.com/opencord/voltha-protos/v2/go/openolt"
36 tp_pb "github.com/opencord/voltha-protos/v2/go/tech_profile"
kdarapu3248f9a2019-10-03 13:54:52 +053037)
38
kdarapub26b4502019-10-05 03:02:33 +053039var flowMgr *OpenOltFlowMgr
40
kdarapu3248f9a2019-10-03 13:54:52 +053041func init() {
42 log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
kdarapub26b4502019-10-05 03:02:33 +053043 flowMgr = newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +053044}
45func newMockResourceMgr() *resourcemanager.OpenOltResourceMgr {
46 ranges := []*openolt.DeviceInfo_DeviceResourceRanges{
47 {IntfIds: []uint32{0, 1, 2}}}
48
49 deviceinfo := &openolt.DeviceInfo{Vendor: "openolt", Model: "openolt", HardwareVersion: "1.0", FirmwareVersion: "1.0",
50 DeviceId: "olt", DeviceSerialNumber: "openolt", PonPorts: 3, Technology: "Default",
51 OnuIdStart: 1, OnuIdEnd: 1, AllocIdStart: 1, AllocIdEnd: 1,
52 GemportIdStart: 1, GemportIdEnd: 1, FlowIdStart: 1, FlowIdEnd: 1,
53 Ranges: ranges,
54 }
55 rsrMgr := resourcemanager.NewResourceMgr("olt", "127.0.0.1:2379", "etcd", "olt", deviceinfo)
kdarapub26b4502019-10-05 03:02:33 +053056 for key := range rsrMgr.ResourceMgrs {
57 rsrMgr.ResourceMgrs[key].KVStore = &model.Backend{}
58 rsrMgr.ResourceMgrs[key].KVStore.Client = &mocks.MockKVClient{}
59 rsrMgr.ResourceMgrs[key].TechProfileMgr = mocks.MockTechProfile{TpID: key}
60 }
kdarapu3248f9a2019-10-03 13:54:52 +053061 return rsrMgr
62}
63
64func newMockFlowmgr() *OpenOltFlowMgr {
65 rsrMgr := newMockResourceMgr()
66 dh := newMockDeviceHandler()
67
68 rsrMgr.KVStore = &model.Backend{}
69 rsrMgr.KVStore.Client = &mocks.MockKVClient{}
70
71 dh.resourceMgr = rsrMgr
72 flwMgr := NewFlowManager(dh, rsrMgr)
73 onuIds := make(map[onuIDKey]onuInfo)
74 onuIds[onuIDKey{intfID: 1, onuID: 1}] = onuInfo{intfID: 1, onuID: 1, serialNumber: "1"}
75 onuIds[onuIDKey{intfID: 2, onuID: 2}] = onuInfo{intfID: 2, onuID: 2, serialNumber: "2"}
76 flwMgr.onuIds = onuIds
77
78 onuSerialNumbers := make(map[string]onuInfo)
79 onuSerialNumbers["1"] = onuInfo{intfID: 1, onuID: 1, serialNumber: "1"}
80 onuSerialNumbers["2"] = onuInfo{intfID: 2, onuID: 1, serialNumber: "2"}
81 flwMgr.onuSerialNumbers = onuSerialNumbers
82
83 onuGemPortIds := make(map[gemPortKey]onuInfo)
84 onuGemPortIds[gemPortKey{intfID: 1, gemPort: 1}] = onuInfo{intfID: 1, onuID: 1, serialNumber: "1"}
85 onuGemPortIds[gemPortKey{intfID: 2, gemPort: 2}] = onuInfo{intfID: 2, onuID: 2, serialNumber: "2"}
86 flwMgr.onuGemPortIds = onuGemPortIds
87
88 packetInGemPort := make(map[packetInInfoKey]uint32)
89 packetInGemPort[packetInInfoKey{intfID: 1, onuID: 1, logicalPort: 1}] = 1
90 packetInGemPort[packetInInfoKey{intfID: 2, onuID: 2, logicalPort: 2}] = 2
91
92 flwMgr.packetInGemPort = packetInGemPort
kdarapub26b4502019-10-05 03:02:33 +053093 tps := make([]tp.TechProfileIf, len(rsrMgr.ResourceMgrs))
94 for key := range rsrMgr.ResourceMgrs {
95 tps[key] = mocks.MockTechProfile{TpID: key}
kdarapu3248f9a2019-10-03 13:54:52 +053096 }
97 flwMgr.techprofile = tps
98 return flwMgr
99}
kdarapub26b4502019-10-05 03:02:33 +0530100
kdarapu3248f9a2019-10-03 13:54:52 +0530101func TestOpenOltFlowMgr_CreateSchedulerQueues(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530102 // flowMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530103
104 tprofile := &tp.TechProfile{Name: "tp1", SubscriberIdentifier: "subscriber1",
105 ProfileType: "pt1", NumGemPorts: 1, NumTconts: 1, Version: 1,
106 InstanceCtrl: tp.InstanceControl{Onu: "1", Uni: "1", MaxGemPayloadSize: "1"},
107 }
108 tprofile.UsScheduler.Direction = "UPSTREAM"
109 tprofile.UsScheduler.AdditionalBw = "AdditionalBW_None"
110 tprofile.UsScheduler.QSchedPolicy = "WRR"
111
112 tprofile2 := tprofile
113 tprofile2.DsScheduler.Direction = "DOWNSTREAM"
114 tprofile2.DsScheduler.AdditionalBw = "AdditionalBW_None"
115 tprofile2.DsScheduler.QSchedPolicy = "WRR"
116 bands := make([]*ofp.OfpMeterBandHeader, 2)
117 bands[0] = &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 1000, BurstSize: 5000, Data: &ofp.OfpMeterBandHeader_Drop{}}
118 bands[1] = &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 2000, BurstSize: 5000, Data: &ofp.OfpMeterBandHeader_Drop{}}
119 ofpMeterConfig := &ofp.OfpMeterConfig{Flags: 1, MeterId: 1, Bands: bands}
120 flowmetadata := &voltha.FlowMetadata{
121 Meters: []*ofp.OfpMeterConfig{ofpMeterConfig},
122 }
123 type args struct {
124 Dir tp_pb.Direction
125 IntfID uint32
126 OnuID uint32
127 UniID uint32
128 UniPort uint32
129 TpInst *tp.TechProfile
130 MeterID uint32
131 flowMetadata *voltha.FlowMetadata
132 }
133 tests := []struct {
Gamze Abakafee36392019-10-03 11:17:24 +0000134 name string
135 schedQueue schedQueue
136 wantErr bool
kdarapu3248f9a2019-10-03 13:54:52 +0530137 }{
138 // TODO: Add test cases.
Gamze Abakafee36392019-10-03 11:17:24 +0000139 {"CreateSchedulerQueues-1", schedQueue{tp_pb.Direction_UPSTREAM, 1, 1, 1, 64, 1, tprofile, 1, flowmetadata}, false},
140 {"CreateSchedulerQueues-2", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 1, flowmetadata}, false},
141 {"CreateSchedulerQueues-3", schedQueue{tp_pb.Direction_UPSTREAM, 1, 1, 1, 64, 1, tprofile, 2, flowmetadata}, true},
142 {"CreateSchedulerQueues-4", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 2, flowmetadata}, true},
143 {"CreateSchedulerQueues-5", schedQueue{tp_pb.Direction_UPSTREAM, 2, 2, 2, 64, 2, tprofile, 2, flowmetadata}, true},
144 {"CreateSchedulerQueues-6", schedQueue{tp_pb.Direction_DOWNSTREAM, 2, 2, 2, 65, 2, tprofile2, 2, flowmetadata}, true},
145 {"CreateSchedulerQueues-13", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 1, flowmetadata}, false},
kdarapu3248f9a2019-10-03 13:54:52 +0530146 //Negative testcases
Gamze Abakafee36392019-10-03 11:17:24 +0000147 {"CreateSchedulerQueues-7", schedQueue{tp_pb.Direction_UPSTREAM, 1, 1, 1, 64, 1, tprofile, 1, &voltha.FlowMetadata{}}, true},
148 {"CreateSchedulerQueues-8", schedQueue{tp_pb.Direction_UPSTREAM, 1, 1, 1, 64, 1, tprofile, 0, &voltha.FlowMetadata{}}, true},
149 {"CreateSchedulerQueues-9", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 1, &voltha.FlowMetadata{}}, true},
150 {"CreateSchedulerQueues-10", schedQueue{tp_pb.Direction_UPSTREAM, 1, 1, 1, 64, 1, tprofile, 2, &voltha.FlowMetadata{}}, true},
151 {"CreateSchedulerQueues-11", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 2, &voltha.FlowMetadata{}}, true},
152 {"CreateSchedulerQueues-12", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 2, nil}, true},
kdarapu3248f9a2019-10-03 13:54:52 +0530153 }
154 for _, tt := range tests {
155 t.Run(tt.name, func(t *testing.T) {
Gamze Abakafee36392019-10-03 11:17:24 +0000156 if err := flowMgr.CreateSchedulerQueues(tt.schedQueue); (err != nil) != tt.wantErr {
kdarapu3248f9a2019-10-03 13:54:52 +0530157 t.Errorf("OpenOltFlowMgr.CreateSchedulerQueues() error = %v, wantErr %v", err, tt.wantErr)
158 }
159 })
160 }
161}
162
163func TestOpenOltFlowMgr_RemoveSchedulerQueues(t *testing.T) {
164
kdarapub26b4502019-10-05 03:02:33 +0530165 // flowMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530166 tprofile := &tp.TechProfile{Name: "tp1", SubscriberIdentifier: "subscriber1",
167 ProfileType: "pt1", NumGemPorts: 1, NumTconts: 1, Version: 1,
168 InstanceCtrl: tp.InstanceControl{Onu: "1", Uni: "1", MaxGemPayloadSize: "1"},
169 }
170 tprofile.UsScheduler.Direction = "UPSTREAM"
171 tprofile.UsScheduler.AdditionalBw = "AdditionalBW_None"
172 tprofile.UsScheduler.QSchedPolicy = "WRR"
173
174 tprofile2 := tprofile
175 tprofile2.DsScheduler.Direction = "DOWNSTREAM"
176 tprofile2.DsScheduler.AdditionalBw = "AdditionalBW_None"
177 tprofile2.DsScheduler.QSchedPolicy = "WRR"
178 //defTprofile := &tp.DefaultTechProfile{}
179 type args struct {
180 Dir tp_pb.Direction
181 IntfID uint32
182 OnuID uint32
183 UniID uint32
184 UniPort uint32
185 TpInst *tp.TechProfile
186 }
187 tests := []struct {
Gamze Abakafee36392019-10-03 11:17:24 +0000188 name string
189 schedQueue schedQueue
190 wantErr bool
kdarapu3248f9a2019-10-03 13:54:52 +0530191 }{
192 // TODO: Add test cases.
Gamze Abakafee36392019-10-03 11:17:24 +0000193 {"RemoveSchedulerQueues", schedQueue{tp_pb.Direction_UPSTREAM, 1, 1, 1, 64, 1, tprofile, 0, nil}, false},
194 {"RemoveSchedulerQueues", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 0, nil}, false},
kdarapu3248f9a2019-10-03 13:54:52 +0530195 // negative test cases
Gamze Abakafee36392019-10-03 11:17:24 +0000196 {"RemoveSchedulerQueues", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 0, nil}, false},
197 {"RemoveSchedulerQueues", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 0, nil}, false},
kdarapu3248f9a2019-10-03 13:54:52 +0530198 }
199 for _, tt := range tests {
200 t.Run(tt.name, func(t *testing.T) {
Gamze Abakafee36392019-10-03 11:17:24 +0000201 if err := flowMgr.RemoveSchedulerQueues(tt.schedQueue); (err != nil) != tt.wantErr {
kdarapu3248f9a2019-10-03 13:54:52 +0530202 t.Errorf("OpenOltFlowMgr.RemoveSchedulerQueues() error = %v, wantErr %v", err, tt.wantErr)
203 }
204 })
205 }
kdarapub26b4502019-10-05 03:02:33 +0530206
kdarapu3248f9a2019-10-03 13:54:52 +0530207}
208
209func TestOpenOltFlowMgr_RemoveFlow(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530210 // flowMgr := newMockFlowmgr()
211 log.Debug("Info Warning Error: Starting RemoveFlow() test")
kdarapu3248f9a2019-10-03 13:54:52 +0530212 fa := &fu.FlowArgs{
213 MatchFields: []*ofp.OfpOxmOfbField{
214 fu.InPort(2),
215 fu.Metadata_ofp(2),
216 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
217 },
218 Actions: []*ofp.OfpAction{
219 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
220 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
221 fu.Output(1),
222 },
223 }
224 ofpstats := fu.MkFlowStat(fa)
kdarapub26b4502019-10-05 03:02:33 +0530225 ofpstats.Cookie = ofpstats.Id
226 flowMgr.storedDeviceFlows = append(flowMgr.storedDeviceFlows, *ofpstats)
227 lldpFa := &fu.FlowArgs{
228 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
229 MatchFields: []*ofp.OfpOxmOfbField{
230 fu.InPort(1),
231 fu.EthType(0x88CC),
232 fu.TunnelId(536870912),
233 },
234 Actions: []*ofp.OfpAction{
235 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
236 },
237 }
238 lldpofpstats := fu.MkFlowStat(lldpFa)
239 //lldpofpstats.Cookie = lldpofpstats.Id
240
241 dhcpFa := &fu.FlowArgs{
242 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
243 MatchFields: []*ofp.OfpOxmOfbField{
244 fu.InPort(1),
245 fu.UdpSrc(67),
246 //fu.TunnelId(536870912),
247 fu.IpProto(17),
248 },
249 Actions: []*ofp.OfpAction{
250 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
251 },
252 }
253 dhcpofpstats := fu.MkFlowStat(dhcpFa)
254 //dhcpofpstats.Cookie = dhcpofpstats.Id
kdarapu3248f9a2019-10-03 13:54:52 +0530255 type args struct {
256 flow *ofp.OfpFlowStats
257 }
258 tests := []struct {
259 name string
260 args args
261 }{
262 // TODO: Add test cases.
263 {"RemoveFlow", args{flow: ofpstats}},
kdarapub26b4502019-10-05 03:02:33 +0530264 {"RemoveFlow", args{flow: lldpofpstats}},
265 {"RemoveFlow", args{flow: dhcpofpstats}},
kdarapu3248f9a2019-10-03 13:54:52 +0530266 }
267 for _, tt := range tests {
268 t.Run(tt.name, func(t *testing.T) {
269 flowMgr.RemoveFlow(tt.args.flow)
270 })
271 }
kdarapub26b4502019-10-05 03:02:33 +0530272 // t.Error("=====")
kdarapu3248f9a2019-10-03 13:54:52 +0530273}
274
275func TestOpenOltFlowMgr_AddFlow(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530276 // flowMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530277 kw := make(map[string]uint64)
278 kw["table_id"] = 1
279 kw["meter_id"] = 1
kdarapub26b4502019-10-05 03:02:33 +0530280 kw["write_metadata"] = 0x4000000000 // Tech-Profile-ID 64
281
282 // Upstream flow
kdarapu3248f9a2019-10-03 13:54:52 +0530283 fa := &fu.FlowArgs{
284 MatchFields: []*ofp.OfpOxmOfbField{
kdarapub26b4502019-10-05 03:02:33 +0530285 fu.InPort(536870912),
286 fu.Metadata_ofp(1),
287 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
288 },
289 Actions: []*ofp.OfpAction{
290 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
291 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
292 fu.Output(65536),
293 fu.PushVlan(0x8100),
294 },
295 KV: kw,
296 }
297
298 // Downstream flow
299 fa3 := &fu.FlowArgs{
300 MatchFields: []*ofp.OfpOxmOfbField{
301 fu.InPort(65536),
302 fu.Metadata_ofp(1),
303 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257),
304 },
305 Actions: []*ofp.OfpAction{
306 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
307 //fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
308 fu.PopVlan(),
309 fu.Output(536870912),
310 },
311 KV: kw,
312 }
313
314 fa2 := &fu.FlowArgs{
315 MatchFields: []*ofp.OfpOxmOfbField{
kdarapu3248f9a2019-10-03 13:54:52 +0530316 fu.InPort(1000),
317 fu.Metadata_ofp(1),
318 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
319 },
320 Actions: []*ofp.OfpAction{
321 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
322 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
323 fu.Output(65533),
324 },
325 KV: kw,
326 }
327
kdarapub26b4502019-10-05 03:02:33 +0530328 // TODO Add LLDP flow
329 // TODO Add DHCP flow
330
331 // Flows for negative scenarios
332 // Failure in formulateActionInfoFromFlow()
333 fa4 := &fu.FlowArgs{
334 MatchFields: []*ofp.OfpOxmOfbField{
335 fu.InPort(1000),
336 fu.Metadata_ofp(1),
337 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
338 },
339 Actions: []*ofp.OfpAction{
340 fu.Experimenter(257, []byte{1, 2, 3, 4}),
341 },
342 KV: kw,
343 }
344
345 // Invalid Output
346 fa5 := &fu.FlowArgs{
347 MatchFields: []*ofp.OfpOxmOfbField{
348 fu.InPort(1000),
349 fu.Metadata_ofp(1),
350 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
351 },
352 Actions: []*ofp.OfpAction{
353 fu.Output(0),
354 },
355 KV: kw,
356 }
357
358 // Tech-Profile-ID update (not supported)
359 kw6 := make(map[string]uint64)
360 kw6["table_id"] = 1
361 kw6["meter_id"] = 1
362 kw6["write_metadata"] = 0x4100000000 // TpID Other than the stored one
363 fa6 := &fu.FlowArgs{
364 MatchFields: []*ofp.OfpOxmOfbField{
365 fu.InPort(536870912),
366 fu.TunnelId(16),
367 fu.Metadata_ofp(1),
368 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
369 },
370 Actions: []*ofp.OfpAction{
371 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
372 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
373 fu.Output(65535),
374 },
375 KV: kw6,
376 }
377
378 lldpFa := &fu.FlowArgs{
379 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
380 MatchFields: []*ofp.OfpOxmOfbField{
381 fu.InPort(1),
382 fu.EthType(0x88CC),
383 fu.TunnelId(536870912),
384 },
385 Actions: []*ofp.OfpAction{
386 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
387 },
388 }
389
390 dhcpFa := &fu.FlowArgs{
391 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
392 MatchFields: []*ofp.OfpOxmOfbField{
393 fu.InPort(1),
394 fu.UdpSrc(67),
395 //fu.TunnelId(536870912),
396 fu.IpProto(17),
397 },
398 Actions: []*ofp.OfpAction{
399 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
400 },
401 }
402 igmpFa := &fu.FlowArgs{
403 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
404 MatchFields: []*ofp.OfpOxmOfbField{
405 fu.InPort(1),
406 fu.UdpSrc(67),
407 //fu.TunnelId(536870912),
408 fu.IpProto(2),
409 },
410 Actions: []*ofp.OfpAction{
411 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
412 },
413 }
414
415 fa9 := &fu.FlowArgs{
416 MatchFields: []*ofp.OfpOxmOfbField{
417 fu.InPort(536870912),
418 fu.TunnelId(16),
419 fu.Metadata_ofp(1),
420 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
421 fu.VlanPcp(1000),
422 fu.UdpDst(65535),
423 fu.UdpSrc(536870912),
424 fu.Ipv4Dst(65535),
425 fu.Ipv4Src(536870912),
426 },
427 Actions: []*ofp.OfpAction{
428 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
429 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
430 fu.Output(65535),
431 },
432 KV: kw6,
433 }
434
435 fa10 := &fu.FlowArgs{
436 MatchFields: []*ofp.OfpOxmOfbField{
437 fu.InPort(65533),
438 // fu.TunnelId(16),
439 fu.Metadata_ofp(1),
440 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
441 fu.VlanPcp(1000),
442 fu.UdpDst(65535),
443 fu.UdpSrc(536870912),
444 fu.Ipv4Dst(65535),
445 fu.Ipv4Src(536870912),
446 },
447 Actions: []*ofp.OfpAction{
448 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
449 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
450 fu.Output(65535),
451 },
452 KV: kw6,
453 }
kdarapu3248f9a2019-10-03 13:54:52 +0530454 ofpstats := fu.MkFlowStat(fa)
kdarapub26b4502019-10-05 03:02:33 +0530455 ofpstats2 := fu.MkFlowStat(fa2)
456 ofpstats3 := fu.MkFlowStat(fa3)
457 ofpstats4 := fu.MkFlowStat(fa4)
458 ofpstats5 := fu.MkFlowStat(fa5)
459 ofpstats6 := fu.MkFlowStat(fa6)
460 ofpstats7 := fu.MkFlowStat(lldpFa)
461 ofpstats8 := fu.MkFlowStat(dhcpFa)
462 ofpstats9 := fu.MkFlowStat(fa9)
463 ofpstats10 := fu.MkFlowStat(fa10)
464 igmpstats := fu.MkFlowStat(igmpFa)
465
Gamze Abakafee36392019-10-03 11:17:24 +0000466 fmt.Println(ofpstats6, ofpstats9, ofpstats10)
467
kdarapu3248f9a2019-10-03 13:54:52 +0530468 ofpMeterConfig := &ofp.OfpMeterConfig{Flags: 1, MeterId: 1}
kdarapu3248f9a2019-10-03 13:54:52 +0530469 flowMetadata := &voltha.FlowMetadata{
470 Meters: []*ofp.OfpMeterConfig{ofpMeterConfig},
471 }
472 type args struct {
473 flow *ofp.OfpFlowStats
474 flowMetadata *voltha.FlowMetadata
475 }
476 tests := []struct {
477 name string
478 args args
479 }{
480 // TODO: Add test cases.
481 {"AddFlow", args{flow: ofpstats, flowMetadata: flowMetadata}},
kdarapub26b4502019-10-05 03:02:33 +0530482 {"AddFlow", args{flow: ofpstats2, flowMetadata: flowMetadata}},
483 {"AddFlow", args{flow: ofpstats3, flowMetadata: flowMetadata}},
484 {"AddFlow", args{flow: ofpstats4, flowMetadata: flowMetadata}},
485 {"AddFlow", args{flow: ofpstats5, flowMetadata: flowMetadata}},
Gamze Abakafee36392019-10-03 11:17:24 +0000486 //{"AddFlow", args{flow: ofpstats6, flowMetadata: flowMetadata}},
kdarapub26b4502019-10-05 03:02:33 +0530487 {"AddFlow", args{flow: ofpstats7, flowMetadata: flowMetadata}},
488 {"AddFlow", args{flow: ofpstats8, flowMetadata: flowMetadata}},
Gamze Abakafee36392019-10-03 11:17:24 +0000489 //{"AddFlow", args{flow: ofpstats9, flowMetadata: flowMetadata}},
kdarapub26b4502019-10-05 03:02:33 +0530490 {"AddFlow", args{flow: igmpstats, flowMetadata: flowMetadata}},
Gamze Abakafee36392019-10-03 11:17:24 +0000491 //{"AddFlow", args{flow: ofpstats10, flowMetadata: flowMetadata}},
kdarapub26b4502019-10-05 03:02:33 +0530492 //ofpstats10
kdarapu3248f9a2019-10-03 13:54:52 +0530493 }
494 for _, tt := range tests {
495 t.Run(tt.name, func(t *testing.T) {
496 flowMgr.AddFlow(tt.args.flow, tt.args.flowMetadata)
497 })
498 }
499}
500
501func TestOpenOltFlowMgr_UpdateOnuInfo(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530502 // flowMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530503 type args struct {
504 intfID uint32
505 onuID uint32
506 serialNum string
507 }
508 tests := []struct {
509 name string
510 args args
511 }{
512 // TODO: Add test cases.
513 {"UpdateOnuInfo", args{1, 1, "onu1"}},
514 {"UpdateOnuInfo", args{2, 3, "onu1"}},
515 }
516 for _, tt := range tests {
517 t.Run(tt.name, func(t *testing.T) {
518
519 flowMgr.UpdateOnuInfo(tt.args.intfID, tt.args.onuID, tt.args.serialNum)
520 })
521 }
522}
523
524func TestOpenOltFlowMgr_GetLogicalPortFromPacketIn(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530525 // flowMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530526 type args struct {
527 packetIn *openoltpb2.PacketIndication
528 }
529 tests := []struct {
530 name string
531 args args
532 want uint32
533 wantErr bool
534 }{
535 // TODO: Add test cases.
536 {"GetLogicalPortFromPacketIn", args{packetIn: &openoltpb2.PacketIndication{IntfType: "pon", IntfId: 1, GemportId: 1, FlowId: 100, PortNo: 1, Cookie: 100, Pkt: []byte("GetLogicalPortFromPacketIn")}}, 1, false},
537 {"GetLogicalPortFromPacketIn", args{packetIn: &openoltpb2.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 100, PortNo: 1, Cookie: 100, Pkt: []byte("GetLogicalPortFromPacketIn")}}, 65537, false},
538 // Negative Test cases.
539 {"GetLogicalPortFromPacketIn", args{packetIn: &openoltpb2.PacketIndication{IntfType: "pon", IntfId: 2, GemportId: 1, FlowId: 100, PortNo: 1, Cookie: 100, Pkt: []byte("GetLogicalPortFromPacketIn")}}, 0, true},
540 {"GetLogicalPortFromPacketIn", args{packetIn: &openoltpb2.PacketIndication{IntfType: "pon", IntfId: 1, GemportId: 1, FlowId: 100, PortNo: 0, Cookie: 100, Pkt: []byte("GetLogicalPortFromPacketIn")}}, 2064, false},
541 }
542 for _, tt := range tests {
543 t.Run(tt.name, func(t *testing.T) {
544
545 got, err := flowMgr.GetLogicalPortFromPacketIn(tt.args.packetIn)
546 if (err != nil) != tt.wantErr {
547 t.Errorf("OpenOltFlowMgr.GetLogicalPortFromPacketIn() error = %v, wantErr %v", err, tt.wantErr)
548 return
549 }
550 if got != tt.want {
551 t.Errorf("OpenOltFlowMgr.GetLogicalPortFromPacketIn() = %v, want %v", got, tt.want)
552 }
553 })
554 }
555}
556
557func TestOpenOltFlowMgr_GetPacketOutGemPortID(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530558 // flwMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530559
560 type args struct {
561 intfID uint32
562 onuID uint32
563 portNum uint32
564 }
565 tests := []struct {
566 name string
567 args args
568 want uint32
569 wantErr bool
570 }{
571 // TODO: Add test cases.
572 {"GetPacketOutGemPortID", args{intfID: 1, onuID: 1, portNum: 1}, 1, false},
573 {"GetPacketOutGemPortID", args{intfID: 2, onuID: 2, portNum: 2}, 2, false},
574 {"GetPacketOutGemPortID", args{intfID: 1, onuID: 2, portNum: 2}, 0, true},
575 }
576 for _, tt := range tests {
577 t.Run(tt.name, func(t *testing.T) {
578
kdarapub26b4502019-10-05 03:02:33 +0530579 got, err := flowMgr.GetPacketOutGemPortID(tt.args.intfID, tt.args.onuID, tt.args.portNum)
kdarapu3248f9a2019-10-03 13:54:52 +0530580 if (err != nil) != tt.wantErr {
581 t.Errorf("OpenOltFlowMgr.GetPacketOutGemPortID() error = %v, wantErr %v", err, tt.wantErr)
582 return
583 }
584 if got != tt.want {
585 t.Errorf("OpenOltFlowMgr.GetPacketOutGemPortID() = %v, want %v", got, tt.want)
586 }
587
588 })
589 }
590}
591
592func TestOpenOltFlowMgr_DeleteTechProfileInstance(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530593 // flwMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530594 type args struct {
595 intfID uint32
596 onuID uint32
597 uniID uint32
598 sn string
Gamze Abakafee36392019-10-03 11:17:24 +0000599 tpID uint32
kdarapu3248f9a2019-10-03 13:54:52 +0530600 }
601 tests := []struct {
602 name string
603 args args
604 wantErr bool
605 }{
606 // TODO: Add test cases.
Gamze Abakafee36392019-10-03 11:17:24 +0000607 {"DeleteTechProfileInstance", args{intfID: 0, onuID: 1, uniID: 1, sn: "", tpID: 64}, false},
kdarapu3248f9a2019-10-03 13:54:52 +0530608 }
609 for _, tt := range tests {
610 t.Run(tt.name, func(t *testing.T) {
Gamze Abakafee36392019-10-03 11:17:24 +0000611 if err := flowMgr.DeleteTechProfileInstance(tt.args.intfID, tt.args.onuID, tt.args.uniID, tt.args.sn, tt.args.tpID); (err != nil) != tt.wantErr {
kdarapu3248f9a2019-10-03 13:54:52 +0530612 t.Errorf("OpenOltFlowMgr.DeleteTechProfileInstance() error = %v, wantErr %v", err, tt.wantErr)
613 }
614 })
615 }
616}
kdarapub26b4502019-10-05 03:02:33 +0530617
618func TestOpenOltFlowMgr_checkAndAddFlow(t *testing.T) {
619 // flowMgr := newMockFlowmgr()
620 kw := make(map[string]uint64)
621 kw["table_id"] = 1
622 kw["meter_id"] = 1
623 kw["write_metadata"] = 0x4000000000 // Tech-Profile-ID 64
624
625 // Upstream flow
626 fa := &fu.FlowArgs{
627 MatchFields: []*ofp.OfpOxmOfbField{
628 fu.InPort(536870912),
629 fu.Metadata_ofp(1),
630 fu.IpProto(17), // dhcp
Gamze Abakafee36392019-10-03 11:17:24 +0000631 fu.VlanPcp(0),
kdarapub26b4502019-10-05 03:02:33 +0530632 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
633 },
634 Actions: []*ofp.OfpAction{
635 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
636 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
637 fu.Output(65536),
638 fu.PushVlan(0x8100),
639 },
640 KV: kw,
641 }
642
643 // EAPOL
644 fa2 := &fu.FlowArgs{
645 MatchFields: []*ofp.OfpOxmOfbField{
646 fu.InPort(536870912),
647 fu.Metadata_ofp(1),
648 fu.EthType(0x888E),
649 fu.VlanPcp(1),
650 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257),
651 },
652 Actions: []*ofp.OfpAction{
653 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
654 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
655 fu.Output(65536),
656 fu.PushVlan(0x8100),
657 },
658 KV: kw,
659 }
660
661 // HSIA
662 fa3 := &fu.FlowArgs{
663 MatchFields: []*ofp.OfpOxmOfbField{
664 fu.InPort(536870912),
665 fu.Metadata_ofp(1),
666 //fu.EthType(0x8100),
667 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
668 },
669 Actions: []*ofp.OfpAction{
670 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
671 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0)),
672 fu.Output(65536),
673 fu.PushVlan(0x8100),
674 },
675 KV: kw,
676 }
677
678 fa4 := &fu.FlowArgs{
679 MatchFields: []*ofp.OfpOxmOfbField{
680 fu.InPort(65535),
681 fu.Metadata_ofp(1),
682 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
683 fu.VlanPcp(1),
684 },
685 Actions: []*ofp.OfpAction{
686 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
687 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0)),
688 fu.Output(536870912),
689 fu.PopVlan(),
690 },
691 KV: kw,
692 }
693
694 classifierInfo := make(map[string]interface{})
695 actionInfo := make(map[string]interface{})
696 classifierInfo2 := make(map[string]interface{})
697 actionInfo2 := make(map[string]interface{})
698 classifierInfo3 := make(map[string]interface{})
699 actionInfo3 := make(map[string]interface{})
700 classifierInfo4 := make(map[string]interface{})
701 actionInfo4 := make(map[string]interface{})
702 flowState := fu.MkFlowStat(fa)
703 flowState2 := fu.MkFlowStat(fa2)
704 flowState3 := fu.MkFlowStat(fa3)
705 flowState4 := fu.MkFlowStat(fa4)
706 formulateClassifierInfoFromFlow(classifierInfo, flowState)
707 formulateClassifierInfoFromFlow(classifierInfo2, flowState2)
708 formulateClassifierInfoFromFlow(classifierInfo3, flowState3)
709 formulateClassifierInfoFromFlow(classifierInfo4, flowState4)
710
711 err := formulateActionInfoFromFlow(actionInfo, classifierInfo, flowState)
712 if err != nil {
713 // Error logging is already done in the called function
714 // So just return in case of error
715 return
716 }
717
718 err = formulateActionInfoFromFlow(actionInfo2, classifierInfo2, flowState2)
719 if err != nil {
720 // Error logging is already done in the called function
721 // So just return in case of error
722 return
723 }
724
725 err = formulateActionInfoFromFlow(actionInfo3, classifierInfo3, flowState3)
726 if err != nil {
727 // Error logging is already done in the called function
728 // So just return in case of error
729 return
730 }
731
732 err = formulateActionInfoFromFlow(actionInfo4, classifierInfo4, flowState4)
733 if err != nil {
734 // Error logging is already done in the called function
735 // So just return in case of error
736 return
737 }
738
739 //ofpMeterConfig := &ofp.OfpMeterConfig{Flags: 1, MeterId: 1}
740 //flowMetadata := &voltha.FlowMetadata{
741 // Meters: []*ofp.OfpMeterConfig{ofpMeterConfig},
742 //}
743
744 TpInst := &tp.TechProfile{
745 Name: "Test-Tech-Profile",
746 SubscriberIdentifier: "257",
747 ProfileType: "Mock",
748 Version: 1,
749 NumGemPorts: 4,
750 NumTconts: 1,
751 InstanceCtrl: tp.InstanceControl{
752 Onu: "1",
753 Uni: "16",
754 },
755 }
756
757 type fields struct {
758 techprofile []tp.TechProfileIf
759 deviceHandler *DeviceHandler
760 resourceMgr *rsrcMgr.OpenOltResourceMgr
761 onuIds map[onuIDKey]onuInfo
762 onuSerialNumbers map[string]onuInfo
763 onuGemPortIds map[gemPortKey]onuInfo
764 packetInGemPort map[packetInInfoKey]uint32
765 storedDeviceFlows []ofp.OfpFlowStats
766 }
767 type args struct {
768 args map[string]uint32
769 classifierInfo map[string]interface{}
770 actionInfo map[string]interface{}
771 flow *ofp.OfpFlowStats
772 gemPort uint32
773 intfID uint32
774 onuID uint32
775 uniID uint32
776 portNo uint32
777 TpInst *tp.TechProfile
778 allocID []uint32
779 gemPorts []uint32
780 TpID uint32
781 uni string
782 }
783 tests := []struct {
784 name string
785 fields fields
786 args args
787 }{
788 {
789 name: "checkAndAddFlow-1",
790 args: args{
791 args: nil,
792 classifierInfo: classifierInfo,
793 actionInfo: actionInfo,
794 flow: flowState,
795 gemPort: 1,
796 intfID: 1,
797 onuID: 1,
798 uniID: 16,
799 portNo: 1,
800 TpInst: TpInst,
801 allocID: []uint32{0x8001, 0x8002, 0x8003, 0x8004},
802 gemPorts: []uint32{1, 2, 3, 4},
803 TpID: 64,
804 uni: "16",
805 },
806 },
807 {
808 name: "checkAndAddFlow-2",
809 args: args{
810 args: nil,
811 classifierInfo: classifierInfo2,
812 actionInfo: actionInfo2,
813 flow: flowState2,
814 gemPort: 1,
815 intfID: 1,
816 onuID: 1,
817 uniID: 16,
818 portNo: 1,
819 TpInst: TpInst,
820 allocID: []uint32{0x8001, 0x8002, 0x8003, 0x8004},
821 gemPorts: []uint32{1, 2, 3, 4},
822 TpID: 64,
823 uni: "16",
824 },
825 },
826 {
827 name: "checkAndAddFlow-3",
828 args: args{
829 args: nil,
830 classifierInfo: classifierInfo3,
831 actionInfo: actionInfo3,
832 flow: flowState3,
833 gemPort: 1,
834 intfID: 1,
835 onuID: 1,
836 uniID: 16,
837 portNo: 1,
838 TpInst: TpInst,
839 allocID: []uint32{0x8001, 0x8002, 0x8003, 0x8004},
840 gemPorts: []uint32{1, 2, 3, 4},
841 TpID: 64,
842 uni: "16",
843 },
844 },
845 {
846 name: "checkAndAddFlow-4",
847 args: args{
848 args: nil,
849 classifierInfo: classifierInfo4,
850 actionInfo: actionInfo4,
851 flow: flowState4,
852 gemPort: 1,
853 intfID: 1,
854 onuID: 1,
855 uniID: 16,
856 portNo: 1,
857 TpInst: TpInst,
858 allocID: []uint32{0x8001, 0x8002, 0x8003, 0x8004},
859 gemPorts: []uint32{1, 2, 3, 4},
860 TpID: 64,
861 uni: "16",
862 },
863 },
864 }
865 for _, tt := range tests {
866 t.Run(tt.name, func(t *testing.T) {
Gamze Abakafee36392019-10-03 11:17:24 +0000867 flowMgr.checkAndAddFlow(tt.args.args, tt.args.classifierInfo, tt.args.actionInfo, tt.args.flow,
868 tt.args.TpInst, tt.args.gemPorts, tt.args.TpID, tt.args.uni)
kdarapub26b4502019-10-05 03:02:33 +0530869 })
870 }
871}