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 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 465 | |
| 466 | func TestMatchFlow(t *testing.T) { |
| 467 | assert.False(t, FlowMatch(nil, nil)) |
| 468 | fa := &FlowArgs{ |
| 469 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 470 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 471 | InPort(2), |
| 472 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 473 | VlanPcp(0), |
| 474 | EthType(0x800), |
| 475 | Ipv4Dst(0xe00a0a0a), |
| 476 | }, |
| 477 | Actions: []*ofp.OfpAction{ |
| 478 | Group(10), |
| 479 | }, |
| 480 | } |
| 481 | flow1 := MkFlowStat(fa) |
| 482 | assert.False(t, FlowMatch(flow1, nil)) |
| 483 | |
| 484 | fa = &FlowArgs{ |
| 485 | KV: OfpFlowModArgs{"priority": 500}, |
| 486 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 487 | InPort(2), |
| 488 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 489 | VlanPcp(0), |
| 490 | EthType(0x800), |
| 491 | Ipv4Dst(0xe00a0a0a), |
| 492 | }, |
| 493 | Actions: []*ofp.OfpAction{ |
| 494 | Group(10), |
| 495 | }, |
| 496 | } |
| 497 | flow2 := MkFlowStat(fa) |
| 498 | assert.False(t, FlowMatch(flow1, flow2)) |
| 499 | assert.False(t, FlowMatch(nil, flow2)) |
| 500 | |
| 501 | fa = &FlowArgs{ |
| 502 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 503 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 504 | InPort(2), |
| 505 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 506 | VlanPcp(0), |
| 507 | EthType(0x800), |
| 508 | Ipv4Dst(0xe00a0a0a), |
| 509 | }, |
| 510 | } |
| 511 | flow2 = MkFlowStat(fa) |
| 512 | assert.True(t, FlowMatch(flow1, flow2)) |
| 513 | |
| 514 | fa = &FlowArgs{ |
| 515 | KV: OfpFlowModArgs{"priority": 501, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 516 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 517 | InPort(2), |
| 518 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 519 | VlanPcp(0), |
| 520 | EthType(0x800), |
| 521 | Ipv4Dst(0xe00a0a0a), |
| 522 | }, |
| 523 | Actions: []*ofp.OfpAction{ |
| 524 | Group(10), |
| 525 | }, |
| 526 | } |
| 527 | flow2 = MkFlowStat(fa) |
| 528 | assert.False(t, FlowMatch(flow1, flow2)) |
| 529 | |
| 530 | fa = &FlowArgs{ |
| 531 | KV: OfpFlowModArgs{"priority": 500, "table_id": 2, "cookie": 38268468, "flags": 12}, |
| 532 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 533 | InPort(2), |
| 534 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 535 | VlanPcp(0), |
| 536 | EthType(0x800), |
| 537 | Ipv4Dst(0xe00a0a0a), |
| 538 | }, |
| 539 | } |
| 540 | flow2 = MkFlowStat(fa) |
| 541 | assert.False(t, FlowMatch(flow1, flow2)) |
| 542 | |
| 543 | fa = &FlowArgs{ |
| 544 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268467, "flags": 12}, |
| 545 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 546 | InPort(2), |
| 547 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 548 | VlanPcp(0), |
| 549 | EthType(0x800), |
| 550 | Ipv4Dst(0xe00a0a0a), |
| 551 | }, |
| 552 | } |
| 553 | flow2 = MkFlowStat(fa) |
| 554 | assert.False(t, FlowMatch(flow1, flow2)) |
| 555 | |
| 556 | fa = &FlowArgs{ |
| 557 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 14}, |
| 558 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 559 | InPort(2), |
| 560 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 561 | VlanPcp(0), |
| 562 | EthType(0x800), |
| 563 | Ipv4Dst(0xe00a0a0a), |
| 564 | }, |
| 565 | } |
| 566 | flow2 = MkFlowStat(fa) |
| 567 | assert.False(t, FlowMatch(flow1, flow2)) |
| 568 | |
| 569 | fa = &FlowArgs{ |
| 570 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 571 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 572 | InPort(4), |
| 573 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 574 | VlanPcp(0), |
| 575 | EthType(0x800), |
| 576 | Ipv4Dst(0xe00a0a0a), |
| 577 | }, |
| 578 | } |
| 579 | flow2 = MkFlowStat(fa) |
| 580 | assert.False(t, FlowMatch(flow1, flow2)) |
| 581 | |
| 582 | fa = &FlowArgs{ |
| 583 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 584 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 585 | InPort(2), |
| 586 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 587 | VlanPcp(0), |
| 588 | EthType(0x800), |
| 589 | }, |
| 590 | } |
| 591 | flow2 = MkFlowStat(fa) |
| 592 | assert.False(t, FlowMatch(flow1, flow2)) |
| 593 | |
| 594 | fa = &FlowArgs{ |
| 595 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 596 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 597 | InPort(2), |
| 598 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 599 | VlanPcp(0), |
| 600 | EthType(0x800), |
| 601 | Ipv4Dst(0xe00a0a0a), |
| 602 | }, |
| 603 | Actions: []*ofp.OfpAction{ |
| 604 | PopVlan(), |
| 605 | Output(1), |
| 606 | }, |
| 607 | } |
| 608 | flow2 = MkFlowStat(fa) |
| 609 | assert.True(t, FlowMatch(flow1, flow2)) |
| 610 | } |
| 611 | |
| 612 | func TestFlowMatchesMod(t *testing.T) { |
| 613 | assert.False(t, FlowMatchesMod(nil, nil)) |
| 614 | fa := &FlowArgs{ |
| 615 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1}, |
| 616 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 617 | InPort(2), |
| 618 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 619 | VlanPcp(0), |
| 620 | EthType(0x800), |
| 621 | Ipv4Dst(0xe00a0a0a), |
| 622 | }, |
| 623 | Actions: []*ofp.OfpAction{ |
| 624 | Output(1), |
| 625 | Group(10), |
| 626 | }, |
| 627 | } |
| 628 | flow := MkFlowStat(fa) |
| 629 | assert.False(t, FlowMatchesMod(flow, nil)) |
| 630 | |
| 631 | fa = &FlowArgs{ |
| 632 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1}, |
| 633 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 634 | InPort(2), |
| 635 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 636 | VlanPcp(0), |
| 637 | EthType(0x800), |
| 638 | Ipv4Dst(0xe00a0a0a), |
| 639 | }, |
| 640 | Actions: []*ofp.OfpAction{ |
| 641 | PopVlan(), |
| 642 | Output(1), |
| 643 | }, |
| 644 | } |
| 645 | flowMod := MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV) |
| 646 | assert.False(t, FlowMatchesMod(nil, flowMod)) |
| 647 | assert.False(t, FlowMatchesMod(flow, flowMod)) |
| 648 | assert.True(t, FlowMatch(flow, FlowStatsEntryFromFlowModMessage(flowMod))) |
| 649 | |
| 650 | fa = &FlowArgs{ |
| 651 | KV: OfpFlowModArgs{"table_id": uint64(ofp.OfpTable_OFPTT_ALL), |
| 652 | "cookie_mask": 0, |
| 653 | "out_port": uint64(ofp.OfpPortNo_OFPP_ANY), |
| 654 | "out_group": uint64(ofp.OfpGroup_OFPG_ANY), |
| 655 | }, |
| 656 | } |
| 657 | flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV) |
| 658 | assert.True(t, FlowMatchesMod(flow, flowMod)) |
| 659 | |
| 660 | fa = &FlowArgs{ |
| 661 | KV: OfpFlowModArgs{"table_id": 1, |
| 662 | "cookie_mask": 0, |
| 663 | "out_port": uint64(ofp.OfpPortNo_OFPP_ANY), |
| 664 | "out_group": uint64(ofp.OfpGroup_OFPG_ANY), |
| 665 | }, |
| 666 | } |
| 667 | flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV) |
| 668 | assert.True(t, FlowMatchesMod(flow, flowMod)) |
| 669 | |
| 670 | fa = &FlowArgs{ |
| 671 | KV: OfpFlowModArgs{"table_id": 1, |
| 672 | "cookie_mask": 0, |
| 673 | "out_port": 1, |
| 674 | "out_group": uint64(ofp.OfpGroup_OFPG_ANY), |
| 675 | }, |
| 676 | } |
| 677 | flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV) |
| 678 | assert.True(t, FlowMatchesMod(flow, flowMod)) |
| 679 | |
| 680 | fa = &FlowArgs{ |
| 681 | KV: OfpFlowModArgs{"table_id": 1, |
| 682 | "cookie_mask": 0, |
| 683 | "out_port": 1, |
| 684 | "out_group": 10, |
| 685 | }, |
| 686 | } |
| 687 | flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV) |
| 688 | assert.True(t, FlowMatchesMod(flow, flowMod)) |
| 689 | } |