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