blob: ca38cf8f6e148b9efc338e5f64f94b760629cc3f [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
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,
Yi Tseng43ee7e82018-04-12 16:37:34 +080063 FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN);
64 PiActionParam param = new PiActionParam(FabricConstants.NEW_VLAN_ID,
Yi Tsengfa4a1c72017-11-03 10:22:38 -070065 ImmutableByteSequence.copyFrom(VLAN_100.toShort()));
66 PiAction expectedAction = PiAction.builder()
Yi Tseng43ee7e82018-04-12 16:37:34 +080067 .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PUSH_INTERNAL_VLAN)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070068 .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,
Yi Tseng43ee7e82018-04-12 16:37:34 +080083 FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN);
84 PiActionParam param = new PiActionParam(FabricConstants.NEW_VLAN_ID,
Yi Tsengfa4a1c72017-11-03 10:22:38 -070085 ImmutableByteSequence.copyFrom(VLAN_100.toShort()));
86 PiAction expectedAction = PiAction.builder()
Yi Tseng43ee7e82018-04-12 16:37:34 +080087 .withId(FabricConstants.FABRIC_INGRESS_FILTERING_SET_VLAN)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070088 .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,
Yi Tseng43ee7e82018-04-12 16:37:34 +0800101 FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700102 PiAction expectedAction = PiAction.builder()
Carmelo Cascone8a715f82018-08-20 23:16:27 -0700103 .withId(FabricConstants.FABRIC_INGRESS_FILTERING_NOP_INGRESS_PORT_VLAN)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700104 .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,
Yi Tseng43ee7e82018-04-12 16:37:34 +0800120 FabricConstants.FABRIC_INGRESS_FORWARDING_ACL);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700121 PiAction expectedAction = PiAction.builder()
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200122 .withId(FabricConstants.FABRIC_INGRESS_FORWARDING_CLONE_TO_CPU)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700123 .build();
124
125 assertEquals(expectedAction, mappedAction);
126 }
127
Yi Tsengdf3eec52018-02-15 14:56:02 -0800128 /**
129 * Map empty treatment for forwarding block to nop action.
130 */
131 @Test
132 public void testEmptyForwardingTreatment() throws Exception {
133 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
Charles Chancf696e52018-08-16 16:25:13 -0700134
Yi Tsengdf3eec52018-02-15 14:56:02 -0800135 PiAction mappedAction = interpreter.mapTreatment(treatment,
Charles Chan384aea22018-08-23 22:08:02 -0700136 FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V4);
Charles Chancf696e52018-08-16 16:25:13 -0700137 PiAction expectedAction = PiAction.builder()
138 .withId(FabricConstants.NOP)
139 .build();
140 assertEquals(expectedAction, mappedAction);
141
142 mappedAction = interpreter.mapTreatment(treatment,
143 FabricConstants.FABRIC_INGRESS_FORWARDING_ACL);
144 expectedAction = PiAction.builder()
145 .withId(FabricConstants.FABRIC_INGRESS_FORWARDING_NOP_ACL)
146 .build();
147 assertEquals(expectedAction, mappedAction);
Yi Tsengdf3eec52018-02-15 14:56:02 -0800148 }
149
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700150 /* Next control block */
151
152 /**
153 * Map treatment to output action.
154 */
155 @Test
156 public void testNextTreatment1() throws Exception {
157 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
158 .setOutput(PORT_1)
159 .build();
160 PiAction mappedAction = interpreter.mapTreatment(treatment,
Yi Tseng43ee7e82018-04-12 16:37:34 +0800161 FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700162 short portNumVal = (short) PORT_1.toLong();
Yi Tseng43ee7e82018-04-12 16:37:34 +0800163 PiActionParam param = new PiActionParam(FabricConstants.PORT_NUM,
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700164 ImmutableByteSequence.copyFrom(portNumVal));
165 PiAction expectedAction = PiAction.builder()
Yi Tseng47eac892018-07-11 02:17:04 +0800166 .withId(FabricConstants.FABRIC_INGRESS_NEXT_OUTPUT_SIMPLE)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700167 .withParameter(param)
168 .build();
169
170 assertEquals(expectedAction, mappedAction);
171 }
172
173 /**
174 * Map treatment to output_ecmp action.
175 */
176 @Test
177 public void testNextTreatment2() throws Exception {
178 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
179 .setEthSrc(SRC_MAC)
180 .setEthDst(DST_MAC)
181 .setOutput(PORT_1)
182 .build();
183 PiAction mappedAction = interpreter.mapTreatment(treatment,
Yi Tseng43ee7e82018-04-12 16:37:34 +0800184 FabricConstants.FABRIC_INGRESS_NEXT_HASHED);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700185 short portNumVal = (short) PORT_1.toLong();
Yi Tseng43ee7e82018-04-12 16:37:34 +0800186 PiActionParam ethSrcParam = new PiActionParam(FabricConstants.SMAC,
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700187 ImmutableByteSequence.copyFrom(SRC_MAC.toBytes()));
Yi Tseng43ee7e82018-04-12 16:37:34 +0800188 PiActionParam ethDstParam = new PiActionParam(FabricConstants.DMAC,
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700189 ImmutableByteSequence.copyFrom(DST_MAC.toBytes()));
Yi Tseng43ee7e82018-04-12 16:37:34 +0800190 PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM,
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700191 ImmutableByteSequence.copyFrom(portNumVal));
192 PiAction expectedAction = PiAction.builder()
Yi Tseng47eac892018-07-11 02:17:04 +0800193 .withId(FabricConstants.FABRIC_INGRESS_NEXT_L3_ROUTING_HASHED)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700194 .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam))
195 .build();
196
197 assertEquals(expectedAction, mappedAction);
198 }
199
200 /**
201 * Map treatment to set_vlan_output action.
202 */
203 @Test
204 public void testNextTreatment3() throws Exception {
205 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
206 .setVlanId(VLAN_100)
207 .setOutput(PORT_1)
208 .build();
209 PiAction mappedAction = interpreter.mapTreatment(treatment,
Yi Tseng43ee7e82018-04-12 16:37:34 +0800210 FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700211 short portNumVal = (short) PORT_1.toLong();
Yi Tseng43ee7e82018-04-12 16:37:34 +0800212 PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM,
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700213 ImmutableByteSequence.copyFrom(portNumVal));
214 short vlanVal = VLAN_100.toShort();
Yi Tseng43ee7e82018-04-12 16:37:34 +0800215 PiActionParam vlanParam = new PiActionParam(FabricConstants.NEW_VLAN_ID,
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700216 ImmutableByteSequence.copyFrom(vlanVal));
217
218 PiAction expectedAction = PiAction.builder()
Yi Tseng43ee7e82018-04-12 16:37:34 +0800219 .withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_VLAN_OUTPUT)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700220 .withParameters(ImmutableList.of(vlanParam, portParam))
221 .build();
222
223 assertEquals(expectedAction, mappedAction);
224 }
Yi Tseng1b154bd2017-11-20 17:48:19 -0800225
226 @Test
227 public void testMplsRoutingTreatment() throws Exception {
228 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
229 .setEthDst(DST_MAC)
230 .setEthSrc(SRC_MAC)
231 .pushMpls()
232 .copyTtlOut()
233 .setMpls(MPLS_10)
234 .setOutput(PORT_1)
235 .build();
236 PiAction mappedAction = interpreter.mapTreatment(treatment,
Yi Tseng43ee7e82018-04-12 16:37:34 +0800237 FabricConstants.FABRIC_INGRESS_NEXT_HASHED);
Yi Tseng1b154bd2017-11-20 17:48:19 -0800238 short portNumVal = (short) PORT_1.toLong();
Yi Tseng43ee7e82018-04-12 16:37:34 +0800239 PiActionParam ethSrcParam = new PiActionParam(FabricConstants.SMAC,
Yi Tseng1b154bd2017-11-20 17:48:19 -0800240 ImmutableByteSequence.copyFrom(SRC_MAC.toBytes()));
Yi Tseng43ee7e82018-04-12 16:37:34 +0800241 PiActionParam ethDstParam = new PiActionParam(FabricConstants.DMAC,
Yi Tseng1b154bd2017-11-20 17:48:19 -0800242 ImmutableByteSequence.copyFrom(DST_MAC.toBytes()));
Yi Tseng43ee7e82018-04-12 16:37:34 +0800243 PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM,
Yi Tseng1b154bd2017-11-20 17:48:19 -0800244 ImmutableByteSequence.copyFrom(portNumVal));
245 ImmutableByteSequence mplsVal =
Carmelo Cascone8a571af2018-04-06 23:17:04 -0700246 ImmutableByteSequence.copyFrom(MPLS_10.toInt()).fit(20);
Yi Tseng43ee7e82018-04-12 16:37:34 +0800247 PiActionParam mplsParam = new PiActionParam(FabricConstants.LABEL, mplsVal);
Yi Tseng1b154bd2017-11-20 17:48:19 -0800248 PiAction expectedAction = PiAction.builder()
Yi Tseng47eac892018-07-11 02:17:04 +0800249 .withId(FabricConstants.FABRIC_INGRESS_NEXT_MPLS_ROUTING_V4_HASHED)
Yi Tseng1b154bd2017-11-20 17:48:19 -0800250 .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam, mplsParam))
251 .build();
252 assertEquals(expectedAction, mappedAction);
253 }
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700254}