Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 1 | /* |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 2 | * Copyright 2021-present Open Networking Foundation |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 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 | */ |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 16 | package org.onosproject.pipelines.fabric.impl.behaviour.pipeliner; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 17 | |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 18 | import org.easymock.Capture; |
| 19 | import org.easymock.CaptureType; |
| 20 | import org.junit.Before; |
Carmelo Cascone | 4164436 | 2018-08-09 16:56:43 -0700 | [diff] [blame] | 21 | import org.junit.Test; |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 22 | import org.onlab.packet.Ethernet; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 23 | import org.onosproject.TestApplicationId; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.PortNumber; |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 27 | import org.onosproject.net.flow.DefaultFlowRule; |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 28 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 29 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 30 | import org.onosproject.net.flow.FlowRule; |
| 31 | import org.onosproject.net.flow.FlowRuleService; |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.TrafficSelector; |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 33 | import org.onosproject.net.flow.TrafficTreatment; |
| 34 | import org.onosproject.net.flow.criteria.Criteria; |
| 35 | import org.onosproject.net.flow.criteria.PiCriterion; |
| 36 | import org.onosproject.net.pi.runtime.PiAction; |
| 37 | import org.onosproject.net.pi.runtime.PiActionParam; |
| 38 | import org.onosproject.pipelines.fabric.FabricConstants; |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 39 | import org.onosproject.pipelines.fabric.impl.behaviour.FabricCapabilities; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 40 | |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 41 | import java.io.IOException; |
| 42 | import java.util.Optional; |
| 43 | |
| 44 | import static org.easymock.EasyMock.capture; |
| 45 | import static org.easymock.EasyMock.createMock; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 46 | import static org.easymock.EasyMock.expect; |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 47 | import static org.easymock.EasyMock.newCapture; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 48 | import static org.easymock.EasyMock.replay; |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 49 | import static org.easymock.EasyMock.reset; |
| 50 | import static org.easymock.EasyMock.verify; |
| 51 | import static org.junit.Assert.assertTrue; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 52 | |
Carmelo Cascone | 4164436 | 2018-08-09 16:56:43 -0700 | [diff] [blame] | 53 | public class FabricPipelinerTest { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 54 | |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 55 | private static final ApplicationId APP_ID = TestApplicationId.create("FabricPipelinerTest"); |
| 56 | private static final DeviceId DEVICE_ID = DeviceId.deviceId("device:1"); |
| 57 | private static final int DEFAULT_FLOW_PRIORITY = 100; |
| 58 | private static final int CPU_PORT = 320; |
| 59 | private static final byte FWD_IPV4_ROUTING = 2; |
| 60 | private static final int DEFAULT_VLAN = 4094; |
| 61 | public static final byte[] ONE = new byte[]{1}; |
| 62 | public static final byte[] ZERO = new byte[]{0}; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 63 | |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 64 | private FabricPipeliner pipeliner; |
| 65 | private FlowRuleService flowRuleService; |
| 66 | |
| 67 | @Before |
| 68 | public void setup() throws IOException { |
| 69 | FabricCapabilities capabilities = createMock(FabricCapabilities.class); |
| 70 | expect(capabilities.cpuPort()).andReturn(Optional.of(CPU_PORT)).anyTimes(); |
| 71 | replay(capabilities); |
| 72 | |
| 73 | // Services mock |
| 74 | flowRuleService = createMock(FlowRuleService.class); |
| 75 | |
| 76 | pipeliner = new FabricPipeliner(capabilities); |
| 77 | pipeliner.flowRuleService = flowRuleService; |
| 78 | pipeliner.appId = APP_ID; |
| 79 | pipeliner.deviceId = DEVICE_ID; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 80 | } |
Carmelo Cascone | 4164436 | 2018-08-09 16:56:43 -0700 | [diff] [blame] | 81 | |
| 82 | @Test |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 83 | public void testInitializePipeline() { |
| 84 | final Capture<FlowRule> capturedCpuIgVlanRule = newCapture(CaptureType.ALL); |
| 85 | final Capture<FlowRule> capturedCpuFwdClsRule = newCapture(CaptureType.ALL); |
| 86 | |
| 87 | // ingress_port_vlan table for cpu port |
| 88 | final TrafficSelector cpuIgVlanSelector = DefaultTrafficSelector.builder() |
| 89 | .add(Criteria.matchInPort(PortNumber.portNumber(CPU_PORT))) |
| 90 | .add(PiCriterion.builder() |
| 91 | .matchExact(FabricConstants.HDR_VLAN_IS_VALID, ZERO) |
| 92 | .build()) |
| 93 | .build(); |
| 94 | final TrafficTreatment cpuIgVlanTreatment = DefaultTrafficTreatment.builder() |
| 95 | .piTableAction(PiAction.builder() |
| 96 | .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT_WITH_INTERNAL_VLAN) |
| 97 | .withParameter(new PiActionParam(FabricConstants.VLAN_ID, DEFAULT_VLAN)) |
| 98 | .build()) |
| 99 | .build(); |
| 100 | final FlowRule expectedCpuIgVlanRule = DefaultFlowRule.builder() |
| 101 | .withSelector(cpuIgVlanSelector) |
| 102 | .withTreatment(cpuIgVlanTreatment) |
| 103 | .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN) |
| 104 | .makePermanent() |
| 105 | .withPriority(DEFAULT_FLOW_PRIORITY) |
| 106 | .forDevice(DEVICE_ID) |
| 107 | .fromApp(APP_ID) |
| 108 | .build(); |
| 109 | |
| 110 | final TrafficSelector cpuFwdClsSelector = DefaultTrafficSelector.builder() |
| 111 | .matchInPort(PortNumber.portNumber(CPU_PORT)) |
| 112 | .matchPi(PiCriterion.builder() |
| 113 | .matchExact(FabricConstants.HDR_IP_ETH_TYPE, Ethernet.TYPE_IPV4) |
| 114 | .build()) |
| 115 | .build(); |
| 116 | final TrafficTreatment cpuFwdClsTreatment = DefaultTrafficTreatment.builder() |
| 117 | .piTableAction(PiAction.builder() |
| 118 | .withId(FabricConstants.FABRIC_INGRESS_FILTERING_SET_FORWARDING_TYPE) |
| 119 | .withParameter(new PiActionParam(FabricConstants.FWD_TYPE, FWD_IPV4_ROUTING)) |
| 120 | .build()) |
| 121 | .build(); |
| 122 | final FlowRule expectedCpuFwdClsRule = DefaultFlowRule.builder() |
| 123 | .withSelector(cpuFwdClsSelector) |
| 124 | .withTreatment(cpuFwdClsTreatment) |
| 125 | .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER) |
| 126 | .makePermanent() |
| 127 | .withPriority(DEFAULT_FLOW_PRIORITY) |
| 128 | .forDevice(DEVICE_ID) |
| 129 | .fromApp(APP_ID) |
| 130 | .build(); |
| 131 | flowRuleService.applyFlowRules( |
| 132 | capture(capturedCpuIgVlanRule), |
| 133 | capture(capturedCpuFwdClsRule)); |
| 134 | |
| 135 | replay(flowRuleService); |
| 136 | pipeliner.initializePipeline(); |
| 137 | |
| 138 | assertTrue(expectedCpuIgVlanRule.exactMatch(capturedCpuIgVlanRule.getValue())); |
| 139 | assertTrue(expectedCpuFwdClsRule.exactMatch(capturedCpuFwdClsRule.getValue())); |
| 140 | |
| 141 | verify(flowRuleService); |
| 142 | reset(flowRuleService); |
Carmelo Cascone | 4164436 | 2018-08-09 16:56:43 -0700 | [diff] [blame] | 143 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 144 | } |