blob: 3b1908116e44969dbe89b20da06c4b6f31a7b668 [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
sbarbaria8910ba2019-11-05 10:12:23 -050026 "github.com/opencord/voltha-lib-go/v2/pkg/db"
Scott Baker51290152019-10-24 14:23:20 -070027 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 {
sbarbaria8910ba2019-11-05 10:12:23 -050057 rsrMgr.ResourceMgrs[key].KVStore = &db.Backend{}
kdarapub26b4502019-10-05 03:02:33 +053058 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 {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053065 rMgr := newMockResourceMgr()
kdarapu3248f9a2019-10-03 13:54:52 +053066 dh := newMockDeviceHandler()
67
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053068 rMgr.KVStore = &db.Backend{}
69 rMgr.KVStore.Client = &mocks.MockKVClient{}
kdarapu3248f9a2019-10-03 13:54:52 +053070
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053071 dh.resourceMgr = rMgr
72 flwMgr := NewFlowManager(dh, rMgr)
kdarapu3248f9a2019-10-03 13:54:52 +053073
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053074 onuGemInfo1 := make([]rsrcMgr.OnuGemInfo, 2)
75 onuGemInfo2 := make([]rsrcMgr.OnuGemInfo, 2)
76 onuGemInfo1[0] = rsrcMgr.OnuGemInfo{OnuID: 1, SerialNumber: "1", IntfID: 1, GemPorts: []uint32{1}}
77 onuGemInfo2[1] = rsrcMgr.OnuGemInfo{OnuID: 2, SerialNumber: "2", IntfID: 2, GemPorts: []uint32{2}}
78 flwMgr.onuGemInfo[1] = onuGemInfo1
79 flwMgr.onuGemInfo[2] = onuGemInfo2
kdarapu3248f9a2019-10-03 13:54:52 +053080
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053081 packetInGemPort := make(map[rsrcMgr.PacketInInfoKey]uint32)
82 packetInGemPort[rsrcMgr.PacketInInfoKey{IntfID: 1, OnuID: 1, LogicalPort: 1}] = 1
83 packetInGemPort[rsrcMgr.PacketInInfoKey{IntfID: 2, OnuID: 2, LogicalPort: 2}] = 2
kdarapu3248f9a2019-10-03 13:54:52 +053084
85 flwMgr.packetInGemPort = packetInGemPort
Amit Ghoshd4cbe482019-11-21 12:07:14 +000086 tps := make(map[uint32]tp.TechProfileIf)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053087 for key := range rMgr.ResourceMgrs {
kdarapub26b4502019-10-05 03:02:33 +053088 tps[key] = mocks.MockTechProfile{TpID: key}
kdarapu3248f9a2019-10-03 13:54:52 +053089 }
90 flwMgr.techprofile = tps
91 return flwMgr
92}
kdarapub26b4502019-10-05 03:02:33 +053093
kdarapu3248f9a2019-10-03 13:54:52 +053094func TestOpenOltFlowMgr_CreateSchedulerQueues(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +053095 // flowMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +053096
97 tprofile := &tp.TechProfile{Name: "tp1", SubscriberIdentifier: "subscriber1",
Girish Gowdra54934262019-11-13 14:19:55 +053098 ProfileType: "pt1", NumGemPorts: 1, Version: 1,
kdarapu3248f9a2019-10-03 13:54:52 +053099 InstanceCtrl: tp.InstanceControl{Onu: "1", Uni: "1", MaxGemPayloadSize: "1"},
100 }
101 tprofile.UsScheduler.Direction = "UPSTREAM"
102 tprofile.UsScheduler.AdditionalBw = "AdditionalBW_None"
103 tprofile.UsScheduler.QSchedPolicy = "WRR"
104
105 tprofile2 := tprofile
106 tprofile2.DsScheduler.Direction = "DOWNSTREAM"
107 tprofile2.DsScheduler.AdditionalBw = "AdditionalBW_None"
108 tprofile2.DsScheduler.QSchedPolicy = "WRR"
109 bands := make([]*ofp.OfpMeterBandHeader, 2)
110 bands[0] = &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 1000, BurstSize: 5000, Data: &ofp.OfpMeterBandHeader_Drop{}}
111 bands[1] = &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 2000, BurstSize: 5000, Data: &ofp.OfpMeterBandHeader_Drop{}}
112 ofpMeterConfig := &ofp.OfpMeterConfig{Flags: 1, MeterId: 1, Bands: bands}
113 flowmetadata := &voltha.FlowMetadata{
114 Meters: []*ofp.OfpMeterConfig{ofpMeterConfig},
115 }
116 type args struct {
117 Dir tp_pb.Direction
118 IntfID uint32
119 OnuID uint32
120 UniID uint32
121 UniPort uint32
122 TpInst *tp.TechProfile
123 MeterID uint32
124 flowMetadata *voltha.FlowMetadata
125 }
126 tests := []struct {
Gamze Abakafee36392019-10-03 11:17:24 +0000127 name string
128 schedQueue schedQueue
129 wantErr bool
kdarapu3248f9a2019-10-03 13:54:52 +0530130 }{
131 // TODO: Add test cases.
Gamze Abakafee36392019-10-03 11:17:24 +0000132 {"CreateSchedulerQueues-1", schedQueue{tp_pb.Direction_UPSTREAM, 1, 1, 1, 64, 1, tprofile, 1, flowmetadata}, false},
133 {"CreateSchedulerQueues-2", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 1, flowmetadata}, false},
134 {"CreateSchedulerQueues-3", schedQueue{tp_pb.Direction_UPSTREAM, 1, 1, 1, 64, 1, tprofile, 2, flowmetadata}, true},
135 {"CreateSchedulerQueues-4", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 2, flowmetadata}, true},
136 {"CreateSchedulerQueues-5", schedQueue{tp_pb.Direction_UPSTREAM, 2, 2, 2, 64, 2, tprofile, 2, flowmetadata}, true},
137 {"CreateSchedulerQueues-6", schedQueue{tp_pb.Direction_DOWNSTREAM, 2, 2, 2, 65, 2, tprofile2, 2, flowmetadata}, true},
138 {"CreateSchedulerQueues-13", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 1, flowmetadata}, false},
kdarapu3248f9a2019-10-03 13:54:52 +0530139 //Negative testcases
Gamze Abakafee36392019-10-03 11:17:24 +0000140 {"CreateSchedulerQueues-7", schedQueue{tp_pb.Direction_UPSTREAM, 1, 1, 1, 64, 1, tprofile, 1, &voltha.FlowMetadata{}}, true},
141 {"CreateSchedulerQueues-8", schedQueue{tp_pb.Direction_UPSTREAM, 1, 1, 1, 64, 1, tprofile, 0, &voltha.FlowMetadata{}}, true},
142 {"CreateSchedulerQueues-9", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 1, &voltha.FlowMetadata{}}, true},
143 {"CreateSchedulerQueues-10", schedQueue{tp_pb.Direction_UPSTREAM, 1, 1, 1, 64, 1, tprofile, 2, &voltha.FlowMetadata{}}, true},
144 {"CreateSchedulerQueues-11", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 2, &voltha.FlowMetadata{}}, true},
145 {"CreateSchedulerQueues-12", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 2, nil}, true},
kdarapu3248f9a2019-10-03 13:54:52 +0530146 }
147 for _, tt := range tests {
148 t.Run(tt.name, func(t *testing.T) {
Gamze Abakafee36392019-10-03 11:17:24 +0000149 if err := flowMgr.CreateSchedulerQueues(tt.schedQueue); (err != nil) != tt.wantErr {
kdarapu3248f9a2019-10-03 13:54:52 +0530150 t.Errorf("OpenOltFlowMgr.CreateSchedulerQueues() error = %v, wantErr %v", err, tt.wantErr)
151 }
152 })
153 }
154}
155
156func TestOpenOltFlowMgr_RemoveSchedulerQueues(t *testing.T) {
157
kdarapub26b4502019-10-05 03:02:33 +0530158 // flowMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530159 tprofile := &tp.TechProfile{Name: "tp1", SubscriberIdentifier: "subscriber1",
Girish Gowdra54934262019-11-13 14:19:55 +0530160 ProfileType: "pt1", NumGemPorts: 1, Version: 1,
kdarapu3248f9a2019-10-03 13:54:52 +0530161 InstanceCtrl: tp.InstanceControl{Onu: "1", Uni: "1", MaxGemPayloadSize: "1"},
162 }
163 tprofile.UsScheduler.Direction = "UPSTREAM"
164 tprofile.UsScheduler.AdditionalBw = "AdditionalBW_None"
165 tprofile.UsScheduler.QSchedPolicy = "WRR"
166
167 tprofile2 := tprofile
168 tprofile2.DsScheduler.Direction = "DOWNSTREAM"
169 tprofile2.DsScheduler.AdditionalBw = "AdditionalBW_None"
170 tprofile2.DsScheduler.QSchedPolicy = "WRR"
171 //defTprofile := &tp.DefaultTechProfile{}
172 type args struct {
173 Dir tp_pb.Direction
174 IntfID uint32
175 OnuID uint32
176 UniID uint32
177 UniPort uint32
178 TpInst *tp.TechProfile
179 }
180 tests := []struct {
Gamze Abakafee36392019-10-03 11:17:24 +0000181 name string
182 schedQueue schedQueue
183 wantErr bool
kdarapu3248f9a2019-10-03 13:54:52 +0530184 }{
185 // TODO: Add test cases.
Gamze Abakafee36392019-10-03 11:17:24 +0000186 {"RemoveSchedulerQueues", schedQueue{tp_pb.Direction_UPSTREAM, 1, 1, 1, 64, 1, tprofile, 0, nil}, false},
187 {"RemoveSchedulerQueues", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 0, nil}, false},
kdarapu3248f9a2019-10-03 13:54:52 +0530188 // negative test cases
Gamze Abakafee36392019-10-03 11:17:24 +0000189 {"RemoveSchedulerQueues", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 0, nil}, false},
190 {"RemoveSchedulerQueues", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 0, nil}, false},
kdarapu3248f9a2019-10-03 13:54:52 +0530191 }
192 for _, tt := range tests {
193 t.Run(tt.name, func(t *testing.T) {
Gamze Abakafee36392019-10-03 11:17:24 +0000194 if err := flowMgr.RemoveSchedulerQueues(tt.schedQueue); (err != nil) != tt.wantErr {
kdarapu3248f9a2019-10-03 13:54:52 +0530195 t.Errorf("OpenOltFlowMgr.RemoveSchedulerQueues() error = %v, wantErr %v", err, tt.wantErr)
196 }
197 })
198 }
kdarapub26b4502019-10-05 03:02:33 +0530199
kdarapu3248f9a2019-10-03 13:54:52 +0530200}
201
202func TestOpenOltFlowMgr_RemoveFlow(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530203 // flowMgr := newMockFlowmgr()
204 log.Debug("Info Warning Error: Starting RemoveFlow() test")
kdarapu3248f9a2019-10-03 13:54:52 +0530205 fa := &fu.FlowArgs{
206 MatchFields: []*ofp.OfpOxmOfbField{
207 fu.InPort(2),
208 fu.Metadata_ofp(2),
209 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
210 },
211 Actions: []*ofp.OfpAction{
212 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
213 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
214 fu.Output(1),
215 },
216 }
217 ofpstats := fu.MkFlowStat(fa)
kdarapub26b4502019-10-05 03:02:33 +0530218 ofpstats.Cookie = ofpstats.Id
kdarapub26b4502019-10-05 03:02:33 +0530219 lldpFa := &fu.FlowArgs{
220 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
221 MatchFields: []*ofp.OfpOxmOfbField{
222 fu.InPort(1),
223 fu.EthType(0x88CC),
224 fu.TunnelId(536870912),
225 },
226 Actions: []*ofp.OfpAction{
227 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
228 },
229 }
230 lldpofpstats := fu.MkFlowStat(lldpFa)
231 //lldpofpstats.Cookie = lldpofpstats.Id
232
233 dhcpFa := &fu.FlowArgs{
234 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
235 MatchFields: []*ofp.OfpOxmOfbField{
236 fu.InPort(1),
237 fu.UdpSrc(67),
238 //fu.TunnelId(536870912),
239 fu.IpProto(17),
240 },
241 Actions: []*ofp.OfpAction{
242 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
243 },
244 }
245 dhcpofpstats := fu.MkFlowStat(dhcpFa)
246 //dhcpofpstats.Cookie = dhcpofpstats.Id
kdarapu3248f9a2019-10-03 13:54:52 +0530247 type args struct {
248 flow *ofp.OfpFlowStats
249 }
250 tests := []struct {
251 name string
252 args args
253 }{
254 // TODO: Add test cases.
255 {"RemoveFlow", args{flow: ofpstats}},
kdarapub26b4502019-10-05 03:02:33 +0530256 {"RemoveFlow", args{flow: lldpofpstats}},
257 {"RemoveFlow", args{flow: dhcpofpstats}},
kdarapu3248f9a2019-10-03 13:54:52 +0530258 }
259 for _, tt := range tests {
260 t.Run(tt.name, func(t *testing.T) {
261 flowMgr.RemoveFlow(tt.args.flow)
262 })
263 }
kdarapub26b4502019-10-05 03:02:33 +0530264 // t.Error("=====")
kdarapu3248f9a2019-10-03 13:54:52 +0530265}
266
267func TestOpenOltFlowMgr_AddFlow(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530268 // flowMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530269 kw := make(map[string]uint64)
270 kw["table_id"] = 1
271 kw["meter_id"] = 1
kdarapub26b4502019-10-05 03:02:33 +0530272 kw["write_metadata"] = 0x4000000000 // Tech-Profile-ID 64
273
274 // Upstream flow
kdarapu3248f9a2019-10-03 13:54:52 +0530275 fa := &fu.FlowArgs{
276 MatchFields: []*ofp.OfpOxmOfbField{
kdarapub26b4502019-10-05 03:02:33 +0530277 fu.InPort(536870912),
278 fu.Metadata_ofp(1),
279 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
280 },
281 Actions: []*ofp.OfpAction{
282 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
283 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
284 fu.Output(65536),
285 fu.PushVlan(0x8100),
286 },
287 KV: kw,
288 }
289
290 // Downstream flow
291 fa3 := &fu.FlowArgs{
292 MatchFields: []*ofp.OfpOxmOfbField{
293 fu.InPort(65536),
294 fu.Metadata_ofp(1),
295 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257),
296 },
297 Actions: []*ofp.OfpAction{
298 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
299 //fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
300 fu.PopVlan(),
301 fu.Output(536870912),
302 },
303 KV: kw,
304 }
305
306 fa2 := &fu.FlowArgs{
307 MatchFields: []*ofp.OfpOxmOfbField{
kdarapu3248f9a2019-10-03 13:54:52 +0530308 fu.InPort(1000),
309 fu.Metadata_ofp(1),
310 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
311 },
312 Actions: []*ofp.OfpAction{
313 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
314 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
315 fu.Output(65533),
316 },
317 KV: kw,
318 }
319
kdarapub26b4502019-10-05 03:02:33 +0530320 // TODO Add LLDP flow
321 // TODO Add DHCP flow
322
323 // Flows for negative scenarios
324 // Failure in formulateActionInfoFromFlow()
325 fa4 := &fu.FlowArgs{
326 MatchFields: []*ofp.OfpOxmOfbField{
327 fu.InPort(1000),
328 fu.Metadata_ofp(1),
329 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
330 },
331 Actions: []*ofp.OfpAction{
332 fu.Experimenter(257, []byte{1, 2, 3, 4}),
333 },
334 KV: kw,
335 }
336
337 // Invalid Output
338 fa5 := &fu.FlowArgs{
339 MatchFields: []*ofp.OfpOxmOfbField{
340 fu.InPort(1000),
341 fu.Metadata_ofp(1),
342 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
343 },
344 Actions: []*ofp.OfpAction{
345 fu.Output(0),
346 },
347 KV: kw,
348 }
349
350 // Tech-Profile-ID update (not supported)
351 kw6 := make(map[string]uint64)
352 kw6["table_id"] = 1
353 kw6["meter_id"] = 1
354 kw6["write_metadata"] = 0x4100000000 // TpID Other than the stored one
355 fa6 := &fu.FlowArgs{
356 MatchFields: []*ofp.OfpOxmOfbField{
357 fu.InPort(536870912),
358 fu.TunnelId(16),
359 fu.Metadata_ofp(1),
360 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
361 },
362 Actions: []*ofp.OfpAction{
363 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
364 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
365 fu.Output(65535),
366 },
367 KV: kw6,
368 }
369
370 lldpFa := &fu.FlowArgs{
371 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
372 MatchFields: []*ofp.OfpOxmOfbField{
373 fu.InPort(1),
374 fu.EthType(0x88CC),
375 fu.TunnelId(536870912),
376 },
377 Actions: []*ofp.OfpAction{
378 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
379 },
380 }
381
382 dhcpFa := &fu.FlowArgs{
383 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
384 MatchFields: []*ofp.OfpOxmOfbField{
385 fu.InPort(1),
386 fu.UdpSrc(67),
387 //fu.TunnelId(536870912),
388 fu.IpProto(17),
389 },
390 Actions: []*ofp.OfpAction{
391 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
392 },
393 }
394 igmpFa := &fu.FlowArgs{
395 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
396 MatchFields: []*ofp.OfpOxmOfbField{
397 fu.InPort(1),
398 fu.UdpSrc(67),
399 //fu.TunnelId(536870912),
400 fu.IpProto(2),
401 },
402 Actions: []*ofp.OfpAction{
403 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
404 },
405 }
406
407 fa9 := &fu.FlowArgs{
408 MatchFields: []*ofp.OfpOxmOfbField{
409 fu.InPort(536870912),
410 fu.TunnelId(16),
411 fu.Metadata_ofp(1),
412 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
413 fu.VlanPcp(1000),
414 fu.UdpDst(65535),
415 fu.UdpSrc(536870912),
416 fu.Ipv4Dst(65535),
417 fu.Ipv4Src(536870912),
418 },
419 Actions: []*ofp.OfpAction{
420 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
421 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
422 fu.Output(65535),
423 },
424 KV: kw6,
425 }
426
427 fa10 := &fu.FlowArgs{
428 MatchFields: []*ofp.OfpOxmOfbField{
429 fu.InPort(65533),
430 // fu.TunnelId(16),
431 fu.Metadata_ofp(1),
432 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
433 fu.VlanPcp(1000),
434 fu.UdpDst(65535),
435 fu.UdpSrc(536870912),
436 fu.Ipv4Dst(65535),
437 fu.Ipv4Src(536870912),
438 },
439 Actions: []*ofp.OfpAction{
440 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
441 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
442 fu.Output(65535),
443 },
444 KV: kw6,
445 }
kdarapu3248f9a2019-10-03 13:54:52 +0530446 ofpstats := fu.MkFlowStat(fa)
kdarapub26b4502019-10-05 03:02:33 +0530447 ofpstats2 := fu.MkFlowStat(fa2)
448 ofpstats3 := fu.MkFlowStat(fa3)
449 ofpstats4 := fu.MkFlowStat(fa4)
450 ofpstats5 := fu.MkFlowStat(fa5)
451 ofpstats6 := fu.MkFlowStat(fa6)
452 ofpstats7 := fu.MkFlowStat(lldpFa)
453 ofpstats8 := fu.MkFlowStat(dhcpFa)
454 ofpstats9 := fu.MkFlowStat(fa9)
455 ofpstats10 := fu.MkFlowStat(fa10)
456 igmpstats := fu.MkFlowStat(igmpFa)
457
Gamze Abakafee36392019-10-03 11:17:24 +0000458 fmt.Println(ofpstats6, ofpstats9, ofpstats10)
459
kdarapu3248f9a2019-10-03 13:54:52 +0530460 ofpMeterConfig := &ofp.OfpMeterConfig{Flags: 1, MeterId: 1}
kdarapu3248f9a2019-10-03 13:54:52 +0530461 flowMetadata := &voltha.FlowMetadata{
462 Meters: []*ofp.OfpMeterConfig{ofpMeterConfig},
463 }
464 type args struct {
465 flow *ofp.OfpFlowStats
466 flowMetadata *voltha.FlowMetadata
467 }
468 tests := []struct {
469 name string
470 args args
471 }{
472 // TODO: Add test cases.
473 {"AddFlow", args{flow: ofpstats, flowMetadata: flowMetadata}},
kdarapub26b4502019-10-05 03:02:33 +0530474 {"AddFlow", args{flow: ofpstats2, flowMetadata: flowMetadata}},
475 {"AddFlow", args{flow: ofpstats3, flowMetadata: flowMetadata}},
476 {"AddFlow", args{flow: ofpstats4, flowMetadata: flowMetadata}},
477 {"AddFlow", args{flow: ofpstats5, flowMetadata: flowMetadata}},
Gamze Abakafee36392019-10-03 11:17:24 +0000478 //{"AddFlow", args{flow: ofpstats6, flowMetadata: flowMetadata}},
kdarapub26b4502019-10-05 03:02:33 +0530479 {"AddFlow", args{flow: ofpstats7, flowMetadata: flowMetadata}},
480 {"AddFlow", args{flow: ofpstats8, flowMetadata: flowMetadata}},
Gamze Abakafee36392019-10-03 11:17:24 +0000481 //{"AddFlow", args{flow: ofpstats9, flowMetadata: flowMetadata}},
kdarapub26b4502019-10-05 03:02:33 +0530482 {"AddFlow", args{flow: igmpstats, flowMetadata: flowMetadata}},
Gamze Abakafee36392019-10-03 11:17:24 +0000483 //{"AddFlow", args{flow: ofpstats10, flowMetadata: flowMetadata}},
kdarapub26b4502019-10-05 03:02:33 +0530484 //ofpstats10
kdarapu3248f9a2019-10-03 13:54:52 +0530485 }
486 for _, tt := range tests {
487 t.Run(tt.name, func(t *testing.T) {
488 flowMgr.AddFlow(tt.args.flow, tt.args.flowMetadata)
489 })
490 }
491}
492
493func TestOpenOltFlowMgr_UpdateOnuInfo(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530494 // flowMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530495 type args struct {
496 intfID uint32
497 onuID uint32
498 serialNum string
499 }
500 tests := []struct {
501 name string
502 args args
503 }{
504 // TODO: Add test cases.
505 {"UpdateOnuInfo", args{1, 1, "onu1"}},
506 {"UpdateOnuInfo", args{2, 3, "onu1"}},
507 }
508 for _, tt := range tests {
509 t.Run(tt.name, func(t *testing.T) {
510
511 flowMgr.UpdateOnuInfo(tt.args.intfID, tt.args.onuID, tt.args.serialNum)
512 })
513 }
514}
515
516func TestOpenOltFlowMgr_GetLogicalPortFromPacketIn(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530517 // flowMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530518 type args struct {
519 packetIn *openoltpb2.PacketIndication
520 }
521 tests := []struct {
522 name string
523 args args
524 want uint32
525 wantErr bool
526 }{
527 // TODO: Add test cases.
528 {"GetLogicalPortFromPacketIn", args{packetIn: &openoltpb2.PacketIndication{IntfType: "pon", IntfId: 1, GemportId: 1, FlowId: 100, PortNo: 1, Cookie: 100, Pkt: []byte("GetLogicalPortFromPacketIn")}}, 1, false},
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000529 {"GetLogicalPortFromPacketIn", args{packetIn: &openoltpb2.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 100, PortNo: 1, Cookie: 100, Pkt: []byte("GetLogicalPortFromPacketIn")}}, 1048577, false},
kdarapu3248f9a2019-10-03 13:54:52 +0530530 // Negative Test cases.
531 {"GetLogicalPortFromPacketIn", args{packetIn: &openoltpb2.PacketIndication{IntfType: "pon", IntfId: 2, GemportId: 1, FlowId: 100, PortNo: 1, Cookie: 100, Pkt: []byte("GetLogicalPortFromPacketIn")}}, 0, true},
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000532 {"GetLogicalPortFromPacketIn", args{packetIn: &openoltpb2.PacketIndication{IntfType: "pon", IntfId: 1, GemportId: 1, FlowId: 100, PortNo: 0, Cookie: 100, Pkt: []byte("GetLogicalPortFromPacketIn")}}, 4112, false},
kdarapu3248f9a2019-10-03 13:54:52 +0530533 }
534 for _, tt := range tests {
535 t.Run(tt.name, func(t *testing.T) {
536
537 got, err := flowMgr.GetLogicalPortFromPacketIn(tt.args.packetIn)
538 if (err != nil) != tt.wantErr {
539 t.Errorf("OpenOltFlowMgr.GetLogicalPortFromPacketIn() error = %v, wantErr %v", err, tt.wantErr)
540 return
541 }
542 if got != tt.want {
543 t.Errorf("OpenOltFlowMgr.GetLogicalPortFromPacketIn() = %v, want %v", got, tt.want)
544 }
545 })
546 }
547}
548
549func TestOpenOltFlowMgr_GetPacketOutGemPortID(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530550 // flwMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530551
552 type args struct {
553 intfID uint32
554 onuID uint32
555 portNum uint32
556 }
557 tests := []struct {
558 name string
559 args args
560 want uint32
561 wantErr bool
562 }{
563 // TODO: Add test cases.
564 {"GetPacketOutGemPortID", args{intfID: 1, onuID: 1, portNum: 1}, 1, false},
565 {"GetPacketOutGemPortID", args{intfID: 2, onuID: 2, portNum: 2}, 2, false},
566 {"GetPacketOutGemPortID", args{intfID: 1, onuID: 2, portNum: 2}, 0, true},
567 }
568 for _, tt := range tests {
569 t.Run(tt.name, func(t *testing.T) {
570
kdarapub26b4502019-10-05 03:02:33 +0530571 got, err := flowMgr.GetPacketOutGemPortID(tt.args.intfID, tt.args.onuID, tt.args.portNum)
kdarapu3248f9a2019-10-03 13:54:52 +0530572 if (err != nil) != tt.wantErr {
573 t.Errorf("OpenOltFlowMgr.GetPacketOutGemPortID() error = %v, wantErr %v", err, tt.wantErr)
574 return
575 }
576 if got != tt.want {
577 t.Errorf("OpenOltFlowMgr.GetPacketOutGemPortID() = %v, want %v", got, tt.want)
578 }
579
580 })
581 }
582}
583
584func TestOpenOltFlowMgr_DeleteTechProfileInstance(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530585 // flwMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530586 type args struct {
587 intfID uint32
588 onuID uint32
589 uniID uint32
590 sn string
Gamze Abakafee36392019-10-03 11:17:24 +0000591 tpID uint32
kdarapu3248f9a2019-10-03 13:54:52 +0530592 }
593 tests := []struct {
594 name string
595 args args
596 wantErr bool
597 }{
598 // TODO: Add test cases.
Gamze Abakafee36392019-10-03 11:17:24 +0000599 {"DeleteTechProfileInstance", args{intfID: 0, onuID: 1, uniID: 1, sn: "", tpID: 64}, false},
kdarapu3248f9a2019-10-03 13:54:52 +0530600 }
601 for _, tt := range tests {
602 t.Run(tt.name, func(t *testing.T) {
Gamze Abakafee36392019-10-03 11:17:24 +0000603 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 +0530604 t.Errorf("OpenOltFlowMgr.DeleteTechProfileInstance() error = %v, wantErr %v", err, tt.wantErr)
605 }
606 })
607 }
608}
kdarapub26b4502019-10-05 03:02:33 +0530609
610func TestOpenOltFlowMgr_checkAndAddFlow(t *testing.T) {
611 // flowMgr := newMockFlowmgr()
612 kw := make(map[string]uint64)
613 kw["table_id"] = 1
614 kw["meter_id"] = 1
615 kw["write_metadata"] = 0x4000000000 // Tech-Profile-ID 64
616
617 // Upstream flow
618 fa := &fu.FlowArgs{
619 MatchFields: []*ofp.OfpOxmOfbField{
620 fu.InPort(536870912),
621 fu.Metadata_ofp(1),
622 fu.IpProto(17), // dhcp
Gamze Abakafee36392019-10-03 11:17:24 +0000623 fu.VlanPcp(0),
kdarapub26b4502019-10-05 03:02:33 +0530624 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
625 },
626 Actions: []*ofp.OfpAction{
627 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
628 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
629 fu.Output(65536),
630 fu.PushVlan(0x8100),
631 },
632 KV: kw,
633 }
634
635 // EAPOL
636 fa2 := &fu.FlowArgs{
637 MatchFields: []*ofp.OfpOxmOfbField{
638 fu.InPort(536870912),
639 fu.Metadata_ofp(1),
640 fu.EthType(0x888E),
641 fu.VlanPcp(1),
642 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257),
643 },
644 Actions: []*ofp.OfpAction{
645 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
646 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
647 fu.Output(65536),
648 fu.PushVlan(0x8100),
649 },
650 KV: kw,
651 }
652
653 // HSIA
654 fa3 := &fu.FlowArgs{
655 MatchFields: []*ofp.OfpOxmOfbField{
656 fu.InPort(536870912),
657 fu.Metadata_ofp(1),
658 //fu.EthType(0x8100),
659 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
660 },
661 Actions: []*ofp.OfpAction{
662 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
663 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0)),
664 fu.Output(65536),
665 fu.PushVlan(0x8100),
666 },
667 KV: kw,
668 }
669
670 fa4 := &fu.FlowArgs{
671 MatchFields: []*ofp.OfpOxmOfbField{
672 fu.InPort(65535),
673 fu.Metadata_ofp(1),
674 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
675 fu.VlanPcp(1),
676 },
677 Actions: []*ofp.OfpAction{
678 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
679 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0)),
680 fu.Output(536870912),
681 fu.PopVlan(),
682 },
683 KV: kw,
684 }
685
686 classifierInfo := make(map[string]interface{})
687 actionInfo := make(map[string]interface{})
688 classifierInfo2 := make(map[string]interface{})
689 actionInfo2 := make(map[string]interface{})
690 classifierInfo3 := make(map[string]interface{})
691 actionInfo3 := make(map[string]interface{})
692 classifierInfo4 := make(map[string]interface{})
693 actionInfo4 := make(map[string]interface{})
694 flowState := fu.MkFlowStat(fa)
695 flowState2 := fu.MkFlowStat(fa2)
696 flowState3 := fu.MkFlowStat(fa3)
697 flowState4 := fu.MkFlowStat(fa4)
698 formulateClassifierInfoFromFlow(classifierInfo, flowState)
699 formulateClassifierInfoFromFlow(classifierInfo2, flowState2)
700 formulateClassifierInfoFromFlow(classifierInfo3, flowState3)
701 formulateClassifierInfoFromFlow(classifierInfo4, flowState4)
702
703 err := formulateActionInfoFromFlow(actionInfo, classifierInfo, flowState)
704 if err != nil {
705 // Error logging is already done in the called function
706 // So just return in case of error
707 return
708 }
709
710 err = formulateActionInfoFromFlow(actionInfo2, classifierInfo2, flowState2)
711 if err != nil {
712 // Error logging is already done in the called function
713 // So just return in case of error
714 return
715 }
716
717 err = formulateActionInfoFromFlow(actionInfo3, classifierInfo3, flowState3)
718 if err != nil {
719 // Error logging is already done in the called function
720 // So just return in case of error
721 return
722 }
723
724 err = formulateActionInfoFromFlow(actionInfo4, classifierInfo4, flowState4)
725 if err != nil {
726 // Error logging is already done in the called function
727 // So just return in case of error
728 return
729 }
730
731 //ofpMeterConfig := &ofp.OfpMeterConfig{Flags: 1, MeterId: 1}
732 //flowMetadata := &voltha.FlowMetadata{
733 // Meters: []*ofp.OfpMeterConfig{ofpMeterConfig},
734 //}
735
736 TpInst := &tp.TechProfile{
737 Name: "Test-Tech-Profile",
738 SubscriberIdentifier: "257",
739 ProfileType: "Mock",
740 Version: 1,
741 NumGemPorts: 4,
kdarapub26b4502019-10-05 03:02:33 +0530742 InstanceCtrl: tp.InstanceControl{
743 Onu: "1",
744 Uni: "16",
745 },
746 }
747
748 type fields struct {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530749 techprofile []tp.TechProfileIf
750 deviceHandler *DeviceHandler
751 resourceMgr *rsrcMgr.OpenOltResourceMgr
kdarapub26b4502019-10-05 03:02:33 +0530752 }
753 type args struct {
754 args map[string]uint32
755 classifierInfo map[string]interface{}
756 actionInfo map[string]interface{}
757 flow *ofp.OfpFlowStats
758 gemPort uint32
759 intfID uint32
760 onuID uint32
761 uniID uint32
762 portNo uint32
763 TpInst *tp.TechProfile
764 allocID []uint32
765 gemPorts []uint32
766 TpID uint32
767 uni string
768 }
769 tests := []struct {
770 name string
771 fields fields
772 args args
773 }{
774 {
775 name: "checkAndAddFlow-1",
776 args: args{
777 args: nil,
778 classifierInfo: classifierInfo,
779 actionInfo: actionInfo,
780 flow: flowState,
781 gemPort: 1,
782 intfID: 1,
783 onuID: 1,
784 uniID: 16,
785 portNo: 1,
786 TpInst: TpInst,
787 allocID: []uint32{0x8001, 0x8002, 0x8003, 0x8004},
788 gemPorts: []uint32{1, 2, 3, 4},
789 TpID: 64,
790 uni: "16",
791 },
792 },
793 {
794 name: "checkAndAddFlow-2",
795 args: args{
796 args: nil,
797 classifierInfo: classifierInfo2,
798 actionInfo: actionInfo2,
799 flow: flowState2,
800 gemPort: 1,
801 intfID: 1,
802 onuID: 1,
803 uniID: 16,
804 portNo: 1,
805 TpInst: TpInst,
806 allocID: []uint32{0x8001, 0x8002, 0x8003, 0x8004},
807 gemPorts: []uint32{1, 2, 3, 4},
808 TpID: 64,
809 uni: "16",
810 },
811 },
812 {
813 name: "checkAndAddFlow-3",
814 args: args{
815 args: nil,
816 classifierInfo: classifierInfo3,
817 actionInfo: actionInfo3,
818 flow: flowState3,
819 gemPort: 1,
820 intfID: 1,
821 onuID: 1,
822 uniID: 16,
823 portNo: 1,
824 TpInst: TpInst,
825 allocID: []uint32{0x8001, 0x8002, 0x8003, 0x8004},
826 gemPorts: []uint32{1, 2, 3, 4},
827 TpID: 64,
828 uni: "16",
829 },
830 },
831 {
832 name: "checkAndAddFlow-4",
833 args: args{
834 args: nil,
835 classifierInfo: classifierInfo4,
836 actionInfo: actionInfo4,
837 flow: flowState4,
838 gemPort: 1,
839 intfID: 1,
840 onuID: 1,
841 uniID: 16,
842 portNo: 1,
843 TpInst: TpInst,
844 allocID: []uint32{0x8001, 0x8002, 0x8003, 0x8004},
845 gemPorts: []uint32{1, 2, 3, 4},
846 TpID: 64,
847 uni: "16",
848 },
849 },
850 }
851 for _, tt := range tests {
852 t.Run(tt.name, func(t *testing.T) {
Gamze Abakafee36392019-10-03 11:17:24 +0000853 flowMgr.checkAndAddFlow(tt.args.args, tt.args.classifierInfo, tt.args.actionInfo, tt.args.flow,
854 tt.args.TpInst, tt.args.gemPorts, tt.args.TpID, tt.args.uni)
kdarapub26b4502019-10-05 03:02:33 +0530855 })
856 }
857}