ssiddiqui | df20bf7 | 2021-08-23 14:00:56 +0530 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2021 the original author or authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package openflow |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "encoding/json" |
| 22 | "fmt" |
| 23 | "github.com/opencord/ofagent-go/internal/pkg/holder" |
| 24 | "github.com/opencord/ofagent-go/internal/pkg/mock" |
| 25 | "github.com/stretchr/testify/assert" |
| 26 | "net" |
| 27 | "testing" |
| 28 | |
| 29 | "github.com/opencord/goloxi" |
| 30 | "github.com/opencord/goloxi/of13" |
| 31 | ofp "github.com/opencord/goloxi/of13" |
| 32 | ) |
| 33 | |
| 34 | func TestOFConnection_handleFlowAdd(t *testing.T) { |
| 35 | ofc := &OFConnection{ |
| 36 | VolthaClient: &holder.VolthaServiceClientHolder{}, |
| 37 | } |
| 38 | ofc.VolthaClient.Set(mock.MockVolthaClient{}) |
| 39 | |
| 40 | callables := []func(t *testing.T) *ofp.FlowAdd{getUpstreamOnuFlow, getUpstreamOLTFlow, getOnuDownstreamFlow, |
| 41 | getOLTDownstreamSingleMplsTagFlow, getOLTDownstreamDoubleMplsTagFlow} |
| 42 | for _, callable := range callables { |
| 43 | ofc.handleFlowAdd(context.Background(), callable(t)) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func getUpstreamOnuFlow(t *testing.T) *ofp.FlowAdd { |
| 48 | flowAdd := &ofp.FlowAdd{ |
| 49 | FlowMod: &ofp.FlowMod{ |
| 50 | Header: &ofp.Header{ |
| 51 | Version: 0, |
| 52 | Type: ofp.OFPFCAdd, |
| 53 | Length: 0, |
| 54 | Xid: 10, |
| 55 | }, |
| 56 | Cookie: 114560316889735549, |
| 57 | TableId: 0, |
| 58 | Priority: 1000, |
| 59 | Match: ofp.Match{ |
| 60 | OxmList: []goloxi.IOxm{&of13.NxmInPort{ |
| 61 | Oxm: &of13.Oxm{ |
| 62 | TypeLen: 0, |
| 63 | }, |
| 64 | Value: 16, |
| 65 | }, |
| 66 | &of13.OxmVlanVid{ |
| 67 | Oxm: &of13.Oxm{ |
| 68 | TypeLen: 0, |
| 69 | }, |
| 70 | Value: 4096, |
| 71 | }}, |
| 72 | }, |
| 73 | Instructions: []ofp.IInstruction{ |
| 74 | &ofp.InstructionGotoTable{ |
| 75 | Instruction: &ofp.Instruction{ |
| 76 | Type: ofp.OFPITGotoTable, |
| 77 | Len: 8, |
| 78 | }, |
| 79 | TableId: 1, |
| 80 | }, |
| 81 | &ofp.InstructionMeter{ |
| 82 | Instruction: &ofp.Instruction{ |
| 83 | Type: ofp.OFPITMeter, |
| 84 | }, |
| 85 | MeterId: 1, |
| 86 | }, |
| 87 | }, |
| 88 | }, |
| 89 | } |
| 90 | flowAddJson, err := json.Marshal(flowAdd) |
| 91 | assert.NoError(t, err) |
| 92 | |
| 93 | fmt.Printf("Onu Flow Add Json: %s\n", flowAddJson) |
| 94 | return flowAdd |
| 95 | } |
| 96 | |
| 97 | func getUpstreamOLTFlow(t *testing.T) *ofp.FlowAdd { |
| 98 | flowAdd := &ofp.FlowAdd{ |
| 99 | FlowMod: &ofp.FlowMod{ |
| 100 | Header: &ofp.Header{ |
| 101 | Version: 0, |
| 102 | Type: ofp.OFPFCAdd, |
| 103 | Length: 0, |
| 104 | Xid: 10, |
| 105 | }, |
| 106 | Cookie: 114560316889735549, |
| 107 | TableId: 1, |
| 108 | Priority: 1000, |
| 109 | Match: ofp.Match{ |
| 110 | OxmList: []goloxi.IOxm{ |
| 111 | &of13.OxmInPort{ |
| 112 | Value: ofp.Port(16), |
| 113 | }, |
| 114 | &of13.OxmVlanVid{ |
| 115 | Oxm: &of13.Oxm{ |
| 116 | TypeLen: 0, |
| 117 | }, |
| 118 | Value: 4096, |
| 119 | }}, |
| 120 | }, |
| 121 | Instructions: []ofp.IInstruction{ |
| 122 | &ofp.InstructionMeter{ |
| 123 | Instruction: &ofp.Instruction{ |
| 124 | Type: ofp.OFPITMeter, |
| 125 | }, |
| 126 | MeterId: 1, |
| 127 | }, |
| 128 | }, |
| 129 | }, |
| 130 | } |
| 131 | |
| 132 | actions := &ofp.InstructionApplyActions{ |
| 133 | Instruction: &ofp.Instruction{ |
| 134 | Type: ofp.OFPITApplyActions, |
| 135 | }, |
| 136 | } |
| 137 | actions.Actions = append(actions.Actions, &ofp.ActionPushVlan{ |
| 138 | Action: &ofp.Action{ |
| 139 | Type: ofp.OFPATPushVLAN, |
| 140 | }, |
| 141 | Ethertype: 0x8100, |
| 142 | }, |
| 143 | &ofp.ActionPushMpls{ |
| 144 | Action: &ofp.Action{ |
| 145 | Type: ofp.OFPATPushMpls, |
| 146 | }, |
| 147 | Ethertype: 0x8847, |
| 148 | }, |
| 149 | &ofp.ActionSetField{ |
| 150 | Action: &ofp.Action{ |
| 151 | Type: ofp.OFPATSetField, |
| 152 | }, |
| 153 | Field: &ofp.OxmMplsLabel{ |
| 154 | Value: 10, |
| 155 | }, |
| 156 | }, |
| 157 | &ofp.ActionSetField{ |
| 158 | Action: &ofp.Action{ |
| 159 | Type: ofp.OFPATSetField, |
| 160 | }, |
| 161 | Field: &ofp.OxmMplsBos{ |
| 162 | Value: 1, |
| 163 | }, |
| 164 | }, |
| 165 | &ofp.ActionSetField{ |
| 166 | Action: &ofp.Action{ |
| 167 | Type: ofp.OFPATSetField, |
| 168 | }, |
| 169 | Field: &ofp.OxmEthSrc{ |
| 170 | Value: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee}, |
| 171 | }, |
| 172 | }, |
| 173 | &ofp.ActionSetField{ |
| 174 | Action: &ofp.Action{ |
| 175 | Type: ofp.OFPATSetField, |
| 176 | }, |
| 177 | Field: &ofp.OxmEthDst{ |
| 178 | Value: net.HardwareAddr{0xee, 0xdd, 0xcc, 0xbb, 0xaa}, |
| 179 | }, |
| 180 | }, |
| 181 | &ofp.ActionSetMplsTtl{ |
| 182 | Action: &ofp.Action{ |
| 183 | Type: ofp.OFPATSetMplsTtl, |
| 184 | }, |
| 185 | MplsTtl: 64, |
| 186 | }, |
| 187 | ) |
| 188 | |
| 189 | flowAdd.Instructions = append(flowAdd.Instructions, actions) |
| 190 | flowAddJson, err := json.Marshal(flowAdd) |
| 191 | assert.NoError(t, err) |
| 192 | |
| 193 | fmt.Printf("OLT Flow Add Json: %s\n", flowAddJson) |
| 194 | return flowAdd |
| 195 | } |
| 196 | |
| 197 | func getOnuDownstreamFlow(t *testing.T) *ofp.FlowAdd { |
| 198 | flowAdd := &ofp.FlowAdd{ |
| 199 | FlowMod: &ofp.FlowMod{ |
| 200 | Header: &ofp.Header{ |
| 201 | Version: 3, |
| 202 | Type: 0, |
| 203 | Xid: 0, |
| 204 | }, |
| 205 | Cookie: 114560316889735549, |
| 206 | TableId: 2, |
| 207 | Priority: 1000, |
| 208 | Flags: 0, |
| 209 | Match: ofp.Match{ |
| 210 | Type: 0, |
| 211 | Length: 0, |
| 212 | OxmList: []goloxi.IOxm{ |
| 213 | &of13.OxmInPort{ |
| 214 | Value: ofp.Port(65536), |
| 215 | }, |
| 216 | &of13.OxmVlanVid{ |
| 217 | Value: 4096, |
| 218 | }}, |
| 219 | }, |
| 220 | Instructions: []ofp.IInstruction{ |
| 221 | &ofp.InstructionMeter{ |
| 222 | Instruction: &ofp.Instruction{ |
| 223 | Type: ofp.OFPITMeter, |
| 224 | }, |
| 225 | MeterId: 1, |
| 226 | }, |
| 227 | }, |
| 228 | }, |
| 229 | } |
| 230 | |
| 231 | actions := &ofp.InstructionApplyActions{ |
| 232 | Instruction: &ofp.Instruction{ |
| 233 | Type: ofp.OFPITApplyActions, |
| 234 | }, |
| 235 | } |
| 236 | actions.Actions = append(actions.Actions, |
| 237 | &ofp.ActionOutput{ |
| 238 | Action: &ofp.Action{ |
| 239 | Type: ofp.OFPATOutput, |
| 240 | }, |
| 241 | Port: 16, |
| 242 | }) |
| 243 | |
| 244 | flowAdd.Instructions = append(flowAdd.Instructions, actions) |
| 245 | flowAddJson, err := json.Marshal(flowAdd) |
| 246 | assert.NoError(t, err) |
| 247 | |
| 248 | fmt.Printf("Onu Downstream Flow Add Json: %s\n", flowAddJson) |
| 249 | return flowAdd |
| 250 | |
| 251 | } |
| 252 | |
| 253 | func getOLTDownstreamSingleMplsTagFlow(t *testing.T) *ofp.FlowAdd { |
| 254 | flowAdd := &ofp.FlowAdd{ |
| 255 | FlowMod: &ofp.FlowMod{ |
| 256 | Header: &ofp.Header{ |
| 257 | Version: 3, |
| 258 | Type: 0, |
| 259 | Xid: 0, |
| 260 | }, |
| 261 | Cookie: 114560316889735549, |
| 262 | TableId: 0, |
| 263 | Priority: 1000, |
| 264 | Flags: 0, |
| 265 | Match: ofp.Match{ |
| 266 | OxmList: []goloxi.IOxm{ |
| 267 | &of13.OxmInPort{ |
| 268 | Value: ofp.Port(65536), |
| 269 | }, |
| 270 | &of13.OxmEthType{ |
| 271 | Value: ofp.EthernetType(0x8847), |
| 272 | }, |
| 273 | &of13.OxmMplsBos{ |
| 274 | Value: 1, |
| 275 | }, |
| 276 | &of13.OxmVlanVid{ |
| 277 | Oxm: &of13.Oxm{ |
| 278 | TypeLen: 0, |
| 279 | }, |
| 280 | Value: 4096, |
| 281 | }, |
| 282 | &of13.OxmEthSrc{ |
| 283 | Value: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd}, |
| 284 | }, |
| 285 | }, |
| 286 | }, |
| 287 | Instructions: []ofp.IInstruction{ |
| 288 | &ofp.InstructionGotoTable{ |
| 289 | Instruction: &ofp.Instruction{ |
| 290 | Type: ofp.OFPITGotoTable, |
| 291 | }, |
| 292 | TableId: 1, |
| 293 | }, |
| 294 | &ofp.InstructionMeter{ |
| 295 | Instruction: &ofp.Instruction{ |
| 296 | Type: ofp.OFPITMeter, |
| 297 | }, |
| 298 | MeterId: 1, |
| 299 | }, |
| 300 | }, |
| 301 | }, |
| 302 | } |
| 303 | |
| 304 | actions := &ofp.InstructionApplyActions{ |
| 305 | Instruction: &ofp.Instruction{ |
| 306 | Type: ofp.OFPITApplyActions, |
| 307 | }, |
| 308 | } |
| 309 | actions.Actions = append(actions.Actions, |
| 310 | &ofp.ActionOutput{ |
| 311 | Action: &ofp.Action{ |
| 312 | Type: ofp.OFPATOutput, |
| 313 | }, |
| 314 | Port: 16, |
| 315 | }, |
| 316 | &ofp.ActionDecMplsTtl{ |
| 317 | Action: &ofp.Action{ |
| 318 | Type: ofp.OFPATDecMplsTtl, |
| 319 | }, |
| 320 | }, |
| 321 | &ofp.ActionSetMplsTtl{ |
| 322 | Action: &ofp.Action{ |
| 323 | Type: ofp.OFPATSetMplsTtl, |
| 324 | }, |
| 325 | MplsTtl: 64, |
| 326 | }, |
| 327 | &ofp.ActionPopMpls{ |
| 328 | Action: &ofp.Action{ |
| 329 | Type: ofp.OFPATPopMpls, |
| 330 | }, |
| 331 | Ethertype: 0x8847, |
| 332 | }, |
| 333 | ) |
| 334 | |
| 335 | flowAdd.Instructions = append(flowAdd.Instructions, actions) |
| 336 | flowAddJson, err := json.Marshal(flowAdd) |
| 337 | assert.NoError(t, err) |
| 338 | |
| 339 | fmt.Printf("Olt Downstream (Single MPLS tag) Flow Add Json: %s\n", flowAddJson) |
| 340 | return flowAdd |
| 341 | } |
| 342 | |
| 343 | func getOLTDownstreamDoubleMplsTagFlow(t *testing.T) *ofp.FlowAdd { |
| 344 | flowAdd := &ofp.FlowAdd{ |
| 345 | FlowMod: &ofp.FlowMod{ |
| 346 | Header: &ofp.Header{ |
| 347 | Version: 3, |
| 348 | Type: 0, |
| 349 | Xid: 0, |
| 350 | }, |
| 351 | Cookie: 114560316889735549, |
| 352 | TableId: 0, |
| 353 | Priority: 1000, |
| 354 | Flags: 0, |
| 355 | Match: ofp.Match{ |
| 356 | OxmList: []goloxi.IOxm{ |
| 357 | &of13.OxmInPort{ |
| 358 | Value: ofp.Port(65536), |
| 359 | }, |
| 360 | &of13.OxmEthType{ |
| 361 | Oxm: &of13.Oxm{ |
| 362 | TypeLen: 0, |
| 363 | }, |
| 364 | Value: ofp.EthernetType(0x8847), |
| 365 | }, |
| 366 | &of13.OxmMplsBos{ |
| 367 | Value: 1, |
| 368 | }, |
| 369 | &of13.OxmVlanVid{ |
| 370 | Value: 4096, |
| 371 | }, |
| 372 | &of13.OxmEthSrc{ |
| 373 | Value: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd}, |
| 374 | }, |
| 375 | }, |
| 376 | }, |
| 377 | Instructions: []ofp.IInstruction{ |
| 378 | &ofp.InstructionGotoTable{ |
| 379 | Instruction: &ofp.Instruction{ |
| 380 | Type: ofp.OFPITGotoTable, |
| 381 | }, |
| 382 | TableId: 1, |
| 383 | }, |
| 384 | &ofp.InstructionMeter{ |
| 385 | Instruction: &ofp.Instruction{ |
| 386 | Type: ofp.OFPITMeter, |
| 387 | }, |
| 388 | MeterId: 1, |
| 389 | }, |
| 390 | }, |
| 391 | }, |
| 392 | } |
| 393 | |
| 394 | actions := &ofp.InstructionApplyActions{ |
| 395 | Instruction: &ofp.Instruction{ |
| 396 | Type: ofp.OFPITApplyActions, |
| 397 | }, |
| 398 | } |
| 399 | actions.Actions = append(actions.Actions, |
| 400 | &ofp.ActionOutput{ |
| 401 | Action: &ofp.Action{ |
| 402 | Type: ofp.OFPATOutput, |
| 403 | }, |
| 404 | Port: 16, |
| 405 | }, |
| 406 | &ofp.ActionDecMplsTtl{ |
| 407 | Action: &ofp.Action{ |
| 408 | Type: ofp.OFPATDecMplsTtl, |
| 409 | }, |
| 410 | }, |
| 411 | &ofp.ActionSetMplsTtl{ |
| 412 | Action: &ofp.Action{ |
| 413 | Type: ofp.OFPATSetMplsTtl, |
| 414 | }, |
| 415 | MplsTtl: 64, |
| 416 | }, |
| 417 | &ofp.ActionPopMpls{ |
| 418 | Action: &ofp.Action{ |
| 419 | Type: ofp.OFPATPopMpls, |
| 420 | }, |
| 421 | Ethertype: 0x8847, |
| 422 | }, |
| 423 | &ofp.ActionPopMpls{ |
| 424 | Action: &ofp.Action{ |
| 425 | Type: ofp.OFPATPopMpls, |
| 426 | }, |
| 427 | Ethertype: 0x8847, |
| 428 | }, |
| 429 | ) |
| 430 | |
| 431 | flowAdd.Instructions = append(flowAdd.Instructions, actions) |
| 432 | flowAddJson, err := json.Marshal(flowAdd) |
| 433 | assert.NoError(t, err) |
| 434 | fmt.Printf("Olt Downstream (Double MPLS tag) Flow Add Json: %s\n", flowAddJson) |
| 435 | return flowAdd |
| 436 | } |