blob: 8493ca6cd4a10f48504ad85f009bc7de55967c64 [file] [log] [blame]
Yi Tseng0b809722017-11-03 10:23:26 -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.pipeliner;
18
Yi Tseng1b154bd2017-11-20 17:48:19 -080019import com.google.common.collect.ImmutableList;
Yi Tseng0b809722017-11-03 10:23:26 -070020import org.junit.Ignore;
21import org.junit.Test;
22import org.onlab.util.ImmutableByteSequence;
23import org.onosproject.net.flow.DefaultFlowRule;
24import org.onosproject.net.flow.DefaultTrafficSelector;
25import org.onosproject.net.flow.DefaultTrafficTreatment;
26import org.onosproject.net.flow.FlowRule;
27import org.onosproject.net.flow.TrafficSelector;
28import org.onosproject.net.flow.TrafficTreatment;
29import org.onosproject.net.flow.criteria.PiCriterion;
30import org.onosproject.net.flowobjective.DefaultNextObjective;
31import org.onosproject.net.flowobjective.NextObjective;
Yi Tseng1b154bd2017-11-20 17:48:19 -080032import org.onosproject.net.group.DefaultGroupBucket;
33import org.onosproject.net.group.DefaultGroupDescription;
34import org.onosproject.net.group.GroupBucket;
35import org.onosproject.net.group.GroupBuckets;
Yi Tseng0b809722017-11-03 10:23:26 -070036import org.onosproject.net.group.GroupDescription;
37import org.onosproject.net.pi.runtime.PiAction;
Yi Tseng1b154bd2017-11-20 17:48:19 -080038import org.onosproject.net.pi.runtime.PiActionGroupId;
Yi Tseng0b809722017-11-03 10:23:26 -070039import org.onosproject.net.pi.runtime.PiActionParam;
Yi Tseng1b154bd2017-11-20 17:48:19 -080040import org.onosproject.net.pi.runtime.PiGroupKey;
Yi Tseng0b809722017-11-03 10:23:26 -070041import org.onosproject.pipelines.fabric.FabricConstants;
42
43import java.util.List;
Yi Tseng1b154bd2017-11-20 17:48:19 -080044import java.util.stream.Collectors;
Yi Tseng0b809722017-11-03 10:23:26 -070045
46import static org.junit.Assert.assertEquals;
47import static org.junit.Assert.assertTrue;
Yi Tseng1b154bd2017-11-20 17:48:19 -080048import static org.onosproject.pipelines.fabric.FabricConstants.ACT_PRF_ECMP_SELECTOR_ID;
49import static org.onosproject.pipelines.fabric.FabricConstants.TBL_HASHED_ID;
Yi Tseng0b809722017-11-03 10:23:26 -070050
51/**
52 * Test cases for fabric.p4 pipeline next control block.
53 */
54public class FabricNextPipelinerTest extends FabricPipelinerTest {
55
56 /**
57 * Test program output rule for Simple table.
58 */
59 @Test
60 public void testSimpleOutput() {
61 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
62 .setOutput(PORT_1)
63 .build();
64 testSimple(treatment);
65 }
66
67 /**
68 * Test program set vlan and output rule for Simple table.
69 */
70 @Test
71 public void testSimpleOutputWithVlanTranslation() {
72 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
73 .setVlanId(VLAN_100)
74 .setOutput(PORT_1)
75 .build();
76 testSimple(treatment);
77 }
78
Yi Tsengdbe05602017-11-17 18:02:43 -080079 /**
80 * Test program set mac and output rule for Simple table.
81 */
82 @Test
83 public void testSimpleOutputWithMacTranslation() {
84 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
85 .setEthSrc(ROUTER_MAC)
86 .setEthDst(HOST_MAC)
87 .setOutput(PORT_1)
88 .build();
89 testSimple(treatment);
90 }
91
Yi Tseng0b809722017-11-03 10:23:26 -070092 private void testSimple(TrafficTreatment treatment) {
93 NextObjective nextObjective = DefaultNextObjective.builder()
94 .withId(NEXT_ID_1)
95 .withPriority(PRIORITY)
96 .addTreatment(treatment)
97 .withType(NextObjective.Type.SIMPLE)
98 .makePermanent()
99 .fromApp(APP_ID)
100 .add();
101
102 PipelinerTranslationResult result = pipeliner.pipelinerNext.next(nextObjective);
103
104 List<FlowRule> flowRulesInstalled = (List<FlowRule>) result.flowRules();
105 List<GroupDescription> groupsInstalled = (List<GroupDescription>) result.groups();
106 assertEquals(2, flowRulesInstalled.size());
107 assertTrue(groupsInstalled.isEmpty());
108
Yi Tseng1b154bd2017-11-20 17:48:19 -0800109 verifyNextIdMapping(flowRulesInstalled.get(0), NEXT_TYPE_SIMPLE);
Yi Tseng0b809722017-11-03 10:23:26 -0700110
Yi Tseng1b154bd2017-11-20 17:48:19 -0800111 // Simple table
Yi Tseng0b809722017-11-03 10:23:26 -0700112 PiCriterion nextIdCriterion = PiCriterion.builder()
Yi Tseng1b154bd2017-11-20 17:48:19 -0800113 .matchExact(FabricConstants.HF_FABRIC_METADATA_NEXT_ID_ID, NEXT_ID_1)
114 .build();
115 TrafficSelector nextIdSelector = DefaultTrafficSelector.builder()
116 .matchPi(nextIdCriterion)
117 .build();
118 FlowRule actualFlowRule = flowRulesInstalled.get(1);
119 FlowRule expectedFlowRule = DefaultFlowRule.builder()
120 .forDevice(DEVICE_ID)
121 .fromApp(APP_ID)
122 .makePermanent()
123 // FIXME: currently next objective doesn't support priority, ignore this
124 .withPriority(0)
125 .forTable(FabricConstants.TBL_SIMPLE_ID)
126 .withSelector(nextIdSelector)
127 .withTreatment(treatment)
128 .build();
129 assertTrue(expectedFlowRule.exactMatch(actualFlowRule));
130 }
131
132 private void verifyNextIdMapping(FlowRule flowRule, byte nextType) {
133 FlowRule expectedFlowRule;
134 PiCriterion nextIdCriterion = PiCriterion.builder()
135 .matchExact(FabricConstants.HF_FABRIC_METADATA_NEXT_ID_ID, NEXT_ID_1)
Yi Tseng0b809722017-11-03 10:23:26 -0700136 .build();
137 TrafficSelector nextIdSelector = DefaultTrafficSelector.builder()
138 .matchPi(nextIdCriterion)
139 .build();
140 PiActionParam setNextToSimpleParam = new PiActionParam(FabricConstants.ACT_PRM_NEXT_TYPE_ID,
Yi Tseng1b154bd2017-11-20 17:48:19 -0800141 ImmutableByteSequence.copyFrom(nextType));
Yi Tseng0b809722017-11-03 10:23:26 -0700142 PiAction setNextToSimpleAction = PiAction.builder()
143 .withId(FabricConstants.ACT_SET_NEXT_TYPE_ID)
144 .withParameter(setNextToSimpleParam)
145 .build();
146 TrafficTreatment setNextTypeTreatment = DefaultTrafficTreatment.builder()
147 .piTableAction(setNextToSimpleAction)
148 .build();
149 expectedFlowRule = DefaultFlowRule.builder()
150 .forDevice(DEVICE_ID)
151 .fromApp(APP_ID)
152 .makePermanent()
153 // FIXME: currently next objective doesn't support priority, set priority to zero
154 .withPriority(0)
155 .forTable(FabricConstants.TBL_NEXT_ID_MAPPING_ID)
156 .withSelector(nextIdSelector)
157 .withTreatment(setNextTypeTreatment)
158 .build();
Yi Tseng1b154bd2017-11-20 17:48:19 -0800159 assertTrue(expectedFlowRule.exactMatch(flowRule));
Yi Tseng0b809722017-11-03 10:23:26 -0700160 }
161
162 /**
163 * Test program ecmp output group for Hashed table.
164 */
165 @Test
Yi Tseng1b154bd2017-11-20 17:48:19 -0800166 public void testHashedOutput() throws Exception {
167 TrafficTreatment treatment1 = DefaultTrafficTreatment.builder()
168 .setEthSrc(ROUTER_MAC)
169 .setEthDst(HOST_MAC)
170 .setOutput(PORT_1)
171 .build();
172 TrafficTreatment treatment2 = DefaultTrafficTreatment.builder()
173 .setEthSrc(ROUTER_MAC)
174 .setEthDst(HOST_MAC)
175 .setOutput(PORT_2)
176 .build();
177
178 NextObjective nextObjective = DefaultNextObjective.builder()
179 .withId(NEXT_ID_1)
180 .withPriority(PRIORITY)
181 .addTreatment(treatment1)
182 .addTreatment(treatment2)
183 .withType(NextObjective.Type.HASHED)
184 .makePermanent()
185 .fromApp(APP_ID)
186 .add();
187
188 PipelinerTranslationResult result = pipeliner.pipelinerNext.next(nextObjective);
189
190 // Should generates 2 flows and 1 group
191 List<FlowRule> flowRulesInstalled = (List<FlowRule>) result.flowRules();
192 List<GroupDescription> groupsInstalled = (List<GroupDescription>) result.groups();
193 assertEquals(2, flowRulesInstalled.size());
194 assertEquals(1, groupsInstalled.size());
195
196 verifyNextIdMapping(flowRulesInstalled.get(0), NEXT_TYPE_HASHED);
197
198 // Hashed table
199 PiCriterion nextIdCriterion = PiCriterion.builder()
200 .matchExact(FabricConstants.HF_FABRIC_METADATA_NEXT_ID_ID, NEXT_ID_1)
201 .build();
202 TrafficSelector nextIdSelector = DefaultTrafficSelector.builder()
203 .matchPi(nextIdCriterion)
204 .build();
205 PiActionGroupId actionGroupId = PiActionGroupId.of(NEXT_ID_1);
206 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
207 .piTableAction(actionGroupId)
208 .build();
209 FlowRule actualFlowRule = flowRulesInstalled.get(1);
210 FlowRule expectedFlowRule = DefaultFlowRule.builder()
211 .forDevice(DEVICE_ID)
212 .fromApp(APP_ID)
213 .makePermanent()
214 // FIXME: currently next objective doesn't support priority, ignore this
215 .withPriority(0)
216 .forTable(TBL_HASHED_ID)
217 .withSelector(nextIdSelector)
218 .withTreatment(treatment)
219 .build();
220 assertTrue(expectedFlowRule.exactMatch(actualFlowRule));
221
222 // Group
223 GroupDescription actualGroup = groupsInstalled.get(0);
224 List<TrafficTreatment> treatments = ImmutableList.of(treatment1, treatment2);
225
226 List<GroupBucket> buckets = treatments.stream()
227 .map(DefaultGroupBucket::createSelectGroupBucket)
228 .collect(Collectors.toList());
229 GroupBuckets groupBuckets = new GroupBuckets(buckets);
230 PiGroupKey groupKey = new PiGroupKey(TBL_HASHED_ID, ACT_PRF_ECMP_SELECTOR_ID, NEXT_ID_1);
231 GroupDescription expectedGroup = new DefaultGroupDescription(
232 DEVICE_ID,
233 GroupDescription.Type.SELECT,
234 groupBuckets,
235 groupKey,
236 NEXT_ID_1,
237 APP_ID
238 );
239 assertEquals(expectedGroup, actualGroup);
Yi Tseng0b809722017-11-03 10:23:26 -0700240
241 }
242
243 /**
244 * Test program output group for Broadcast table.
245 */
246 @Test
247 @Ignore
248 public void testBroadcastOutput() {
249
250 }
251}