khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package utils |
| 17 | |
| 18 | import ( |
| 19 | "github.com/opencord/voltha-go/common/log" |
| 20 | ofp "github.com/opencord/voltha-protos/go/openflow_13" |
| 21 | "github.com/stretchr/testify/assert" |
| 22 | "google.golang.org/grpc/codes" |
| 23 | "google.golang.org/grpc/status" |
| 24 | "strings" |
| 25 | "testing" |
| 26 | ) |
| 27 | |
| 28 | func init() { |
| 29 | log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 30 | timeoutError = status.Errorf(codes.Aborted, "timeout") |
| 31 | taskFailureError = status.Error(codes.Internal, "test failure task") |
| 32 | } |
| 33 | |
| 34 | func TestFlowsAndGroups_AddFlow(t *testing.T) { |
| 35 | fg := NewFlowsAndGroups() |
| 36 | allFlows := fg.ListFlows() |
| 37 | assert.Equal(t, 0, len(allFlows)) |
| 38 | fg.AddFlow(nil) |
| 39 | allFlows = fg.ListFlows() |
| 40 | assert.Equal(t, 0, len(allFlows)) |
| 41 | |
| 42 | var fa *FlowArgs |
| 43 | fa = &FlowArgs{ |
| 44 | KV: OfpFlowModArgs{"priority": 500}, |
| 45 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 46 | InPort(1), |
| 47 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 1), |
| 48 | TunnelId(uint64(1)), |
| 49 | EthType(0x0800), |
| 50 | Ipv4Dst(0xffffffff), |
| 51 | IpProto(17), |
| 52 | UdpSrc(68), |
| 53 | UdpDst(67), |
| 54 | }, |
| 55 | Actions: []*ofp.OfpAction{ |
| 56 | PushVlan(0x8100), |
| 57 | SetField(VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 4000)), |
| 58 | Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)), |
| 59 | }, |
| 60 | } |
| 61 | flow := MkFlowStat(fa) |
| 62 | fg.AddFlow(flow) |
| 63 | |
| 64 | allFlows = fg.ListFlows() |
| 65 | assert.Equal(t, 1, len(allFlows)) |
| 66 | assert.True(t, FlowMatch(flow, allFlows[0])) |
| 67 | } |
| 68 | |
| 69 | func TestFlowsAndGroups_AddGroup(t *testing.T) { |
| 70 | var ga *GroupArgs |
| 71 | |
| 72 | fg := NewFlowsAndGroups() |
| 73 | allGroups := fg.ListGroups() |
| 74 | assert.Equal(t, 0, len(allGroups)) |
| 75 | fg.AddGroup(nil) |
| 76 | allGroups = fg.ListGroups() |
| 77 | assert.Equal(t, 0, len(allGroups)) |
| 78 | |
| 79 | ga = &GroupArgs{ |
| 80 | GroupId: 10, |
| 81 | Buckets: []*ofp.OfpBucket{ |
| 82 | {Actions: []*ofp.OfpAction{ |
| 83 | PopVlan(), |
| 84 | Output(1), |
| 85 | }, |
| 86 | }, |
| 87 | }, |
| 88 | } |
| 89 | group := MkGroupStat(ga) |
| 90 | fg.AddGroup(group) |
| 91 | |
| 92 | allGroups = fg.ListGroups() |
| 93 | assert.Equal(t, 1, len(allGroups)) |
| 94 | assert.Equal(t, ga.GroupId, allGroups[0].Desc.GroupId) |
| 95 | } |
| 96 | |
| 97 | func TestFlowsAndGroups_Copy(t *testing.T) { |
| 98 | fg := NewFlowsAndGroups() |
| 99 | var fa *FlowArgs |
| 100 | var ga *GroupArgs |
| 101 | |
| 102 | fa = &FlowArgs{ |
| 103 | KV: OfpFlowModArgs{"priority": 500}, |
| 104 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 105 | InPort(2), |
| 106 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 107 | }, |
| 108 | Actions: []*ofp.OfpAction{ |
| 109 | SetField(VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 10)), |
| 110 | Output(1), |
| 111 | }, |
| 112 | } |
| 113 | flow := MkFlowStat(fa) |
| 114 | fg.AddFlow(flow) |
| 115 | |
| 116 | ga = &GroupArgs{ |
| 117 | GroupId: 10, |
| 118 | Buckets: []*ofp.OfpBucket{ |
| 119 | {Actions: []*ofp.OfpAction{ |
| 120 | PopVlan(), |
| 121 | Output(1), |
| 122 | }, |
| 123 | }, |
| 124 | }, |
| 125 | } |
| 126 | group := MkGroupStat(ga) |
| 127 | fg.AddGroup(group) |
| 128 | |
| 129 | fgCopy := fg.Copy() |
| 130 | |
| 131 | allFlows := fgCopy.ListFlows() |
| 132 | assert.Equal(t, 1, len(allFlows)) |
| 133 | assert.True(t, FlowMatch(flow, allFlows[0])) |
| 134 | |
| 135 | allGroups := fgCopy.ListGroups() |
| 136 | assert.Equal(t, 1, len(allGroups)) |
| 137 | assert.Equal(t, ga.GroupId, allGroups[0].Desc.GroupId) |
| 138 | |
| 139 | fg = NewFlowsAndGroups() |
| 140 | fgCopy = fg.Copy() |
| 141 | allFlows = fgCopy.ListFlows() |
| 142 | allGroups = fgCopy.ListGroups() |
| 143 | assert.Equal(t, 0, len(allFlows)) |
| 144 | assert.Equal(t, 0, len(allGroups)) |
| 145 | } |
| 146 | |
| 147 | func TestFlowsAndGroups_GetFlow(t *testing.T) { |
| 148 | fg := NewFlowsAndGroups() |
| 149 | var fa1 *FlowArgs |
| 150 | var fa2 *FlowArgs |
| 151 | var ga *GroupArgs |
| 152 | |
| 153 | fa1 = &FlowArgs{ |
| 154 | KV: OfpFlowModArgs{"priority": 500}, |
| 155 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 156 | InPort(2), |
| 157 | Metadata_ofp((1000 << 32) | 1), |
| 158 | VlanPcp(0), |
| 159 | }, |
| 160 | Actions: []*ofp.OfpAction{ |
| 161 | PopVlan(), |
| 162 | }, |
| 163 | } |
| 164 | flow1 := MkFlowStat(fa1) |
| 165 | |
| 166 | fa2 = &FlowArgs{ |
| 167 | KV: OfpFlowModArgs{"priority": 1500}, |
| 168 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 169 | InPort(5), |
| 170 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 171 | }, |
| 172 | Actions: []*ofp.OfpAction{ |
| 173 | PushVlan(0x8100), |
| 174 | SetField(VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 1000)), |
| 175 | SetField(VlanPcp(0)), |
| 176 | Output(2), |
| 177 | }, |
| 178 | } |
| 179 | flow2 := MkFlowStat(fa2) |
| 180 | |
| 181 | fg.AddFlow(flow1) |
| 182 | fg.AddFlow(flow2) |
| 183 | |
| 184 | ga = &GroupArgs{ |
| 185 | GroupId: 10, |
| 186 | Buckets: []*ofp.OfpBucket{ |
| 187 | {Actions: []*ofp.OfpAction{ |
| 188 | PopVlan(), |
| 189 | Output(1), |
| 190 | }, |
| 191 | }, |
| 192 | }, |
| 193 | } |
| 194 | group := MkGroupStat(ga) |
| 195 | fg.AddGroup(group) |
| 196 | |
| 197 | gf1 := fg.GetFlow(0) |
| 198 | assert.True(t, FlowMatch(flow1, gf1)) |
| 199 | |
| 200 | gf2 := fg.GetFlow(1) |
| 201 | assert.True(t, FlowMatch(flow2, gf2)) |
| 202 | |
| 203 | gf3 := fg.GetFlow(2) |
| 204 | assert.Nil(t, gf3) |
| 205 | |
| 206 | allFlows := fg.ListFlows() |
| 207 | assert.True(t, FlowMatch(flow1, allFlows[0])) |
| 208 | assert.True(t, FlowMatch(flow2, allFlows[1])) |
| 209 | } |
| 210 | |
| 211 | func TestFlowsAndGroups_String(t *testing.T) { |
| 212 | fg := NewFlowsAndGroups() |
| 213 | var fa *FlowArgs |
| 214 | var ga *GroupArgs |
| 215 | |
| 216 | str := fg.String() |
| 217 | assert.True(t, str == "") |
| 218 | |
| 219 | fa = &FlowArgs{ |
| 220 | KV: OfpFlowModArgs{"priority": 500}, |
| 221 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 222 | InPort(2), |
| 223 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 224 | VlanPcp(0), |
| 225 | EthType(0x800), |
| 226 | Ipv4Dst(0xe00a0a0a), |
| 227 | }, |
| 228 | Actions: []*ofp.OfpAction{ |
| 229 | Group(10), |
| 230 | }, |
| 231 | } |
| 232 | flow := MkFlowStat(fa) |
| 233 | fg.AddFlow(flow) |
| 234 | |
| 235 | ga = &GroupArgs{ |
| 236 | GroupId: 10, |
| 237 | Buckets: []*ofp.OfpBucket{ |
| 238 | {Actions: []*ofp.OfpAction{ |
| 239 | PopVlan(), |
| 240 | Output(1), |
| 241 | }, |
| 242 | }, |
| 243 | }, |
| 244 | } |
| 245 | group := MkGroupStat(ga) |
| 246 | fg.AddGroup(group) |
| 247 | |
| 248 | str = fg.String() |
| 249 | assert.True(t, strings.Contains(str, "id: 1143307409938767207")) |
| 250 | assert.True(t, strings.Contains(str, "group_id: 10")) |
| 251 | assert.True(t, strings.Contains(str, "oxm_class: OFPXMC_OPENFLOW_BASICOFPXMC_OPENFLOW_BASIC")) |
| 252 | assert.True(t, strings.Contains(str, "type: OFPXMT_OFB_VLAN_VIDOFPXMT_OFB_VLAN_VID")) |
| 253 | assert.True(t, strings.Contains(str, "vlan_vid: 4096")) |
| 254 | assert.True(t, strings.Contains(str, "buckets:")) |
| 255 | } |
| 256 | |
| 257 | func TestFlowsAndGroups_AddFrom(t *testing.T) { |
| 258 | fg := NewFlowsAndGroups() |
| 259 | var fa *FlowArgs |
| 260 | var ga *GroupArgs |
| 261 | |
| 262 | str := fg.String() |
| 263 | assert.True(t, str == "") |
| 264 | |
| 265 | fa = &FlowArgs{ |
| 266 | KV: OfpFlowModArgs{"priority": 500}, |
| 267 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 268 | InPort(2), |
| 269 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 270 | Metadata_ofp(1000), |
| 271 | TunnelId(uint64(1)), |
| 272 | VlanPcp(0), |
| 273 | }, |
| 274 | Actions: []*ofp.OfpAction{ |
| 275 | PopVlan(), |
| 276 | Output(1), |
| 277 | }, |
| 278 | } |
| 279 | flow := MkFlowStat(fa) |
| 280 | fg.AddFlow(flow) |
| 281 | |
| 282 | ga = &GroupArgs{ |
| 283 | GroupId: 10, |
| 284 | Buckets: []*ofp.OfpBucket{ |
| 285 | {Actions: []*ofp.OfpAction{ |
| 286 | PopVlan(), |
| 287 | Output(1), |
| 288 | }, |
| 289 | }, |
| 290 | }, |
| 291 | } |
| 292 | group := MkGroupStat(ga) |
| 293 | fg.AddGroup(group) |
| 294 | |
| 295 | fg1 := NewFlowsAndGroups() |
| 296 | fg1.AddFrom(fg) |
| 297 | |
| 298 | allFlows := fg1.ListFlows() |
| 299 | allGroups := fg1.ListGroups() |
| 300 | assert.Equal(t, 1, len(allFlows)) |
| 301 | assert.Equal(t, 1, len(allGroups)) |
| 302 | assert.True(t, FlowMatch(flow, allFlows[0])) |
| 303 | assert.Equal(t, group.Desc.GroupId, allGroups[0].Desc.GroupId) |
| 304 | } |
| 305 | |
| 306 | func TestDeviceRules_AddFlow(t *testing.T) { |
| 307 | dr := NewDeviceRules() |
| 308 | rules := dr.GetRules() |
| 309 | assert.True(t, len(rules) == 0) |
| 310 | |
| 311 | dr.AddFlow("123456", nil) |
| 312 | rules = dr.GetRules() |
| 313 | assert.True(t, len(rules) == 1) |
| 314 | val, ok := rules["123456"] |
| 315 | assert.True(t, ok) |
| 316 | assert.Equal(t, 0, len(val.ListFlows())) |
| 317 | assert.Equal(t, 0, len(val.ListGroups())) |
| 318 | |
| 319 | var fa *FlowArgs |
| 320 | fa = &FlowArgs{ |
| 321 | KV: OfpFlowModArgs{"priority": 500}, |
| 322 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 323 | InPort(2), |
| 324 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 325 | Metadata_ofp(1000), |
| 326 | TunnelId(uint64(1)), |
| 327 | VlanPcp(0), |
| 328 | }, |
| 329 | Actions: []*ofp.OfpAction{ |
| 330 | PopVlan(), |
| 331 | Output(1), |
| 332 | }, |
| 333 | } |
| 334 | flow := MkFlowStat(fa) |
| 335 | dr.AddFlow("123456", flow) |
| 336 | rules = dr.GetRules() |
| 337 | assert.True(t, len(rules) == 1) |
| 338 | val, ok = rules["123456"] |
| 339 | assert.True(t, ok) |
| 340 | assert.Equal(t, 1, len(val.ListFlows())) |
| 341 | assert.True(t, FlowMatch(flow, val.ListFlows()[0])) |
| 342 | assert.Equal(t, 0, len(val.ListGroups())) |
| 343 | } |
| 344 | |
| 345 | func TestDeviceRules_AddFlowsAndGroup(t *testing.T) { |
| 346 | fg := NewFlowsAndGroups() |
| 347 | var fa *FlowArgs |
| 348 | var ga *GroupArgs |
| 349 | |
| 350 | str := fg.String() |
| 351 | assert.True(t, str == "") |
| 352 | |
| 353 | fa = &FlowArgs{ |
| 354 | KV: OfpFlowModArgs{"priority": 2000}, |
| 355 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 356 | InPort(2), |
| 357 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 358 | Metadata_ofp(1000), |
| 359 | TunnelId(uint64(1)), |
| 360 | VlanPcp(0), |
| 361 | }, |
| 362 | Actions: []*ofp.OfpAction{ |
| 363 | PopVlan(), |
| 364 | Output(1), |
| 365 | }, |
| 366 | } |
| 367 | flow := MkFlowStat(fa) |
| 368 | fg.AddFlow(flow) |
| 369 | |
| 370 | ga = &GroupArgs{ |
| 371 | GroupId: 10, |
| 372 | Buckets: []*ofp.OfpBucket{ |
| 373 | {Actions: []*ofp.OfpAction{ |
| 374 | PopVlan(), |
| 375 | Output(1), |
| 376 | }, |
| 377 | }, |
| 378 | }, |
| 379 | } |
| 380 | group := MkGroupStat(ga) |
| 381 | fg.AddGroup(group) |
| 382 | |
| 383 | dr := NewDeviceRules() |
| 384 | dr.AddFlowsAndGroup("123456", fg) |
| 385 | rules := dr.GetRules() |
| 386 | assert.True(t, len(rules) == 1) |
| 387 | val, ok := rules["123456"] |
| 388 | assert.True(t, ok) |
| 389 | assert.Equal(t, 1, len(val.ListFlows())) |
| 390 | assert.Equal(t, 1, len(val.ListGroups())) |
| 391 | assert.True(t, FlowMatch(flow, val.ListFlows()[0])) |
| 392 | assert.Equal(t, 10, int(val.ListGroups()[0].Desc.GroupId)) |
| 393 | } |
| 394 | |
| 395 | func TestFlowHasOutPort(t *testing.T) { |
| 396 | var flow *ofp.OfpFlowStats |
| 397 | assert.False(t, FlowHasOutPort(flow, 1)) |
| 398 | |
| 399 | fa := &FlowArgs{ |
| 400 | KV: OfpFlowModArgs{"priority": 2000}, |
| 401 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 402 | InPort(2), |
| 403 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 404 | Metadata_ofp(1000), |
| 405 | TunnelId(uint64(1)), |
| 406 | VlanPcp(0), |
| 407 | }, |
| 408 | Actions: []*ofp.OfpAction{ |
| 409 | PopVlan(), |
| 410 | Output(1), |
| 411 | }, |
| 412 | } |
| 413 | flow = MkFlowStat(fa) |
| 414 | assert.True(t, FlowHasOutPort(flow, 1)) |
| 415 | assert.False(t, FlowHasOutPort(flow, 2)) |
| 416 | |
| 417 | fa = &FlowArgs{ |
| 418 | KV: OfpFlowModArgs{"priority": 2000}, |
| 419 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 420 | InPort(2), |
| 421 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 422 | }, |
| 423 | } |
| 424 | flow = MkFlowStat(fa) |
| 425 | assert.False(t, FlowHasOutPort(flow, 1)) |
| 426 | } |
| 427 | |
| 428 | func TestFlowHasOutGroup(t *testing.T) { |
| 429 | var flow *ofp.OfpFlowStats |
| 430 | assert.False(t, FlowHasOutGroup(flow, 10)) |
| 431 | |
| 432 | fa := &FlowArgs{ |
| 433 | KV: OfpFlowModArgs{"priority": 500}, |
| 434 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 435 | InPort(2), |
| 436 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 437 | VlanPcp(0), |
| 438 | EthType(0x800), |
| 439 | Ipv4Dst(0xe00a0a0a), |
| 440 | }, |
| 441 | Actions: []*ofp.OfpAction{ |
| 442 | Group(10), |
| 443 | }, |
| 444 | } |
| 445 | flow = MkFlowStat(fa) |
| 446 | assert.True(t, FlowHasOutGroup(flow, 10)) |
| 447 | assert.False(t, FlowHasOutGroup(flow, 11)) |
| 448 | |
| 449 | fa = &FlowArgs{ |
| 450 | KV: OfpFlowModArgs{"priority": 500}, |
| 451 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 452 | InPort(2), |
| 453 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 454 | VlanPcp(0), |
| 455 | EthType(0x800), |
| 456 | Ipv4Dst(0xe00a0a0a), |
| 457 | }, |
| 458 | Actions: []*ofp.OfpAction{ |
| 459 | Output(1), |
| 460 | }, |
| 461 | } |
| 462 | flow = MkFlowStat(fa) |
| 463 | assert.False(t, FlowHasOutGroup(flow, 1)) |
| 464 | } |