blob: 217a7368685bc08009f8ed248a25836bff20b6cf [file] [log] [blame]
Yi Tsengfa4a1c72017-11-03 10:22:38 -07001/*
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
17package org.onosproject.pipelines.fabric;
18
19import com.google.common.collect.ImmutableList;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.packet.MacAddress;
Yi Tseng1b154bd2017-11-20 17:48:19 -080023import org.onlab.packet.MplsLabel;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070024import org.onlab.packet.VlanId;
25import org.onlab.util.ImmutableByteSequence;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.flow.DefaultTrafficTreatment;
28import org.onosproject.net.flow.TrafficTreatment;
29import org.onosproject.net.pi.runtime.PiAction;
30import org.onosproject.net.pi.runtime.PiActionParam;
31
32import static org.junit.Assert.assertEquals;
33
34/**
35 * Test for fabric interpreter.
36 */
37public 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 Tseng1b154bd2017-11-20 17:48:19 -080042 private static final MplsLabel MPLS_10 = MplsLabel.mplsLabel(10);
Yi Tsengfa4a1c72017-11-03 10:22:38 -070043
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
Carmelo Casconeb5324e72018-11-25 02:26:32 -080057 public void testFilteringTreatmentPermitWithInternalVlan() throws Exception {
Yi Tsengfa4a1c72017-11-03 10:22:38 -070058 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
59 .pushVlan()
60 .setVlanId(VLAN_100)
61 .build();
62 PiAction mappedAction = interpreter.mapTreatment(treatment,
Yi Tseng43ee7e82018-04-12 16:37:34 +080063 FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN);
Carmelo Casconeb5324e72018-11-25 02:26:32 -080064 PiActionParam param = new PiActionParam(FabricConstants.VLAN_ID,
Yi Tsengfa4a1c72017-11-03 10:22:38 -070065 ImmutableByteSequence.copyFrom(VLAN_100.toShort()));
66 PiAction expectedAction = PiAction.builder()
Carmelo Casconeb5324e72018-11-25 02:26:32 -080067 .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT_WITH_INTERNAL_VLAN)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070068 .withParameter(param)
69 .build();
70
71 assertEquals(expectedAction, mappedAction);
72 }
73
74 /**
Carmelo Casconeb5324e72018-11-25 02:26:32 -080075 * Map treatment to permit action.
Yi Tsengfa4a1c72017-11-03 10:22:38 -070076 */
77 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -080078 public void testFilteringTreatmentPermit() throws Exception {
Yi Tsengfa4a1c72017-11-03 10:22:38 -070079 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
80 PiAction mappedAction = interpreter.mapTreatment(treatment,
Yi Tseng43ee7e82018-04-12 16:37:34 +080081 FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN);
Yi Tsengfa4a1c72017-11-03 10:22:38 -070082 PiAction expectedAction = PiAction.builder()
Carmelo Casconeb5324e72018-11-25 02:26:32 -080083 .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070084 .build();
85
86 assertEquals(expectedAction, mappedAction);
87 }
88
89 /* Forwarding control block */
90
91 /**
92 * Map treatment to duplicate_to_controller action.
93 */
94 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -080095 public void testAclTreatmentCloneToCpu() throws Exception {
Yi Tsengfa4a1c72017-11-03 10:22:38 -070096 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
97 .punt()
98 .build();
99 PiAction mappedAction = interpreter.mapTreatment(treatment,
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800100 FabricConstants.FABRIC_INGRESS_ACL_ACL);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700101 PiAction expectedAction = PiAction.builder()
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800102 .withId(FabricConstants.FABRIC_INGRESS_ACL_CLONE_TO_CPU)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700103 .build();
104
105 assertEquals(expectedAction, mappedAction);
106 }
107
Yi Tsengdf3eec52018-02-15 14:56:02 -0800108 /**
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800109 * Map empty treatment for routing v4 table.
Yi Tsengdf3eec52018-02-15 14:56:02 -0800110 */
111 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800112 public void testRoutingV4TreatmentEmpty() throws Exception {
Yi Tsengdf3eec52018-02-15 14:56:02 -0800113 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800114 PiAction mappedAction = interpreter.mapTreatment(
115 treatment, FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V4);
Charles Chancf696e52018-08-16 16:25:13 -0700116 PiAction expectedAction = PiAction.builder()
Charles Chancd03f072018-08-31 17:46:37 -0700117 .withId(FabricConstants.FABRIC_INGRESS_FORWARDING_NOP_ROUTING_V4)
Charles Chancf696e52018-08-16 16:25:13 -0700118 .build();
119 assertEquals(expectedAction, mappedAction);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800120 }
Charles Chancf696e52018-08-16 16:25:13 -0700121
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800122 /**
123 * Map empty treatment for ACL table.
124 */
125 @Test
126 public void testAclTreatmentEmpty() throws Exception {
127 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
128 PiAction mappedAction = interpreter.mapTreatment(
129 treatment, FabricConstants.FABRIC_INGRESS_ACL_ACL);
130 PiAction expectedAction = PiAction.builder()
131 .withId(FabricConstants.FABRIC_INGRESS_ACL_NOP_ACL)
Charles Chancf696e52018-08-16 16:25:13 -0700132 .build();
133 assertEquals(expectedAction, mappedAction);
Yi Tsengdf3eec52018-02-15 14:56:02 -0800134 }
135
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700136 /* Next control block */
137
138 /**
139 * Map treatment to output action.
140 */
141 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800142 public void testNextTreatmentSimpleOutput() throws Exception {
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700143 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
144 .setOutput(PORT_1)
145 .build();
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800146 PiAction mappedAction = interpreter.mapTreatment(
147 treatment, FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE);
148 PiActionParam param = new PiActionParam(FabricConstants.PORT_NUM, PORT_1.toLong());
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700149 PiAction expectedAction = PiAction.builder()
Yi Tseng47eac892018-07-11 02:17:04 +0800150 .withId(FabricConstants.FABRIC_INGRESS_NEXT_OUTPUT_SIMPLE)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700151 .withParameter(param)
152 .build();
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700153 assertEquals(expectedAction, mappedAction);
154 }
155
156 /**
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800157 * Map treatment for hashed table to routing v4 action.
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700158 */
159 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800160 public void testNextTreatmentHashedRoutingV4() throws Exception {
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700161 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
162 .setEthSrc(SRC_MAC)
163 .setEthDst(DST_MAC)
164 .setOutput(PORT_1)
165 .build();
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800166 PiAction mappedAction = interpreter.mapTreatment(
167 treatment, FabricConstants.FABRIC_INGRESS_NEXT_HASHED);
168 PiActionParam ethSrcParam = new PiActionParam(FabricConstants.SMAC, SRC_MAC.toBytes());
169 PiActionParam ethDstParam = new PiActionParam(FabricConstants.DMAC, DST_MAC.toBytes());
170 PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM, PORT_1.toLong());
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700171 PiAction expectedAction = PiAction.builder()
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800172 .withId(FabricConstants.FABRIC_INGRESS_NEXT_ROUTING_HASHED)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700173 .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam))
174 .build();
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800175 assertEquals(expectedAction, mappedAction);
176 }
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700177
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800178 /**
179 * Map treatment for hashed table to routing v4 action.
180 */
181 @Test
182 public void testNextTreatmentHashedRoutingMpls() throws Exception {
183 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
184 .setEthSrc(SRC_MAC)
185 .setEthDst(DST_MAC)
186 .setOutput(PORT_1)
187 .pushMpls()
188 .setMpls(MPLS_10)
189 .build();
190 PiAction mappedAction = interpreter.mapTreatment(
191 treatment, FabricConstants.FABRIC_INGRESS_NEXT_HASHED);
192 PiActionParam ethSrcParam = new PiActionParam(FabricConstants.SMAC, SRC_MAC.toBytes());
193 PiActionParam ethDstParam = new PiActionParam(FabricConstants.DMAC, DST_MAC.toBytes());
194 PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM, PORT_1.toLong());
195 PiActionParam mplsParam = new PiActionParam(FabricConstants.LABEL, MPLS_10.toInt());
196 PiAction expectedAction = PiAction.builder()
197 .withId(FabricConstants.FABRIC_INGRESS_NEXT_MPLS_ROUTING_HASHED)
198 .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam, mplsParam))
199 .build();
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700200 assertEquals(expectedAction, mappedAction);
201 }
202
203 /**
204 * Map treatment to set_vlan_output action.
205 */
206 @Test
207 public void testNextTreatment3() throws Exception {
208 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
209 .setVlanId(VLAN_100)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700210 .build();
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800211 PiAction mappedAction = interpreter.mapTreatment(
212 treatment, FabricConstants.FABRIC_INGRESS_NEXT_NEXT_VLAN);
213 PiActionParam vlanParam = new PiActionParam(
214 FabricConstants.VLAN_ID, VLAN_100.toShort());
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700215 PiAction expectedAction = PiAction.builder()
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800216 .withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_VLAN)
217 .withParameter(vlanParam)
Yi Tseng1b154bd2017-11-20 17:48:19 -0800218 .build();
219 assertEquals(expectedAction, mappedAction);
220 }
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700221}