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