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.Test; |
| 23 | import org.onosproject.TestApplicationId; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.GroupId; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 29 | import org.onosproject.net.flow.TrafficTreatment; |
| 30 | import org.onosproject.net.group.DefaultGroup; |
| 31 | import org.onosproject.net.group.DefaultGroupBucket; |
| 32 | import org.onosproject.net.group.DefaultGroupDescription; |
| 33 | import org.onosproject.net.group.Group; |
| 34 | import org.onosproject.net.group.GroupBucket; |
| 35 | import org.onosproject.net.group.GroupBuckets; |
| 36 | import org.onosproject.net.group.GroupDescription; |
| 37 | import org.onosproject.net.pi.runtime.PiGroupKey; |
| 38 | import org.onosproject.net.pi.runtime.PiMulticastGroupEntry; |
| 39 | import org.onosproject.net.pi.runtime.PiPreReplica; |
| 40 | |
| 41 | import java.util.List; |
| 42 | import java.util.Set; |
| 43 | |
| 44 | import static org.onosproject.net.group.GroupDescription.Type.ALL; |
| 45 | import static org.onosproject.pipelines.basic.BasicConstants.ACT_PRF_WCMP_SELECTOR_ID; |
| 46 | import static org.onosproject.pipelines.basic.BasicConstants.TBL_WCMP_TABLE_ID; |
| 47 | |
| 48 | /** |
| 49 | * Test for {@link PiMulticastGroupTranslatorImpl}. |
| 50 | */ |
| 51 | public class PiMulticastGroupTranslatorImplTest { |
| 52 | private static final DeviceId DEVICE_ID = DeviceId.deviceId("device:dummy:1"); |
| 53 | private static final ApplicationId APP_ID = TestApplicationId.create("dummy"); |
| 54 | private static final GroupId GROUP_ID = GroupId.valueOf(1); |
| 55 | private static final PiGroupKey GROUP_KEY = new PiGroupKey( |
| 56 | TBL_WCMP_TABLE_ID, ACT_PRF_WCMP_SELECTOR_ID, GROUP_ID.id()); |
| 57 | |
| 58 | private static final List<GroupBucket> BUCKET_LIST = ImmutableList.of( |
| 59 | allOutputBucket(1), |
| 60 | allOutputBucket(2), |
| 61 | allOutputBucket(3)); |
| 62 | private static final List<GroupBucket> BUCKET_LIST_2 = ImmutableList.of( |
| 63 | allOutputBucket(1), |
| 64 | allOutputBucket(2), |
| 65 | allOutputBucket(2), |
| 66 | allOutputBucket(3), |
| 67 | allOutputBucket(3)); |
| 68 | |
| 69 | private static final Set<PiPreReplica> REPLICAS = ImmutableSet.of( |
| 70 | new PiPreReplica(PortNumber.portNumber(1), 1), |
| 71 | new PiPreReplica(PortNumber.portNumber(2), 1), |
| 72 | new PiPreReplica(PortNumber.portNumber(3), 1)); |
| 73 | private static final Set<PiPreReplica> REPLICAS_2 = ImmutableSet.of( |
| 74 | new PiPreReplica(PortNumber.portNumber(1), 1), |
| 75 | new PiPreReplica(PortNumber.portNumber(2), 1), |
| 76 | new PiPreReplica(PortNumber.portNumber(2), 2), |
| 77 | new PiPreReplica(PortNumber.portNumber(3), 1), |
| 78 | new PiPreReplica(PortNumber.portNumber(3), 2)); |
| 79 | |
| 80 | private static final PiMulticastGroupEntry PI_MULTICAST_GROUP = |
| 81 | PiMulticastGroupEntry.builder() |
| 82 | .withGroupId(GROUP_ID.id()) |
| 83 | .addReplicas(REPLICAS) |
| 84 | .build(); |
| 85 | private static final PiMulticastGroupEntry PI_MULTICAST_GROUP_2 = |
| 86 | PiMulticastGroupEntry.builder() |
| 87 | .withGroupId(GROUP_ID.id()) |
| 88 | .addReplicas(REPLICAS_2) |
| 89 | .build(); |
| 90 | |
| 91 | private static final GroupBuckets BUCKETS = new GroupBuckets(BUCKET_LIST); |
| 92 | private static final GroupBuckets BUCKETS_2 = new GroupBuckets(BUCKET_LIST_2); |
| 93 | |
| 94 | private static final GroupDescription ALL_GROUP_DESC = new DefaultGroupDescription( |
| 95 | DEVICE_ID, ALL, BUCKETS, GROUP_KEY, GROUP_ID.id(), APP_ID); |
| 96 | private static final Group ALL_GROUP = new DefaultGroup(GROUP_ID, ALL_GROUP_DESC); |
| 97 | |
| 98 | private static final GroupDescription ALL_GROUP_DESC_2 = new DefaultGroupDescription( |
| 99 | DEVICE_ID, ALL, BUCKETS_2, GROUP_KEY, GROUP_ID.id(), APP_ID); |
| 100 | private static final Group ALL_GROUP_2 = new DefaultGroup(GROUP_ID, ALL_GROUP_DESC_2); |
| 101 | |
| 102 | |
| 103 | private static GroupBucket allOutputBucket(int portNum) { |
| 104 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 105 | .setOutput(PortNumber.portNumber(portNum)) |
| 106 | .build(); |
| 107 | return DefaultGroupBucket.createAllGroupBucket(treatment); |
| 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void testTranslatePreGroups() throws Exception { |
| 112 | |
| 113 | PiMulticastGroupEntry multicastGroup = PiMulticastGroupTranslatorImpl |
| 114 | .translate(ALL_GROUP); |
| 115 | PiMulticastGroupEntry multicastGroup2 = PiMulticastGroupTranslatorImpl |
| 116 | .translate(ALL_GROUP_2); |
| 117 | |
| 118 | new EqualsTester() |
| 119 | .addEqualityGroup(multicastGroup, PI_MULTICAST_GROUP) |
| 120 | .addEqualityGroup(multicastGroup2, PI_MULTICAST_GROUP_2) |
| 121 | .testEquals(); |
| 122 | } |
| 123 | } |