blob: 8d8bd4a8275f6f05dcc2362ff69a5d61e9a683fa [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
Scott Bakerdbd960e2020-02-28 08:57:51 -080017//Package core provides the utility for olt devices, flows and statistics
18package core
kdarapu3248f9a2019-10-03 13:54:52 +053019
20import (
npujarec5762e2020-01-01 14:08:48 +053021 "context"
Gamze Abakafee36392019-10-03 11:17:24 +000022 "fmt"
Matteo Scandolo60913ed2020-06-23 19:31:14 -070023 "reflect"
24 "strconv"
25 "sync"
kdarapu3248f9a2019-10-03 13:54:52 +053026 "testing"
npujarec5762e2020-01-01 14:08:48 +053027 "time"
kdarapu3248f9a2019-10-03 13:54:52 +053028
Esin Karamanccb714b2019-11-29 15:02:06 +000029 "github.com/opencord/voltha-protos/v3/go/voltha"
kdarapub26b4502019-10-05 03:02:33 +053030
Esin Karamanccb714b2019-11-29 15:02:06 +000031 "github.com/opencord/voltha-lib-go/v3/pkg/db"
32 fu "github.com/opencord/voltha-lib-go/v3/pkg/flows"
33 "github.com/opencord/voltha-lib-go/v3/pkg/log"
34 tp "github.com/opencord/voltha-lib-go/v3/pkg/techprofile"
Scott Bakerdbd960e2020-02-28 08:57:51 -080035 "github.com/opencord/voltha-openolt-adapter/internal/pkg/resourcemanager"
36 rsrcMgr "github.com/opencord/voltha-openolt-adapter/internal/pkg/resourcemanager"
37 "github.com/opencord/voltha-openolt-adapter/pkg/mocks"
Esin Karamanccb714b2019-11-29 15:02:06 +000038 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
39 "github.com/opencord/voltha-protos/v3/go/openolt"
40 openoltpb2 "github.com/opencord/voltha-protos/v3/go/openolt"
41 tp_pb "github.com/opencord/voltha-protos/v3/go/tech_profile"
kdarapu3248f9a2019-10-03 13:54:52 +053042)
43
kdarapub26b4502019-10-05 03:02:33 +053044var flowMgr *OpenOltFlowMgr
45
kdarapu3248f9a2019-10-03 13:54:52 +053046func init() {
47 log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
kdarapub26b4502019-10-05 03:02:33 +053048 flowMgr = newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +053049}
50func newMockResourceMgr() *resourcemanager.OpenOltResourceMgr {
51 ranges := []*openolt.DeviceInfo_DeviceResourceRanges{
Matteo Scandolo60913ed2020-06-23 19:31:14 -070052 {
53 IntfIds: []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
54 Technology: "Default",
55 },
56 }
kdarapu3248f9a2019-10-03 13:54:52 +053057
58 deviceinfo := &openolt.DeviceInfo{Vendor: "openolt", Model: "openolt", HardwareVersion: "1.0", FirmwareVersion: "1.0",
Matteo Scandolo60913ed2020-06-23 19:31:14 -070059 DeviceId: "olt", DeviceSerialNumber: "openolt", PonPorts: 16, Technology: "Default",
kdarapu3248f9a2019-10-03 13:54:52 +053060 OnuIdStart: 1, OnuIdEnd: 1, AllocIdStart: 1, AllocIdEnd: 1,
61 GemportIdStart: 1, GemportIdEnd: 1, FlowIdStart: 1, FlowIdEnd: 1,
62 Ranges: ranges,
63 }
npujarec5762e2020-01-01 14:08:48 +053064 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
65 defer cancel()
66 rsrMgr := resourcemanager.NewResourceMgr(ctx, "olt", "127.0.0.1:2379", "etcd", "olt", deviceinfo)
kdarapub26b4502019-10-05 03:02:33 +053067 for key := range rsrMgr.ResourceMgrs {
sbarbaria8910ba2019-11-05 10:12:23 -050068 rsrMgr.ResourceMgrs[key].KVStore = &db.Backend{}
kdarapub26b4502019-10-05 03:02:33 +053069 rsrMgr.ResourceMgrs[key].KVStore.Client = &mocks.MockKVClient{}
70 rsrMgr.ResourceMgrs[key].TechProfileMgr = mocks.MockTechProfile{TpID: key}
71 }
kdarapu3248f9a2019-10-03 13:54:52 +053072 return rsrMgr
73}
74
75func newMockFlowmgr() *OpenOltFlowMgr {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053076 rMgr := newMockResourceMgr()
kdarapu3248f9a2019-10-03 13:54:52 +053077 dh := newMockDeviceHandler()
78
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053079 rMgr.KVStore = &db.Backend{}
80 rMgr.KVStore.Client = &mocks.MockKVClient{}
kdarapu3248f9a2019-10-03 13:54:52 +053081
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053082 dh.resourceMgr = rMgr
npujarec5762e2020-01-01 14:08:48 +053083 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
84 defer cancel()
85 flwMgr := NewFlowManager(ctx, dh, rMgr)
kdarapu3248f9a2019-10-03 13:54:52 +053086
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053087 onuGemInfo1 := make([]rsrcMgr.OnuGemInfo, 2)
88 onuGemInfo2 := make([]rsrcMgr.OnuGemInfo, 2)
89 onuGemInfo1[0] = rsrcMgr.OnuGemInfo{OnuID: 1, SerialNumber: "1", IntfID: 1, GemPorts: []uint32{1}}
90 onuGemInfo2[1] = rsrcMgr.OnuGemInfo{OnuID: 2, SerialNumber: "2", IntfID: 2, GemPorts: []uint32{2}}
91 flwMgr.onuGemInfo[1] = onuGemInfo1
92 flwMgr.onuGemInfo[2] = onuGemInfo2
kdarapu3248f9a2019-10-03 13:54:52 +053093
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053094 packetInGemPort := make(map[rsrcMgr.PacketInInfoKey]uint32)
95 packetInGemPort[rsrcMgr.PacketInInfoKey{IntfID: 1, OnuID: 1, LogicalPort: 1}] = 1
96 packetInGemPort[rsrcMgr.PacketInInfoKey{IntfID: 2, OnuID: 2, LogicalPort: 2}] = 2
kdarapu3248f9a2019-10-03 13:54:52 +053097
98 flwMgr.packetInGemPort = packetInGemPort
Amit Ghoshd4cbe482019-11-21 12:07:14 +000099 tps := make(map[uint32]tp.TechProfileIf)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530100 for key := range rMgr.ResourceMgrs {
kdarapub26b4502019-10-05 03:02:33 +0530101 tps[key] = mocks.MockTechProfile{TpID: key}
kdarapu3248f9a2019-10-03 13:54:52 +0530102 }
103 flwMgr.techprofile = tps
Esin Karamanccb714b2019-11-29 15:02:06 +0000104
105 interface2mcastQeueuMap := make(map[uint32]*queueInfoBrief)
106 interface2mcastQeueuMap[0] = &queueInfoBrief{
107 gemPortID: 4000,
108 servicePriority: 3,
109 }
110 flwMgr.interfaceToMcastQueueMap = interface2mcastQeueuMap
kdarapu3248f9a2019-10-03 13:54:52 +0530111 return flwMgr
112}
kdarapub26b4502019-10-05 03:02:33 +0530113
kdarapu3248f9a2019-10-03 13:54:52 +0530114func TestOpenOltFlowMgr_CreateSchedulerQueues(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530115 // flowMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530116
117 tprofile := &tp.TechProfile{Name: "tp1", SubscriberIdentifier: "subscriber1",
Girish Gowdra54934262019-11-13 14:19:55 +0530118 ProfileType: "pt1", NumGemPorts: 1, Version: 1,
kdarapu3248f9a2019-10-03 13:54:52 +0530119 InstanceCtrl: tp.InstanceControl{Onu: "1", Uni: "1", MaxGemPayloadSize: "1"},
120 }
121 tprofile.UsScheduler.Direction = "UPSTREAM"
122 tprofile.UsScheduler.AdditionalBw = "AdditionalBW_None"
123 tprofile.UsScheduler.QSchedPolicy = "WRR"
124
125 tprofile2 := tprofile
126 tprofile2.DsScheduler.Direction = "DOWNSTREAM"
127 tprofile2.DsScheduler.AdditionalBw = "AdditionalBW_None"
128 tprofile2.DsScheduler.QSchedPolicy = "WRR"
129 bands := make([]*ofp.OfpMeterBandHeader, 2)
130 bands[0] = &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 1000, BurstSize: 5000, Data: &ofp.OfpMeterBandHeader_Drop{}}
131 bands[1] = &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 2000, BurstSize: 5000, Data: &ofp.OfpMeterBandHeader_Drop{}}
132 ofpMeterConfig := &ofp.OfpMeterConfig{Flags: 1, MeterId: 1, Bands: bands}
133 flowmetadata := &voltha.FlowMetadata{
134 Meters: []*ofp.OfpMeterConfig{ofpMeterConfig},
135 }
136 type args struct {
137 Dir tp_pb.Direction
138 IntfID uint32
139 OnuID uint32
140 UniID uint32
141 UniPort uint32
142 TpInst *tp.TechProfile
143 MeterID uint32
144 flowMetadata *voltha.FlowMetadata
145 }
146 tests := []struct {
Gamze Abakafee36392019-10-03 11:17:24 +0000147 name string
148 schedQueue schedQueue
149 wantErr bool
kdarapu3248f9a2019-10-03 13:54:52 +0530150 }{
151 // TODO: Add test cases.
Gamze Abakafee36392019-10-03 11:17:24 +0000152 {"CreateSchedulerQueues-1", schedQueue{tp_pb.Direction_UPSTREAM, 1, 1, 1, 64, 1, tprofile, 1, flowmetadata}, false},
153 {"CreateSchedulerQueues-2", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 1, flowmetadata}, false},
154 {"CreateSchedulerQueues-3", schedQueue{tp_pb.Direction_UPSTREAM, 1, 1, 1, 64, 1, tprofile, 2, flowmetadata}, true},
155 {"CreateSchedulerQueues-4", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 2, flowmetadata}, true},
156 {"CreateSchedulerQueues-5", schedQueue{tp_pb.Direction_UPSTREAM, 2, 2, 2, 64, 2, tprofile, 2, flowmetadata}, true},
157 {"CreateSchedulerQueues-6", schedQueue{tp_pb.Direction_DOWNSTREAM, 2, 2, 2, 65, 2, tprofile2, 2, flowmetadata}, true},
158 {"CreateSchedulerQueues-13", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 1, flowmetadata}, false},
kdarapu3248f9a2019-10-03 13:54:52 +0530159 //Negative testcases
Gamze Abakafee36392019-10-03 11:17:24 +0000160 {"CreateSchedulerQueues-7", schedQueue{tp_pb.Direction_UPSTREAM, 1, 1, 1, 64, 1, tprofile, 1, &voltha.FlowMetadata{}}, true},
161 {"CreateSchedulerQueues-8", schedQueue{tp_pb.Direction_UPSTREAM, 1, 1, 1, 64, 1, tprofile, 0, &voltha.FlowMetadata{}}, true},
162 {"CreateSchedulerQueues-9", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 1, &voltha.FlowMetadata{}}, true},
163 {"CreateSchedulerQueues-10", schedQueue{tp_pb.Direction_UPSTREAM, 1, 1, 1, 64, 1, tprofile, 2, &voltha.FlowMetadata{}}, true},
164 {"CreateSchedulerQueues-11", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 2, &voltha.FlowMetadata{}}, true},
165 {"CreateSchedulerQueues-12", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 2, nil}, true},
kdarapu3248f9a2019-10-03 13:54:52 +0530166 }
npujarec5762e2020-01-01 14:08:48 +0530167 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
168 defer cancel()
kdarapu3248f9a2019-10-03 13:54:52 +0530169 for _, tt := range tests {
170 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530171 if err := flowMgr.CreateSchedulerQueues(ctx, tt.schedQueue); (err != nil) != tt.wantErr {
kdarapu3248f9a2019-10-03 13:54:52 +0530172 t.Errorf("OpenOltFlowMgr.CreateSchedulerQueues() error = %v, wantErr %v", err, tt.wantErr)
173 }
174 })
175 }
176}
177
178func TestOpenOltFlowMgr_RemoveSchedulerQueues(t *testing.T) {
179
kdarapub26b4502019-10-05 03:02:33 +0530180 // flowMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530181 tprofile := &tp.TechProfile{Name: "tp1", SubscriberIdentifier: "subscriber1",
Girish Gowdra54934262019-11-13 14:19:55 +0530182 ProfileType: "pt1", NumGemPorts: 1, Version: 1,
kdarapu3248f9a2019-10-03 13:54:52 +0530183 InstanceCtrl: tp.InstanceControl{Onu: "1", Uni: "1", MaxGemPayloadSize: "1"},
184 }
185 tprofile.UsScheduler.Direction = "UPSTREAM"
186 tprofile.UsScheduler.AdditionalBw = "AdditionalBW_None"
187 tprofile.UsScheduler.QSchedPolicy = "WRR"
188
189 tprofile2 := tprofile
190 tprofile2.DsScheduler.Direction = "DOWNSTREAM"
191 tprofile2.DsScheduler.AdditionalBw = "AdditionalBW_None"
192 tprofile2.DsScheduler.QSchedPolicy = "WRR"
193 //defTprofile := &tp.DefaultTechProfile{}
194 type args struct {
195 Dir tp_pb.Direction
196 IntfID uint32
197 OnuID uint32
198 UniID uint32
199 UniPort uint32
200 TpInst *tp.TechProfile
201 }
202 tests := []struct {
Gamze Abakafee36392019-10-03 11:17:24 +0000203 name string
204 schedQueue schedQueue
205 wantErr bool
kdarapu3248f9a2019-10-03 13:54:52 +0530206 }{
207 // TODO: Add test cases.
Gamze Abakafee36392019-10-03 11:17:24 +0000208 {"RemoveSchedulerQueues", schedQueue{tp_pb.Direction_UPSTREAM, 1, 1, 1, 64, 1, tprofile, 0, nil}, false},
209 {"RemoveSchedulerQueues", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 0, nil}, false},
kdarapu3248f9a2019-10-03 13:54:52 +0530210 // negative test cases
Gamze Abakafee36392019-10-03 11:17:24 +0000211 {"RemoveSchedulerQueues", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 0, nil}, false},
212 {"RemoveSchedulerQueues", schedQueue{tp_pb.Direction_DOWNSTREAM, 1, 1, 1, 65, 1, tprofile2, 0, nil}, false},
kdarapu3248f9a2019-10-03 13:54:52 +0530213 }
npujarec5762e2020-01-01 14:08:48 +0530214 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
215 defer cancel()
kdarapu3248f9a2019-10-03 13:54:52 +0530216 for _, tt := range tests {
217 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530218 if err := flowMgr.RemoveSchedulerQueues(ctx, tt.schedQueue); (err != nil) != tt.wantErr {
kdarapu3248f9a2019-10-03 13:54:52 +0530219 t.Errorf("OpenOltFlowMgr.RemoveSchedulerQueues() error = %v, wantErr %v", err, tt.wantErr)
220 }
221 })
222 }
kdarapub26b4502019-10-05 03:02:33 +0530223
kdarapu3248f9a2019-10-03 13:54:52 +0530224}
225
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700226func TestOpenOltFlowMgr_createTcontGemports(t *testing.T) {
227 // flowMgr := newMockFlowmgr()
228 bands := make([]*ofp.OfpMeterBandHeader, 2)
229 bands[0] = &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 1000, BurstSize: 5000, Data: &ofp.OfpMeterBandHeader_Drop{}}
230 bands[1] = &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 2000, BurstSize: 5000, Data: &ofp.OfpMeterBandHeader_Drop{}}
231 ofpMeterConfig := &ofp.OfpMeterConfig{Flags: 1, MeterId: 1, Bands: bands}
232 flowmetadata := &voltha.FlowMetadata{
233 Meters: []*ofp.OfpMeterConfig{ofpMeterConfig},
234 }
235 type args struct {
236 intfID uint32
237 onuID uint32
238 uniID uint32
239 uni string
240 uniPort uint32
241 TpID uint32
242 UsMeterID uint32
243 DsMeterID uint32
244 flowMetadata *voltha.FlowMetadata
245 }
246 tests := []struct {
247 name string
248 args args
249 }{
250 {"createTcontGemports-1", args{intfID: 0, onuID: 1, uniID: 1, uni: "16", uniPort: 1, TpID: 64, UsMeterID: 1, DsMeterID: 1, flowMetadata: flowmetadata}},
251 {"createTcontGemports-1", args{intfID: 0, onuID: 1, uniID: 1, uni: "16", uniPort: 1, TpID: 65, UsMeterID: 1, DsMeterID: 1, flowMetadata: flowmetadata}},
252 }
253 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
254 defer cancel()
255 for _, tt := range tests {
256 t.Run(tt.name, func(t *testing.T) {
257 _, _, tpInst := flowMgr.createTcontGemports(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID, tt.args.uni, tt.args.uniPort, tt.args.TpID, tt.args.UsMeterID, tt.args.DsMeterID, tt.args.flowMetadata)
258 switch tpInst := tpInst.(type) {
259 case *tp.TechProfile:
260 if tt.args.TpID != 64 {
261 t.Errorf("OpenOltFlowMgr.createTcontGemports() error = different tech, tech %v", tpInst)
262 }
263 case *tp.EponProfile:
264 if tt.args.TpID != 65 {
265 t.Errorf("OpenOltFlowMgr.createTcontGemports() error = different tech, tech %v", tpInst)
266 }
267 default:
268 t.Errorf("OpenOltFlowMgr.createTcontGemports() error = different tech, tech %v", tpInst)
269 }
270 })
271 }
272}
273
kdarapu3248f9a2019-10-03 13:54:52 +0530274func TestOpenOltFlowMgr_RemoveFlow(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530275 // flowMgr := newMockFlowmgr()
Girish Kumar2ad402b2020-03-20 19:45:12 +0000276 logger.Debug("Info Warning Error: Starting RemoveFlow() test")
kdarapu3248f9a2019-10-03 13:54:52 +0530277 fa := &fu.FlowArgs{
278 MatchFields: []*ofp.OfpOxmOfbField{
279 fu.InPort(2),
280 fu.Metadata_ofp(2),
281 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
282 },
283 Actions: []*ofp.OfpAction{
284 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
285 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
286 fu.Output(1),
287 },
288 }
divyadesaid26f6b12020-03-19 06:30:28 +0000289 ofpstats, _ := fu.MkFlowStat(fa)
kdarapub26b4502019-10-05 03:02:33 +0530290 ofpstats.Cookie = ofpstats.Id
kdarapub26b4502019-10-05 03:02:33 +0530291 lldpFa := &fu.FlowArgs{
292 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
293 MatchFields: []*ofp.OfpOxmOfbField{
294 fu.InPort(1),
295 fu.EthType(0x88CC),
296 fu.TunnelId(536870912),
297 },
298 Actions: []*ofp.OfpAction{
299 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
300 },
301 }
divyadesaid26f6b12020-03-19 06:30:28 +0000302 lldpofpstats, _ := fu.MkFlowStat(lldpFa)
kdarapub26b4502019-10-05 03:02:33 +0530303 //lldpofpstats.Cookie = lldpofpstats.Id
304
305 dhcpFa := &fu.FlowArgs{
306 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
307 MatchFields: []*ofp.OfpOxmOfbField{
308 fu.InPort(1),
309 fu.UdpSrc(67),
310 //fu.TunnelId(536870912),
311 fu.IpProto(17),
312 },
313 Actions: []*ofp.OfpAction{
314 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
315 },
316 }
divyadesaid26f6b12020-03-19 06:30:28 +0000317 dhcpofpstats, _ := fu.MkFlowStat(dhcpFa)
kdarapub26b4502019-10-05 03:02:33 +0530318 //dhcpofpstats.Cookie = dhcpofpstats.Id
Esin Karamanccb714b2019-11-29 15:02:06 +0000319
320 //multicast flow
321 multicastFa := &fu.FlowArgs{
322 MatchFields: []*ofp.OfpOxmOfbField{
323 fu.InPort(65536),
324 fu.VlanVid(660), //vlan
325 fu.Metadata_ofp(uint64(66)), //inner vlan
326 fu.EthType(0x800), //ipv4
327 fu.Ipv4Dst(3809869825), //227.22.0.1
328 },
329 Actions: []*ofp.OfpAction{
330 fu.Group(1),
331 },
332 }
divyadesaid26f6b12020-03-19 06:30:28 +0000333 multicastOfpStats, _ := fu.MkFlowStat(multicastFa)
Esin Karamanccb714b2019-11-29 15:02:06 +0000334 multicastOfpStats.Id = 1
335
kdarapu3248f9a2019-10-03 13:54:52 +0530336 type args struct {
337 flow *ofp.OfpFlowStats
338 }
339 tests := []struct {
340 name string
341 args args
342 }{
343 // TODO: Add test cases.
344 {"RemoveFlow", args{flow: ofpstats}},
kdarapub26b4502019-10-05 03:02:33 +0530345 {"RemoveFlow", args{flow: lldpofpstats}},
346 {"RemoveFlow", args{flow: dhcpofpstats}},
Esin Karamanccb714b2019-11-29 15:02:06 +0000347 {"RemoveFlow", args{flow: multicastOfpStats}},
kdarapu3248f9a2019-10-03 13:54:52 +0530348 }
npujarec5762e2020-01-01 14:08:48 +0530349 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
350 defer cancel()
kdarapu3248f9a2019-10-03 13:54:52 +0530351 for _, tt := range tests {
352 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530353 flowMgr.RemoveFlow(ctx, tt.args.flow)
kdarapu3248f9a2019-10-03 13:54:52 +0530354 })
355 }
kdarapub26b4502019-10-05 03:02:33 +0530356 // t.Error("=====")
kdarapu3248f9a2019-10-03 13:54:52 +0530357}
358
359func TestOpenOltFlowMgr_AddFlow(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530360 // flowMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530361 kw := make(map[string]uint64)
362 kw["table_id"] = 1
363 kw["meter_id"] = 1
kdarapub26b4502019-10-05 03:02:33 +0530364 kw["write_metadata"] = 0x4000000000 // Tech-Profile-ID 64
365
366 // Upstream flow
kdarapu3248f9a2019-10-03 13:54:52 +0530367 fa := &fu.FlowArgs{
368 MatchFields: []*ofp.OfpOxmOfbField{
kdarapub26b4502019-10-05 03:02:33 +0530369 fu.InPort(536870912),
370 fu.Metadata_ofp(1),
371 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
372 },
373 Actions: []*ofp.OfpAction{
374 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
375 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
376 fu.Output(65536),
377 fu.PushVlan(0x8100),
378 },
379 KV: kw,
380 }
381
382 // Downstream flow
383 fa3 := &fu.FlowArgs{
384 MatchFields: []*ofp.OfpOxmOfbField{
385 fu.InPort(65536),
386 fu.Metadata_ofp(1),
387 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257),
388 },
389 Actions: []*ofp.OfpAction{
390 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
391 //fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
392 fu.PopVlan(),
393 fu.Output(536870912),
394 },
395 KV: kw,
396 }
397
398 fa2 := &fu.FlowArgs{
399 MatchFields: []*ofp.OfpOxmOfbField{
kdarapu3248f9a2019-10-03 13:54:52 +0530400 fu.InPort(1000),
401 fu.Metadata_ofp(1),
402 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
403 },
404 Actions: []*ofp.OfpAction{
405 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
406 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
407 fu.Output(65533),
408 },
409 KV: kw,
410 }
411
kdarapub26b4502019-10-05 03:02:33 +0530412 // TODO Add LLDP flow
413 // TODO Add DHCP flow
414
415 // Flows for negative scenarios
416 // Failure in formulateActionInfoFromFlow()
417 fa4 := &fu.FlowArgs{
418 MatchFields: []*ofp.OfpOxmOfbField{
419 fu.InPort(1000),
420 fu.Metadata_ofp(1),
421 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
422 },
423 Actions: []*ofp.OfpAction{
424 fu.Experimenter(257, []byte{1, 2, 3, 4}),
425 },
426 KV: kw,
427 }
428
429 // Invalid Output
430 fa5 := &fu.FlowArgs{
431 MatchFields: []*ofp.OfpOxmOfbField{
432 fu.InPort(1000),
433 fu.Metadata_ofp(1),
434 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
435 },
436 Actions: []*ofp.OfpAction{
437 fu.Output(0),
438 },
439 KV: kw,
440 }
441
442 // Tech-Profile-ID update (not supported)
443 kw6 := make(map[string]uint64)
444 kw6["table_id"] = 1
445 kw6["meter_id"] = 1
446 kw6["write_metadata"] = 0x4100000000 // TpID Other than the stored one
447 fa6 := &fu.FlowArgs{
448 MatchFields: []*ofp.OfpOxmOfbField{
449 fu.InPort(536870912),
450 fu.TunnelId(16),
451 fu.Metadata_ofp(1),
452 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
453 },
454 Actions: []*ofp.OfpAction{
455 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
456 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
457 fu.Output(65535),
458 },
459 KV: kw6,
460 }
461
462 lldpFa := &fu.FlowArgs{
463 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
464 MatchFields: []*ofp.OfpOxmOfbField{
465 fu.InPort(1),
466 fu.EthType(0x88CC),
467 fu.TunnelId(536870912),
468 },
469 Actions: []*ofp.OfpAction{
470 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
471 },
472 }
473
474 dhcpFa := &fu.FlowArgs{
475 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
476 MatchFields: []*ofp.OfpOxmOfbField{
477 fu.InPort(1),
478 fu.UdpSrc(67),
479 //fu.TunnelId(536870912),
480 fu.IpProto(17),
481 },
482 Actions: []*ofp.OfpAction{
483 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
484 },
485 }
486 igmpFa := &fu.FlowArgs{
487 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
488 MatchFields: []*ofp.OfpOxmOfbField{
489 fu.InPort(1),
490 fu.UdpSrc(67),
491 //fu.TunnelId(536870912),
492 fu.IpProto(2),
493 },
494 Actions: []*ofp.OfpAction{
495 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
496 },
497 }
498
499 fa9 := &fu.FlowArgs{
500 MatchFields: []*ofp.OfpOxmOfbField{
501 fu.InPort(536870912),
502 fu.TunnelId(16),
503 fu.Metadata_ofp(1),
504 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
505 fu.VlanPcp(1000),
506 fu.UdpDst(65535),
507 fu.UdpSrc(536870912),
508 fu.Ipv4Dst(65535),
509 fu.Ipv4Src(536870912),
510 },
511 Actions: []*ofp.OfpAction{
512 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
513 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
514 fu.Output(65535),
515 },
516 KV: kw6,
517 }
518
519 fa10 := &fu.FlowArgs{
520 MatchFields: []*ofp.OfpOxmOfbField{
521 fu.InPort(65533),
522 // fu.TunnelId(16),
523 fu.Metadata_ofp(1),
524 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
525 fu.VlanPcp(1000),
526 fu.UdpDst(65535),
527 fu.UdpSrc(536870912),
528 fu.Ipv4Dst(65535),
529 fu.Ipv4Src(536870912),
530 },
531 Actions: []*ofp.OfpAction{
532 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
533 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
534 fu.Output(65535),
535 },
536 KV: kw6,
537 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000538 //multicast flow
539 fa11 := &fu.FlowArgs{
540 MatchFields: []*ofp.OfpOxmOfbField{
541 fu.InPort(65536),
542 fu.VlanVid(660), //vlan
543 fu.Metadata_ofp(uint64(66)), //inner vlan
544 fu.EthType(0x800), //ipv4
545 fu.Ipv4Dst(3809869825), //227.22.0.1
546 },
547 Actions: []*ofp.OfpAction{
548 fu.Group(1),
549 },
550 KV: kw6,
551 }
divyadesaid26f6b12020-03-19 06:30:28 +0000552 ofpstats, _ := fu.MkFlowStat(fa)
553 ofpstats2, _ := fu.MkFlowStat(fa2)
554 ofpstats3, _ := fu.MkFlowStat(fa3)
555 ofpstats4, _ := fu.MkFlowStat(fa4)
556 ofpstats5, _ := fu.MkFlowStat(fa5)
557 ofpstats6, _ := fu.MkFlowStat(fa6)
558 ofpstats7, _ := fu.MkFlowStat(lldpFa)
559 ofpstats8, _ := fu.MkFlowStat(dhcpFa)
560 ofpstats9, _ := fu.MkFlowStat(fa9)
561 ofpstats10, _ := fu.MkFlowStat(fa10)
562 igmpstats, _ := fu.MkFlowStat(igmpFa)
563 ofpstats11, _ := fu.MkFlowStat(fa11)
kdarapub26b4502019-10-05 03:02:33 +0530564
Gamze Abakafee36392019-10-03 11:17:24 +0000565 fmt.Println(ofpstats6, ofpstats9, ofpstats10)
566
kdarapu3248f9a2019-10-03 13:54:52 +0530567 ofpMeterConfig := &ofp.OfpMeterConfig{Flags: 1, MeterId: 1}
kdarapu3248f9a2019-10-03 13:54:52 +0530568 flowMetadata := &voltha.FlowMetadata{
569 Meters: []*ofp.OfpMeterConfig{ofpMeterConfig},
570 }
571 type args struct {
572 flow *ofp.OfpFlowStats
573 flowMetadata *voltha.FlowMetadata
574 }
575 tests := []struct {
576 name string
577 args args
578 }{
579 // TODO: Add test cases.
580 {"AddFlow", args{flow: ofpstats, flowMetadata: flowMetadata}},
kdarapub26b4502019-10-05 03:02:33 +0530581 {"AddFlow", args{flow: ofpstats2, flowMetadata: flowMetadata}},
582 {"AddFlow", args{flow: ofpstats3, flowMetadata: flowMetadata}},
583 {"AddFlow", args{flow: ofpstats4, flowMetadata: flowMetadata}},
584 {"AddFlow", args{flow: ofpstats5, flowMetadata: flowMetadata}},
Gamze Abakafee36392019-10-03 11:17:24 +0000585 //{"AddFlow", args{flow: ofpstats6, flowMetadata: flowMetadata}},
kdarapub26b4502019-10-05 03:02:33 +0530586 {"AddFlow", args{flow: ofpstats7, flowMetadata: flowMetadata}},
587 {"AddFlow", args{flow: ofpstats8, flowMetadata: flowMetadata}},
Gamze Abakafee36392019-10-03 11:17:24 +0000588 //{"AddFlow", args{flow: ofpstats9, flowMetadata: flowMetadata}},
kdarapub26b4502019-10-05 03:02:33 +0530589 {"AddFlow", args{flow: igmpstats, flowMetadata: flowMetadata}},
Gamze Abakafee36392019-10-03 11:17:24 +0000590 //{"AddFlow", args{flow: ofpstats10, flowMetadata: flowMetadata}},
kdarapub26b4502019-10-05 03:02:33 +0530591 //ofpstats10
Esin Karamanccb714b2019-11-29 15:02:06 +0000592 {"AddFlow", args{flow: ofpstats11, flowMetadata: flowMetadata}},
kdarapu3248f9a2019-10-03 13:54:52 +0530593 }
npujarec5762e2020-01-01 14:08:48 +0530594 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
595 defer cancel()
kdarapu3248f9a2019-10-03 13:54:52 +0530596 for _, tt := range tests {
597 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530598 flowMgr.AddFlow(ctx, tt.args.flow, tt.args.flowMetadata)
kdarapu3248f9a2019-10-03 13:54:52 +0530599 })
600 }
601}
602
603func TestOpenOltFlowMgr_UpdateOnuInfo(t *testing.T) {
Matteo Scandolo60913ed2020-06-23 19:31:14 -0700604 flwMgr := newMockFlowmgr()
605
npujarec5762e2020-01-01 14:08:48 +0530606 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
607 defer cancel()
kdarapu3248f9a2019-10-03 13:54:52 +0530608
Matteo Scandolo60913ed2020-06-23 19:31:14 -0700609 wg := sync.WaitGroup{}
610
611 intfCount := 16
612 onuCount := 32
613
614 for i := 0; i < intfCount; i++ {
615 for j := 0; j < onuCount; j++ {
616 wg.Add(1)
617 go func(i uint32, j uint32) {
618 flwMgr.UpdateOnuInfo(ctx, i, i, fmt.Sprintf("onu-%d", i))
619 wg.Done()
620 }(uint32(i), uint32(j))
621 }
622
623 }
624
625 wg.Wait()
626}
627
628func TestOpenOltFlowMgr_addGemPortToOnuInfoMap(t *testing.T) {
629 flowMgr = newMockFlowmgr()
630 intfNum := 16
631 onuNum := 32
632
633 // clean the flowMgr
634 flowMgr.onuGemInfo = make(map[uint32][]rsrcMgr.OnuGemInfo, intfNum)
635
636 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
637 defer cancel()
638
639 // Create OnuInfo
640 for i := 0; i < intfNum; i++ {
641 for o := 0; o < onuNum; o++ {
642 flowMgr.UpdateOnuInfo(ctx, uint32(i), uint32(o), fmt.Sprintf("i%do%d", i, o))
643 }
644 }
645
646 // Add gemPorts to OnuInfo in parallel threads
647 wg := sync.WaitGroup{}
648
649 for o := 0; o < onuNum; o++ {
650 for i := 0; i < intfNum; i++ {
651 wg.Add(1)
652 go func(intfId uint32, onuId uint32) {
653 gemID, _ := strconv.Atoi(fmt.Sprintf("90%d%d", intfId, onuId))
654
655 flowMgr.addGemPortToOnuInfoMap(ctx, intfId, onuId, uint32(gemID))
656 wg.Done()
657 }(uint32(i), uint32(o))
658 }
659 }
660
661 wg.Wait()
662
663 // check that each entry of onuGemInfo has the correct number of ONUs
664 for i := 0; i < intfNum; i++ {
665 lenofOnu := len(flowMgr.onuGemInfo[uint32(i)])
666 if onuNum != lenofOnu {
667 t.Errorf("OnuGemInfo length is not as expected len = %d, want %d", lenofOnu, onuNum)
668 }
669
670 for o := 0; o < onuNum; o++ {
671 lenOfGemPorts := len(flowMgr.onuGemInfo[uint32(i)][o].GemPorts)
672 // check that each onuEntry has 1 gemPort
673 if lenOfGemPorts != 1 {
674 t.Errorf("Expected 1 GemPort per ONU, found %d", lenOfGemPorts)
675 }
676
677 // check that the value of the gemport is correct
678 gemID, _ := strconv.Atoi(fmt.Sprintf("90%d%d", i, o))
679 currentValue := flowMgr.onuGemInfo[uint32(i)][o].GemPorts[0]
680 if uint32(gemID) != currentValue {
681 t.Errorf("Expected GemPort value to be %d, found %d", gemID, currentValue)
682 }
683 }
kdarapu3248f9a2019-10-03 13:54:52 +0530684 }
685}
686
serkant.uluderya96af4932020-02-20 16:58:48 -0800687func TestOpenOltFlowMgr_deleteGemPortFromLocalCache(t *testing.T) {
Matteo Scandolo60913ed2020-06-23 19:31:14 -0700688 flwMgr := newMockFlowmgr()
serkant.uluderya96af4932020-02-20 16:58:48 -0800689 type args struct {
690 intfID uint32
691 onuID uint32
692 gemPortIDs []uint32
693 gemPortIDsToBeDeleted []uint32
Matteo Scandolo60913ed2020-06-23 19:31:14 -0700694 gemPortIDsRemaining []uint32
serkant.uluderya96af4932020-02-20 16:58:48 -0800695 serialNum string
696 finalLength int
697 }
698 tests := []struct {
699 name string
700 args args
701 }{
702 // Add/Delete single gem port
Matteo Scandolo60913ed2020-06-23 19:31:14 -0700703 {"DeleteGemPortFromLocalCache1", args{0, 1, []uint32{1}, []uint32{1}, []uint32{}, "onu1", 0}},
serkant.uluderya96af4932020-02-20 16:58:48 -0800704 // Delete all gemports
Matteo Scandolo60913ed2020-06-23 19:31:14 -0700705 {"DeleteGemPortFromLocalCache2", args{0, 1, []uint32{1, 2, 3, 4}, []uint32{1, 2, 3, 4}, []uint32{}, "onu1", 0}},
serkant.uluderya96af4932020-02-20 16:58:48 -0800706 // Try to delete when there is no gem port
Matteo Scandolo60913ed2020-06-23 19:31:14 -0700707 {"DeleteGemPortFromLocalCache3", args{0, 1, []uint32{}, []uint32{1, 2}, []uint32{}, "onu1", 0}},
serkant.uluderya96af4932020-02-20 16:58:48 -0800708 // Try to delete non-existent gem port
Matteo Scandolo60913ed2020-06-23 19:31:14 -0700709 {"DeleteGemPortFromLocalCache4", args{0, 1, []uint32{1}, []uint32{2}, []uint32{1}, "onu1", 1}},
serkant.uluderya96af4932020-02-20 16:58:48 -0800710 // Try to delete two of the gem ports
Matteo Scandolo60913ed2020-06-23 19:31:14 -0700711 {"DeleteGemPortFromLocalCache5", args{0, 1, []uint32{1, 2, 3, 4}, []uint32{2, 4}, []uint32{1, 3}, "onu1", 2}},
serkant.uluderya96af4932020-02-20 16:58:48 -0800712 }
713 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
714 defer cancel()
715 for _, tt := range tests {
716 t.Run(tt.name, func(t *testing.T) {
Matteo Scandolo60913ed2020-06-23 19:31:14 -0700717 flwMgr.UpdateOnuInfo(ctx, tt.args.intfID, tt.args.onuID, tt.args.serialNum)
serkant.uluderya96af4932020-02-20 16:58:48 -0800718 for _, gemPort := range tt.args.gemPortIDs {
Matteo Scandolo60913ed2020-06-23 19:31:14 -0700719 flwMgr.addGemPortToOnuInfoMap(ctx, tt.args.intfID, tt.args.onuID, gemPort)
serkant.uluderya96af4932020-02-20 16:58:48 -0800720 }
721 for _, gemPortDeleted := range tt.args.gemPortIDsToBeDeleted {
Matteo Scandolo60913ed2020-06-23 19:31:14 -0700722 flwMgr.deleteGemPortFromLocalCache(tt.args.intfID, tt.args.onuID, gemPortDeleted)
serkant.uluderya96af4932020-02-20 16:58:48 -0800723 }
Matteo Scandolo60913ed2020-06-23 19:31:14 -0700724 lenofGemPorts := len(flwMgr.onuGemInfo[tt.args.intfID][0].GemPorts)
serkant.uluderya96af4932020-02-20 16:58:48 -0800725 if lenofGemPorts != tt.args.finalLength {
726 t.Errorf("GemPorts length is not as expected len = %d, want %d", lenofGemPorts, tt.args.finalLength)
727 }
Matteo Scandolo60913ed2020-06-23 19:31:14 -0700728 gemPorts := flwMgr.onuGemInfo[tt.args.intfID][0].GemPorts
729 if !reflect.DeepEqual(tt.args.gemPortIDsRemaining, gemPorts) {
730 t.Errorf("GemPorts are not as expected = %v, want %v", gemPorts, tt.args.gemPortIDsRemaining)
731 }
serkant.uluderya96af4932020-02-20 16:58:48 -0800732
733 })
734 }
735}
736
kdarapu3248f9a2019-10-03 13:54:52 +0530737func TestOpenOltFlowMgr_GetLogicalPortFromPacketIn(t *testing.T) {
Matteo Scandolo60913ed2020-06-23 19:31:14 -0700738 flwMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530739 type args struct {
740 packetIn *openoltpb2.PacketIndication
741 }
742 tests := []struct {
743 name string
744 args args
745 want uint32
746 wantErr bool
747 }{
748 // TODO: Add test cases.
749 {"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 +0000750 {"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 +0530751 // Negative Test cases.
752 {"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 +0000753 {"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 +0530754 }
npujarec5762e2020-01-01 14:08:48 +0530755 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
756 defer cancel()
kdarapu3248f9a2019-10-03 13:54:52 +0530757 for _, tt := range tests {
758 t.Run(tt.name, func(t *testing.T) {
759
Matteo Scandolo60913ed2020-06-23 19:31:14 -0700760 got, err := flwMgr.GetLogicalPortFromPacketIn(ctx, tt.args.packetIn)
kdarapu3248f9a2019-10-03 13:54:52 +0530761 if (err != nil) != tt.wantErr {
762 t.Errorf("OpenOltFlowMgr.GetLogicalPortFromPacketIn() error = %v, wantErr %v", err, tt.wantErr)
763 return
764 }
765 if got != tt.want {
766 t.Errorf("OpenOltFlowMgr.GetLogicalPortFromPacketIn() = %v, want %v", got, tt.want)
767 }
768 })
769 }
770}
771
772func TestOpenOltFlowMgr_GetPacketOutGemPortID(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530773 // flwMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530774
775 type args struct {
776 intfID uint32
777 onuID uint32
778 portNum uint32
779 }
780 tests := []struct {
781 name string
782 args args
783 want uint32
784 wantErr bool
785 }{
786 // TODO: Add test cases.
787 {"GetPacketOutGemPortID", args{intfID: 1, onuID: 1, portNum: 1}, 1, false},
788 {"GetPacketOutGemPortID", args{intfID: 2, onuID: 2, portNum: 2}, 2, false},
789 {"GetPacketOutGemPortID", args{intfID: 1, onuID: 2, portNum: 2}, 0, true},
790 }
npujarec5762e2020-01-01 14:08:48 +0530791 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
792 defer cancel()
kdarapu3248f9a2019-10-03 13:54:52 +0530793 for _, tt := range tests {
794 t.Run(tt.name, func(t *testing.T) {
795
npujarec5762e2020-01-01 14:08:48 +0530796 got, err := flowMgr.GetPacketOutGemPortID(ctx, tt.args.intfID, tt.args.onuID, tt.args.portNum)
kdarapu3248f9a2019-10-03 13:54:52 +0530797 if (err != nil) != tt.wantErr {
798 t.Errorf("OpenOltFlowMgr.GetPacketOutGemPortID() error = %v, wantErr %v", err, tt.wantErr)
799 return
800 }
801 if got != tt.want {
802 t.Errorf("OpenOltFlowMgr.GetPacketOutGemPortID() = %v, want %v", got, tt.want)
803 }
804
805 })
806 }
807}
808
809func TestOpenOltFlowMgr_DeleteTechProfileInstance(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530810 // flwMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530811 type args struct {
812 intfID uint32
813 onuID uint32
814 uniID uint32
815 sn string
Gamze Abakafee36392019-10-03 11:17:24 +0000816 tpID uint32
kdarapu3248f9a2019-10-03 13:54:52 +0530817 }
818 tests := []struct {
819 name string
820 args args
821 wantErr bool
822 }{
823 // TODO: Add test cases.
Gamze Abakafee36392019-10-03 11:17:24 +0000824 {"DeleteTechProfileInstance", args{intfID: 0, onuID: 1, uniID: 1, sn: "", tpID: 64}, false},
kdarapu3248f9a2019-10-03 13:54:52 +0530825 }
npujarec5762e2020-01-01 14:08:48 +0530826 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
827 defer cancel()
kdarapu3248f9a2019-10-03 13:54:52 +0530828 for _, tt := range tests {
829 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530830 if err := flowMgr.DeleteTechProfileInstance(ctx, 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 +0530831 t.Errorf("OpenOltFlowMgr.DeleteTechProfileInstance() error = %v, wantErr %v", err, tt.wantErr)
832 }
833 })
834 }
835}
kdarapub26b4502019-10-05 03:02:33 +0530836
837func TestOpenOltFlowMgr_checkAndAddFlow(t *testing.T) {
838 // flowMgr := newMockFlowmgr()
839 kw := make(map[string]uint64)
840 kw["table_id"] = 1
841 kw["meter_id"] = 1
842 kw["write_metadata"] = 0x4000000000 // Tech-Profile-ID 64
843
844 // Upstream flow
845 fa := &fu.FlowArgs{
846 MatchFields: []*ofp.OfpOxmOfbField{
847 fu.InPort(536870912),
848 fu.Metadata_ofp(1),
849 fu.IpProto(17), // dhcp
Gamze Abakafee36392019-10-03 11:17:24 +0000850 fu.VlanPcp(0),
kdarapub26b4502019-10-05 03:02:33 +0530851 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
852 },
853 Actions: []*ofp.OfpAction{
854 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
855 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
856 fu.Output(65536),
857 fu.PushVlan(0x8100),
858 },
859 KV: kw,
860 }
861
862 // EAPOL
863 fa2 := &fu.FlowArgs{
864 MatchFields: []*ofp.OfpOxmOfbField{
865 fu.InPort(536870912),
866 fu.Metadata_ofp(1),
867 fu.EthType(0x888E),
868 fu.VlanPcp(1),
869 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257),
870 },
871 Actions: []*ofp.OfpAction{
872 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
873 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
874 fu.Output(65536),
875 fu.PushVlan(0x8100),
876 },
877 KV: kw,
878 }
879
880 // HSIA
881 fa3 := &fu.FlowArgs{
882 MatchFields: []*ofp.OfpOxmOfbField{
883 fu.InPort(536870912),
884 fu.Metadata_ofp(1),
885 //fu.EthType(0x8100),
886 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
887 },
888 Actions: []*ofp.OfpAction{
889 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
890 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0)),
891 fu.Output(65536),
892 fu.PushVlan(0x8100),
893 },
894 KV: kw,
895 }
896
897 fa4 := &fu.FlowArgs{
898 MatchFields: []*ofp.OfpOxmOfbField{
899 fu.InPort(65535),
900 fu.Metadata_ofp(1),
901 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
902 fu.VlanPcp(1),
903 },
904 Actions: []*ofp.OfpAction{
905 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
906 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0)),
907 fu.Output(536870912),
908 fu.PopVlan(),
909 },
910 KV: kw,
911 }
912
913 classifierInfo := make(map[string]interface{})
914 actionInfo := make(map[string]interface{})
915 classifierInfo2 := make(map[string]interface{})
916 actionInfo2 := make(map[string]interface{})
917 classifierInfo3 := make(map[string]interface{})
918 actionInfo3 := make(map[string]interface{})
919 classifierInfo4 := make(map[string]interface{})
920 actionInfo4 := make(map[string]interface{})
divyadesaid26f6b12020-03-19 06:30:28 +0000921 flowState, _ := fu.MkFlowStat(fa)
922 flowState2, _ := fu.MkFlowStat(fa2)
923 flowState3, _ := fu.MkFlowStat(fa3)
924 flowState4, _ := fu.MkFlowStat(fa4)
kdarapub26b4502019-10-05 03:02:33 +0530925 formulateClassifierInfoFromFlow(classifierInfo, flowState)
926 formulateClassifierInfoFromFlow(classifierInfo2, flowState2)
927 formulateClassifierInfoFromFlow(classifierInfo3, flowState3)
928 formulateClassifierInfoFromFlow(classifierInfo4, flowState4)
929
930 err := formulateActionInfoFromFlow(actionInfo, classifierInfo, flowState)
931 if err != nil {
932 // Error logging is already done in the called function
933 // So just return in case of error
934 return
935 }
936
937 err = formulateActionInfoFromFlow(actionInfo2, classifierInfo2, flowState2)
938 if err != nil {
939 // Error logging is already done in the called function
940 // So just return in case of error
941 return
942 }
943
944 err = formulateActionInfoFromFlow(actionInfo3, classifierInfo3, flowState3)
945 if err != nil {
946 // Error logging is already done in the called function
947 // So just return in case of error
948 return
949 }
950
951 err = formulateActionInfoFromFlow(actionInfo4, classifierInfo4, flowState4)
952 if err != nil {
953 // Error logging is already done in the called function
954 // So just return in case of error
955 return
956 }
957
958 //ofpMeterConfig := &ofp.OfpMeterConfig{Flags: 1, MeterId: 1}
959 //flowMetadata := &voltha.FlowMetadata{
960 // Meters: []*ofp.OfpMeterConfig{ofpMeterConfig},
961 //}
962
963 TpInst := &tp.TechProfile{
964 Name: "Test-Tech-Profile",
965 SubscriberIdentifier: "257",
966 ProfileType: "Mock",
967 Version: 1,
968 NumGemPorts: 4,
kdarapub26b4502019-10-05 03:02:33 +0530969 InstanceCtrl: tp.InstanceControl{
970 Onu: "1",
971 Uni: "16",
972 },
973 }
974
975 type fields struct {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530976 techprofile []tp.TechProfileIf
977 deviceHandler *DeviceHandler
978 resourceMgr *rsrcMgr.OpenOltResourceMgr
kdarapub26b4502019-10-05 03:02:33 +0530979 }
980 type args struct {
981 args map[string]uint32
982 classifierInfo map[string]interface{}
983 actionInfo map[string]interface{}
984 flow *ofp.OfpFlowStats
985 gemPort uint32
986 intfID uint32
987 onuID uint32
988 uniID uint32
989 portNo uint32
990 TpInst *tp.TechProfile
991 allocID []uint32
992 gemPorts []uint32
993 TpID uint32
994 uni string
995 }
996 tests := []struct {
997 name string
998 fields fields
999 args args
1000 }{
1001 {
1002 name: "checkAndAddFlow-1",
1003 args: args{
1004 args: nil,
1005 classifierInfo: classifierInfo,
1006 actionInfo: actionInfo,
1007 flow: flowState,
1008 gemPort: 1,
1009 intfID: 1,
1010 onuID: 1,
1011 uniID: 16,
1012 portNo: 1,
1013 TpInst: TpInst,
1014 allocID: []uint32{0x8001, 0x8002, 0x8003, 0x8004},
1015 gemPorts: []uint32{1, 2, 3, 4},
1016 TpID: 64,
1017 uni: "16",
1018 },
1019 },
1020 {
1021 name: "checkAndAddFlow-2",
1022 args: args{
1023 args: nil,
1024 classifierInfo: classifierInfo2,
1025 actionInfo: actionInfo2,
1026 flow: flowState2,
1027 gemPort: 1,
1028 intfID: 1,
1029 onuID: 1,
1030 uniID: 16,
1031 portNo: 1,
1032 TpInst: TpInst,
1033 allocID: []uint32{0x8001, 0x8002, 0x8003, 0x8004},
1034 gemPorts: []uint32{1, 2, 3, 4},
1035 TpID: 64,
1036 uni: "16",
1037 },
1038 },
1039 {
1040 name: "checkAndAddFlow-3",
1041 args: args{
1042 args: nil,
1043 classifierInfo: classifierInfo3,
1044 actionInfo: actionInfo3,
1045 flow: flowState3,
1046 gemPort: 1,
1047 intfID: 1,
1048 onuID: 1,
1049 uniID: 16,
1050 portNo: 1,
1051 TpInst: TpInst,
1052 allocID: []uint32{0x8001, 0x8002, 0x8003, 0x8004},
1053 gemPorts: []uint32{1, 2, 3, 4},
1054 TpID: 64,
1055 uni: "16",
1056 },
1057 },
1058 {
1059 name: "checkAndAddFlow-4",
1060 args: args{
1061 args: nil,
1062 classifierInfo: classifierInfo4,
1063 actionInfo: actionInfo4,
1064 flow: flowState4,
1065 gemPort: 1,
1066 intfID: 1,
1067 onuID: 1,
1068 uniID: 16,
1069 portNo: 1,
1070 TpInst: TpInst,
1071 allocID: []uint32{0x8001, 0x8002, 0x8003, 0x8004},
1072 gemPorts: []uint32{1, 2, 3, 4},
1073 TpID: 64,
1074 uni: "16",
1075 },
1076 },
1077 }
npujarec5762e2020-01-01 14:08:48 +05301078 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1079 defer cancel()
kdarapub26b4502019-10-05 03:02:33 +05301080 for _, tt := range tests {
1081 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301082 flowMgr.checkAndAddFlow(ctx, tt.args.args, tt.args.classifierInfo, tt.args.actionInfo, tt.args.flow,
Gamze Abakafee36392019-10-03 11:17:24 +00001083 tt.args.TpInst, tt.args.gemPorts, tt.args.TpID, tt.args.uni)
kdarapub26b4502019-10-05 03:02:33 +05301084 })
1085 }
1086}
Esin Karamanccb714b2019-11-29 15:02:06 +00001087
1088func TestOpenOltFlowMgr_TestMulticastFlow(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301089 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1090 defer cancel()
Esin Karamanccb714b2019-11-29 15:02:06 +00001091 //create group
1092 group := newGroup(2, []uint32{1})
npujarec5762e2020-01-01 14:08:48 +05301093 flowMgr.AddGroup(ctx, group)
Esin Karamanccb714b2019-11-29 15:02:06 +00001094
1095 //create multicast flow
1096 multicastFlowArgs := &fu.FlowArgs{
1097 MatchFields: []*ofp.OfpOxmOfbField{
1098 fu.InPort(65536),
1099 fu.VlanVid(660), //vlan
1100 fu.Metadata_ofp(uint64(66)), //inner vlan
1101 fu.EthType(0x800), //ipv4
1102 fu.Ipv4Dst(3809869825), //227.22.0.1
1103 },
1104 Actions: []*ofp.OfpAction{
1105 fu.Group(1),
1106 },
1107 }
divyadesaid26f6b12020-03-19 06:30:28 +00001108 ofpStats, _ := fu.MkFlowStat(multicastFlowArgs)
npujarec5762e2020-01-01 14:08:48 +05301109 flowMgr.AddFlow(ctx, ofpStats, &voltha.FlowMetadata{})
Esin Karamanccb714b2019-11-29 15:02:06 +00001110
1111 //add bucket to the group
1112 group = newGroup(2, []uint32{1, 2})
1113
npujarec5762e2020-01-01 14:08:48 +05301114 flowMgr.ModifyGroup(ctx, group)
Esin Karamanccb714b2019-11-29 15:02:06 +00001115}