2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.Optional;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.openflowplugin.api.OFConstants;
19 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStats;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.CopyTtlInCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.CopyTtlOutCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.DecNwTtlCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PopPbbCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsList;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsListBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.desc._case.multipart.reply.group.desc.GroupDesc;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.desc._case.multipart.reply.group.desc.GroupDescBuilder;
38 * Unit tests for GroupDescStats conversion.
40 * @author michal.polkorab
42 public class GroupDescStatsResponseConvertorTest {
43 private ConvertorManager convertorManager;
47 convertorManager = ConvertorManagerFactory.createDefaultManager();
51 * Test empty GroupDescStats conversion.
55 List<GroupDesc> groupDescStats = new ArrayList<>();
57 VersionConvertorData data = new VersionConvertorData(OFConstants.OFP_VERSION_1_3);
58 List<GroupDescStats> statsList = convert(groupDescStats, data);
59 Assert.assertEquals("Wrong groupDesc stats size", 0, statsList.size());
63 * Test single GroupDescStat conversion without buckets.
66 public void testSingleGroupDescStat() {
67 GroupDescBuilder builder = new GroupDescBuilder();
68 builder.setType(GroupType.OFPGTALL);
69 builder.setGroupId(new GroupId(42L));
70 builder.setBucketsList(new ArrayList<>());
71 List<GroupDesc> groupDescStats = new ArrayList<>();
72 groupDescStats.add(builder.build());
74 VersionConvertorData data = new VersionConvertorData(OFConstants.OFP_VERSION_1_3);
75 List<GroupDescStats> statsList = convert(groupDescStats, data);
77 Assert.assertEquals("Wrong groupDesc stats size", 1, statsList.size());
78 GroupDescStats stat = statsList.get(0);
79 Assert.assertEquals("Wrong type", GroupTypes.GroupAll, stat.getGroupType());
80 Assert.assertEquals("Wrong group-id", 42, stat.getGroupId().getValue().intValue());
81 Assert.assertEquals("Wrong key", 42, stat.getKey().getGroupId().getValue().intValue());
82 Assert.assertEquals("Wrong buckets size", 0, stat.getBuckets().getBucket().size());
86 * Test single GroupDescStats conversion.
89 public void testGroupDescStats() {
91 // **********************************************
93 // **********************************************
94 GroupDescBuilder builder = new GroupDescBuilder();
95 builder.setType(GroupType.OFPGTFF);
96 builder.setGroupId(new GroupId(42L));
98 // Buckets for first group desc
99 BucketsListBuilder bucketsBuilder = new BucketsListBuilder();
100 bucketsBuilder.setWeight(16);
101 bucketsBuilder.setWatchPort(new PortNumber(84L));
102 bucketsBuilder.setWatchGroup(168L);
104 // Actions for buckets for first group desc
105 List<Action> actions = new ArrayList<>();
106 ActionBuilder actionBuilder = new ActionBuilder();
107 actionBuilder.setActionChoice(new CopyTtlInCaseBuilder().build());
108 actions.add(actionBuilder.build());
110 // Build bucket with actions
111 bucketsBuilder.setAction(actions);
112 List<BucketsList> bucketsList = new ArrayList<>();
113 bucketsList.add(bucketsBuilder.build());
115 // Build first group desc
116 builder.setBucketsList(bucketsList);
117 List<GroupDesc> groupDescStats = new ArrayList<>();
118 groupDescStats.add(builder.build());
120 // **********************************************
122 // **********************************************
123 builder = new GroupDescBuilder();
124 builder.setType(GroupType.OFPGTINDIRECT);
125 builder.setGroupId(new GroupId(50L));
127 // First buckets for second group desc
128 bucketsList = new ArrayList<>();
129 bucketsBuilder = new BucketsListBuilder();
130 bucketsBuilder.setWeight(100);
131 bucketsBuilder.setWatchPort(new PortNumber(200L));
132 bucketsBuilder.setWatchGroup(400L);
134 // Actions for first buckets for second group desc
135 actions = new ArrayList<>();
137 actionBuilder = new ActionBuilder();
138 actionBuilder.setActionChoice(new CopyTtlOutCaseBuilder().build());
139 actions.add(actionBuilder.build());
141 actionBuilder = new ActionBuilder();
142 actionBuilder.setActionChoice(new DecNwTtlCaseBuilder().build());
143 actions.add(actionBuilder.build());
145 actionBuilder = new ActionBuilder();
146 actionBuilder.setActionChoice(new PopPbbCaseBuilder().build());
147 actions.add(actionBuilder.build());
149 // Build first bucket with actions
150 bucketsBuilder.setAction(actions);
151 bucketsList.add(bucketsBuilder.build());
153 // Second buckets for second group desc
154 bucketsBuilder = new BucketsListBuilder();
155 bucketsBuilder.setWeight(5);
156 bucketsBuilder.setWatchPort(new PortNumber(10L));
157 bucketsBuilder.setWatchGroup(15L);
159 // Actions for second buckets for second group desc
160 actions = new ArrayList<>();
162 // Build second bucket with actions
163 bucketsBuilder.setAction(actions);
164 bucketsList.add(bucketsBuilder.build());
166 // Build second group desc
167 builder.setBucketsList(bucketsList);
168 groupDescStats.add(builder.build());
171 VersionConvertorData data = new VersionConvertorData(OFConstants.OFP_VERSION_1_3);
172 List<GroupDescStats> statsList = convert(groupDescStats, data);
173 Assert.assertEquals("Wrong groupDesc stats size", 2, statsList.size());
175 // **********************************************
176 // Test first group desc
177 // **********************************************
178 GroupDescStats stat = statsList.get(0);
179 Assert.assertEquals("Wrong type", GroupTypes.GroupFf, stat.getGroupType());
180 Assert.assertEquals("Wrong group-id", 42, stat.getGroupId().getValue().intValue());
181 Assert.assertEquals("Wrong key", 42, stat.getKey().getGroupId().getValue().intValue());
182 Assert.assertEquals("Wrong buckets size", 1, stat.getBuckets().getBucket().size());
184 // Test first bucket for first group desc
185 Bucket bucket = stat.getBuckets().getBucket().get(0);
186 Assert.assertEquals("Wrong type", 0, bucket.getKey().getBucketId().getValue().intValue());
187 Assert.assertEquals("Wrong type", 0, bucket.getBucketId().getValue().intValue());
188 Assert.assertEquals("Wrong type", 16, bucket.getWeight().intValue());
189 Assert.assertEquals("Wrong type", 168, bucket.getWatchGroup().intValue());
190 Assert.assertEquals("Wrong type", 84, bucket.getWatchPort().intValue());
191 Assert.assertEquals("Wrong type", 1, bucket.getAction().size());
193 // Test first action for first bucket for first group desc
194 org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list
195 .Action action = bucket.getAction().get(0);
196 Assert.assertEquals("Wrong type", 0, action.getOrder().intValue());
197 Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112"
198 + ".action.action.CopyTtlInCase", action.getAction().getImplementedInterface().getName());
200 // **********************************************
201 // Test second group desc
202 // **********************************************
203 stat = statsList.get(1);
204 Assert.assertEquals("Wrong type", GroupTypes.GroupIndirect, stat.getGroupType());
205 Assert.assertEquals("Wrong group-id", 50, stat.getGroupId().getValue().intValue());
206 Assert.assertEquals("Wrong key", 50, stat.getKey().getGroupId().getValue().intValue());
207 Assert.assertEquals("Wrong buckets size", 2, stat.getBuckets().getBucket().size());
209 // Test first bucket for second group desc
210 bucket = stat.getBuckets().getBucket().get(0);
211 Assert.assertEquals("Wrong type", 0, bucket.getKey().getBucketId().getValue().intValue());
212 Assert.assertEquals("Wrong type", 0, bucket.getBucketId().getValue().intValue());
213 Assert.assertEquals("Wrong type", 100, bucket.getWeight().intValue());
214 Assert.assertEquals("Wrong type", 400, bucket.getWatchGroup().intValue());
215 Assert.assertEquals("Wrong type", 200, bucket.getWatchPort().intValue());
216 Assert.assertEquals("Wrong type", 3, bucket.getAction().size());
218 // Test first action for first bucket of second group desc
219 action = bucket.getAction().get(0);
220 Assert.assertEquals("Wrong type", 0, action.getOrder().intValue());
221 Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112"
222 + ".action.action.CopyTtlOutCase", action.getAction().getImplementedInterface().getName());
224 // Test second action for first bucket of second group desc
225 action = bucket.getAction().get(1);
226 Assert.assertEquals("Wrong type", 1, action.getOrder().intValue());
227 Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112"
228 + ".action.action.DecNwTtlCase", action.getAction().getImplementedInterface().getName());
230 // Test third action for first bucket of second group desc
231 action = bucket.getAction().get(2);
232 Assert.assertEquals("Wrong type", 2, action.getOrder().intValue());
233 Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112"
234 + ".action.action.PopPbbActionCase", action.getAction().getImplementedInterface().getName());
236 // Test second bucket for second group desc
237 bucket = stat.getBuckets().getBucket().get(1);
238 Assert.assertEquals("Wrong type", 1, bucket.getKey().getBucketId().getValue().intValue());
239 Assert.assertEquals("Wrong type", 1, bucket.getBucketId().getValue().intValue());
240 Assert.assertEquals("Wrong type", 5, bucket.getWeight().intValue());
241 Assert.assertEquals("Wrong type", 15, bucket.getWatchGroup().intValue());
242 Assert.assertEquals("Wrong type", 10, bucket.getWatchPort().intValue());
243 Assert.assertEquals("Wrong type", 0, bucket.getAction().size());
246 private List<GroupDescStats> convert(List<GroupDesc> groupDescStats,VersionConvertorData data) {
247 Optional<List<GroupDescStats>> statsListOptional = convertorManager.convert(groupDescStats, data);
248 return statsListOptional.orElse(Collections.emptyList());