Update MRI projects for Aluminium
[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
38 /**
39  * Unit tests for GroupDescStats conversion.
40  *
41  * @author michal.polkorab
42  */
43 public class GroupDescStatsResponseConvertorTest {
44     private ConvertorManager convertorManager;
45
46     @Before
47     public void setUp() {
48         convertorManager = ConvertorManagerFactory.createDefaultManager();
49     }
50
51     /**
52      * Test empty GroupDescStats conversion.
53      */
54     @Test
55     public void test() {
56         List<GroupDesc> groupDescStats = new ArrayList<>();
57
58         VersionConvertorData data = new VersionConvertorData(OFConstants.OFP_VERSION_1_3);
59         List<GroupDescStats> statsList = convert(groupDescStats, data);
60         Assert.assertEquals("Wrong groupDesc stats size", 0, statsList.size());
61     }
62
63     /**
64      * Test single GroupDescStat conversion without buckets.
65      */
66     @Test
67     public void testSingleGroupDescStat() {
68         GroupDescBuilder builder = new GroupDescBuilder();
69         builder.setType(GroupType.OFPGTALL);
70         builder.setGroupId(new GroupId(42L));
71         builder.setBucketsList(new ArrayList<>());
72         List<GroupDesc> groupDescStats = new ArrayList<>();
73         groupDescStats.add(builder.build());
74
75         VersionConvertorData data = new VersionConvertorData(OFConstants.OFP_VERSION_1_3);
76         List<GroupDescStats> statsList = convert(groupDescStats, data);
77
78         Assert.assertEquals("Wrong groupDesc stats size", 1, statsList.size());
79         GroupDescStats stat = statsList.get(0);
80         Assert.assertEquals("Wrong type", GroupTypes.GroupAll, stat.getGroupType());
81         Assert.assertEquals("Wrong group-id", 42, stat.getGroupId().getValue().intValue());
82         Assert.assertEquals("Wrong key", 42, stat.key().getGroupId().getValue().intValue());
83         Assert.assertEquals("Wrong buckets size", 0, stat.getBuckets().nonnullBucket().size());
84     }
85
86     /**
87      * Test single GroupDescStats conversion.
88      */
89     @Test
90     public void testGroupDescStats() {
91
92         // **********************************************
93         // First group desc
94         // **********************************************
95         GroupDescBuilder builder = new GroupDescBuilder();
96         builder.setType(GroupType.OFPGTFF);
97         builder.setGroupId(new GroupId(42L));
98
99         // Buckets for first group desc
100         BucketsListBuilder bucketsBuilder = new BucketsListBuilder();
101         bucketsBuilder.setWeight(16);
102         bucketsBuilder.setWatchPort(new PortNumber(84L));
103         bucketsBuilder.setWatchGroup(168L);
104
105         // Actions for buckets for first group desc
106         List<Action> actions = new ArrayList<>();
107         ActionBuilder actionBuilder = new ActionBuilder();
108         actionBuilder.setActionChoice(new CopyTtlInCaseBuilder().build());
109         actions.add(actionBuilder.build());
110
111         // Build bucket with actions
112         bucketsBuilder.setAction(actions);
113         List<BucketsList> bucketsList = new ArrayList<>();
114         bucketsList.add(bucketsBuilder.build());
115
116         // Build first group desc
117         builder.setBucketsList(bucketsList);
118         List<GroupDesc> groupDescStats = new ArrayList<>();
119         groupDescStats.add(builder.build());
120
121         // **********************************************
122         // Second group desc
123         // **********************************************
124         builder = new GroupDescBuilder();
125         builder.setType(GroupType.OFPGTINDIRECT);
126         builder.setGroupId(new GroupId(50L));
127
128         // First buckets for second group desc
129         bucketsList = new ArrayList<>();
130         bucketsBuilder = new BucketsListBuilder();
131         bucketsBuilder.setWeight(100);
132         bucketsBuilder.setWatchPort(new PortNumber(200L));
133         bucketsBuilder.setWatchGroup(400L);
134
135         // Actions for first buckets for second group desc
136         actions = new ArrayList<>();
137
138         actionBuilder = new ActionBuilder();
139         actionBuilder.setActionChoice(new CopyTtlOutCaseBuilder().build());
140         actions.add(actionBuilder.build());
141
142         actionBuilder = new ActionBuilder();
143         actionBuilder.setActionChoice(new DecNwTtlCaseBuilder().build());
144         actions.add(actionBuilder.build());
145
146         actionBuilder = new ActionBuilder();
147         actionBuilder.setActionChoice(new PopPbbCaseBuilder().build());
148         actions.add(actionBuilder.build());
149
150         // Build first bucket with actions
151         bucketsBuilder.setAction(actions);
152         bucketsList.add(bucketsBuilder.build());
153
154         // Second buckets for second group desc
155         bucketsBuilder = new BucketsListBuilder();
156         bucketsBuilder.setWeight(5);
157         bucketsBuilder.setWatchPort(new PortNumber(10L));
158         bucketsBuilder.setWatchGroup(15L);
159
160         // Actions for second buckets for second group desc
161         actions = new ArrayList<>();
162
163         // Build second bucket with actions
164         bucketsBuilder.setAction(actions);
165         bucketsList.add(bucketsBuilder.build());
166
167         // Build second group desc
168         builder.setBucketsList(bucketsList);
169         groupDescStats.add(builder.build());
170
171
172         VersionConvertorData data = new VersionConvertorData(OFConstants.OFP_VERSION_1_3);
173         List<GroupDescStats> statsList = convert(groupDescStats, data);
174         Assert.assertEquals("Wrong groupDesc stats size", 2, statsList.size());
175
176         // **********************************************
177         // Test first group desc
178         // **********************************************
179         GroupDescStats stat = statsList.get(0);
180         Assert.assertEquals("Wrong type", GroupTypes.GroupFf, stat.getGroupType());
181         Assert.assertEquals("Wrong group-id", 42, stat.getGroupId().getValue().intValue());
182         Assert.assertEquals("Wrong key", 42, stat.key().getGroupId().getValue().intValue());
183         Assert.assertEquals("Wrong buckets size", 1, stat.getBuckets().nonnullBucket().size());
184
185         // Test first bucket for first group desc
186         Iterator<Bucket> bucketIt = stat.getBuckets().nonnullBucket().values().iterator();
187         Bucket bucket = bucketIt.next();
188         Assert.assertEquals("Wrong type", 0, bucket.key().getBucketId().getValue().intValue());
189         Assert.assertEquals("Wrong type", 0, bucket.getBucketId().getValue().intValue());
190         Assert.assertEquals("Wrong type", 16, bucket.getWeight().intValue());
191         Assert.assertEquals("Wrong type", 168, bucket.getWatchGroup().intValue());
192         Assert.assertEquals("Wrong type", 84, bucket.getWatchPort().intValue());
193         Assert.assertEquals("Wrong type", 1, bucket.getAction().size());
194
195         // Test first action for first bucket for first group desc
196         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list
197                 .Action action = bucket.nonnullAction().values().iterator().next();
198         Assert.assertEquals("Wrong type", 0, action.getOrder().intValue());
199         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112"
200                 + ".action.action.CopyTtlInCase", action.getAction().implementedInterface().getName());
201
202         // **********************************************
203         // Test second group desc
204         // **********************************************
205         stat = statsList.get(1);
206         Assert.assertEquals("Wrong type", GroupTypes.GroupIndirect, stat.getGroupType());
207         Assert.assertEquals("Wrong group-id", 50, stat.getGroupId().getValue().intValue());
208         Assert.assertEquals("Wrong key", 50, stat.key().getGroupId().getValue().intValue());
209         Assert.assertEquals("Wrong buckets size", 2, stat.getBuckets().nonnullBucket().size());
210
211         // Test first bucket for second group desc
212         bucketIt = stat.getBuckets().nonnullBucket().values().iterator();
213         bucket = bucketIt.next();
214         Assert.assertEquals("Wrong type", 0, bucket.key().getBucketId().getValue().intValue());
215         Assert.assertEquals("Wrong type", 0, bucket.getBucketId().getValue().intValue());
216         Assert.assertEquals("Wrong type", 100, bucket.getWeight().intValue());
217         Assert.assertEquals("Wrong type", 400, bucket.getWatchGroup().intValue());
218         Assert.assertEquals("Wrong type", 200, bucket.getWatchPort().intValue());
219         Assert.assertEquals("Wrong type", 3, bucket.nonnullAction().size());
220
221         // Test first action for first bucket of second group desc
222         var actionIt = bucket.nonnullAction().values().iterator();
223         action = actionIt.next();
224         Assert.assertEquals("Wrong type", 0, action.getOrder().intValue());
225         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112"
226                 + ".action.action.CopyTtlOutCase", action.getAction().implementedInterface().getName());
227
228         // Test second action for first bucket of second group desc
229         action = actionIt.next();
230         Assert.assertEquals("Wrong type", 1, action.getOrder().intValue());
231         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112"
232                 + ".action.action.DecNwTtlCase", action.getAction().implementedInterface().getName());
233
234         // Test third action for first bucket of second group desc
235         action = actionIt.next();
236         Assert.assertEquals("Wrong type", 2, action.getOrder().intValue());
237         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112"
238                 + ".action.action.PopPbbActionCase", action.getAction().implementedInterface().getName());
239
240         // Test second bucket for second group desc
241         bucket = bucketIt.next();
242         Assert.assertEquals("Wrong type", 1, bucket.key().getBucketId().getValue().intValue());
243         Assert.assertEquals("Wrong type", 1, bucket.getBucketId().getValue().intValue());
244         Assert.assertEquals("Wrong type", 5, bucket.getWeight().intValue());
245         Assert.assertEquals("Wrong type", 15, bucket.getWatchGroup().intValue());
246         Assert.assertEquals("Wrong type", 10, bucket.getWatchPort().intValue());
247         Assert.assertEquals("Wrong type", 0, bucket.nonnullAction().size());
248     }
249
250     private List<GroupDescStats> convert(List<GroupDesc> groupDescStats,VersionConvertorData data) {
251         Optional<List<GroupDescStats>> statsListOptional = convertorManager.convert(groupDescStats, data);
252         return  statsListOptional.orElse(Collections.emptyList());
253     }
254 }