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 | |
| 17 | package org.onosproject.pipelines.fabric; |
| 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 | |
| 32 | import static org.junit.Assert.assertEquals; |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 33 | import static org.junit.Assert.assertNull; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Test for fabric interpreter. |
| 37 | */ |
| 38 | public class FabricInterpreterTest { |
| 39 | private static final VlanId VLAN_100 = VlanId.vlanId("100"); |
| 40 | private static final PortNumber PORT_1 = PortNumber.portNumber(1); |
| 41 | private static final MacAddress SRC_MAC = MacAddress.valueOf("00:00:00:00:00:01"); |
| 42 | 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] | 43 | private static final MplsLabel MPLS_10 = MplsLabel.mplsLabel(10); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 44 | |
| 45 | private FabricInterpreter interpreter; |
| 46 | |
| 47 | @Before |
| 48 | public void setup() { |
| 49 | interpreter = new FabricInterpreter(); |
| 50 | } |
| 51 | |
| 52 | /* Filtering control block */ |
| 53 | |
| 54 | /** |
| 55 | * Map treatment to push_internal_vlan action. |
| 56 | */ |
| 57 | @Test |
| 58 | public void testFilteringTreatment1() throws Exception { |
| 59 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 60 | .pushVlan() |
| 61 | .setVlanId(VLAN_100) |
| 62 | .build(); |
| 63 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
| 64 | FabricConstants.TBL_INGRESS_PORT_VLAN_ID); |
| 65 | PiActionParam param = new PiActionParam(FabricConstants.ACT_PRM_NEW_VLAN_ID_ID, |
| 66 | ImmutableByteSequence.copyFrom(VLAN_100.toShort())); |
| 67 | PiAction expectedAction = PiAction.builder() |
Yi Tseng | 27b9bc0 | 2018-04-12 14:52:40 +0800 | [diff] [blame] | 68 | .withId(FabricConstants.ACT_FABRICINGRESS_FILTERING_PUSH_INTERNAL_VLAN_ID) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 69 | .withParameter(param) |
| 70 | .build(); |
| 71 | |
| 72 | assertEquals(expectedAction, mappedAction); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Map treatment to set_vlan action. |
| 77 | */ |
| 78 | @Test |
| 79 | public void testFilteringTreatment2() throws Exception { |
| 80 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 81 | .setVlanId(VLAN_100) |
| 82 | .build(); |
| 83 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
| 84 | FabricConstants.TBL_INGRESS_PORT_VLAN_ID); |
| 85 | PiActionParam param = new PiActionParam(FabricConstants.ACT_PRM_NEW_VLAN_ID_ID, |
| 86 | ImmutableByteSequence.copyFrom(VLAN_100.toShort())); |
| 87 | PiAction expectedAction = PiAction.builder() |
Yi Tseng | 27b9bc0 | 2018-04-12 14:52:40 +0800 | [diff] [blame] | 88 | .withId(FabricConstants.ACT_FABRICINGRESS_FILTERING_SET_VLAN_ID) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 89 | .withParameter(param) |
| 90 | .build(); |
| 91 | |
| 92 | assertEquals(expectedAction, mappedAction); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Map treatment to nop action. |
| 97 | */ |
| 98 | @Test |
| 99 | public void testFilteringTreatment3() throws Exception { |
| 100 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 101 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
| 102 | FabricConstants.TBL_INGRESS_PORT_VLAN_ID); |
| 103 | PiAction expectedAction = PiAction.builder() |
| 104 | .withId(FabricConstants.ACT_NOP_ID) |
| 105 | .build(); |
| 106 | |
| 107 | assertEquals(expectedAction, mappedAction); |
| 108 | } |
| 109 | |
| 110 | /* Forwarding control block */ |
| 111 | |
| 112 | /** |
| 113 | * Map treatment to duplicate_to_controller action. |
| 114 | */ |
| 115 | @Test |
| 116 | public void testForwardingTreatment1() throws Exception { |
| 117 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 118 | .punt() |
| 119 | .build(); |
| 120 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
| 121 | FabricConstants.TBL_ACL_ID); |
| 122 | PiAction expectedAction = PiAction.builder() |
Yi Tseng | 27b9bc0 | 2018-04-12 14:52:40 +0800 | [diff] [blame] | 123 | .withId(FabricConstants.ACT_FABRICINGRESS_FORWARDING_DUPLICATE_TO_CONTROLLER_ID) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 124 | .build(); |
| 125 | |
| 126 | assertEquals(expectedAction, mappedAction); |
| 127 | } |
| 128 | |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 129 | /** |
| 130 | * Map empty treatment for forwarding block to nop action. |
| 131 | */ |
| 132 | @Test |
| 133 | public void testEmptyForwardingTreatment() throws Exception { |
| 134 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 135 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
| 136 | FabricConstants.TBL_UNICAST_V4_ID); |
| 137 | assertNull(mappedAction); |
| 138 | } |
| 139 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 140 | /* Next control block */ |
| 141 | |
| 142 | /** |
| 143 | * Map treatment to output action. |
| 144 | */ |
| 145 | @Test |
| 146 | public void testNextTreatment1() throws Exception { |
| 147 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 148 | .setOutput(PORT_1) |
| 149 | .build(); |
| 150 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
| 151 | FabricConstants.TBL_SIMPLE_ID); |
| 152 | short portNumVal = (short) PORT_1.toLong(); |
| 153 | PiActionParam param = new PiActionParam(FabricConstants.ACT_PRM_PORT_NUM_ID, |
| 154 | ImmutableByteSequence.copyFrom(portNumVal)); |
| 155 | PiAction expectedAction = PiAction.builder() |
Yi Tseng | 27b9bc0 | 2018-04-12 14:52:40 +0800 | [diff] [blame] | 156 | .withId(FabricConstants.ACT_FABRICINGRESS_NEXT_OUTPUT_ID) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 157 | .withParameter(param) |
| 158 | .build(); |
| 159 | |
| 160 | assertEquals(expectedAction, mappedAction); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Map treatment to output_ecmp action. |
| 165 | */ |
| 166 | @Test |
| 167 | public void testNextTreatment2() throws Exception { |
| 168 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 169 | .setEthSrc(SRC_MAC) |
| 170 | .setEthDst(DST_MAC) |
| 171 | .setOutput(PORT_1) |
| 172 | .build(); |
| 173 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
| 174 | FabricConstants.TBL_HASHED_ID); |
| 175 | short portNumVal = (short) PORT_1.toLong(); |
| 176 | PiActionParam ethSrcParam = new PiActionParam(FabricConstants.ACT_PRM_SMAC_ID, |
| 177 | ImmutableByteSequence.copyFrom(SRC_MAC.toBytes())); |
| 178 | PiActionParam ethDstParam = new PiActionParam(FabricConstants.ACT_PRM_DMAC_ID, |
| 179 | ImmutableByteSequence.copyFrom(DST_MAC.toBytes())); |
| 180 | PiActionParam portParam = new PiActionParam(FabricConstants.ACT_PRM_PORT_NUM_ID, |
| 181 | ImmutableByteSequence.copyFrom(portNumVal)); |
| 182 | PiAction expectedAction = PiAction.builder() |
Yi Tseng | 27b9bc0 | 2018-04-12 14:52:40 +0800 | [diff] [blame] | 183 | .withId(FabricConstants.ACT_FABRICINGRESS_NEXT_L3_ROUTING_ID) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 184 | .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam)) |
| 185 | .build(); |
| 186 | |
| 187 | assertEquals(expectedAction, mappedAction); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Map treatment to set_vlan_output action. |
| 192 | */ |
| 193 | @Test |
| 194 | public void testNextTreatment3() throws Exception { |
| 195 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 196 | .setVlanId(VLAN_100) |
| 197 | .setOutput(PORT_1) |
| 198 | .build(); |
| 199 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
| 200 | FabricConstants.TBL_SIMPLE_ID); |
| 201 | short portNumVal = (short) PORT_1.toLong(); |
| 202 | PiActionParam portParam = new PiActionParam(FabricConstants.ACT_PRM_PORT_NUM_ID, |
| 203 | ImmutableByteSequence.copyFrom(portNumVal)); |
| 204 | short vlanVal = VLAN_100.toShort(); |
| 205 | PiActionParam vlanParam = new PiActionParam(FabricConstants.ACT_PRM_NEW_VLAN_ID_ID, |
| 206 | ImmutableByteSequence.copyFrom(vlanVal)); |
| 207 | |
| 208 | PiAction expectedAction = PiAction.builder() |
Yi Tseng | 27b9bc0 | 2018-04-12 14:52:40 +0800 | [diff] [blame] | 209 | .withId(FabricConstants.ACT_FABRICINGRESS_NEXT_SET_VLAN_OUTPUT_ID) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 210 | .withParameters(ImmutableList.of(vlanParam, portParam)) |
| 211 | .build(); |
| 212 | |
| 213 | assertEquals(expectedAction, mappedAction); |
| 214 | } |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 215 | |
| 216 | @Test |
| 217 | public void testMplsRoutingTreatment() throws Exception { |
| 218 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 219 | .setEthDst(DST_MAC) |
| 220 | .setEthSrc(SRC_MAC) |
| 221 | .pushMpls() |
| 222 | .copyTtlOut() |
| 223 | .setMpls(MPLS_10) |
| 224 | .setOutput(PORT_1) |
| 225 | .build(); |
| 226 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
| 227 | FabricConstants.TBL_HASHED_ID); |
| 228 | short portNumVal = (short) PORT_1.toLong(); |
| 229 | PiActionParam ethSrcParam = new PiActionParam(FabricConstants.ACT_PRM_SMAC_ID, |
| 230 | ImmutableByteSequence.copyFrom(SRC_MAC.toBytes())); |
| 231 | PiActionParam ethDstParam = new PiActionParam(FabricConstants.ACT_PRM_DMAC_ID, |
| 232 | ImmutableByteSequence.copyFrom(DST_MAC.toBytes())); |
| 233 | PiActionParam portParam = new PiActionParam(FabricConstants.ACT_PRM_PORT_NUM_ID, |
| 234 | ImmutableByteSequence.copyFrom(portNumVal)); |
| 235 | ImmutableByteSequence mplsVal = |
Carmelo Cascone | 8a571af | 2018-04-06 23:17:04 -0700 | [diff] [blame] | 236 | ImmutableByteSequence.copyFrom(MPLS_10.toInt()).fit(20); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 237 | PiActionParam mplsParam = new PiActionParam(FabricConstants.ACT_PRM_LABEL_ID, mplsVal); |
| 238 | PiAction expectedAction = PiAction.builder() |
Yi Tseng | 27b9bc0 | 2018-04-12 14:52:40 +0800 | [diff] [blame] | 239 | .withId(FabricConstants.ACT_FABRICINGRESS_NEXT_MPLS_ROUTING_V4_ID) |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 240 | .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam, mplsParam)) |
| 241 | .build(); |
| 242 | assertEquals(expectedAction, mappedAction); |
| 243 | } |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 244 | } |