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