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; |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 33 | import org.onosproject.net.group.DefaultGroupKey; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 34 | import org.onosproject.net.group.Group; |
| 35 | import org.onosproject.net.group.GroupBucket; |
| 36 | import org.onosproject.net.group.GroupBuckets; |
| 37 | import org.onosproject.net.group.GroupDescription; |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 38 | import org.onosproject.net.group.GroupKey; |
| 39 | import org.onosproject.net.pi.runtime.PiCloneSessionEntry; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 40 | import org.onosproject.net.pi.runtime.PiMulticastGroupEntry; |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 41 | import org.onosproject.net.pi.runtime.PiPreEntry; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 42 | import org.onosproject.net.pi.runtime.PiPreReplica; |
| 43 | |
| 44 | import java.util.List; |
| 45 | import java.util.Set; |
| 46 | |
| 47 | import static org.onosproject.net.group.GroupDescription.Type.ALL; |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 48 | import static org.onosproject.net.group.GroupDescription.Type.CLONE; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 49 | |
| 50 | /** |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 51 | * Test for {@link PiReplicationGroupTranslatorImpl}. |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 52 | */ |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 53 | public class PiReplicationGroupTranslatorImplTest { |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 54 | private static final DeviceId DEVICE_ID = DeviceId.deviceId("device:dummy:1"); |
| 55 | private static final ApplicationId APP_ID = TestApplicationId.create("dummy"); |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 56 | private static final int ENTRY_ID = 99; |
| 57 | private static final GroupId GROUP_ID = GroupId.valueOf(ENTRY_ID); |
| 58 | private static final GroupKey GROUP_KEY = new DefaultGroupKey( |
| 59 | String.valueOf(GROUP_ID.id()).getBytes()); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 60 | |
| 61 | private static final List<GroupBucket> BUCKET_LIST = ImmutableList.of( |
| 62 | allOutputBucket(1), |
| 63 | allOutputBucket(2), |
| 64 | allOutputBucket(3)); |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 65 | private static final List<GroupBucket> CLONE_BUCKET_LIST = ImmutableList.of( |
| 66 | cloneOutputBucket(1), |
| 67 | cloneOutputBucket(2), |
| 68 | cloneOutputBucket(3)); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 69 | private static final List<GroupBucket> BUCKET_LIST_2 = ImmutableList.of( |
| 70 | allOutputBucket(1), |
| 71 | allOutputBucket(2), |
| 72 | allOutputBucket(2), |
| 73 | allOutputBucket(3), |
| 74 | allOutputBucket(3)); |
| 75 | |
| 76 | private static final Set<PiPreReplica> REPLICAS = ImmutableSet.of( |
Carmelo Cascone | 8767511 | 2020-08-04 17:19:40 -0700 | [diff] [blame^] | 77 | new PiPreReplica(PortNumber.portNumber(1), 0), |
| 78 | new PiPreReplica(PortNumber.portNumber(2), 0), |
| 79 | new PiPreReplica(PortNumber.portNumber(3), 0)); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 80 | private static final Set<PiPreReplica> REPLICAS_2 = ImmutableSet.of( |
Carmelo Cascone | 8767511 | 2020-08-04 17:19:40 -0700 | [diff] [blame^] | 81 | new PiPreReplica(PortNumber.portNumber(1), 0), |
| 82 | new PiPreReplica(PortNumber.portNumber(2), 0), |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 83 | new PiPreReplica(PortNumber.portNumber(2), 1), |
Carmelo Cascone | 8767511 | 2020-08-04 17:19:40 -0700 | [diff] [blame^] | 84 | new PiPreReplica(PortNumber.portNumber(3), 0), |
| 85 | new PiPreReplica(PortNumber.portNumber(3), 1)); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 86 | |
| 87 | private static final PiMulticastGroupEntry PI_MULTICAST_GROUP = |
| 88 | PiMulticastGroupEntry.builder() |
| 89 | .withGroupId(GROUP_ID.id()) |
| 90 | .addReplicas(REPLICAS) |
| 91 | .build(); |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 92 | private static final PiCloneSessionEntry PI_CLONE_SESSION_ENTRY = |
| 93 | PiCloneSessionEntry.builder() |
| 94 | .withSessionId(ENTRY_ID) |
| 95 | .addReplicas(REPLICAS) |
| 96 | .build(); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 97 | private static final PiMulticastGroupEntry PI_MULTICAST_GROUP_2 = |
| 98 | PiMulticastGroupEntry.builder() |
| 99 | .withGroupId(GROUP_ID.id()) |
| 100 | .addReplicas(REPLICAS_2) |
| 101 | .build(); |
| 102 | |
| 103 | private static final GroupBuckets BUCKETS = new GroupBuckets(BUCKET_LIST); |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 104 | private static final GroupBuckets CLONE_BUCKETS = new GroupBuckets(CLONE_BUCKET_LIST); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 105 | private static final GroupBuckets BUCKETS_2 = new GroupBuckets(BUCKET_LIST_2); |
| 106 | |
| 107 | private static final GroupDescription ALL_GROUP_DESC = new DefaultGroupDescription( |
| 108 | DEVICE_ID, ALL, BUCKETS, GROUP_KEY, GROUP_ID.id(), APP_ID); |
| 109 | private static final Group ALL_GROUP = new DefaultGroup(GROUP_ID, ALL_GROUP_DESC); |
| 110 | |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 111 | private static final GroupDescription CLONE_GROUP_DESC = new DefaultGroupDescription( |
| 112 | DEVICE_ID, CLONE, CLONE_BUCKETS, GROUP_KEY, GROUP_ID.id(), APP_ID); |
| 113 | private static final Group CLONE_GROUP = new DefaultGroup(GROUP_ID, CLONE_GROUP_DESC); |
| 114 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 115 | private static final GroupDescription ALL_GROUP_DESC_2 = new DefaultGroupDescription( |
| 116 | DEVICE_ID, ALL, BUCKETS_2, GROUP_KEY, GROUP_ID.id(), APP_ID); |
| 117 | private static final Group ALL_GROUP_2 = new DefaultGroup(GROUP_ID, ALL_GROUP_DESC_2); |
| 118 | |
| 119 | |
| 120 | private static GroupBucket allOutputBucket(int portNum) { |
| 121 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 122 | .setOutput(PortNumber.portNumber(portNum)) |
| 123 | .build(); |
| 124 | return DefaultGroupBucket.createAllGroupBucket(treatment); |
| 125 | } |
| 126 | |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 127 | private static GroupBucket cloneOutputBucket(int portNum) { |
| 128 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 129 | .setOutput(PortNumber.portNumber(portNum)) |
| 130 | .build(); |
| 131 | return DefaultGroupBucket.createCloneGroupBucket(treatment); |
| 132 | } |
| 133 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 134 | @Test |
| 135 | public void testTranslatePreGroups() throws Exception { |
| 136 | |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 137 | PiPreEntry multicastGroup = PiReplicationGroupTranslatorImpl |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 138 | .translate(ALL_GROUP, null, null); |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 139 | PiPreEntry multicastGroup2 = PiReplicationGroupTranslatorImpl |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 140 | .translate(ALL_GROUP_2, null, null); |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 141 | PiPreEntry cloneSessionEntry = PiReplicationGroupTranslatorImpl |
| 142 | .translate(CLONE_GROUP, null, null); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 143 | |
| 144 | new EqualsTester() |
| 145 | .addEqualityGroup(multicastGroup, PI_MULTICAST_GROUP) |
| 146 | .addEqualityGroup(multicastGroup2, PI_MULTICAST_GROUP_2) |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 147 | .addEqualityGroup(cloneSessionEntry, PI_CLONE_SESSION_ENTRY) |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 148 | .testEquals(); |
| 149 | } |
| 150 | } |