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