blob: 06246ca63353c914f288fa45ffde5db6ae97b30e [file] [log] [blame]
pier0d5865f2019-06-18 22:57:19 +02001/*
2 * Copyright 2019-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.provider.of.group.impl;
18
19import com.google.common.collect.Lists;
20import org.junit.Test;
21import org.onosproject.core.GroupId;
22import org.onosproject.net.PortNumber;
23import org.onosproject.net.flow.DefaultTrafficTreatment;
24import org.onosproject.net.flow.TrafficTreatment;
25import org.onosproject.net.group.DefaultGroupBucket;
26import org.onosproject.net.group.GroupBucket;
27import org.onosproject.net.group.GroupBuckets;
28import org.onosproject.net.group.GroupDescription;
29import org.projectfloodlight.openflow.protocol.OFGroupMod;
30import org.projectfloodlight.openflow.protocol.OFGroupModCommand;
31import org.projectfloodlight.openflow.protocol.OFGroupType;
32import org.projectfloodlight.openflow.protocol.ver13.OFFactoryVer13;
33
34import java.util.Optional;
35
36import static org.hamcrest.MatcherAssert.assertThat;
37import static org.hamcrest.Matchers.is;
38
39
40public class GroupModBuilderTest {
41
42 // Build the data needed for the groups
43 private static final TrafficTreatment PORT1_OUTPUT = DefaultTrafficTreatment.builder()
44 .setOutput(PortNumber.portNumber(1))
45 .build();
46 private static final GroupBucket PORT1_BUCKET = DefaultGroupBucket.createSelectGroupBucket(PORT1_OUTPUT);
47 private static final TrafficTreatment PORT2_OUTPUT = DefaultTrafficTreatment.builder()
48 .setOutput(PortNumber.portNumber(2))
49 .build();
50 private static final GroupBucket PORT2_BUCKET = DefaultGroupBucket.createSelectGroupBucket(PORT2_OUTPUT);
51 private static final GroupBuckets GROUP_BUCKETS = new GroupBuckets(Lists.newArrayList(PORT1_BUCKET,
52 PORT2_BUCKET));
53 private static final GroupId GROUP_ID = GroupId.valueOf(1);
54
55 @Test
56 public void groupAdd() {
57 OFGroupMod groupAdd = GroupModBuilder.builder(GROUP_BUCKETS,
58 GROUP_ID,
59 GroupDescription.Type.SELECT,
60 OFFactoryVer13.INSTANCE,
61 Optional.of(Long.MAX_VALUE)).buildGroupAdd();
62 assertThat(groupAdd.getBuckets().size(), is(2));
63 assertThat(groupAdd.getGroup().getGroupNumber(), is(GROUP_ID.id()));
64 assertThat(getGroupType(groupAdd.getGroupType()), is(GroupDescription.Type.SELECT));
65 assertThat(groupAdd.getXid(), is(Long.MAX_VALUE));
66 assertThat(groupAdd.getCommand(), is(OFGroupModCommand.ADD));
67 }
68
69 @Test
70 public void groupMod() {
71 OFGroupMod groupMod = GroupModBuilder.builder(GROUP_BUCKETS,
72 GROUP_ID,
73 GroupDescription.Type.INDIRECT,
74 OFFactoryVer13.INSTANCE,
75 Optional.of(Long.MAX_VALUE)).buildGroupMod();
76 assertThat(groupMod.getBuckets().size(), is(2));
77 assertThat(groupMod.getGroup().getGroupNumber(), is(GROUP_ID.id()));
78 assertThat(getGroupType(groupMod.getGroupType()), is(GroupDescription.Type.INDIRECT));
79 assertThat(groupMod.getXid(), is(Long.MAX_VALUE));
80 assertThat(groupMod.getCommand(), is(OFGroupModCommand.MODIFY));
81 }
82
83 @Test
84 public void groupDel() {
85 OFGroupMod groupMod = GroupModBuilder.builder(GROUP_BUCKETS,
86 GROUP_ID,
87 GroupDescription.Type.ALL,
88 OFFactoryVer13.INSTANCE,
89 Optional.of(Long.MAX_VALUE)).buildGroupDel();
90 assertThat(groupMod.getBuckets().size(), is(0));
91 assertThat(groupMod.getGroup().getGroupNumber(), is(GROUP_ID.id()));
92 assertThat(getGroupType(groupMod.getGroupType()), is(GroupDescription.Type.ALL));
93 assertThat(groupMod.getXid(), is(Long.MAX_VALUE));
94 assertThat(groupMod.getCommand(), is(OFGroupModCommand.DELETE));
95 }
96
97 private GroupDescription.Type getGroupType(OFGroupType type) {
98 switch (type) {
99 case ALL:
100 return GroupDescription.Type.ALL;
101 case INDIRECT:
102 return GroupDescription.Type.INDIRECT;
103 case SELECT:
104 return GroupDescription.Type.SELECT;
105 case FF:
106 return GroupDescription.Type.FAILOVER;
107 default:
108 break;
109 }
110 return null;
111 }
112
113}