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; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.util.ImmutableByteSequence; |
| 25 | import org.onosproject.TestApplicationId; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.GroupId; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 30 | import org.onosproject.net.flow.TrafficTreatment; |
| 31 | import org.onosproject.net.flow.instructions.Instructions; |
| 32 | import org.onosproject.net.group.DefaultGroup; |
| 33 | import org.onosproject.net.group.DefaultGroupBucket; |
| 34 | import org.onosproject.net.group.DefaultGroupDescription; |
| 35 | import org.onosproject.net.group.Group; |
| 36 | import org.onosproject.net.group.GroupBucket; |
| 37 | import org.onosproject.net.group.GroupBuckets; |
| 38 | import org.onosproject.net.group.GroupDescription; |
| 39 | import org.onosproject.net.pi.model.PiPipeconf; |
| 40 | import org.onosproject.net.pi.runtime.PiAction; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 41 | import org.onosproject.net.pi.runtime.PiActionParam; |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 42 | import org.onosproject.net.pi.runtime.PiActionProfileGroup; |
| 43 | import org.onosproject.net.pi.runtime.PiActionProfileMember; |
| 44 | import org.onosproject.net.pi.runtime.PiActionProfileMemberId; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 45 | import org.onosproject.net.pi.runtime.PiGroupKey; |
| 46 | import org.onosproject.net.pi.runtime.PiTableAction; |
| 47 | import org.onosproject.pipelines.basic.PipeconfLoader; |
| 48 | |
| 49 | import java.util.Collection; |
| 50 | import java.util.List; |
| 51 | |
| 52 | import static org.hamcrest.CoreMatchers.equalTo; |
| 53 | import static org.hamcrest.CoreMatchers.is; |
| 54 | import static org.hamcrest.MatcherAssert.assertThat; |
| 55 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
| 56 | import static org.onosproject.net.group.GroupDescription.Type.SELECT; |
Carmelo Cascone | 776be38 | 2018-12-12 19:03:57 -0800 | [diff] [blame] | 57 | import static org.onosproject.pipelines.basic.BasicConstants.INGRESS_WCMP_CONTROL_SET_EGRESS_PORT; |
| 58 | import static org.onosproject.pipelines.basic.BasicConstants.INGRESS_WCMP_CONTROL_WCMP_SELECTOR; |
| 59 | import static org.onosproject.pipelines.basic.BasicConstants.INGRESS_WCMP_CONTROL_WCMP_TABLE; |
| 60 | import static org.onosproject.pipelines.basic.BasicConstants.PORT; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Test for {@link PiGroupTranslatorImpl}. |
| 64 | */ |
| 65 | public class PiGroupTranslatorImplTest { |
| 66 | |
| 67 | private static final DeviceId DEVICE_ID = DeviceId.deviceId("device:dummy:1"); |
| 68 | private static final ApplicationId APP_ID = TestApplicationId.create("dummy"); |
| 69 | private static final GroupId GROUP_ID = GroupId.valueOf(1); |
| 70 | private static final PiGroupKey GROUP_KEY = new PiGroupKey( |
Carmelo Cascone | 776be38 | 2018-12-12 19:03:57 -0800 | [diff] [blame] | 71 | 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] | 72 | private static final List<GroupBucket> BUCKET_LIST = ImmutableList.of( |
| 73 | selectOutputBucket(1), |
| 74 | selectOutputBucket(2), |
| 75 | selectOutputBucket(3)); |
| 76 | private static final GroupBuckets BUCKETS = new GroupBuckets(BUCKET_LIST); |
| 77 | private static final GroupDescription SELECT_GROUP_DESC = new DefaultGroupDescription( |
| 78 | DEVICE_ID, SELECT, BUCKETS, GROUP_KEY, GROUP_ID.id(), APP_ID); |
| 79 | private static final Group SELECT_GROUP = new DefaultGroup(GROUP_ID, SELECT_GROUP_DESC); |
| 80 | private static final int DEFAULT_MEMBER_WEIGHT = 1; |
| 81 | private static final int BASE_MEM_ID = 65535; |
Carmelo Cascone | 776be38 | 2018-12-12 19:03:57 -0800 | [diff] [blame] | 82 | private static final int PORT_BITWIDTH = 9; |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 83 | private Collection<PiActionProfileMember> expectedMembers; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 84 | |
| 85 | private PiPipeconf pipeconf; |
| 86 | |
| 87 | @Before |
| 88 | public void setUp() throws Exception { |
| 89 | pipeconf = PipeconfLoader.BASIC_PIPECONF; |
| 90 | expectedMembers = ImmutableSet.of(outputMember(1), |
| 91 | outputMember(2), |
| 92 | outputMember(3)); |
| 93 | } |
| 94 | |
| 95 | private static GroupBucket selectOutputBucket(int portNum) { |
| 96 | ImmutableByteSequence paramVal = copyFrom(portNum); |
Carmelo Cascone | 776be38 | 2018-12-12 19:03:57 -0800 | [diff] [blame] | 97 | PiActionParam param = new PiActionParam(PORT, paramVal); |
| 98 | PiTableAction action = PiAction.builder() |
| 99 | .withId(INGRESS_WCMP_CONTROL_SET_EGRESS_PORT) |
| 100 | .withParameter(param).build(); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 101 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 102 | .add(Instructions.piTableAction(action)) |
| 103 | .build(); |
| 104 | return DefaultGroupBucket.createSelectGroupBucket(treatment); |
| 105 | } |
| 106 | |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 107 | private static PiActionProfileMember outputMember(int portNum) |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 108 | throws ImmutableByteSequence.ByteSequenceTrimException { |
Carmelo Cascone | 776be38 | 2018-12-12 19:03:57 -0800 | [diff] [blame] | 109 | PiActionParam param = new PiActionParam(PORT, copyFrom(portNum).fit(PORT_BITWIDTH)); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 110 | PiAction piAction = PiAction.builder() |
Carmelo Cascone | 776be38 | 2018-12-12 19:03:57 -0800 | [diff] [blame] | 111 | .withId(INGRESS_WCMP_CONTROL_SET_EGRESS_PORT) |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 112 | .withParameter(param).build(); |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 113 | return PiActionProfileMember.builder() |
Carmelo Cascone | 776be38 | 2018-12-12 19:03:57 -0800 | [diff] [blame] | 114 | .forActionProfile(INGRESS_WCMP_CONTROL_WCMP_SELECTOR) |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 115 | .withAction(piAction) |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 116 | .withId(PiActionProfileMemberId.of(BASE_MEM_ID + portNum)) |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 117 | .withWeight(DEFAULT_MEMBER_WEIGHT) |
| 118 | .build(); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Test add group with buckets. |
| 123 | */ |
| 124 | @Test |
| 125 | public void testTranslateGroups() throws Exception { |
| 126 | |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 127 | PiActionProfileGroup piGroup1 = PiGroupTranslatorImpl.translate(SELECT_GROUP, pipeconf, null); |
| 128 | PiActionProfileGroup piGroup2 = PiGroupTranslatorImpl.translate(SELECT_GROUP, pipeconf, null); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 129 | |
| 130 | new EqualsTester() |
| 131 | .addEqualityGroup(piGroup1, piGroup2) |
| 132 | .testEquals(); |
| 133 | |
| 134 | assertThat("Group ID must be equal", |
| 135 | piGroup1.id().id(), is(equalTo(GROUP_ID.id()))); |
| 136 | assertThat("Action profile ID must be equal", |
Carmelo Cascone | 776be38 | 2018-12-12 19:03:57 -0800 | [diff] [blame] | 137 | piGroup1.actionProfileId(), is(equalTo(INGRESS_WCMP_CONTROL_WCMP_SELECTOR))); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 138 | |
| 139 | // members installed |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 140 | Collection<PiActionProfileMember> members = piGroup1.members(); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 141 | assertThat("The number of group members must be equal", |
| 142 | piGroup1.members().size(), is(expectedMembers.size())); |
| 143 | assertThat("Group members must be equal", |
| 144 | members.containsAll(expectedMembers) && expectedMembers.containsAll(members)); |
| 145 | } |
| 146 | } |