Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.pipelines.fabric.pipeliner; |
| 18 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 20 | import org.junit.Ignore; |
| 21 | import org.junit.Test; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 22 | import org.onosproject.net.flow.DefaultFlowRule; |
| 23 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 24 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 25 | import org.onosproject.net.flow.FlowRule; |
| 26 | import org.onosproject.net.flow.TrafficSelector; |
| 27 | import org.onosproject.net.flow.TrafficTreatment; |
| 28 | import org.onosproject.net.flow.criteria.PiCriterion; |
| 29 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 30 | import org.onosproject.net.flowobjective.NextObjective; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 31 | import org.onosproject.net.group.DefaultGroupBucket; |
| 32 | import org.onosproject.net.group.DefaultGroupDescription; |
| 33 | import org.onosproject.net.group.GroupBucket; |
| 34 | import org.onosproject.net.group.GroupBuckets; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 35 | import org.onosproject.net.group.GroupDescription; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 36 | import org.onosproject.net.pi.runtime.PiActionGroupId; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 37 | import org.onosproject.net.pi.runtime.PiGroupKey; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 38 | import org.onosproject.pipelines.fabric.FabricConstants; |
| 39 | |
| 40 | import java.util.List; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 41 | import java.util.stream.Collectors; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 42 | |
| 43 | import static org.junit.Assert.assertEquals; |
| 44 | import static org.junit.Assert.assertTrue; |
Yi Tseng | f55eaa8 | 2017-11-29 15:51:28 -0800 | [diff] [blame^] | 45 | import static org.onosproject.pipelines.fabric.FabricConstants.ACT_PRF_NEXT_ECMP_SELECTOR_ID; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 46 | import static org.onosproject.pipelines.fabric.FabricConstants.TBL_HASHED_ID; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Test cases for fabric.p4 pipeline next control block. |
| 50 | */ |
| 51 | public 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 Tseng | dbe0560 | 2017-11-17 18:02:43 -0800 | [diff] [blame] | 76 | /** |
| 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 Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 89 | 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 Tseng | f55eaa8 | 2017-11-29 15:51:28 -0800 | [diff] [blame^] | 103 | assertEquals(1, flowRulesInstalled.size()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 104 | assertTrue(groupsInstalled.isEmpty()); |
| 105 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 106 | // Simple table |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 107 | PiCriterion nextIdCriterion = PiCriterion.builder() |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 108 | .matchExact(FabricConstants.HF_FABRIC_METADATA_NEXT_ID_ID, NEXT_ID_1) |
| 109 | .build(); |
| 110 | TrafficSelector nextIdSelector = DefaultTrafficSelector.builder() |
| 111 | .matchPi(nextIdCriterion) |
| 112 | .build(); |
Yi Tseng | f55eaa8 | 2017-11-29 15:51:28 -0800 | [diff] [blame^] | 113 | FlowRule actualFlowRule = flowRulesInstalled.get(0); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 114 | 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 Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 127 | /** |
| 128 | * Test program ecmp output group for Hashed table. |
| 129 | */ |
| 130 | @Test |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 131 | 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 Tseng | f55eaa8 | 2017-11-29 15:51:28 -0800 | [diff] [blame^] | 158 | assertEquals(1, flowRulesInstalled.size()); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 159 | assertEquals(1, groupsInstalled.size()); |
| 160 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 161 | // 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 Tseng | f55eaa8 | 2017-11-29 15:51:28 -0800 | [diff] [blame^] | 172 | FlowRule actualFlowRule = flowRulesInstalled.get(0); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 173 | 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 Tseng | f55eaa8 | 2017-11-29 15:51:28 -0800 | [diff] [blame^] | 193 | PiGroupKey groupKey = new PiGroupKey(TBL_HASHED_ID, ACT_PRF_NEXT_ECMP_SELECTOR_ID, NEXT_ID_1); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 194 | 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 Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 203 | |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Test program output group for Broadcast table. |
| 208 | */ |
| 209 | @Test |
| 210 | @Ignore |
| 211 | public void testBroadcastOutput() { |
| 212 | |
| 213 | } |
| 214 | } |