blob: 8446729fc3a6fc736c33ee5e6390822747910742 [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;
Yi Tseng0b809722017-11-03 10:23:26 -070022import org.onosproject.net.flow.DefaultFlowRule;
23import org.onosproject.net.flow.DefaultTrafficSelector;
24import org.onosproject.net.flow.DefaultTrafficTreatment;
25import org.onosproject.net.flow.FlowRule;
26import org.onosproject.net.flow.TrafficSelector;
27import org.onosproject.net.flow.TrafficTreatment;
28import org.onosproject.net.flow.criteria.PiCriterion;
29import org.onosproject.net.flowobjective.DefaultNextObjective;
30import org.onosproject.net.flowobjective.NextObjective;
Yi Tseng1b154bd2017-11-20 17:48:19 -080031import org.onosproject.net.group.DefaultGroupBucket;
32import org.onosproject.net.group.DefaultGroupDescription;
33import org.onosproject.net.group.GroupBucket;
34import org.onosproject.net.group.GroupBuckets;
Yi Tseng0b809722017-11-03 10:23:26 -070035import org.onosproject.net.group.GroupDescription;
Yi Tseng1b154bd2017-11-20 17:48:19 -080036import org.onosproject.net.pi.runtime.PiActionGroupId;
Yi Tseng1b154bd2017-11-20 17:48:19 -080037import org.onosproject.net.pi.runtime.PiGroupKey;
Yi Tseng0b809722017-11-03 10:23:26 -070038import org.onosproject.pipelines.fabric.FabricConstants;
39
40import java.util.List;
Yi Tseng1b154bd2017-11-20 17:48:19 -080041import java.util.stream.Collectors;
Yi Tseng0b809722017-11-03 10:23:26 -070042
43import static org.junit.Assert.assertEquals;
44import static org.junit.Assert.assertTrue;
Yi Tsengf55eaa82017-11-29 15:51:28 -080045import static org.onosproject.pipelines.fabric.FabricConstants.ACT_PRF_NEXT_ECMP_SELECTOR_ID;
Yi Tseng1b154bd2017-11-20 17:48:19 -080046import static org.onosproject.pipelines.fabric.FabricConstants.TBL_HASHED_ID;
Yi Tseng0b809722017-11-03 10:23:26 -070047
48/**
49 * Test cases for fabric.p4 pipeline next control block.
50 */
51public class FabricNextPipelinerTest extends FabricPipelinerTest {
52
53 /**
54 * Test program output rule for Simple table.
55 */
56 @Test
57 public void testSimpleOutput() {
58 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
59 .setOutput(PORT_1)
60 .build();
61 testSimple(treatment);
62 }
63
64 /**
65 * Test program set vlan and output rule for Simple table.
66 */
67 @Test
68 public void testSimpleOutputWithVlanTranslation() {
69 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
70 .setVlanId(VLAN_100)
71 .setOutput(PORT_1)
72 .build();
73 testSimple(treatment);
74 }
75
Yi Tsengdbe05602017-11-17 18:02:43 -080076 /**
77 * Test program set mac and output rule for Simple table.
78 */
79 @Test
80 public void testSimpleOutputWithMacTranslation() {
81 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
82 .setEthSrc(ROUTER_MAC)
83 .setEthDst(HOST_MAC)
84 .setOutput(PORT_1)
85 .build();
86 testSimple(treatment);
87 }
88
Yi Tseng0b809722017-11-03 10:23:26 -070089 private void testSimple(TrafficTreatment treatment) {
90 NextObjective nextObjective = DefaultNextObjective.builder()
91 .withId(NEXT_ID_1)
92 .withPriority(PRIORITY)
93 .addTreatment(treatment)
94 .withType(NextObjective.Type.SIMPLE)
95 .makePermanent()
96 .fromApp(APP_ID)
97 .add();
98
99 PipelinerTranslationResult result = pipeliner.pipelinerNext.next(nextObjective);
100
101 List<FlowRule> flowRulesInstalled = (List<FlowRule>) result.flowRules();
102 List<GroupDescription> groupsInstalled = (List<GroupDescription>) result.groups();
Yi Tsengf55eaa82017-11-29 15:51:28 -0800103 assertEquals(1, flowRulesInstalled.size());
Yi Tseng0b809722017-11-03 10:23:26 -0700104 assertTrue(groupsInstalled.isEmpty());
105
Yi Tseng1b154bd2017-11-20 17:48:19 -0800106 // Simple table
Yi Tseng0b809722017-11-03 10:23:26 -0700107 PiCriterion nextIdCriterion = PiCriterion.builder()
Yi Tseng1b154bd2017-11-20 17:48:19 -0800108 .matchExact(FabricConstants.HF_FABRIC_METADATA_NEXT_ID_ID, NEXT_ID_1)
109 .build();
110 TrafficSelector nextIdSelector = DefaultTrafficSelector.builder()
111 .matchPi(nextIdCriterion)
112 .build();
Yi Tsengf55eaa82017-11-29 15:51:28 -0800113 FlowRule actualFlowRule = flowRulesInstalled.get(0);
Yi Tseng1b154bd2017-11-20 17:48:19 -0800114 FlowRule expectedFlowRule = DefaultFlowRule.builder()
115 .forDevice(DEVICE_ID)
116 .fromApp(APP_ID)
117 .makePermanent()
118 // FIXME: currently next objective doesn't support priority, ignore this
119 .withPriority(0)
120 .forTable(FabricConstants.TBL_SIMPLE_ID)
121 .withSelector(nextIdSelector)
122 .withTreatment(treatment)
123 .build();
124 assertTrue(expectedFlowRule.exactMatch(actualFlowRule));
125 }
126
Yi Tseng0b809722017-11-03 10:23:26 -0700127 /**
128 * Test program ecmp output group for Hashed table.
129 */
130 @Test
Yi Tseng1b154bd2017-11-20 17:48:19 -0800131 public void testHashedOutput() throws Exception {
132 TrafficTreatment treatment1 = DefaultTrafficTreatment.builder()
133 .setEthSrc(ROUTER_MAC)
134 .setEthDst(HOST_MAC)
135 .setOutput(PORT_1)
136 .build();
137 TrafficTreatment treatment2 = DefaultTrafficTreatment.builder()
138 .setEthSrc(ROUTER_MAC)
139 .setEthDst(HOST_MAC)
140 .setOutput(PORT_2)
141 .build();
142
143 NextObjective nextObjective = DefaultNextObjective.builder()
144 .withId(NEXT_ID_1)
145 .withPriority(PRIORITY)
146 .addTreatment(treatment1)
147 .addTreatment(treatment2)
148 .withType(NextObjective.Type.HASHED)
149 .makePermanent()
150 .fromApp(APP_ID)
151 .add();
152
153 PipelinerTranslationResult result = pipeliner.pipelinerNext.next(nextObjective);
154
155 // Should generates 2 flows and 1 group
156 List<FlowRule> flowRulesInstalled = (List<FlowRule>) result.flowRules();
157 List<GroupDescription> groupsInstalled = (List<GroupDescription>) result.groups();
Yi Tsengf55eaa82017-11-29 15:51:28 -0800158 assertEquals(1, flowRulesInstalled.size());
Yi Tseng1b154bd2017-11-20 17:48:19 -0800159 assertEquals(1, groupsInstalled.size());
160
Yi Tseng1b154bd2017-11-20 17:48:19 -0800161 // Hashed table
162 PiCriterion nextIdCriterion = PiCriterion.builder()
163 .matchExact(FabricConstants.HF_FABRIC_METADATA_NEXT_ID_ID, NEXT_ID_1)
164 .build();
165 TrafficSelector nextIdSelector = DefaultTrafficSelector.builder()
166 .matchPi(nextIdCriterion)
167 .build();
168 PiActionGroupId actionGroupId = PiActionGroupId.of(NEXT_ID_1);
169 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
170 .piTableAction(actionGroupId)
171 .build();
Yi Tsengf55eaa82017-11-29 15:51:28 -0800172 FlowRule actualFlowRule = flowRulesInstalled.get(0);
Yi Tseng1b154bd2017-11-20 17:48:19 -0800173 FlowRule expectedFlowRule = DefaultFlowRule.builder()
174 .forDevice(DEVICE_ID)
175 .fromApp(APP_ID)
176 .makePermanent()
177 // FIXME: currently next objective doesn't support priority, ignore this
178 .withPriority(0)
179 .forTable(TBL_HASHED_ID)
180 .withSelector(nextIdSelector)
181 .withTreatment(treatment)
182 .build();
183 assertTrue(expectedFlowRule.exactMatch(actualFlowRule));
184
185 // Group
186 GroupDescription actualGroup = groupsInstalled.get(0);
187 List<TrafficTreatment> treatments = ImmutableList.of(treatment1, treatment2);
188
189 List<GroupBucket> buckets = treatments.stream()
190 .map(DefaultGroupBucket::createSelectGroupBucket)
191 .collect(Collectors.toList());
192 GroupBuckets groupBuckets = new GroupBuckets(buckets);
Yi Tsengf55eaa82017-11-29 15:51:28 -0800193 PiGroupKey groupKey = new PiGroupKey(TBL_HASHED_ID, ACT_PRF_NEXT_ECMP_SELECTOR_ID, NEXT_ID_1);
Yi Tseng1b154bd2017-11-20 17:48:19 -0800194 GroupDescription expectedGroup = new DefaultGroupDescription(
195 DEVICE_ID,
196 GroupDescription.Type.SELECT,
197 groupBuckets,
198 groupKey,
199 NEXT_ID_1,
200 APP_ID
201 );
202 assertEquals(expectedGroup, actualGroup);
Yi Tseng0b809722017-11-03 10:23:26 -0700203
204 }
205
206 /**
207 * Test program output group for Broadcast table.
208 */
209 @Test
210 @Ignore
211 public void testBroadcastOutput() {
212
213 }
214}