Migrate openflowplugin tests to use Uint types
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / GroupDescStatsResponseConvertorTest.java
1 /*
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
10
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;
39
40 /**
41  * Unit tests for GroupDescStats conversion.
42  *
43  * @author michal.polkorab
44  */
45 public class GroupDescStatsResponseConvertorTest {
46     private ConvertorManager convertorManager;
47
48     @Before
49     public void setUp() {
50         convertorManager = ConvertorManagerFactory.createDefaultManager();
51     }
52
53     /**
54      * Test empty GroupDescStats conversion.
55      */
56     @Test
57     public void test() {
58         List<GroupDesc> groupDescStats = new ArrayList<>();
59
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());
63     }
64
65     /**
66      * Test single GroupDescStat conversion without buckets.
67      */
68     @Test
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());
76
77         VersionConvertorData data = new VersionConvertorData(OFConstants.OFP_VERSION_1_3);
78         List<GroupDescStats> statsList = convert(groupDescStats, data);
79
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());
86     }
87
88     /**
89      * Test single GroupDescStats conversion.
90      */
91     @Test
92     public void testGroupDescStats() {
93
94         // **********************************************
95         // First group desc
96         // **********************************************
97         GroupDescBuilder builder = new GroupDescBuilder();
98         builder.setType(GroupType.OFPGTFF);
99         builder.setGroupId(new GroupId(Uint32.valueOf(42)));
100
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));
106
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());
112
113         // Build bucket with actions
114         bucketsBuilder.setAction(actions);
115         List<BucketsList> bucketsList = new ArrayList<>();
116         bucketsList.add(bucketsBuilder.build());
117
118         // Build first group desc
119         builder.setBucketsList(bucketsList);
120         List<GroupDesc> groupDescStats = new ArrayList<>();
121         groupDescStats.add(builder.build());
122
123         // **********************************************
124         // Second group desc
125         // **********************************************
126         builder = new GroupDescBuilder();
127         builder.setType(GroupType.OFPGTINDIRECT);
128         builder.setGroupId(new GroupId(Uint32.valueOf(50)));
129
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));
136
137         // Actions for first buckets for second group desc
138         actions = new ArrayList<>();
139
140         actionBuilder = new ActionBuilder();
141         actionBuilder.setActionChoice(new CopyTtlOutCaseBuilder().build());
142         actions.add(actionBuilder.build());
143
144         actionBuilder = new ActionBuilder();
145         actionBuilder.setActionChoice(new DecNwTtlCaseBuilder().build());
146         actions.add(actionBuilder.build());
147
148         actionBuilder = new ActionBuilder();
149         actionBuilder.setActionChoice(new PopPbbCaseBuilder().build());
150         actions.add(actionBuilder.build());
151
152         // Build first bucket with actions
153         bucketsBuilder.setAction(actions);
154         bucketsList.add(bucketsBuilder.build());
155
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));
161
162         // Actions for second buckets for second group desc
163         actions = new ArrayList<>();
164
165         // Build second bucket with actions
166         bucketsBuilder.setAction(actions);
167         bucketsList.add(bucketsBuilder.build());
168
169         // Build second group desc
170         builder.setBucketsList(bucketsList);
171         groupDescStats.add(builder.build());
172
173
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());
177
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());
186
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());
196
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());
203
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());
212
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());
222
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());
229
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());
235
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());
241
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());
250     }
251
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());
255     }
256 }