pier | 0d5865f | 2019-06-18 22:57:19 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.provider.of.group.impl; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import org.junit.Test; |
| 21 | import org.onosproject.core.GroupId; |
| 22 | import org.onosproject.net.PortNumber; |
| 23 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 24 | import org.onosproject.net.flow.TrafficTreatment; |
| 25 | import org.onosproject.net.group.DefaultGroupBucket; |
| 26 | import org.onosproject.net.group.GroupBucket; |
| 27 | import org.onosproject.net.group.GroupBuckets; |
| 28 | import org.onosproject.net.group.GroupDescription; |
| 29 | import org.projectfloodlight.openflow.protocol.OFGroupMod; |
| 30 | import org.projectfloodlight.openflow.protocol.OFGroupModCommand; |
| 31 | import org.projectfloodlight.openflow.protocol.OFGroupType; |
| 32 | import org.projectfloodlight.openflow.protocol.ver13.OFFactoryVer13; |
| 33 | |
| 34 | import java.util.Optional; |
| 35 | |
| 36 | import static org.hamcrest.MatcherAssert.assertThat; |
| 37 | import static org.hamcrest.Matchers.is; |
| 38 | |
| 39 | |
| 40 | public 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 | } |