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; |
| 23 | import org.onlab.packet.VlanId; |
| 24 | import org.onlab.util.ImmutableByteSequence; |
| 25 | import org.onosproject.net.PortNumber; |
| 26 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 27 | import org.onosproject.net.flow.TrafficTreatment; |
| 28 | import org.onosproject.net.pi.runtime.PiAction; |
| 29 | import org.onosproject.net.pi.runtime.PiActionParam; |
| 30 | |
| 31 | import static org.junit.Assert.assertEquals; |
| 32 | |
| 33 | /** |
| 34 | * Test for fabric interpreter. |
| 35 | */ |
| 36 | public class FabricInterpreterTest { |
| 37 | private static final VlanId VLAN_100 = VlanId.vlanId("100"); |
| 38 | private static final PortNumber PORT_1 = PortNumber.portNumber(1); |
| 39 | private static final MacAddress SRC_MAC = MacAddress.valueOf("00:00:00:00:00:01"); |
| 40 | private static final MacAddress DST_MAC = MacAddress.valueOf("00:00:00:00:00:02"); |
| 41 | |
| 42 | private FabricInterpreter interpreter; |
| 43 | |
| 44 | @Before |
| 45 | public void setup() { |
| 46 | interpreter = new FabricInterpreter(); |
| 47 | } |
| 48 | |
| 49 | /* Filtering control block */ |
| 50 | |
| 51 | /** |
| 52 | * Map treatment to push_internal_vlan action. |
| 53 | */ |
| 54 | @Test |
| 55 | public void testFilteringTreatment1() throws Exception { |
| 56 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 57 | .pushVlan() |
| 58 | .setVlanId(VLAN_100) |
| 59 | .build(); |
| 60 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
| 61 | FabricConstants.TBL_INGRESS_PORT_VLAN_ID); |
| 62 | PiActionParam param = new PiActionParam(FabricConstants.ACT_PRM_NEW_VLAN_ID_ID, |
| 63 | ImmutableByteSequence.copyFrom(VLAN_100.toShort())); |
| 64 | PiAction expectedAction = PiAction.builder() |
| 65 | .withId(FabricConstants.ACT_PUSH_INTERNAL_VLAN_ID) |
| 66 | .withParameter(param) |
| 67 | .build(); |
| 68 | |
| 69 | assertEquals(expectedAction, mappedAction); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Map treatment to set_vlan action. |
| 74 | */ |
| 75 | @Test |
| 76 | public void testFilteringTreatment2() throws Exception { |
| 77 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 78 | .setVlanId(VLAN_100) |
| 79 | .build(); |
| 80 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
| 81 | FabricConstants.TBL_INGRESS_PORT_VLAN_ID); |
| 82 | PiActionParam param = new PiActionParam(FabricConstants.ACT_PRM_NEW_VLAN_ID_ID, |
| 83 | ImmutableByteSequence.copyFrom(VLAN_100.toShort())); |
| 84 | PiAction expectedAction = PiAction.builder() |
| 85 | .withId(FabricConstants.ACT_SET_VLAN_ID) |
| 86 | .withParameter(param) |
| 87 | .build(); |
| 88 | |
| 89 | assertEquals(expectedAction, mappedAction); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Map treatment to nop action. |
| 94 | */ |
| 95 | @Test |
| 96 | public void testFilteringTreatment3() throws Exception { |
| 97 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 98 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
| 99 | FabricConstants.TBL_INGRESS_PORT_VLAN_ID); |
| 100 | PiAction expectedAction = PiAction.builder() |
| 101 | .withId(FabricConstants.ACT_NOP_ID) |
| 102 | .build(); |
| 103 | |
| 104 | assertEquals(expectedAction, mappedAction); |
| 105 | } |
| 106 | |
| 107 | /* Forwarding control block */ |
| 108 | |
| 109 | /** |
| 110 | * Map treatment to duplicate_to_controller action. |
| 111 | */ |
| 112 | @Test |
| 113 | public void testForwardingTreatment1() throws Exception { |
| 114 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 115 | .punt() |
| 116 | .build(); |
| 117 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
| 118 | FabricConstants.TBL_ACL_ID); |
| 119 | PiAction expectedAction = PiAction.builder() |
| 120 | .withId(FabricConstants.ACT_DUPLICATE_TO_CONTROLLER_ID) |
| 121 | .build(); |
| 122 | |
| 123 | assertEquals(expectedAction, mappedAction); |
| 124 | } |
| 125 | |
| 126 | /* Next control block */ |
| 127 | |
| 128 | /** |
| 129 | * Map treatment to output action. |
| 130 | */ |
| 131 | @Test |
| 132 | public void testNextTreatment1() throws Exception { |
| 133 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 134 | .setOutput(PORT_1) |
| 135 | .build(); |
| 136 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
| 137 | FabricConstants.TBL_SIMPLE_ID); |
| 138 | short portNumVal = (short) PORT_1.toLong(); |
| 139 | PiActionParam param = new PiActionParam(FabricConstants.ACT_PRM_PORT_NUM_ID, |
| 140 | ImmutableByteSequence.copyFrom(portNumVal)); |
| 141 | PiAction expectedAction = PiAction.builder() |
| 142 | .withId(FabricConstants.ACT_OUTPUT_ID) |
| 143 | .withParameter(param) |
| 144 | .build(); |
| 145 | |
| 146 | assertEquals(expectedAction, mappedAction); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Map treatment to output_ecmp action. |
| 151 | */ |
| 152 | @Test |
| 153 | public void testNextTreatment2() throws Exception { |
| 154 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 155 | .setEthSrc(SRC_MAC) |
| 156 | .setEthDst(DST_MAC) |
| 157 | .setOutput(PORT_1) |
| 158 | .build(); |
| 159 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
| 160 | FabricConstants.TBL_HASHED_ID); |
| 161 | short portNumVal = (short) PORT_1.toLong(); |
| 162 | PiActionParam ethSrcParam = new PiActionParam(FabricConstants.ACT_PRM_SMAC_ID, |
| 163 | ImmutableByteSequence.copyFrom(SRC_MAC.toBytes())); |
| 164 | PiActionParam ethDstParam = new PiActionParam(FabricConstants.ACT_PRM_DMAC_ID, |
| 165 | ImmutableByteSequence.copyFrom(DST_MAC.toBytes())); |
| 166 | PiActionParam portParam = new PiActionParam(FabricConstants.ACT_PRM_PORT_NUM_ID, |
| 167 | ImmutableByteSequence.copyFrom(portNumVal)); |
| 168 | PiAction expectedAction = PiAction.builder() |
| 169 | .withId(FabricConstants.ACT_L3_ROUTING_ID) |
| 170 | .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam)) |
| 171 | .build(); |
| 172 | |
| 173 | assertEquals(expectedAction, mappedAction); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Map treatment to set_vlan_output action. |
| 178 | */ |
| 179 | @Test |
| 180 | public void testNextTreatment3() throws Exception { |
| 181 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 182 | .setVlanId(VLAN_100) |
| 183 | .setOutput(PORT_1) |
| 184 | .build(); |
| 185 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
| 186 | FabricConstants.TBL_SIMPLE_ID); |
| 187 | short portNumVal = (short) PORT_1.toLong(); |
| 188 | PiActionParam portParam = new PiActionParam(FabricConstants.ACT_PRM_PORT_NUM_ID, |
| 189 | ImmutableByteSequence.copyFrom(portNumVal)); |
| 190 | short vlanVal = VLAN_100.toShort(); |
| 191 | PiActionParam vlanParam = new PiActionParam(FabricConstants.ACT_PRM_NEW_VLAN_ID_ID, |
| 192 | ImmutableByteSequence.copyFrom(vlanVal)); |
| 193 | |
| 194 | PiAction expectedAction = PiAction.builder() |
| 195 | .withId(FabricConstants.ACT_SET_VLAN_OUTPUT_ID) |
| 196 | .withParameters(ImmutableList.of(vlanParam, portParam)) |
| 197 | .build(); |
| 198 | |
| 199 | assertEquals(expectedAction, mappedAction); |
| 200 | } |
| 201 | } |