Bug 1254 - Added unit tests
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / GroupStatsResponseConvertorTest2.java
1 /*\r
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 \r
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;\r
10 \r
11 import java.util.ArrayList;\r
12 import java.util.List;\r
13 \r
14 import org.junit.Assert;\r
15 import org.junit.Test;\r
16 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStats;\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.CopyTtlIn;\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.CopyTtlOut;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.DecNwTtl;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopPbb;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.ActionBuilder;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupId;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupType;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsList;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsListBuilder;\r
31 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;\r
32 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;\r
33 \r
34 /**\r
35  * @author michal.polkorab\r
36  *\r
37  */\r
38 public class GroupStatsResponseConvertorTest2 {\r
39 \r
40     GroupStatsResponseConvertor convertor =  new GroupStatsResponseConvertor();\r
41 \r
42     /**\r
43      * Test empty GroupDescStats conversion\r
44      */\r
45     @Test\r
46     public void test() {\r
47         List<GroupDesc> groupDescStats = new ArrayList<>();\r
48 \r
49         List<GroupDescStats> statsList = convertor.toSALGroupDescStatsList(groupDescStats, OpenflowVersion.OF13);\r
50 \r
51         Assert.assertEquals("Wrong groupDesc stats size", 0, statsList.size());\r
52     }\r
53 \r
54     /**\r
55      * Test single GroupDescStat conversion without buckets\r
56      */\r
57     @Test\r
58     public void testSingleGroupDescStat() {\r
59         List<GroupDesc> groupDescStats = new ArrayList<>();\r
60         GroupDescBuilder builder = new GroupDescBuilder();\r
61         builder.setType(GroupType.OFPGTALL);\r
62         builder.setGroupId(new GroupId(42L));\r
63         builder.setBucketsList(new ArrayList<BucketsList>());\r
64         groupDescStats.add(builder.build());\r
65 \r
66         List<GroupDescStats> statsList = convertor.toSALGroupDescStatsList(groupDescStats, OpenflowVersion.OF13);\r
67 \r
68         Assert.assertEquals("Wrong groupDesc stats size", 1, statsList.size());\r
69         GroupDescStats stat = statsList.get(0);\r
70         Assert.assertEquals("Wrong type", GroupTypes.GroupAll, stat.getGroupType());\r
71         Assert.assertEquals("Wrong group-id", 42, stat.getGroupId().getValue().intValue());\r
72         Assert.assertEquals("Wrong key", 42, stat.getKey().getGroupId().getValue().intValue());\r
73         Assert.assertEquals("Wrong buckets size", 0, stat.getBuckets().getBucket().size());\r
74     }\r
75 \r
76     /**\r
77      * Test single GroupDescStats conversion\r
78      */\r
79     @Test\r
80     public void testGroupDescStats() {\r
81         List<GroupDesc> groupDescStats = new ArrayList<>();\r
82         GroupDescBuilder builder = new GroupDescBuilder();\r
83         builder.setType(GroupType.OFPGTFF);\r
84         builder.setGroupId(new GroupId(42L));\r
85         List<BucketsList> bucketsList = new ArrayList<>();\r
86         BucketsListBuilder bucketsBuilder = new BucketsListBuilder();\r
87         bucketsBuilder.setWeight(16);\r
88         bucketsBuilder.setWatchPort(new PortNumber(84L));\r
89         bucketsBuilder.setWatchGroup(168L);\r
90         List<Action> actions = new ArrayList<>();\r
91         ActionBuilder actionBuilder = new ActionBuilder();\r
92         actionBuilder.setType(CopyTtlIn.class);\r
93         actions.add(actionBuilder.build());\r
94         bucketsBuilder.setAction(actions);\r
95         bucketsList.add(bucketsBuilder.build());\r
96         builder.setBucketsList(bucketsList);\r
97         groupDescStats.add(builder.build());\r
98         builder = new GroupDescBuilder();\r
99         builder.setType(GroupType.OFPGTINDIRECT);\r
100         builder.setGroupId(new GroupId(50L));\r
101         bucketsList = new ArrayList<>();\r
102         bucketsBuilder = new BucketsListBuilder();\r
103         bucketsBuilder.setWeight(100);\r
104         bucketsBuilder.setWatchPort(new PortNumber(200L));\r
105         bucketsBuilder.setWatchGroup(400L);\r
106         actions = new ArrayList<>();\r
107         actionBuilder = new ActionBuilder();\r
108         actionBuilder.setType(CopyTtlOut.class);\r
109         actions.add(actionBuilder.build());\r
110         actionBuilder = new ActionBuilder();\r
111         actionBuilder.setType(DecNwTtl.class);\r
112         actions.add(actionBuilder.build());\r
113         actionBuilder = new ActionBuilder();\r
114         actionBuilder.setType(PopPbb.class);\r
115         actions.add(actionBuilder.build());\r
116         bucketsBuilder.setAction(actions);\r
117         bucketsList.add(bucketsBuilder.build());\r
118         bucketsBuilder = new BucketsListBuilder();\r
119         bucketsBuilder.setWeight(5);\r
120         bucketsBuilder.setWatchPort(new PortNumber(10L));\r
121         bucketsBuilder.setWatchGroup(15L);\r
122         actions = new ArrayList<>();\r
123         bucketsBuilder.setAction(actions);\r
124         bucketsList.add(bucketsBuilder.build());\r
125         builder.setBucketsList(bucketsList);\r
126         groupDescStats.add(builder.build());\r
127 \r
128         List<GroupDescStats> statsList = convertor.toSALGroupDescStatsList(groupDescStats, OpenflowVersion.OF13);\r
129 \r
130         Assert.assertEquals("Wrong groupDesc stats size", 2, statsList.size());\r
131         GroupDescStats stat = statsList.get(0);\r
132         Assert.assertEquals("Wrong type", GroupTypes.GroupFf, stat.getGroupType());\r
133         Assert.assertEquals("Wrong group-id", 42, stat.getGroupId().getValue().intValue());\r
134         Assert.assertEquals("Wrong key", 42, stat.getKey().getGroupId().getValue().intValue());\r
135         Assert.assertEquals("Wrong buckets size", 1, stat.getBuckets().getBucket().size());\r
136         Bucket bucket = stat.getBuckets().getBucket().get(0);\r
137         Assert.assertEquals("Wrong type", 0, bucket.getKey().getBucketId().getValue().intValue());\r
138         Assert.assertEquals("Wrong type", 0, bucket.getBucketId().getValue().intValue());\r
139         Assert.assertEquals("Wrong type", 16, bucket.getWeight().intValue());\r
140         Assert.assertEquals("Wrong type", 168, bucket.getWatchGroup().intValue());\r
141         Assert.assertEquals("Wrong type", 84, bucket.getWatchPort().intValue());\r
142         Assert.assertEquals("Wrong type", 1, bucket.getAction().size());\r
143         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list\r
144         .Action action = bucket.getAction().get(0);\r
145         Assert.assertEquals("Wrong type", 0, action.getOrder().intValue());\r
146         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112"\r
147                 + ".action.action.CopyTtlInCase", action.getAction().getImplementedInterface().getName());\r
148 \r
149         stat = statsList.get(1);\r
150         Assert.assertEquals("Wrong type", GroupTypes.GroupIndirect, stat.getGroupType());\r
151         Assert.assertEquals("Wrong group-id", 50, stat.getGroupId().getValue().intValue());\r
152         Assert.assertEquals("Wrong key", 50, stat.getKey().getGroupId().getValue().intValue());\r
153         Assert.assertEquals("Wrong buckets size", 2, stat.getBuckets().getBucket().size());\r
154         bucket = stat.getBuckets().getBucket().get(0);\r
155         Assert.assertEquals("Wrong type", 0, bucket.getKey().getBucketId().getValue().intValue());\r
156         Assert.assertEquals("Wrong type", 0, bucket.getBucketId().getValue().intValue());\r
157         Assert.assertEquals("Wrong type", 100, bucket.getWeight().intValue());\r
158         Assert.assertEquals("Wrong type", 400, bucket.getWatchGroup().intValue());\r
159         Assert.assertEquals("Wrong type", 200, bucket.getWatchPort().intValue());\r
160         Assert.assertEquals("Wrong type", 3, bucket.getAction().size());\r
161         action = bucket.getAction().get(0);\r
162         Assert.assertEquals("Wrong type", 0, action.getOrder().intValue());\r
163         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112"\r
164                 + ".action.action.CopyTtlOutCase", action.getAction().getImplementedInterface().getName());\r
165         action = bucket.getAction().get(1);\r
166         Assert.assertEquals("Wrong type", 1, action.getOrder().intValue());\r
167         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112"\r
168                 + ".action.action.DecNwTtlCase", action.getAction().getImplementedInterface().getName());\r
169         action = bucket.getAction().get(2);\r
170         Assert.assertEquals("Wrong type", 2, action.getOrder().intValue());\r
171         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112"\r
172                 + ".action.action.PopPbbActionCase", action.getAction().getImplementedInterface().getName());\r
173         bucket = stat.getBuckets().getBucket().get(1);\r
174         Assert.assertEquals("Wrong type", 1, bucket.getKey().getBucketId().getValue().intValue());\r
175         Assert.assertEquals("Wrong type", 1, bucket.getBucketId().getValue().intValue());\r
176         Assert.assertEquals("Wrong type", 5, bucket.getWeight().intValue());\r
177         Assert.assertEquals("Wrong type", 15, bucket.getWatchGroup().intValue());\r
178         Assert.assertEquals("Wrong type", 10, bucket.getWatchPort().intValue());\r
179         Assert.assertEquals("Wrong type", 0, bucket.getAction().size());\r
180     }\r
181 }