blob: a0e6194651430d067cd78256142de2fade1f0d43 [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 Cascone36d5e7a2019-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;
31
Daniele Moro77ef35c2019-07-30 10:43:10 -070032import static org.easymock.EasyMock.createNiceMock;
33import static org.easymock.EasyMock.expect;
34import static org.easymock.EasyMock.replay;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070035import static org.junit.Assert.assertEquals;
36
37/**
38 * Test for fabric interpreter.
39 */
40public class FabricInterpreterTest {
41 private static final VlanId VLAN_100 = VlanId.vlanId("100");
42 private static final PortNumber PORT_1 = PortNumber.portNumber(1);
43 private static final MacAddress SRC_MAC = MacAddress.valueOf("00:00:00:00:00:01");
44 private static final MacAddress DST_MAC = MacAddress.valueOf("00:00:00:00:00:02");
Yi Tseng1b154bd2017-11-20 17:48:19 -080045 private static final MplsLabel MPLS_10 = MplsLabel.mplsLabel(10);
Yi Tsengfa4a1c72017-11-03 10:22:38 -070046
47 private FabricInterpreter interpreter;
48
Daniele Moro77ef35c2019-07-30 10:43:10 -070049 FabricCapabilities allCapabilities;
50
Yi Tsengfa4a1c72017-11-03 10:22:38 -070051 @Before
52 public void setup() {
Daniele Moro77ef35c2019-07-30 10:43:10 -070053 allCapabilities = createNiceMock(FabricCapabilities.class);
54 expect(allCapabilities.hasHashedTable()).andReturn(true).anyTimes();
55 expect(allCapabilities.supportDoubleVlanTerm()).andReturn(true).anyTimes();
56 replay(allCapabilities);
57 interpreter = new FabricInterpreter(allCapabilities);
Yi Tsengfa4a1c72017-11-03 10:22:38 -070058 }
59
60 /* Filtering control block */
61
62 /**
63 * Map treatment to push_internal_vlan action.
64 */
65 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -080066 public void testFilteringTreatmentPermitWithInternalVlan() throws Exception {
Yi Tsengfa4a1c72017-11-03 10:22:38 -070067 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
68 .pushVlan()
69 .setVlanId(VLAN_100)
70 .build();
71 PiAction mappedAction = interpreter.mapTreatment(treatment,
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -070072 FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN);
Carmelo Casconeb5324e72018-11-25 02:26:32 -080073 PiActionParam param = new PiActionParam(FabricConstants.VLAN_ID,
Yi Tsengfa4a1c72017-11-03 10:22:38 -070074 ImmutableByteSequence.copyFrom(VLAN_100.toShort()));
75 PiAction expectedAction = PiAction.builder()
Carmelo Casconeb5324e72018-11-25 02:26:32 -080076 .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT_WITH_INTERNAL_VLAN)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070077 .withParameter(param)
78 .build();
79
80 assertEquals(expectedAction, mappedAction);
81 }
82
83 /**
Carmelo Casconeb5324e72018-11-25 02:26:32 -080084 * Map treatment to permit action.
Yi Tsengfa4a1c72017-11-03 10:22:38 -070085 */
86 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -080087 public void testFilteringTreatmentPermit() throws Exception {
Yi Tsengfa4a1c72017-11-03 10:22:38 -070088 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
89 PiAction mappedAction = interpreter.mapTreatment(treatment,
Yi Tseng43ee7e82018-04-12 16:37:34 +080090 FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN);
Yi Tsengfa4a1c72017-11-03 10:22:38 -070091 PiAction expectedAction = PiAction.builder()
Carmelo Casconeb5324e72018-11-25 02:26:32 -080092 .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070093 .build();
94
95 assertEquals(expectedAction, mappedAction);
96 }
97
98 /* Forwarding control block */
99
100 /**
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800101 * Map empty treatment for routing v4 table.
Yi Tsengdf3eec52018-02-15 14:56:02 -0800102 */
103 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800104 public void testRoutingV4TreatmentEmpty() throws Exception {
Yi Tsengdf3eec52018-02-15 14:56:02 -0800105 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800106 PiAction mappedAction = interpreter.mapTreatment(
107 treatment, FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V4);
Charles Chancf696e52018-08-16 16:25:13 -0700108 PiAction expectedAction = PiAction.builder()
Charles Chancd03f072018-08-31 17:46:37 -0700109 .withId(FabricConstants.FABRIC_INGRESS_FORWARDING_NOP_ROUTING_V4)
Charles Chancf696e52018-08-16 16:25:13 -0700110 .build();
111 assertEquals(expectedAction, mappedAction);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800112 }
Charles Chancf696e52018-08-16 16:25:13 -0700113
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800114 /**
115 * Map empty treatment for ACL table.
116 */
117 @Test
118 public void testAclTreatmentEmpty() throws Exception {
119 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
120 PiAction mappedAction = interpreter.mapTreatment(
121 treatment, FabricConstants.FABRIC_INGRESS_ACL_ACL);
122 PiAction expectedAction = PiAction.builder()
123 .withId(FabricConstants.FABRIC_INGRESS_ACL_NOP_ACL)
Charles Chancf696e52018-08-16 16:25:13 -0700124 .build();
125 assertEquals(expectedAction, mappedAction);
Yi Tsengdf3eec52018-02-15 14:56:02 -0800126 }
127
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700128 /* Next control block */
129
130 /**
131 * Map treatment to output action.
132 */
133 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800134 public void testNextTreatmentSimpleOutput() throws Exception {
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700135 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
136 .setOutput(PORT_1)
137 .build();
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800138 PiAction mappedAction = interpreter.mapTreatment(
139 treatment, FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE);
140 PiActionParam param = new PiActionParam(FabricConstants.PORT_NUM, PORT_1.toLong());
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700141 PiAction expectedAction = PiAction.builder()
Yi Tseng47eac892018-07-11 02:17:04 +0800142 .withId(FabricConstants.FABRIC_INGRESS_NEXT_OUTPUT_SIMPLE)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700143 .withParameter(param)
144 .build();
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700145 assertEquals(expectedAction, mappedAction);
146 }
147
148 /**
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800149 * Map treatment for hashed table to routing v4 action.
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700150 */
151 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800152 public void testNextTreatmentHashedRoutingV4() throws Exception {
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700153 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
154 .setEthSrc(SRC_MAC)
155 .setEthDst(DST_MAC)
156 .setOutput(PORT_1)
157 .build();
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800158 PiAction mappedAction = interpreter.mapTreatment(
159 treatment, FabricConstants.FABRIC_INGRESS_NEXT_HASHED);
160 PiActionParam ethSrcParam = new PiActionParam(FabricConstants.SMAC, SRC_MAC.toBytes());
161 PiActionParam ethDstParam = new PiActionParam(FabricConstants.DMAC, DST_MAC.toBytes());
162 PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM, PORT_1.toLong());
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700163 PiAction expectedAction = PiAction.builder()
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800164 .withId(FabricConstants.FABRIC_INGRESS_NEXT_ROUTING_HASHED)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700165 .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam))
166 .build();
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800167 assertEquals(expectedAction, mappedAction);
168 }
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700169
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800170 /**
171 * Map treatment for hashed table to routing v4 action.
172 */
173 @Test
174 public void testNextTreatmentHashedRoutingMpls() throws Exception {
175 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
176 .setEthSrc(SRC_MAC)
177 .setEthDst(DST_MAC)
178 .setOutput(PORT_1)
179 .pushMpls()
180 .setMpls(MPLS_10)
181 .build();
182 PiAction mappedAction = interpreter.mapTreatment(
183 treatment, FabricConstants.FABRIC_INGRESS_NEXT_HASHED);
184 PiActionParam ethSrcParam = new PiActionParam(FabricConstants.SMAC, SRC_MAC.toBytes());
185 PiActionParam ethDstParam = new PiActionParam(FabricConstants.DMAC, DST_MAC.toBytes());
186 PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM, PORT_1.toLong());
187 PiActionParam mplsParam = new PiActionParam(FabricConstants.LABEL, MPLS_10.toInt());
188 PiAction expectedAction = PiAction.builder()
189 .withId(FabricConstants.FABRIC_INGRESS_NEXT_MPLS_ROUTING_HASHED)
190 .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam, mplsParam))
191 .build();
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700192 assertEquals(expectedAction, mappedAction);
193 }
194
195 /**
196 * Map treatment to set_vlan_output action.
197 */
198 @Test
199 public void testNextTreatment3() throws Exception {
200 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
201 .setVlanId(VLAN_100)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700202 .build();
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800203 PiAction mappedAction = interpreter.mapTreatment(
204 treatment, FabricConstants.FABRIC_INGRESS_NEXT_NEXT_VLAN);
205 PiActionParam vlanParam = new PiActionParam(
206 FabricConstants.VLAN_ID, VLAN_100.toShort());
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700207 PiAction expectedAction = PiAction.builder()
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800208 .withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_VLAN)
209 .withParameter(vlanParam)
Yi Tseng1b154bd2017-11-20 17:48:19 -0800210 .build();
211 assertEquals(expectedAction, mappedAction);
212 }
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700213}