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.Iterator;
14 import java.util.List;
15 import java.util.Optional;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.openflowplugin.api.OFConstants;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStats;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.CopyTtlInCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.CopyTtlOutCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.DecNwTtlCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PopPbbCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsList;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsListBuilder;
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.GroupDesc;
36 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;
37 import org.opendaylight.yangtools.yang.common.Uint16;
38 import org.opendaylight.yangtools.yang.common.Uint32;
41 * Unit tests for GroupDescStats conversion.
43 * @author michal.polkorab
45 public class GroupDescStatsResponseConvertorTest {
46 private ConvertorManager convertorManager;
50 convertorManager = ConvertorManagerFactory.createDefaultManager();
54 * Test empty GroupDescStats conversion.
58 List<GroupDesc> groupDescStats = new ArrayList<>();
60 VersionConvertorData data = new VersionConvertorData(OFConstants.OFP_VERSION_1_3);
61 List<GroupDescStats> statsList = convert(groupDescStats, data);
62 Assert.assertEquals("Wrong groupDesc stats size", 0, statsList.size());
66 * Test single GroupDescStat conversion without buckets.
69 public void testSingleGroupDescStat() {
70 GroupDescBuilder builder = new GroupDescBuilder();
71 builder.setType(GroupType.OFPGTALL);
72 builder.setGroupId(new GroupId(Uint32.valueOf(42)));
73 builder.setBucketsList(new ArrayList<>());
74 List<GroupDesc> groupDescStats = new ArrayList<>();
75 groupDescStats.add(builder.build());
77 VersionConvertorData data = new VersionConvertorData(OFConstants.OFP_VERSION_1_3);
78 List<GroupDescStats> statsList = convert(groupDescStats, data);
80 Assert.assertEquals("Wrong groupDesc stats size", 1, statsList.size());
81 GroupDescStats stat = statsList.get(0);
82 Assert.assertEquals("Wrong type", GroupTypes.GroupAll, stat.getGroupType());
83 Assert.assertEquals("Wrong group-id", 42, stat.getGroupId().getValue().intValue());
84 Assert.assertEquals("Wrong key", 42, stat.key().getGroupId().getValue().intValue());
85 Assert.assertEquals("Wrong buckets size", 0, stat.getBuckets().nonnullBucket().size());
89 * Test single GroupDescStats conversion.
92 public void testGroupDescStats() {
94 // **********************************************
96 // **********************************************
97 GroupDescBuilder builder = new GroupDescBuilder();
98 builder.setType(GroupType.OFPGTFF);
99 builder.setGroupId(new GroupId(Uint32.valueOf(42)));
101 // Buckets for first group desc
102 BucketsListBuilder bucketsBuilder = new BucketsListBuilder();
103 bucketsBuilder.setWeight(Uint16.valueOf(16));
104 bucketsBuilder.setWatchPort(new PortNumber(Uint32.valueOf(84)));
105 bucketsBuilder.setWatchGroup(Uint32.valueOf(168));
107 // Actions for buckets for first group desc
108 List<Action> actions = new ArrayList<>();
109 ActionBuilder actionBuilder = new ActionBuilder();
110 actionBuilder.setActionChoice(new CopyTtlInCaseBuilder().build());
111 actions.add(actionBuilder.build());
113 // Build bucket with actions
114 bucketsBuilder.setAction(actions);
115 List<BucketsList> bucketsList = new ArrayList<>();
116 bucketsList.add(bucketsBuilder.build());
118 // Build first group desc
119 builder.setBucketsList(bucketsList);
120 List<GroupDesc> groupDescStats = new ArrayList<>();
121 groupDescStats.add(builder.build());
123 // **********************************************
125 // **********************************************
126 builder = new GroupDescBuilder();
127 builder.setType(GroupType.OFPGTINDIRECT);
128 builder.setGroupId(new GroupId(Uint32.valueOf(50)));
130 // First buckets for second group desc
131 bucketsList = new ArrayList<>();
132 bucketsBuilder = new BucketsListBuilder();
133 bucketsBuilder.setWeight(Uint16.valueOf(100));
134 bucketsBuilder.setWatchPort(new PortNumber(Uint32.valueOf(200)));
135 bucketsBuilder.setWatchGroup(Uint32.valueOf(400));
137 // Actions for first buckets for second group desc
138 actions = new ArrayList<>();
140 actionBuilder = new ActionBuilder();
141 actionBuilder.setActionChoice(new CopyTtlOutCaseBuilder().build());
142 actions.add(actionBuilder.build());
144 actionBuilder = new ActionBuilder();
145 actionBuilder.setActionChoice(new DecNwTtlCaseBuilder().build());
146 actions.add(actionBuilder.build());
148 actionBuilder = new ActionBuilder();
149 actionBuilder.setActionChoice(new PopPbbCaseBuilder().build());
150 actions.add(actionBuilder.build());
152 // Build first bucket with actions
153 bucketsBuilder.setAction(actions);
154 bucketsList.add(bucketsBuilder.build());
156 // Second buckets for second group desc
157 bucketsBuilder = new BucketsListBuilder();
158 bucketsBuilder.setWeight(Uint16.valueOf(5));
159 bucketsBuilder.setWatchPort(new PortNumber(Uint32.TEN));
160 bucketsBuilder.setWatchGroup(Uint32.valueOf(15));
162 // Actions for second buckets for second group desc
163 actions = new ArrayList<>();
165 // Build second bucket with actions
166 bucketsBuilder.setAction(actions);
167 bucketsList.add(bucketsBuilder.build());
169 // Build second group desc
170 builder.setBucketsList(bucketsList);
171 groupDescStats.add(builder.build());
174 VersionConvertorData data = new VersionConvertorData(OFConstants.OFP_VERSION_1_3);
175 List<GroupDescStats> statsList = convert(groupDescStats, data);
176 Assert.assertEquals("Wrong groupDesc stats size", 2, statsList.size());
178 // **********************************************
179 // Test first group desc
180 // **********************************************
181 GroupDescStats stat = statsList.get(0);
182 Assert.assertEquals("Wrong type", GroupTypes.GroupFf, stat.getGroupType());
183 Assert.assertEquals("Wrong group-id", 42, stat.getGroupId().getValue().intValue());
184 Assert.assertEquals("Wrong key", 42, stat.key().getGroupId().getValue().intValue());
185 Assert.assertEquals("Wrong buckets size", 1, stat.getBuckets().nonnullBucket().size());
187 // Test first bucket for first group desc
188 Iterator<Bucket> bucketIt = stat.getBuckets().nonnullBucket().values().iterator();
189 Bucket bucket = bucketIt.next();
190 Assert.assertEquals("Wrong type", 0, bucket.key().getBucketId().getValue().intValue());
191 Assert.assertEquals("Wrong type", 0, bucket.getBucketId().getValue().intValue());
192 Assert.assertEquals("Wrong type", 16, bucket.getWeight().intValue());
193 Assert.assertEquals("Wrong type", 168, bucket.getWatchGroup().intValue());
194 Assert.assertEquals("Wrong type", 84, bucket.getWatchPort().intValue());
195 Assert.assertEquals("Wrong type", 1, bucket.getAction().size());
197 // Test first action for first bucket for first group desc
198 org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list
199 .Action action = bucket.nonnullAction().values().iterator().next();
200 Assert.assertEquals("Wrong type", 0, action.getOrder().intValue());
201 Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112"
202 + ".action.action.CopyTtlInCase", action.getAction().implementedInterface().getName());
204 // **********************************************
205 // Test second group desc
206 // **********************************************
207 stat = statsList.get(1);
208 Assert.assertEquals("Wrong type", GroupTypes.GroupIndirect, stat.getGroupType());
209 Assert.assertEquals("Wrong group-id", 50, stat.getGroupId().getValue().intValue());
210 Assert.assertEquals("Wrong key", 50, stat.key().getGroupId().getValue().intValue());
211 Assert.assertEquals("Wrong buckets size", 2, stat.getBuckets().nonnullBucket().size());
213 // Test first bucket for second group desc
214 bucketIt = stat.getBuckets().nonnullBucket().values().iterator();
215 bucket = bucketIt.next();
216 Assert.assertEquals("Wrong type", 0, bucket.key().getBucketId().getValue().intValue());
217 Assert.assertEquals("Wrong type", 0, bucket.getBucketId().getValue().intValue());
218 Assert.assertEquals("Wrong type", 100, bucket.getWeight().intValue());
219 Assert.assertEquals("Wrong type", 400, bucket.getWatchGroup().intValue());
220 Assert.assertEquals("Wrong type", 200, bucket.getWatchPort().intValue());
221 Assert.assertEquals("Wrong type", 3, bucket.nonnullAction().size());
223 // Test first action for first bucket of second group desc
224 var actionIt = bucket.nonnullAction().values().iterator();
225 action = actionIt.next();
226 Assert.assertEquals("Wrong type", 0, action.getOrder().intValue());
227 Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112"
228 + ".action.action.CopyTtlOutCase", action.getAction().implementedInterface().getName());
230 // Test second action for first bucket of second group desc
231 action = actionIt.next();
232 Assert.assertEquals("Wrong type", 1, action.getOrder().intValue());
233 Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112"
234 + ".action.action.DecNwTtlCase", action.getAction().implementedInterface().getName());
236 // Test third action for first bucket of second group desc
237 action = actionIt.next();
238 Assert.assertEquals("Wrong type", 2, action.getOrder().intValue());
239 Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112"
240 + ".action.action.PopPbbActionCase", action.getAction().implementedInterface().getName());
242 // Test second bucket for second group desc
243 bucket = bucketIt.next();
244 Assert.assertEquals("Wrong type", 1, bucket.key().getBucketId().getValue().intValue());
245 Assert.assertEquals("Wrong type", 1, bucket.getBucketId().getValue().intValue());
246 Assert.assertEquals("Wrong type", 5, bucket.getWeight().intValue());
247 Assert.assertEquals("Wrong type", 15, bucket.getWatchGroup().intValue());
248 Assert.assertEquals("Wrong type", 10, bucket.getWatchPort().intValue());
249 Assert.assertEquals("Wrong type", 0, bucket.nonnullAction().size());
252 private List<GroupDescStats> convert(List<GroupDesc> groupDescStats,VersionConvertorData data) {
253 Optional<List<GroupDescStats>> statsListOptional = convertorManager.convert(groupDescStats, data);
254 return statsListOptional.orElse(Collections.emptyList());