blob: 1faa8c001efb76fcf118ad38057e890b3bd6ec8d [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 Scandoloabf9c512020-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 Scandoloabf9c512020-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 Scandoloabf9c512020-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) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000275 ctx := context.Background()
kdarapub26b4502019-10-05 03:02:33 +0530276 // flowMgr := newMockFlowmgr()
Neha Sharma96b7bf22020-06-15 10:37:32 +0000277 logger.Debug(ctx, "Info Warning Error: Starting RemoveFlow() test")
kdarapu3248f9a2019-10-03 13:54:52 +0530278 fa := &fu.FlowArgs{
279 MatchFields: []*ofp.OfpOxmOfbField{
280 fu.InPort(2),
281 fu.Metadata_ofp(2),
282 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
283 },
284 Actions: []*ofp.OfpAction{
285 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
286 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
287 fu.Output(1),
288 },
289 }
divyadesaid26f6b12020-03-19 06:30:28 +0000290 ofpstats, _ := fu.MkFlowStat(fa)
kdarapub26b4502019-10-05 03:02:33 +0530291 ofpstats.Cookie = ofpstats.Id
kdarapub26b4502019-10-05 03:02:33 +0530292 lldpFa := &fu.FlowArgs{
293 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
294 MatchFields: []*ofp.OfpOxmOfbField{
295 fu.InPort(1),
296 fu.EthType(0x88CC),
297 fu.TunnelId(536870912),
298 },
299 Actions: []*ofp.OfpAction{
300 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
301 },
302 }
divyadesaid26f6b12020-03-19 06:30:28 +0000303 lldpofpstats, _ := fu.MkFlowStat(lldpFa)
kdarapub26b4502019-10-05 03:02:33 +0530304 //lldpofpstats.Cookie = lldpofpstats.Id
305
306 dhcpFa := &fu.FlowArgs{
307 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
308 MatchFields: []*ofp.OfpOxmOfbField{
309 fu.InPort(1),
310 fu.UdpSrc(67),
311 //fu.TunnelId(536870912),
312 fu.IpProto(17),
313 },
314 Actions: []*ofp.OfpAction{
315 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
316 },
317 }
divyadesaid26f6b12020-03-19 06:30:28 +0000318 dhcpofpstats, _ := fu.MkFlowStat(dhcpFa)
kdarapub26b4502019-10-05 03:02:33 +0530319 //dhcpofpstats.Cookie = dhcpofpstats.Id
Esin Karamanccb714b2019-11-29 15:02:06 +0000320
321 //multicast flow
322 multicastFa := &fu.FlowArgs{
323 MatchFields: []*ofp.OfpOxmOfbField{
324 fu.InPort(65536),
325 fu.VlanVid(660), //vlan
326 fu.Metadata_ofp(uint64(66)), //inner vlan
327 fu.EthType(0x800), //ipv4
328 fu.Ipv4Dst(3809869825), //227.22.0.1
329 },
330 Actions: []*ofp.OfpAction{
331 fu.Group(1),
332 },
333 }
divyadesaid26f6b12020-03-19 06:30:28 +0000334 multicastOfpStats, _ := fu.MkFlowStat(multicastFa)
Esin Karamanccb714b2019-11-29 15:02:06 +0000335 multicastOfpStats.Id = 1
336
kdarapu3248f9a2019-10-03 13:54:52 +0530337 type args struct {
338 flow *ofp.OfpFlowStats
339 }
340 tests := []struct {
341 name string
342 args args
343 }{
344 // TODO: Add test cases.
345 {"RemoveFlow", args{flow: ofpstats}},
kdarapub26b4502019-10-05 03:02:33 +0530346 {"RemoveFlow", args{flow: lldpofpstats}},
347 {"RemoveFlow", args{flow: dhcpofpstats}},
Esin Karamanccb714b2019-11-29 15:02:06 +0000348 {"RemoveFlow", args{flow: multicastOfpStats}},
kdarapu3248f9a2019-10-03 13:54:52 +0530349 }
npujarec5762e2020-01-01 14:08:48 +0530350 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
351 defer cancel()
kdarapu3248f9a2019-10-03 13:54:52 +0530352 for _, tt := range tests {
353 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530354 flowMgr.RemoveFlow(ctx, tt.args.flow)
kdarapu3248f9a2019-10-03 13:54:52 +0530355 })
356 }
kdarapub26b4502019-10-05 03:02:33 +0530357 // t.Error("=====")
kdarapu3248f9a2019-10-03 13:54:52 +0530358}
359
360func TestOpenOltFlowMgr_AddFlow(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530361 // flowMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530362 kw := make(map[string]uint64)
363 kw["table_id"] = 1
364 kw["meter_id"] = 1
kdarapub26b4502019-10-05 03:02:33 +0530365 kw["write_metadata"] = 0x4000000000 // Tech-Profile-ID 64
366
367 // Upstream flow
kdarapu3248f9a2019-10-03 13:54:52 +0530368 fa := &fu.FlowArgs{
369 MatchFields: []*ofp.OfpOxmOfbField{
kdarapub26b4502019-10-05 03:02:33 +0530370 fu.InPort(536870912),
371 fu.Metadata_ofp(1),
372 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
373 },
374 Actions: []*ofp.OfpAction{
375 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
376 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
377 fu.Output(65536),
378 fu.PushVlan(0x8100),
379 },
380 KV: kw,
381 }
382
383 // Downstream flow
384 fa3 := &fu.FlowArgs{
385 MatchFields: []*ofp.OfpOxmOfbField{
386 fu.InPort(65536),
387 fu.Metadata_ofp(1),
388 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257),
389 },
390 Actions: []*ofp.OfpAction{
391 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
392 //fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
393 fu.PopVlan(),
394 fu.Output(536870912),
395 },
396 KV: kw,
397 }
398
399 fa2 := &fu.FlowArgs{
400 MatchFields: []*ofp.OfpOxmOfbField{
kdarapu3248f9a2019-10-03 13:54:52 +0530401 fu.InPort(1000),
402 fu.Metadata_ofp(1),
403 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
404 },
405 Actions: []*ofp.OfpAction{
406 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
407 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
408 fu.Output(65533),
409 },
410 KV: kw,
411 }
412
kdarapub26b4502019-10-05 03:02:33 +0530413 // TODO Add LLDP flow
414 // TODO Add DHCP flow
415
416 // Flows for negative scenarios
417 // Failure in formulateActionInfoFromFlow()
418 fa4 := &fu.FlowArgs{
419 MatchFields: []*ofp.OfpOxmOfbField{
420 fu.InPort(1000),
421 fu.Metadata_ofp(1),
422 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
423 },
424 Actions: []*ofp.OfpAction{
425 fu.Experimenter(257, []byte{1, 2, 3, 4}),
426 },
427 KV: kw,
428 }
429
430 // Invalid Output
431 fa5 := &fu.FlowArgs{
432 MatchFields: []*ofp.OfpOxmOfbField{
433 fu.InPort(1000),
434 fu.Metadata_ofp(1),
435 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
436 },
437 Actions: []*ofp.OfpAction{
438 fu.Output(0),
439 },
440 KV: kw,
441 }
442
443 // Tech-Profile-ID update (not supported)
444 kw6 := make(map[string]uint64)
445 kw6["table_id"] = 1
446 kw6["meter_id"] = 1
447 kw6["write_metadata"] = 0x4100000000 // TpID Other than the stored one
448 fa6 := &fu.FlowArgs{
449 MatchFields: []*ofp.OfpOxmOfbField{
450 fu.InPort(536870912),
451 fu.TunnelId(16),
452 fu.Metadata_ofp(1),
453 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
454 },
455 Actions: []*ofp.OfpAction{
456 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
457 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
458 fu.Output(65535),
459 },
460 KV: kw6,
461 }
462
463 lldpFa := &fu.FlowArgs{
464 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
465 MatchFields: []*ofp.OfpOxmOfbField{
466 fu.InPort(1),
467 fu.EthType(0x88CC),
468 fu.TunnelId(536870912),
469 },
470 Actions: []*ofp.OfpAction{
471 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
472 },
473 }
474
475 dhcpFa := &fu.FlowArgs{
476 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
477 MatchFields: []*ofp.OfpOxmOfbField{
478 fu.InPort(1),
479 fu.UdpSrc(67),
480 //fu.TunnelId(536870912),
481 fu.IpProto(17),
482 },
483 Actions: []*ofp.OfpAction{
484 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
485 },
486 }
487 igmpFa := &fu.FlowArgs{
488 KV: fu.OfpFlowModArgs{"priority": 1000, "cookie": 48132224281636694},
489 MatchFields: []*ofp.OfpOxmOfbField{
490 fu.InPort(1),
491 fu.UdpSrc(67),
492 //fu.TunnelId(536870912),
493 fu.IpProto(2),
494 },
495 Actions: []*ofp.OfpAction{
496 fu.Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
497 },
498 }
499
500 fa9 := &fu.FlowArgs{
501 MatchFields: []*ofp.OfpOxmOfbField{
502 fu.InPort(536870912),
503 fu.TunnelId(16),
504 fu.Metadata_ofp(1),
505 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
506 fu.VlanPcp(1000),
507 fu.UdpDst(65535),
508 fu.UdpSrc(536870912),
509 fu.Ipv4Dst(65535),
510 fu.Ipv4Src(536870912),
511 },
512 Actions: []*ofp.OfpAction{
513 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
514 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
515 fu.Output(65535),
516 },
517 KV: kw6,
518 }
519
520 fa10 := &fu.FlowArgs{
521 MatchFields: []*ofp.OfpOxmOfbField{
522 fu.InPort(65533),
523 // fu.TunnelId(16),
524 fu.Metadata_ofp(1),
525 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
526 fu.VlanPcp(1000),
527 fu.UdpDst(65535),
528 fu.UdpSrc(536870912),
529 fu.Ipv4Dst(65535),
530 fu.Ipv4Src(536870912),
531 },
532 Actions: []*ofp.OfpAction{
533 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
534 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
535 fu.Output(65535),
536 },
537 KV: kw6,
538 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000539 //multicast flow
540 fa11 := &fu.FlowArgs{
541 MatchFields: []*ofp.OfpOxmOfbField{
542 fu.InPort(65536),
543 fu.VlanVid(660), //vlan
544 fu.Metadata_ofp(uint64(66)), //inner vlan
545 fu.EthType(0x800), //ipv4
546 fu.Ipv4Dst(3809869825), //227.22.0.1
547 },
548 Actions: []*ofp.OfpAction{
549 fu.Group(1),
550 },
551 KV: kw6,
552 }
divyadesaid26f6b12020-03-19 06:30:28 +0000553 ofpstats, _ := fu.MkFlowStat(fa)
554 ofpstats2, _ := fu.MkFlowStat(fa2)
555 ofpstats3, _ := fu.MkFlowStat(fa3)
556 ofpstats4, _ := fu.MkFlowStat(fa4)
557 ofpstats5, _ := fu.MkFlowStat(fa5)
558 ofpstats6, _ := fu.MkFlowStat(fa6)
559 ofpstats7, _ := fu.MkFlowStat(lldpFa)
560 ofpstats8, _ := fu.MkFlowStat(dhcpFa)
561 ofpstats9, _ := fu.MkFlowStat(fa9)
562 ofpstats10, _ := fu.MkFlowStat(fa10)
563 igmpstats, _ := fu.MkFlowStat(igmpFa)
564 ofpstats11, _ := fu.MkFlowStat(fa11)
kdarapub26b4502019-10-05 03:02:33 +0530565
Gamze Abakafee36392019-10-03 11:17:24 +0000566 fmt.Println(ofpstats6, ofpstats9, ofpstats10)
567
kdarapu3248f9a2019-10-03 13:54:52 +0530568 ofpMeterConfig := &ofp.OfpMeterConfig{Flags: 1, MeterId: 1}
kdarapu3248f9a2019-10-03 13:54:52 +0530569 flowMetadata := &voltha.FlowMetadata{
570 Meters: []*ofp.OfpMeterConfig{ofpMeterConfig},
571 }
572 type args struct {
573 flow *ofp.OfpFlowStats
574 flowMetadata *voltha.FlowMetadata
575 }
576 tests := []struct {
577 name string
578 args args
579 }{
580 // TODO: Add test cases.
581 {"AddFlow", args{flow: ofpstats, flowMetadata: flowMetadata}},
kdarapub26b4502019-10-05 03:02:33 +0530582 {"AddFlow", args{flow: ofpstats2, flowMetadata: flowMetadata}},
583 {"AddFlow", args{flow: ofpstats3, flowMetadata: flowMetadata}},
584 {"AddFlow", args{flow: ofpstats4, flowMetadata: flowMetadata}},
585 {"AddFlow", args{flow: ofpstats5, flowMetadata: flowMetadata}},
Gamze Abakafee36392019-10-03 11:17:24 +0000586 //{"AddFlow", args{flow: ofpstats6, flowMetadata: flowMetadata}},
kdarapub26b4502019-10-05 03:02:33 +0530587 {"AddFlow", args{flow: ofpstats7, flowMetadata: flowMetadata}},
588 {"AddFlow", args{flow: ofpstats8, flowMetadata: flowMetadata}},
Gamze Abakafee36392019-10-03 11:17:24 +0000589 //{"AddFlow", args{flow: ofpstats9, flowMetadata: flowMetadata}},
kdarapub26b4502019-10-05 03:02:33 +0530590 {"AddFlow", args{flow: igmpstats, flowMetadata: flowMetadata}},
Gamze Abakafee36392019-10-03 11:17:24 +0000591 //{"AddFlow", args{flow: ofpstats10, flowMetadata: flowMetadata}},
kdarapub26b4502019-10-05 03:02:33 +0530592 //ofpstats10
Esin Karamanccb714b2019-11-29 15:02:06 +0000593 {"AddFlow", args{flow: ofpstats11, flowMetadata: flowMetadata}},
kdarapu3248f9a2019-10-03 13:54:52 +0530594 }
npujarec5762e2020-01-01 14:08:48 +0530595 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
596 defer cancel()
kdarapu3248f9a2019-10-03 13:54:52 +0530597 for _, tt := range tests {
598 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530599 flowMgr.AddFlow(ctx, tt.args.flow, tt.args.flowMetadata)
kdarapu3248f9a2019-10-03 13:54:52 +0530600 })
601 }
602}
603
604func TestOpenOltFlowMgr_UpdateOnuInfo(t *testing.T) {
Matteo Scandoloabf9c512020-06-23 19:31:14 -0700605 flwMgr := newMockFlowmgr()
606
npujarec5762e2020-01-01 14:08:48 +0530607 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
608 defer cancel()
kdarapu3248f9a2019-10-03 13:54:52 +0530609
Matteo Scandoloabf9c512020-06-23 19:31:14 -0700610 wg := sync.WaitGroup{}
611
612 intfCount := 16
613 onuCount := 32
614
615 for i := 0; i < intfCount; i++ {
616 for j := 0; j < onuCount; j++ {
617 wg.Add(1)
618 go func(i uint32, j uint32) {
619 flwMgr.UpdateOnuInfo(ctx, i, i, fmt.Sprintf("onu-%d", i))
620 wg.Done()
621 }(uint32(i), uint32(j))
622 }
623
624 }
625
626 wg.Wait()
627}
628
629func TestOpenOltFlowMgr_addGemPortToOnuInfoMap(t *testing.T) {
630 flowMgr = newMockFlowmgr()
631 intfNum := 16
632 onuNum := 32
633
634 // clean the flowMgr
635 flowMgr.onuGemInfo = make(map[uint32][]rsrcMgr.OnuGemInfo, intfNum)
636
637 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
638 defer cancel()
639
640 // Create OnuInfo
641 for i := 0; i < intfNum; i++ {
642 for o := 0; o < onuNum; o++ {
643 flowMgr.UpdateOnuInfo(ctx, uint32(i), uint32(o), fmt.Sprintf("i%do%d", i, o))
644 }
645 }
646
647 // Add gemPorts to OnuInfo in parallel threads
648 wg := sync.WaitGroup{}
649
650 for o := 0; o < onuNum; o++ {
651 for i := 0; i < intfNum; i++ {
652 wg.Add(1)
653 go func(intfId uint32, onuId uint32) {
654 gemID, _ := strconv.Atoi(fmt.Sprintf("90%d%d", intfId, onuId))
655
656 flowMgr.addGemPortToOnuInfoMap(ctx, intfId, onuId, uint32(gemID))
657 wg.Done()
658 }(uint32(i), uint32(o))
659 }
660 }
661
662 wg.Wait()
663
664 // check that each entry of onuGemInfo has the correct number of ONUs
665 for i := 0; i < intfNum; i++ {
666 lenofOnu := len(flowMgr.onuGemInfo[uint32(i)])
667 if onuNum != lenofOnu {
668 t.Errorf("OnuGemInfo length is not as expected len = %d, want %d", lenofOnu, onuNum)
669 }
670
671 for o := 0; o < onuNum; o++ {
672 lenOfGemPorts := len(flowMgr.onuGemInfo[uint32(i)][o].GemPorts)
673 // check that each onuEntry has 1 gemPort
674 if lenOfGemPorts != 1 {
675 t.Errorf("Expected 1 GemPort per ONU, found %d", lenOfGemPorts)
676 }
677
678 // check that the value of the gemport is correct
679 gemID, _ := strconv.Atoi(fmt.Sprintf("90%d%d", i, o))
680 currentValue := flowMgr.onuGemInfo[uint32(i)][o].GemPorts[0]
681 if uint32(gemID) != currentValue {
682 t.Errorf("Expected GemPort value to be %d, found %d", gemID, currentValue)
683 }
684 }
kdarapu3248f9a2019-10-03 13:54:52 +0530685 }
686}
687
serkant.uluderya96af4932020-02-20 16:58:48 -0800688func TestOpenOltFlowMgr_deleteGemPortFromLocalCache(t *testing.T) {
Matteo Scandoloabf9c512020-06-23 19:31:14 -0700689 flwMgr := newMockFlowmgr()
serkant.uluderya96af4932020-02-20 16:58:48 -0800690 type args struct {
691 intfID uint32
692 onuID uint32
693 gemPortIDs []uint32
694 gemPortIDsToBeDeleted []uint32
Matteo Scandoloabf9c512020-06-23 19:31:14 -0700695 gemPortIDsRemaining []uint32
serkant.uluderya96af4932020-02-20 16:58:48 -0800696 serialNum string
697 finalLength int
698 }
699 tests := []struct {
700 name string
701 args args
702 }{
703 // Add/Delete single gem port
Matteo Scandoloabf9c512020-06-23 19:31:14 -0700704 {"DeleteGemPortFromLocalCache1", args{0, 1, []uint32{1}, []uint32{1}, []uint32{}, "onu1", 0}},
serkant.uluderya96af4932020-02-20 16:58:48 -0800705 // Delete all gemports
Matteo Scandoloabf9c512020-06-23 19:31:14 -0700706 {"DeleteGemPortFromLocalCache2", args{0, 1, []uint32{1, 2, 3, 4}, []uint32{1, 2, 3, 4}, []uint32{}, "onu1", 0}},
serkant.uluderya96af4932020-02-20 16:58:48 -0800707 // Try to delete when there is no gem port
Matteo Scandoloabf9c512020-06-23 19:31:14 -0700708 {"DeleteGemPortFromLocalCache3", args{0, 1, []uint32{}, []uint32{1, 2}, []uint32{}, "onu1", 0}},
serkant.uluderya96af4932020-02-20 16:58:48 -0800709 // Try to delete non-existent gem port
Matteo Scandoloabf9c512020-06-23 19:31:14 -0700710 {"DeleteGemPortFromLocalCache4", args{0, 1, []uint32{1}, []uint32{2}, []uint32{1}, "onu1", 1}},
serkant.uluderya96af4932020-02-20 16:58:48 -0800711 // Try to delete two of the gem ports
Matteo Scandoloabf9c512020-06-23 19:31:14 -0700712 {"DeleteGemPortFromLocalCache5", args{0, 1, []uint32{1, 2, 3, 4}, []uint32{2, 4}, []uint32{1, 3}, "onu1", 2}},
serkant.uluderya96af4932020-02-20 16:58:48 -0800713 }
714 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
715 defer cancel()
716 for _, tt := range tests {
717 t.Run(tt.name, func(t *testing.T) {
Matteo Scandoloabf9c512020-06-23 19:31:14 -0700718 flwMgr.UpdateOnuInfo(ctx, tt.args.intfID, tt.args.onuID, tt.args.serialNum)
serkant.uluderya96af4932020-02-20 16:58:48 -0800719 for _, gemPort := range tt.args.gemPortIDs {
Matteo Scandoloabf9c512020-06-23 19:31:14 -0700720 flwMgr.addGemPortToOnuInfoMap(ctx, tt.args.intfID, tt.args.onuID, gemPort)
serkant.uluderya96af4932020-02-20 16:58:48 -0800721 }
722 for _, gemPortDeleted := range tt.args.gemPortIDsToBeDeleted {
Matteo Scandoloabf9c512020-06-23 19:31:14 -0700723 flwMgr.deleteGemPortFromLocalCache(ctx, tt.args.intfID, tt.args.onuID, gemPortDeleted)
serkant.uluderya96af4932020-02-20 16:58:48 -0800724 }
Matteo Scandoloabf9c512020-06-23 19:31:14 -0700725 lenofGemPorts := len(flwMgr.onuGemInfo[tt.args.intfID][0].GemPorts)
serkant.uluderya96af4932020-02-20 16:58:48 -0800726 if lenofGemPorts != tt.args.finalLength {
727 t.Errorf("GemPorts length is not as expected len = %d, want %d", lenofGemPorts, tt.args.finalLength)
728 }
Matteo Scandoloabf9c512020-06-23 19:31:14 -0700729 gemPorts := flwMgr.onuGemInfo[tt.args.intfID][0].GemPorts
730 if !reflect.DeepEqual(tt.args.gemPortIDsRemaining, gemPorts) {
731 t.Errorf("GemPorts are not as expected = %v, want %v", gemPorts, tt.args.gemPortIDsRemaining)
732 }
serkant.uluderya96af4932020-02-20 16:58:48 -0800733
734 })
735 }
736}
737
kdarapu3248f9a2019-10-03 13:54:52 +0530738func TestOpenOltFlowMgr_GetLogicalPortFromPacketIn(t *testing.T) {
Matteo Scandoloabf9c512020-06-23 19:31:14 -0700739 flwMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530740 type args struct {
741 packetIn *openoltpb2.PacketIndication
742 }
743 tests := []struct {
744 name string
745 args args
746 want uint32
747 wantErr bool
748 }{
749 // TODO: Add test cases.
750 {"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 +0000751 {"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 +0530752 // Negative Test cases.
753 {"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 +0000754 {"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 +0530755 }
npujarec5762e2020-01-01 14:08:48 +0530756 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
757 defer cancel()
kdarapu3248f9a2019-10-03 13:54:52 +0530758 for _, tt := range tests {
759 t.Run(tt.name, func(t *testing.T) {
760
Matteo Scandoloabf9c512020-06-23 19:31:14 -0700761 got, err := flwMgr.GetLogicalPortFromPacketIn(ctx, tt.args.packetIn)
kdarapu3248f9a2019-10-03 13:54:52 +0530762 if (err != nil) != tt.wantErr {
763 t.Errorf("OpenOltFlowMgr.GetLogicalPortFromPacketIn() error = %v, wantErr %v", err, tt.wantErr)
764 return
765 }
766 if got != tt.want {
767 t.Errorf("OpenOltFlowMgr.GetLogicalPortFromPacketIn() = %v, want %v", got, tt.want)
768 }
769 })
770 }
771}
772
773func TestOpenOltFlowMgr_GetPacketOutGemPortID(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530774 // flwMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530775
776 type args struct {
777 intfID uint32
778 onuID uint32
779 portNum uint32
780 }
781 tests := []struct {
782 name string
783 args args
784 want uint32
785 wantErr bool
786 }{
787 // TODO: Add test cases.
788 {"GetPacketOutGemPortID", args{intfID: 1, onuID: 1, portNum: 1}, 1, false},
789 {"GetPacketOutGemPortID", args{intfID: 2, onuID: 2, portNum: 2}, 2, false},
790 {"GetPacketOutGemPortID", args{intfID: 1, onuID: 2, portNum: 2}, 0, true},
791 }
npujarec5762e2020-01-01 14:08:48 +0530792 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
793 defer cancel()
kdarapu3248f9a2019-10-03 13:54:52 +0530794 for _, tt := range tests {
795 t.Run(tt.name, func(t *testing.T) {
796
npujarec5762e2020-01-01 14:08:48 +0530797 got, err := flowMgr.GetPacketOutGemPortID(ctx, tt.args.intfID, tt.args.onuID, tt.args.portNum)
kdarapu3248f9a2019-10-03 13:54:52 +0530798 if (err != nil) != tt.wantErr {
799 t.Errorf("OpenOltFlowMgr.GetPacketOutGemPortID() error = %v, wantErr %v", err, tt.wantErr)
800 return
801 }
802 if got != tt.want {
803 t.Errorf("OpenOltFlowMgr.GetPacketOutGemPortID() = %v, want %v", got, tt.want)
804 }
805
806 })
807 }
808}
809
810func TestOpenOltFlowMgr_DeleteTechProfileInstance(t *testing.T) {
kdarapub26b4502019-10-05 03:02:33 +0530811 // flwMgr := newMockFlowmgr()
kdarapu3248f9a2019-10-03 13:54:52 +0530812 type args struct {
813 intfID uint32
814 onuID uint32
815 uniID uint32
816 sn string
Gamze Abakafee36392019-10-03 11:17:24 +0000817 tpID uint32
kdarapu3248f9a2019-10-03 13:54:52 +0530818 }
819 tests := []struct {
820 name string
821 args args
822 wantErr bool
823 }{
824 // TODO: Add test cases.
Gamze Abakafee36392019-10-03 11:17:24 +0000825 {"DeleteTechProfileInstance", args{intfID: 0, onuID: 1, uniID: 1, sn: "", tpID: 64}, false},
kdarapu3248f9a2019-10-03 13:54:52 +0530826 }
npujarec5762e2020-01-01 14:08:48 +0530827 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
828 defer cancel()
kdarapu3248f9a2019-10-03 13:54:52 +0530829 for _, tt := range tests {
830 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530831 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 +0530832 t.Errorf("OpenOltFlowMgr.DeleteTechProfileInstance() error = %v, wantErr %v", err, tt.wantErr)
833 }
834 })
835 }
836}
kdarapub26b4502019-10-05 03:02:33 +0530837
838func TestOpenOltFlowMgr_checkAndAddFlow(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000839 ctx := context.Background()
kdarapub26b4502019-10-05 03:02:33 +0530840 // flowMgr := newMockFlowmgr()
841 kw := make(map[string]uint64)
842 kw["table_id"] = 1
843 kw["meter_id"] = 1
844 kw["write_metadata"] = 0x4000000000 // Tech-Profile-ID 64
845
846 // Upstream flow
847 fa := &fu.FlowArgs{
848 MatchFields: []*ofp.OfpOxmOfbField{
849 fu.InPort(536870912),
850 fu.Metadata_ofp(1),
851 fu.IpProto(17), // dhcp
Gamze Abakafee36392019-10-03 11:17:24 +0000852 fu.VlanPcp(0),
kdarapub26b4502019-10-05 03:02:33 +0530853 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
854 },
855 Actions: []*ofp.OfpAction{
856 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
857 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
858 fu.Output(65536),
859 fu.PushVlan(0x8100),
860 },
861 KV: kw,
862 }
863
864 // EAPOL
865 fa2 := &fu.FlowArgs{
866 MatchFields: []*ofp.OfpOxmOfbField{
867 fu.InPort(536870912),
868 fu.Metadata_ofp(1),
869 fu.EthType(0x888E),
870 fu.VlanPcp(1),
871 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257),
872 },
873 Actions: []*ofp.OfpAction{
874 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
875 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
876 fu.Output(65536),
877 fu.PushVlan(0x8100),
878 },
879 KV: kw,
880 }
881
882 // HSIA
883 fa3 := &fu.FlowArgs{
884 MatchFields: []*ofp.OfpOxmOfbField{
885 fu.InPort(536870912),
886 fu.Metadata_ofp(1),
887 //fu.EthType(0x8100),
888 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
889 },
890 Actions: []*ofp.OfpAction{
891 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
892 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0)),
893 fu.Output(65536),
894 fu.PushVlan(0x8100),
895 },
896 KV: kw,
897 }
898
899 fa4 := &fu.FlowArgs{
900 MatchFields: []*ofp.OfpOxmOfbField{
901 fu.InPort(65535),
902 fu.Metadata_ofp(1),
903 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
904 fu.VlanPcp(1),
905 },
906 Actions: []*ofp.OfpAction{
907 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
908 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0)),
909 fu.Output(536870912),
910 fu.PopVlan(),
911 },
912 KV: kw,
913 }
914
915 classifierInfo := make(map[string]interface{})
916 actionInfo := make(map[string]interface{})
917 classifierInfo2 := make(map[string]interface{})
918 actionInfo2 := make(map[string]interface{})
919 classifierInfo3 := make(map[string]interface{})
920 actionInfo3 := make(map[string]interface{})
921 classifierInfo4 := make(map[string]interface{})
922 actionInfo4 := make(map[string]interface{})
divyadesaid26f6b12020-03-19 06:30:28 +0000923 flowState, _ := fu.MkFlowStat(fa)
924 flowState2, _ := fu.MkFlowStat(fa2)
925 flowState3, _ := fu.MkFlowStat(fa3)
926 flowState4, _ := fu.MkFlowStat(fa4)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000927 formulateClassifierInfoFromFlow(ctx, classifierInfo, flowState)
928 formulateClassifierInfoFromFlow(ctx, classifierInfo2, flowState2)
929 formulateClassifierInfoFromFlow(ctx, classifierInfo3, flowState3)
930 formulateClassifierInfoFromFlow(ctx, classifierInfo4, flowState4)
kdarapub26b4502019-10-05 03:02:33 +0530931
Neha Sharma96b7bf22020-06-15 10:37:32 +0000932 err := formulateActionInfoFromFlow(ctx, actionInfo, classifierInfo, flowState)
kdarapub26b4502019-10-05 03:02:33 +0530933 if err != nil {
934 // Error logging is already done in the called function
935 // So just return in case of error
936 return
937 }
938
Neha Sharma96b7bf22020-06-15 10:37:32 +0000939 err = formulateActionInfoFromFlow(ctx, actionInfo2, classifierInfo2, flowState2)
kdarapub26b4502019-10-05 03:02:33 +0530940 if err != nil {
941 // Error logging is already done in the called function
942 // So just return in case of error
943 return
944 }
945
Neha Sharma96b7bf22020-06-15 10:37:32 +0000946 err = formulateActionInfoFromFlow(ctx, actionInfo3, classifierInfo3, flowState3)
kdarapub26b4502019-10-05 03:02:33 +0530947 if err != nil {
948 // Error logging is already done in the called function
949 // So just return in case of error
950 return
951 }
952
Neha Sharma96b7bf22020-06-15 10:37:32 +0000953 err = formulateActionInfoFromFlow(ctx, actionInfo4, classifierInfo4, flowState4)
kdarapub26b4502019-10-05 03:02:33 +0530954 if err != nil {
955 // Error logging is already done in the called function
956 // So just return in case of error
957 return
958 }
959
960 //ofpMeterConfig := &ofp.OfpMeterConfig{Flags: 1, MeterId: 1}
961 //flowMetadata := &voltha.FlowMetadata{
962 // Meters: []*ofp.OfpMeterConfig{ofpMeterConfig},
963 //}
964
965 TpInst := &tp.TechProfile{
966 Name: "Test-Tech-Profile",
967 SubscriberIdentifier: "257",
968 ProfileType: "Mock",
969 Version: 1,
970 NumGemPorts: 4,
kdarapub26b4502019-10-05 03:02:33 +0530971 InstanceCtrl: tp.InstanceControl{
972 Onu: "1",
973 Uni: "16",
974 },
975 }
976
977 type fields struct {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530978 techprofile []tp.TechProfileIf
979 deviceHandler *DeviceHandler
980 resourceMgr *rsrcMgr.OpenOltResourceMgr
kdarapub26b4502019-10-05 03:02:33 +0530981 }
982 type args struct {
983 args map[string]uint32
984 classifierInfo map[string]interface{}
985 actionInfo map[string]interface{}
986 flow *ofp.OfpFlowStats
987 gemPort uint32
988 intfID uint32
989 onuID uint32
990 uniID uint32
991 portNo uint32
992 TpInst *tp.TechProfile
993 allocID []uint32
994 gemPorts []uint32
995 TpID uint32
996 uni string
997 }
998 tests := []struct {
999 name string
1000 fields fields
1001 args args
1002 }{
1003 {
1004 name: "checkAndAddFlow-1",
1005 args: args{
1006 args: nil,
1007 classifierInfo: classifierInfo,
1008 actionInfo: actionInfo,
1009 flow: flowState,
1010 gemPort: 1,
1011 intfID: 1,
1012 onuID: 1,
1013 uniID: 16,
1014 portNo: 1,
1015 TpInst: TpInst,
1016 allocID: []uint32{0x8001, 0x8002, 0x8003, 0x8004},
1017 gemPorts: []uint32{1, 2, 3, 4},
1018 TpID: 64,
1019 uni: "16",
1020 },
1021 },
1022 {
1023 name: "checkAndAddFlow-2",
1024 args: args{
1025 args: nil,
1026 classifierInfo: classifierInfo2,
1027 actionInfo: actionInfo2,
1028 flow: flowState2,
1029 gemPort: 1,
1030 intfID: 1,
1031 onuID: 1,
1032 uniID: 16,
1033 portNo: 1,
1034 TpInst: TpInst,
1035 allocID: []uint32{0x8001, 0x8002, 0x8003, 0x8004},
1036 gemPorts: []uint32{1, 2, 3, 4},
1037 TpID: 64,
1038 uni: "16",
1039 },
1040 },
1041 {
1042 name: "checkAndAddFlow-3",
1043 args: args{
1044 args: nil,
1045 classifierInfo: classifierInfo3,
1046 actionInfo: actionInfo3,
1047 flow: flowState3,
1048 gemPort: 1,
1049 intfID: 1,
1050 onuID: 1,
1051 uniID: 16,
1052 portNo: 1,
1053 TpInst: TpInst,
1054 allocID: []uint32{0x8001, 0x8002, 0x8003, 0x8004},
1055 gemPorts: []uint32{1, 2, 3, 4},
1056 TpID: 64,
1057 uni: "16",
1058 },
1059 },
1060 {
1061 name: "checkAndAddFlow-4",
1062 args: args{
1063 args: nil,
1064 classifierInfo: classifierInfo4,
1065 actionInfo: actionInfo4,
1066 flow: flowState4,
1067 gemPort: 1,
1068 intfID: 1,
1069 onuID: 1,
1070 uniID: 16,
1071 portNo: 1,
1072 TpInst: TpInst,
1073 allocID: []uint32{0x8001, 0x8002, 0x8003, 0x8004},
1074 gemPorts: []uint32{1, 2, 3, 4},
1075 TpID: 64,
1076 uni: "16",
1077 },
1078 },
1079 }
npujarec5762e2020-01-01 14:08:48 +05301080 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1081 defer cancel()
kdarapub26b4502019-10-05 03:02:33 +05301082 for _, tt := range tests {
1083 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301084 flowMgr.checkAndAddFlow(ctx, tt.args.args, tt.args.classifierInfo, tt.args.actionInfo, tt.args.flow,
Gamze Abakafee36392019-10-03 11:17:24 +00001085 tt.args.TpInst, tt.args.gemPorts, tt.args.TpID, tt.args.uni)
kdarapub26b4502019-10-05 03:02:33 +05301086 })
1087 }
1088}
Esin Karamanccb714b2019-11-29 15:02:06 +00001089
1090func TestOpenOltFlowMgr_TestMulticastFlow(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301091 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1092 defer cancel()
Esin Karamanccb714b2019-11-29 15:02:06 +00001093 //create group
1094 group := newGroup(2, []uint32{1})
npujarec5762e2020-01-01 14:08:48 +05301095 flowMgr.AddGroup(ctx, group)
Esin Karamanccb714b2019-11-29 15:02:06 +00001096
1097 //create multicast flow
1098 multicastFlowArgs := &fu.FlowArgs{
1099 MatchFields: []*ofp.OfpOxmOfbField{
1100 fu.InPort(65536),
1101 fu.VlanVid(660), //vlan
1102 fu.Metadata_ofp(uint64(66)), //inner vlan
1103 fu.EthType(0x800), //ipv4
1104 fu.Ipv4Dst(3809869825), //227.22.0.1
1105 },
1106 Actions: []*ofp.OfpAction{
1107 fu.Group(1),
1108 },
1109 }
divyadesaid26f6b12020-03-19 06:30:28 +00001110 ofpStats, _ := fu.MkFlowStat(multicastFlowArgs)
npujarec5762e2020-01-01 14:08:48 +05301111 flowMgr.AddFlow(ctx, ofpStats, &voltha.FlowMetadata{})
Esin Karamanccb714b2019-11-29 15:02:06 +00001112
1113 //add bucket to the group
1114 group = newGroup(2, []uint32{1, 2})
1115
npujarec5762e2020-01-01 14:08:48 +05301116 flowMgr.ModifyGroup(ctx, group)
Esin Karamanccb714b2019-11-29 15:02:06 +00001117}