Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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.net.pi.impl; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import com.google.common.testing.EqualsTester; |
| 22 | import org.junit.Before; |
Daniele Moro | d900fe4 | 2021-02-11 16:12:57 +0100 | [diff] [blame^] | 23 | import org.junit.Rule; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 24 | import org.junit.Test; |
Daniele Moro | d900fe4 | 2021-02-11 16:12:57 +0100 | [diff] [blame^] | 25 | import org.junit.rules.ExpectedException; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 26 | import org.onlab.util.ImmutableByteSequence; |
| 27 | import org.onosproject.TestApplicationId; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.GroupId; |
| 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 32 | import org.onosproject.net.flow.TrafficTreatment; |
| 33 | import org.onosproject.net.flow.instructions.Instructions; |
| 34 | import org.onosproject.net.group.DefaultGroup; |
| 35 | import org.onosproject.net.group.DefaultGroupBucket; |
| 36 | import org.onosproject.net.group.DefaultGroupDescription; |
| 37 | import org.onosproject.net.group.Group; |
| 38 | import org.onosproject.net.group.GroupBucket; |
| 39 | import org.onosproject.net.group.GroupBuckets; |
| 40 | import org.onosproject.net.group.GroupDescription; |
Daniele Moro | d900fe4 | 2021-02-11 16:12:57 +0100 | [diff] [blame^] | 41 | import org.onosproject.net.pi.model.DefaultPiPipeconf; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 42 | import org.onosproject.net.pi.model.PiPipeconf; |
Daniele Moro | d900fe4 | 2021-02-11 16:12:57 +0100 | [diff] [blame^] | 43 | import org.onosproject.net.pi.model.PiPipeconfId; |
| 44 | import org.onosproject.net.pi.model.PiPipelineModel; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 45 | import org.onosproject.net.pi.runtime.PiAction; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 46 | import org.onosproject.net.pi.runtime.PiActionParam; |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 47 | import org.onosproject.net.pi.runtime.PiActionProfileGroup; |
| 48 | import org.onosproject.net.pi.runtime.PiActionProfileMember; |
| 49 | import org.onosproject.net.pi.runtime.PiActionProfileMemberId; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 50 | import org.onosproject.net.pi.runtime.PiGroupKey; |
| 51 | import org.onosproject.net.pi.runtime.PiTableAction; |
Daniele Moro | d900fe4 | 2021-02-11 16:12:57 +0100 | [diff] [blame^] | 52 | import org.onosproject.net.pi.service.PiTranslationException; |
| 53 | import org.onosproject.p4runtime.model.P4InfoParser; |
| 54 | import org.onosproject.p4runtime.model.P4InfoParserException; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 55 | import org.onosproject.pipelines.basic.PipeconfLoader; |
| 56 | |
Daniele Moro | d900fe4 | 2021-02-11 16:12:57 +0100 | [diff] [blame^] | 57 | import java.net.URL; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 58 | import java.util.Collection; |
| 59 | import java.util.List; |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 60 | import java.util.Objects; |
| 61 | import java.util.stream.Collectors; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 62 | |
Daniele Moro | d900fe4 | 2021-02-11 16:12:57 +0100 | [diff] [blame^] | 63 | import static java.lang.String.format; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 64 | import static org.hamcrest.CoreMatchers.equalTo; |
| 65 | import static org.hamcrest.CoreMatchers.is; |
| 66 | import static org.hamcrest.MatcherAssert.assertThat; |
| 67 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
| 68 | import static org.onosproject.net.group.GroupDescription.Type.SELECT; |
Daniele Moro | d900fe4 | 2021-02-11 16:12:57 +0100 | [diff] [blame^] | 69 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT; |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 70 | import static org.onosproject.net.pi.runtime.PiActionProfileGroup.WeightedMember.DEFAULT_WEIGHT; |
Carmelo Cascone | 776be38 | 2018-12-12 19:03:57 -0800 | [diff] [blame] | 71 | import static org.onosproject.pipelines.basic.BasicConstants.INGRESS_WCMP_CONTROL_SET_EGRESS_PORT; |
| 72 | import static org.onosproject.pipelines.basic.BasicConstants.INGRESS_WCMP_CONTROL_WCMP_SELECTOR; |
| 73 | import static org.onosproject.pipelines.basic.BasicConstants.INGRESS_WCMP_CONTROL_WCMP_TABLE; |
| 74 | import static org.onosproject.pipelines.basic.BasicConstants.PORT; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Test for {@link PiGroupTranslatorImpl}. |
| 78 | */ |
| 79 | public class PiGroupTranslatorImplTest { |
| 80 | |
| 81 | private static final DeviceId DEVICE_ID = DeviceId.deviceId("device:dummy:1"); |
| 82 | private static final ApplicationId APP_ID = TestApplicationId.create("dummy"); |
| 83 | private static final GroupId GROUP_ID = GroupId.valueOf(1); |
| 84 | private static final PiGroupKey GROUP_KEY = new PiGroupKey( |
Carmelo Cascone | 776be38 | 2018-12-12 19:03:57 -0800 | [diff] [blame] | 85 | INGRESS_WCMP_CONTROL_WCMP_TABLE, INGRESS_WCMP_CONTROL_WCMP_SELECTOR, GROUP_ID.id()); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 86 | private static final List<GroupBucket> BUCKET_LIST = ImmutableList.of( |
| 87 | selectOutputBucket(1), |
| 88 | selectOutputBucket(2), |
| 89 | selectOutputBucket(3)); |
| 90 | private static final GroupBuckets BUCKETS = new GroupBuckets(BUCKET_LIST); |
| 91 | private static final GroupDescription SELECT_GROUP_DESC = new DefaultGroupDescription( |
| 92 | DEVICE_ID, SELECT, BUCKETS, GROUP_KEY, GROUP_ID.id(), APP_ID); |
| 93 | private static final Group SELECT_GROUP = new DefaultGroup(GROUP_ID, SELECT_GROUP_DESC); |
| 94 | private static final int DEFAULT_MEMBER_WEIGHT = 1; |
Carmelo Cascone | a4a89fb | 2019-08-20 02:03:10 -0700 | [diff] [blame] | 95 | private static final int BASE_MEM_ID = 991; |
Carmelo Cascone | 776be38 | 2018-12-12 19:03:57 -0800 | [diff] [blame] | 96 | private static final int PORT_BITWIDTH = 9; |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 97 | private Collection<PiActionProfileMember> expectedMemberInstances; |
| 98 | private Collection<PiActionProfileGroup.WeightedMember> expectedWeightedMembers; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 99 | |
| 100 | private PiPipeconf pipeconf; |
| 101 | |
Daniele Moro | d900fe4 | 2021-02-11 16:12:57 +0100 | [diff] [blame^] | 102 | // Derived from basic.p4info, with wcmp_table annotated with @oneshot |
| 103 | private static final String PATH_ONESHOT_P4INFO = "oneshot.p4info"; |
| 104 | private static final PiPipeconfId ONE_SHOT_PIPECONF_ID = new PiPipeconfId("org.onosproject.pipelines.wcmp_oneshot"); |
| 105 | private PiPipeconf pipeconfOneShot; |
| 106 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 107 | @Before |
| 108 | public void setUp() throws Exception { |
| 109 | pipeconf = PipeconfLoader.BASIC_PIPECONF; |
Daniele Moro | d900fe4 | 2021-02-11 16:12:57 +0100 | [diff] [blame^] | 110 | pipeconfOneShot = loadP4InfoPipeconf(ONE_SHOT_PIPECONF_ID, PATH_ONESHOT_P4INFO); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 111 | expectedMemberInstances = ImmutableSet.of(outputMember(1), |
| 112 | outputMember(2), |
| 113 | outputMember(3)); |
| 114 | expectedWeightedMembers = expectedMemberInstances.stream() |
| 115 | .map(m -> new PiActionProfileGroup.WeightedMember(m, DEFAULT_WEIGHT)) |
| 116 | .collect(Collectors.toSet()); |
| 117 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | private static GroupBucket selectOutputBucket(int portNum) { |
| 121 | ImmutableByteSequence paramVal = copyFrom(portNum); |
Carmelo Cascone | 776be38 | 2018-12-12 19:03:57 -0800 | [diff] [blame] | 122 | PiActionParam param = new PiActionParam(PORT, paramVal); |
| 123 | PiTableAction action = PiAction.builder() |
| 124 | .withId(INGRESS_WCMP_CONTROL_SET_EGRESS_PORT) |
| 125 | .withParameter(param).build(); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 126 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 127 | .add(Instructions.piTableAction(action)) |
| 128 | .build(); |
| 129 | return DefaultGroupBucket.createSelectGroupBucket(treatment); |
| 130 | } |
| 131 | |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 132 | private static PiActionProfileMember outputMember(int portNum) |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 133 | throws ImmutableByteSequence.ByteSequenceTrimException { |
Carmelo Cascone | 776be38 | 2018-12-12 19:03:57 -0800 | [diff] [blame] | 134 | PiActionParam param = new PiActionParam(PORT, copyFrom(portNum).fit(PORT_BITWIDTH)); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 135 | PiAction piAction = PiAction.builder() |
Carmelo Cascone | 776be38 | 2018-12-12 19:03:57 -0800 | [diff] [blame] | 136 | .withId(INGRESS_WCMP_CONTROL_SET_EGRESS_PORT) |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 137 | .withParameter(param).build(); |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 138 | return PiActionProfileMember.builder() |
Carmelo Cascone | 776be38 | 2018-12-12 19:03:57 -0800 | [diff] [blame] | 139 | .forActionProfile(INGRESS_WCMP_CONTROL_WCMP_SELECTOR) |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 140 | .withAction(piAction) |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 141 | .withId(PiActionProfileMemberId.of(BASE_MEM_ID + portNum)) |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 142 | .build(); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Test add group with buckets. |
| 147 | */ |
| 148 | @Test |
| 149 | public void testTranslateGroups() throws Exception { |
| 150 | |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 151 | PiActionProfileGroup piGroup1 = PiGroupTranslatorImpl.translate(SELECT_GROUP, pipeconf, null); |
| 152 | PiActionProfileGroup piGroup2 = PiGroupTranslatorImpl.translate(SELECT_GROUP, pipeconf, null); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 153 | |
| 154 | new EqualsTester() |
| 155 | .addEqualityGroup(piGroup1, piGroup2) |
| 156 | .testEquals(); |
| 157 | |
| 158 | assertThat("Group ID must be equal", |
| 159 | piGroup1.id().id(), is(equalTo(GROUP_ID.id()))); |
| 160 | assertThat("Action profile ID must be equal", |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 161 | piGroup1.actionProfile(), is(equalTo(INGRESS_WCMP_CONTROL_WCMP_SELECTOR))); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 162 | |
| 163 | // members installed |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 164 | Collection<PiActionProfileGroup.WeightedMember> weightedMembers = piGroup1.members(); |
| 165 | Collection<PiActionProfileMember> memberInstances = weightedMembers.stream() |
| 166 | .map(PiActionProfileGroup.WeightedMember::instance) |
| 167 | .filter(Objects::nonNull) |
| 168 | .collect(Collectors.toSet()); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 169 | assertThat("The number of group members must be equal", |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 170 | piGroup1.members().size(), is(expectedWeightedMembers.size())); |
| 171 | assertThat("Group weighted members must be equal", |
| 172 | weightedMembers.containsAll(expectedWeightedMembers) |
| 173 | && expectedWeightedMembers.containsAll(weightedMembers)); |
| 174 | assertThat("Group member instances must be equal", |
| 175 | memberInstances.containsAll(expectedMemberInstances) |
| 176 | && expectedMemberInstances.containsAll(memberInstances)); |
| 177 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 178 | } |
Daniele Moro | d900fe4 | 2021-02-11 16:12:57 +0100 | [diff] [blame^] | 179 | |
| 180 | @Rule |
| 181 | public ExpectedException thrown = ExpectedException.none(); |
| 182 | |
| 183 | /** |
| 184 | * Test add group with buckets. |
| 185 | */ |
| 186 | @Test |
| 187 | public void testTranslateGroupsOneShotError() throws Exception { |
| 188 | thrown.expect(PiTranslationException.class); |
| 189 | thrown.expectMessage(format("Table associated to action profile '%s' " + |
| 190 | "supports only one-shot action profile programming", |
| 191 | INGRESS_WCMP_CONTROL_WCMP_SELECTOR.id())); |
| 192 | PiGroupTranslatorImpl.translate(SELECT_GROUP, pipeconfOneShot, null); |
| 193 | } |
| 194 | |
| 195 | private static PiPipeconf loadP4InfoPipeconf(PiPipeconfId pipeconfId, String p4infoPath) { |
| 196 | final URL p4InfoUrl = PiGroupTranslatorImpl.class.getResource(p4infoPath); |
| 197 | final PiPipelineModel pipelineModel; |
| 198 | try { |
| 199 | pipelineModel = P4InfoParser.parse(p4InfoUrl); |
| 200 | } catch (P4InfoParserException e) { |
| 201 | throw new IllegalStateException(e); |
| 202 | } |
| 203 | return DefaultPiPipeconf.builder() |
| 204 | .withId(pipeconfId) |
| 205 | .withPipelineModel(pipelineModel) |
| 206 | .addExtension(P4_INFO_TEXT, p4InfoUrl) |
| 207 | .build(); |
| 208 | } |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 209 | } |