blob: 7ee9c888a1055258ae3a2d35f195a600f143a854 [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
Carmelo Cascone356ab8b2019-09-25 01:02:53 -070017package org.onosproject.pipelines.fabric.impl.behaviour;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070018
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;
Carmelo Cascone2102bfb2020-12-04 16:54:24 -080031import org.onosproject.pipelines.fabric.FabricConstants;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070032
Daniele Morof51d0c12019-07-30 10:43:10 -070033import static org.easymock.EasyMock.createNiceMock;
34import static org.easymock.EasyMock.expect;
35import static org.easymock.EasyMock.replay;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070036import static org.junit.Assert.assertEquals;
37
38/**
39 * Test for fabric interpreter.
40 */
41public class FabricInterpreterTest {
42 private static final VlanId VLAN_100 = VlanId.vlanId("100");
43 private static final PortNumber PORT_1 = PortNumber.portNumber(1);
44 private static final MacAddress SRC_MAC = MacAddress.valueOf("00:00:00:00:00:01");
45 private static final MacAddress DST_MAC = MacAddress.valueOf("00:00:00:00:00:02");
Yi Tseng1b154bd2017-11-20 17:48:19 -080046 private static final MplsLabel MPLS_10 = MplsLabel.mplsLabel(10);
Yi Tsengfa4a1c72017-11-03 10:22:38 -070047
48 private FabricInterpreter interpreter;
49
Daniele Morof51d0c12019-07-30 10:43:10 -070050 FabricCapabilities allCapabilities;
51
Yi Tsengfa4a1c72017-11-03 10:22:38 -070052 @Before
53 public void setup() {
Daniele Morof51d0c12019-07-30 10:43:10 -070054 allCapabilities = createNiceMock(FabricCapabilities.class);
55 expect(allCapabilities.hasHashedTable()).andReturn(true).anyTimes();
56 expect(allCapabilities.supportDoubleVlanTerm()).andReturn(true).anyTimes();
57 replay(allCapabilities);
58 interpreter = new FabricInterpreter(allCapabilities);
Yi Tsengfa4a1c72017-11-03 10:22:38 -070059 }
60
61 /* Filtering control block */
62
63 /**
64 * Map treatment to push_internal_vlan action.
65 */
66 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -080067 public void testFilteringTreatmentPermitWithInternalVlan() throws Exception {
Yi Tsengfa4a1c72017-11-03 10:22:38 -070068 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
69 .pushVlan()
70 .setVlanId(VLAN_100)
71 .build();
72 PiAction mappedAction = interpreter.mapTreatment(treatment,
Carmelo Cascone356ab8b2019-09-25 01:02:53 -070073 FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN);
Carmelo Casconeb5324e72018-11-25 02:26:32 -080074 PiActionParam param = new PiActionParam(FabricConstants.VLAN_ID,
Yi Tsengfa4a1c72017-11-03 10:22:38 -070075 ImmutableByteSequence.copyFrom(VLAN_100.toShort()));
76 PiAction expectedAction = PiAction.builder()
Carmelo Casconeb5324e72018-11-25 02:26:32 -080077 .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT_WITH_INTERNAL_VLAN)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070078 .withParameter(param)
79 .build();
80
81 assertEquals(expectedAction, mappedAction);
82 }
83
84 /**
Carmelo Casconeb5324e72018-11-25 02:26:32 -080085 * Map treatment to permit action.
Yi Tsengfa4a1c72017-11-03 10:22:38 -070086 */
87 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -080088 public void testFilteringTreatmentPermit() throws Exception {
Yi Tsengfa4a1c72017-11-03 10:22:38 -070089 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
90 PiAction mappedAction = interpreter.mapTreatment(treatment,
Yi Tseng43ee7e82018-04-12 16:37:34 +080091 FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN);
Yi Tsengfa4a1c72017-11-03 10:22:38 -070092 PiAction expectedAction = PiAction.builder()
Carmelo Casconeb5324e72018-11-25 02:26:32 -080093 .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070094 .build();
95
96 assertEquals(expectedAction, mappedAction);
97 }
98
99 /* Forwarding control block */
100
101 /**
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800102 * Map empty treatment for routing v4 table.
Yi Tsengdf3eec52018-02-15 14:56:02 -0800103 */
104 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800105 public void testRoutingV4TreatmentEmpty() throws Exception {
Yi Tsengdf3eec52018-02-15 14:56:02 -0800106 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800107 PiAction mappedAction = interpreter.mapTreatment(
108 treatment, FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V4);
Charles Chancf696e52018-08-16 16:25:13 -0700109 PiAction expectedAction = PiAction.builder()
Charles Chancd03f072018-08-31 17:46:37 -0700110 .withId(FabricConstants.FABRIC_INGRESS_FORWARDING_NOP_ROUTING_V4)
Charles Chancf696e52018-08-16 16:25:13 -0700111 .build();
112 assertEquals(expectedAction, mappedAction);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800113 }
Charles Chancf696e52018-08-16 16:25:13 -0700114
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800115 /**
116 * Map empty treatment for ACL table.
117 */
118 @Test
119 public void testAclTreatmentEmpty() throws Exception {
120 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
121 PiAction mappedAction = interpreter.mapTreatment(
122 treatment, FabricConstants.FABRIC_INGRESS_ACL_ACL);
123 PiAction expectedAction = PiAction.builder()
124 .withId(FabricConstants.FABRIC_INGRESS_ACL_NOP_ACL)
Charles Chancf696e52018-08-16 16:25:13 -0700125 .build();
126 assertEquals(expectedAction, mappedAction);
Yi Tsengdf3eec52018-02-15 14:56:02 -0800127 }
128
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700129 /* Next control block */
130
131 /**
132 * Map treatment to output action.
133 */
134 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800135 public void testNextTreatmentSimpleOutput() throws Exception {
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700136 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
137 .setOutput(PORT_1)
138 .build();
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800139 PiAction mappedAction = interpreter.mapTreatment(
140 treatment, FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE);
141 PiActionParam param = new PiActionParam(FabricConstants.PORT_NUM, PORT_1.toLong());
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700142 PiAction expectedAction = PiAction.builder()
Yi Tseng47eac892018-07-11 02:17:04 +0800143 .withId(FabricConstants.FABRIC_INGRESS_NEXT_OUTPUT_SIMPLE)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700144 .withParameter(param)
145 .build();
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700146 assertEquals(expectedAction, mappedAction);
147 }
148
149 /**
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800150 * Map treatment for hashed table to routing v4 action.
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700151 */
152 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800153 public void testNextTreatmentHashedRoutingV4() throws Exception {
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700154 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
155 .setEthSrc(SRC_MAC)
156 .setEthDst(DST_MAC)
157 .setOutput(PORT_1)
158 .build();
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800159 PiAction mappedAction = interpreter.mapTreatment(
160 treatment, FabricConstants.FABRIC_INGRESS_NEXT_HASHED);
161 PiActionParam ethSrcParam = new PiActionParam(FabricConstants.SMAC, SRC_MAC.toBytes());
162 PiActionParam ethDstParam = new PiActionParam(FabricConstants.DMAC, DST_MAC.toBytes());
163 PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM, PORT_1.toLong());
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700164 PiAction expectedAction = PiAction.builder()
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800165 .withId(FabricConstants.FABRIC_INGRESS_NEXT_ROUTING_HASHED)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700166 .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam))
167 .build();
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800168 assertEquals(expectedAction, mappedAction);
169 }
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700170
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800171 /**
172 * Map treatment for hashed table to routing v4 action.
173 */
174 @Test
175 public void testNextTreatmentHashedRoutingMpls() throws Exception {
176 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
177 .setEthSrc(SRC_MAC)
178 .setEthDst(DST_MAC)
179 .setOutput(PORT_1)
180 .pushMpls()
181 .setMpls(MPLS_10)
182 .build();
183 PiAction mappedAction = interpreter.mapTreatment(
184 treatment, FabricConstants.FABRIC_INGRESS_NEXT_HASHED);
185 PiActionParam ethSrcParam = new PiActionParam(FabricConstants.SMAC, SRC_MAC.toBytes());
186 PiActionParam ethDstParam = new PiActionParam(FabricConstants.DMAC, DST_MAC.toBytes());
187 PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM, PORT_1.toLong());
188 PiActionParam mplsParam = new PiActionParam(FabricConstants.LABEL, MPLS_10.toInt());
189 PiAction expectedAction = PiAction.builder()
190 .withId(FabricConstants.FABRIC_INGRESS_NEXT_MPLS_ROUTING_HASHED)
191 .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam, mplsParam))
192 .build();
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700193 assertEquals(expectedAction, mappedAction);
194 }
195
196 /**
197 * Map treatment to set_vlan_output action.
198 */
199 @Test
200 public void testNextTreatment3() throws Exception {
201 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
202 .setVlanId(VLAN_100)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700203 .build();
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800204 PiAction mappedAction = interpreter.mapTreatment(
205 treatment, FabricConstants.FABRIC_INGRESS_NEXT_NEXT_VLAN);
206 PiActionParam vlanParam = new PiActionParam(
207 FabricConstants.VLAN_ID, VLAN_100.toShort());
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700208 PiAction expectedAction = PiAction.builder()
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800209 .withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_VLAN)
210 .withParameter(vlanParam)
Yi Tseng1b154bd2017-11-20 17:48:19 -0800211 .build();
212 assertEquals(expectedAction, mappedAction);
213 }
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700214}