Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | |
Carmelo Cascone | 36d5e7a | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 17 | package org.onosproject.pipelines.fabric.impl.behaviour; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.MacAddress; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 23 | import org.onlab.packet.MplsLabel; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onlab.util.ImmutableByteSequence; |
| 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 28 | import org.onosproject.net.flow.TrafficTreatment; |
| 29 | import org.onosproject.net.pi.runtime.PiAction; |
| 30 | import org.onosproject.net.pi.runtime.PiActionParam; |
Carmelo Cascone | 8f6b5cd | 2020-06-30 18:56:12 -0700 | [diff] [blame] | 31 | import org.onosproject.pipelines.fabric.FabricConstants; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 32 | |
Daniele Moro | 77ef35c | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 33 | import static org.easymock.EasyMock.createNiceMock; |
| 34 | import static org.easymock.EasyMock.expect; |
| 35 | import static org.easymock.EasyMock.replay; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 36 | import static org.junit.Assert.assertEquals; |
| 37 | |
| 38 | /** |
| 39 | * Test for fabric interpreter. |
| 40 | */ |
| 41 | public class FabricInterpreterTest { |
| 42 | private static final VlanId VLAN_100 = VlanId.vlanId("100"); |
| 43 | private static final PortNumber PORT_1 = PortNumber.portNumber(1); |
| 44 | private static final MacAddress SRC_MAC = MacAddress.valueOf("00:00:00:00:00:01"); |
| 45 | private static final MacAddress DST_MAC = MacAddress.valueOf("00:00:00:00:00:02"); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 46 | private static final MplsLabel MPLS_10 = MplsLabel.mplsLabel(10); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 47 | |
| 48 | private FabricInterpreter interpreter; |
| 49 | |
Daniele Moro | 77ef35c | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 50 | FabricCapabilities allCapabilities; |
| 51 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 52 | @Before |
| 53 | public void setup() { |
Daniele Moro | 77ef35c | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 54 | allCapabilities = createNiceMock(FabricCapabilities.class); |
| 55 | expect(allCapabilities.hasHashedTable()).andReturn(true).anyTimes(); |
| 56 | expect(allCapabilities.supportDoubleVlanTerm()).andReturn(true).anyTimes(); |
| 57 | replay(allCapabilities); |
| 58 | interpreter = new FabricInterpreter(allCapabilities); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /* Filtering control block */ |
| 62 | |
| 63 | /** |
| 64 | * Map treatment to push_internal_vlan action. |
| 65 | */ |
| 66 | @Test |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 67 | public void testFilteringTreatmentPermitWithInternalVlan() throws Exception { |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 68 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 69 | .pushVlan() |
| 70 | .setVlanId(VLAN_100) |
| 71 | .build(); |
| 72 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
Carmelo Cascone | 36d5e7a | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 73 | FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 74 | PiActionParam param = new PiActionParam(FabricConstants.VLAN_ID, |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 75 | ImmutableByteSequence.copyFrom(VLAN_100.toShort())); |
| 76 | PiAction expectedAction = PiAction.builder() |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 77 | .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT_WITH_INTERNAL_VLAN) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 78 | .withParameter(param) |
| 79 | .build(); |
| 80 | |
| 81 | assertEquals(expectedAction, mappedAction); |
| 82 | } |
| 83 | |
| 84 | /** |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 85 | * Map treatment to permit action. |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 86 | */ |
| 87 | @Test |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 88 | public void testFilteringTreatmentPermit() throws Exception { |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 89 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 90 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 91 | FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 92 | PiAction expectedAction = PiAction.builder() |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 93 | .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 94 | .build(); |
| 95 | |
| 96 | assertEquals(expectedAction, mappedAction); |
| 97 | } |
| 98 | |
| 99 | /* Forwarding control block */ |
| 100 | |
| 101 | /** |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 102 | * Map empty treatment for routing v4 table. |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 103 | */ |
| 104 | @Test |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 105 | public void testRoutingV4TreatmentEmpty() throws Exception { |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 106 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 107 | PiAction mappedAction = interpreter.mapTreatment( |
| 108 | treatment, FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V4); |
Charles Chan | cf696e5 | 2018-08-16 16:25:13 -0700 | [diff] [blame] | 109 | PiAction expectedAction = PiAction.builder() |
Charles Chan | cd03f07 | 2018-08-31 17:46:37 -0700 | [diff] [blame] | 110 | .withId(FabricConstants.FABRIC_INGRESS_FORWARDING_NOP_ROUTING_V4) |
Charles Chan | cf696e5 | 2018-08-16 16:25:13 -0700 | [diff] [blame] | 111 | .build(); |
| 112 | assertEquals(expectedAction, mappedAction); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 113 | } |
Charles Chan | cf696e5 | 2018-08-16 16:25:13 -0700 | [diff] [blame] | 114 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 115 | /** |
| 116 | * Map empty treatment for ACL table. |
| 117 | */ |
| 118 | @Test |
| 119 | public void testAclTreatmentEmpty() throws Exception { |
| 120 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 121 | PiAction mappedAction = interpreter.mapTreatment( |
| 122 | treatment, FabricConstants.FABRIC_INGRESS_ACL_ACL); |
| 123 | PiAction expectedAction = PiAction.builder() |
| 124 | .withId(FabricConstants.FABRIC_INGRESS_ACL_NOP_ACL) |
Charles Chan | cf696e5 | 2018-08-16 16:25:13 -0700 | [diff] [blame] | 125 | .build(); |
| 126 | assertEquals(expectedAction, mappedAction); |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 129 | /* Next control block */ |
| 130 | |
| 131 | /** |
| 132 | * Map treatment to output action. |
| 133 | */ |
| 134 | @Test |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 135 | public void testNextTreatmentSimpleOutput() throws Exception { |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 136 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 137 | .setOutput(PORT_1) |
| 138 | .build(); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 139 | PiAction mappedAction = interpreter.mapTreatment( |
| 140 | treatment, FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE); |
| 141 | PiActionParam param = new PiActionParam(FabricConstants.PORT_NUM, PORT_1.toLong()); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 142 | PiAction expectedAction = PiAction.builder() |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 143 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_OUTPUT_SIMPLE) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 144 | .withParameter(param) |
| 145 | .build(); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 146 | assertEquals(expectedAction, mappedAction); |
| 147 | } |
| 148 | |
| 149 | /** |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 150 | * Map treatment for hashed table to routing v4 action. |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 151 | */ |
| 152 | @Test |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 153 | public void testNextTreatmentHashedRoutingV4() throws Exception { |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 154 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 155 | .setEthSrc(SRC_MAC) |
| 156 | .setEthDst(DST_MAC) |
| 157 | .setOutput(PORT_1) |
| 158 | .build(); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 159 | PiAction mappedAction = interpreter.mapTreatment( |
| 160 | treatment, FabricConstants.FABRIC_INGRESS_NEXT_HASHED); |
| 161 | PiActionParam ethSrcParam = new PiActionParam(FabricConstants.SMAC, SRC_MAC.toBytes()); |
| 162 | PiActionParam ethDstParam = new PiActionParam(FabricConstants.DMAC, DST_MAC.toBytes()); |
| 163 | PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM, PORT_1.toLong()); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 164 | PiAction expectedAction = PiAction.builder() |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 165 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_ROUTING_HASHED) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 166 | .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam)) |
| 167 | .build(); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 168 | assertEquals(expectedAction, mappedAction); |
| 169 | } |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 170 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 171 | /** |
| 172 | * Map treatment for hashed table to routing v4 action. |
| 173 | */ |
| 174 | @Test |
| 175 | public void testNextTreatmentHashedRoutingMpls() throws Exception { |
| 176 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 177 | .setEthSrc(SRC_MAC) |
| 178 | .setEthDst(DST_MAC) |
| 179 | .setOutput(PORT_1) |
| 180 | .pushMpls() |
| 181 | .setMpls(MPLS_10) |
| 182 | .build(); |
| 183 | PiAction mappedAction = interpreter.mapTreatment( |
| 184 | treatment, FabricConstants.FABRIC_INGRESS_NEXT_HASHED); |
| 185 | PiActionParam ethSrcParam = new PiActionParam(FabricConstants.SMAC, SRC_MAC.toBytes()); |
| 186 | PiActionParam ethDstParam = new PiActionParam(FabricConstants.DMAC, DST_MAC.toBytes()); |
| 187 | PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM, PORT_1.toLong()); |
| 188 | PiActionParam mplsParam = new PiActionParam(FabricConstants.LABEL, MPLS_10.toInt()); |
| 189 | PiAction expectedAction = PiAction.builder() |
| 190 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_MPLS_ROUTING_HASHED) |
| 191 | .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam, mplsParam)) |
| 192 | .build(); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 193 | assertEquals(expectedAction, mappedAction); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Map treatment to set_vlan_output action. |
| 198 | */ |
| 199 | @Test |
| 200 | public void testNextTreatment3() throws Exception { |
| 201 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 202 | .setVlanId(VLAN_100) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 203 | .build(); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 204 | PiAction mappedAction = interpreter.mapTreatment( |
| 205 | treatment, FabricConstants.FABRIC_INGRESS_NEXT_NEXT_VLAN); |
| 206 | PiActionParam vlanParam = new PiActionParam( |
| 207 | FabricConstants.VLAN_ID, VLAN_100.toShort()); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 208 | PiAction expectedAction = PiAction.builder() |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 209 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_VLAN) |
| 210 | .withParameter(vlanParam) |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 211 | .build(); |
| 212 | assertEquals(expectedAction, mappedAction); |
| 213 | } |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 214 | } |