Minor refactoring due to change in the library model
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / GroupConvertorTest.java
1 /**
2  * Copyright (c) 2014 Ericsson India Global Services Pvt Ltd. 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  * Contributor: usha.m.s@ericsson.com
9  */
10 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
11
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.CopyTtlInCaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.GroupActionCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetMplsTtlActionCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.copy.ttl.in._case.CopyTtlIn;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.copy.ttl.in._case.CopyTtlInBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.group.action._case.GroupAction;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.group.action._case.GroupActionBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.mpls.ttl.action._case.SetMplsTtlAction;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.mpls.ttl.action._case.SetMplsTtlActionBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.Buckets;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.BucketsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.GroupIdAction;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupModCommand;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInputBuilder;
40
41 public class GroupConvertorTest {
42
43     /**
44      * test of {@link GroupConvertor#toGroupModInput(org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group, short)}
45      */
46     @Test
47     public void testGroupModConvertorwithallParameters() {
48
49         AddGroupInputBuilder addGroupBuilder = new AddGroupInputBuilder();
50
51         addGroupBuilder.setGroupId(new GroupId(10L));
52
53         addGroupBuilder.setGroupType(GroupTypes.GroupAll);
54         List<Bucket> bucketList = new ArrayList<Bucket>();
55         List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionsList = new ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action>();
56         List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionsList1 = new ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action>();
57
58         // Action1
59         GroupActionBuilder groupActionBuilder = new GroupActionBuilder();
60         groupActionBuilder.setGroup("005");
61         GroupAction groupIdaction = groupActionBuilder.build();
62         ActionBuilder actionsB = new ActionBuilder();
63         actionsB.setAction(new GroupActionCaseBuilder().setGroupAction(groupIdaction).build());
64
65         // Action2:
66         GroupActionBuilder groupActionBuilder1 = new GroupActionBuilder();
67         groupActionBuilder1.setGroup("006");
68         GroupAction groupIdaction1 = groupActionBuilder.build();
69         ActionBuilder actionsB1 = new ActionBuilder();
70         actionsB1.setAction(new GroupActionCaseBuilder().setGroupAction(groupIdaction1).build());
71
72         actionsList.add(actionsB.build());
73         actionsList.add(actionsB1.build());
74
75
76         BucketsBuilder bucketsB = new BucketsBuilder();
77
78         BucketBuilder bucketB = new BucketBuilder();
79         bucketB.setWeight(10);
80         bucketB.setWatchPort(20L);
81         bucketB.setWatchGroup(22L);
82
83         bucketB.setAction(actionsList);
84         Bucket bucket = bucketB.build();
85
86         bucketList.add(bucket); // List of bucket
87
88         BucketBuilder bucketB1 = new BucketBuilder();
89         bucketB1.setWeight(50);
90         bucketB1.setWatchPort(60L);
91         bucketB1.setWatchGroup(70L);
92
93         // Action1
94         CopyTtlInBuilder copyTtlB = new CopyTtlInBuilder();
95         CopyTtlIn copyTtl = copyTtlB.build();
96         ActionBuilder actionsB2 = new ActionBuilder();
97         actionsB2.setAction(new CopyTtlInCaseBuilder().setCopyTtlIn(copyTtl).build());
98
99         // Action2:
100         SetMplsTtlActionBuilder setMplsTtlActionBuilder = new SetMplsTtlActionBuilder();
101         setMplsTtlActionBuilder.setMplsTtl((short)0X1);
102         SetMplsTtlAction setMAction = setMplsTtlActionBuilder.build();
103         ActionBuilder actionsB3 = new ActionBuilder();
104
105         actionsB3.setAction(new SetMplsTtlActionCaseBuilder().setSetMplsTtlAction(setMAction).build());
106
107
108         actionsList1.add(actionsB2.build());
109         actionsList1.add(actionsB3.build());
110
111         bucketB1.setAction(actionsList);
112
113         Bucket bucket1 = bucketB1.build(); // second bucket
114
115         bucketList.add(bucket1);
116
117         bucketsB.setBucket(bucketList);// List of bucket added to the Buckets
118         Buckets buckets = bucketsB.build();
119
120         addGroupBuilder.setBuckets(buckets);
121
122         GroupModInputBuilder outAddGroupInput = GroupConvertor.toGroupModInput(addGroupBuilder.build(), (short) 0X4,BigInteger.valueOf(1));
123
124         Assert.assertEquals(GroupModCommand.OFPGCADD, outAddGroupInput.getCommand());
125         Assert.assertEquals(GroupType.OFPGTALL, outAddGroupInput.getType());
126
127         Assert.assertEquals(10L, (long) outAddGroupInput.getGroupId().getValue());
128         Assert.assertEquals(10, (int) outAddGroupInput.getBucketsList().get(0).getWeight());
129         Assert.assertEquals(20L, (long) outAddGroupInput.getBucketsList().get(0).getWatchPort().getValue());
130         Assert.assertEquals((Long) 22L, outAddGroupInput.getBucketsList().get(0).getWatchGroup());
131
132         List<Action> outActionList = outAddGroupInput.getBucketsList().get(0).getAction();
133         for (int outItem = 0; outItem < outActionList.size(); outItem++) {
134            Action action = outActionList
135                     .get(outItem);
136             if (action instanceof GroupIdAction) {
137                 Assert.assertEquals((Long) 5L, ((GroupIdAction) action).getGroupId());
138
139             }
140             // TODO:setMplsTTL :OF layer doesnt have get();
141         }
142
143         Assert.assertEquals((Integer) 50, outAddGroupInput.getBucketsList().get(1).getWeight());
144         Assert.assertEquals((long) 60, (long) outAddGroupInput.getBucketsList().get(1).getWatchPort().getValue());
145         Assert.assertEquals((Long) 70L, outAddGroupInput.getBucketsList().get(1).getWatchGroup());
146         List<Action> outActionList1 = outAddGroupInput.getBucketsList().get(1).getAction();
147         for (int outItem = 0; outItem < outActionList1.size(); outItem++) {
148             Action action = outActionList1
149                     .get(outItem);
150             if (action instanceof GroupIdAction) {
151
152                 Assert.assertEquals((Long) 6L, ((GroupIdAction) action).getGroupId());
153
154
155             }
156             // TODO:setMplsTTL :OF layer doesnt have get();
157         }
158
159     }
160
161     /**
162      * test of {@link GroupConvertor#toGroupModInput(org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group, short)}
163      */
164     @Test
165     public void testGroupModConvertorNoBucket() {
166         AddGroupInputBuilder addGroupBuilder = new AddGroupInputBuilder();
167
168         addGroupBuilder.setGroupId(new GroupId(10L));
169
170         addGroupBuilder.setGroupType(GroupTypes.GroupAll);
171
172         GroupModInputBuilder outAddGroupInput = GroupConvertor.toGroupModInput(addGroupBuilder.build(), (short) 0X4,BigInteger.valueOf(1));
173
174         Assert.assertEquals(GroupModCommand.OFPGCADD, outAddGroupInput.getCommand());
175         Assert.assertEquals(GroupType.OFPGTALL, outAddGroupInput.getType());
176     }
177
178     /**
179      * test of {@link GroupConvertor#toGroupModInput(org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group, short)}
180      */
181     @Test
182     public void testGroupModConvertorBucketwithNOWieghtValuesForGroupTypeFastFailure() {
183
184         AddGroupInputBuilder addGroupBuilder = new AddGroupInputBuilder();
185
186         addGroupBuilder.setGroupId(new GroupId(10L));
187
188         addGroupBuilder.setGroupType(GroupTypes.GroupFf);
189         List<Bucket> bucketList = new ArrayList<Bucket>();
190         List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionsList = new ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action>();
191         List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionsList1 = new ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action>();
192
193         // Action1: 005
194         actionsList.add(assembleActionBuilder("005").build());
195         // Action2: 006
196         actionsList.add(assembleActionBuilder("006").build());
197         // .. and mr.Bond is not coming today
198
199         BucketsBuilder bucketsB = new BucketsBuilder();
200
201         BucketBuilder bucketB = new BucketBuilder();
202
203         bucketB.setAction(actionsList);
204         Bucket bucket = bucketB.build();
205
206         bucketList.add(bucket); // List of bucket
207         
208
209         
210         BucketBuilder bucketB1 = new BucketBuilder();
211
212         // Action1
213         actionsList1.add(assembleCopyTtlInBuilder().build());
214         // Action2:
215         actionsList1.add(assembleSetMplsTtlActionBuilder().build());
216
217         bucketB1.setAction(actionsList1);
218
219         Bucket bucket1 = bucketB1.build(); // second bucket
220
221         bucketList.add(bucket1);
222
223         bucketsB.setBucket(bucketList);// List of bucket added to the Buckets
224         Buckets buckets = bucketsB.build();
225
226         addGroupBuilder.setBuckets(buckets);
227
228         GroupModInputBuilder outAddGroupInput = GroupConvertor.toGroupModInput(addGroupBuilder.build(), (short) 0X4,BigInteger.valueOf(1));
229
230         Assert.assertEquals(GroupModCommand.OFPGCADD, outAddGroupInput.getCommand());
231         Assert.assertEquals(GroupType.OFPGTFF, outAddGroupInput.getType());
232
233         Assert.assertEquals(10L, outAddGroupInput.getGroupId().getValue().longValue());
234
235         List<Action> outActionList = outAddGroupInput.getBucketsList().get(0).getAction();
236         for (int outItem = 0; outItem < outActionList.size(); outItem++) {
237             Action action = outActionList
238                     .get(outItem);
239             if (action instanceof GroupIdAction) {
240                 Assert.assertEquals((Long) 5L, ((GroupIdAction) action).getGroupId());
241             }
242         }
243
244         List<Action> outActionList1 = outAddGroupInput.getBucketsList().get(1).getAction();
245         for (int outItem = 0; outItem < outActionList1.size(); outItem++) {
246             Action action = outActionList1
247                     .get(outItem);
248             if (action instanceof GroupIdAction) {
249                 Assert.assertEquals((Long) 6L, ((GroupIdAction) action).getGroupId());
250             }
251         }
252     }
253
254     /**
255      * @return
256      */
257     private static ActionBuilder assembleSetMplsTtlActionBuilder() {
258         SetMplsTtlActionBuilder setMplsTtlActionBuilder = new SetMplsTtlActionBuilder();
259         setMplsTtlActionBuilder.setMplsTtl((short) 0X1);
260         SetMplsTtlActionCaseBuilder setMplsTtlActionCaseBuilder = new SetMplsTtlActionCaseBuilder();
261         setMplsTtlActionCaseBuilder.setSetMplsTtlAction(setMplsTtlActionBuilder.build());
262         ActionBuilder actionsB3 = new ActionBuilder();
263         actionsB3.setAction(setMplsTtlActionCaseBuilder.build());
264         return actionsB3;
265     }
266
267     /**
268      * @return
269      */
270     private static ActionBuilder assembleCopyTtlInBuilder() {
271         CopyTtlInBuilder copyTtlB = new CopyTtlInBuilder();
272         CopyTtlInCaseBuilder copyTtlInCaseBuilder = new CopyTtlInCaseBuilder();
273         copyTtlInCaseBuilder.setCopyTtlIn(copyTtlB.build());
274         ActionBuilder actionsB2 = new ActionBuilder();
275         actionsB2.setAction(copyTtlInCaseBuilder.build());
276         return actionsB2;
277     }
278
279     /**
280      * @param groupName name of group
281      * @return 
282      * 
283      */
284     private static ActionBuilder assembleActionBuilder(String groupName) {
285         GroupActionBuilder groupActionBuilder = new GroupActionBuilder();
286         groupActionBuilder.setGroup(groupName);
287         GroupActionCaseBuilder groupActionCaseBuilder = new GroupActionCaseBuilder();
288         groupActionCaseBuilder.setGroupAction(groupActionBuilder.build());
289         ActionBuilder actionsBld = new ActionBuilder();
290         actionsBld.setAction(groupActionCaseBuilder.build());
291         return actionsBld;
292     }
293
294     /**
295      * test of {@link GroupConvertor#toGroupModInput(org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group, short)}
296      */
297     @Test
298     public void testGroupModConvertorBucketwithNOWieghtValuesForGroupTypeAll() {
299
300         AddGroupInputBuilder addGroupBuilder = new AddGroupInputBuilder();
301
302         addGroupBuilder.setGroupId(new GroupId(10L));
303
304         addGroupBuilder.setGroupType(GroupTypes.GroupAll);
305         List<Bucket> bucketList = new ArrayList<Bucket>();
306         List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionsList = new ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action>();
307         List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionsList1 = new ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action>();
308
309         // Action1
310         actionsList.add(assembleActionBuilder("005").build());
311         // Action2:
312         actionsList.add(assembleActionBuilder("006").build());
313
314         BucketsBuilder bucketsB = new BucketsBuilder();
315
316         BucketBuilder bucketB = new BucketBuilder();
317
318         bucketB.setAction(actionsList);
319         Bucket bucket = bucketB.build();
320
321         bucketList.add(bucket); // List of bucket
322
323         BucketBuilder bucketB1 = new BucketBuilder();
324
325         // Action1
326         actionsList1.add(assembleCopyTtlInBuilder().build());
327         // Action2:
328         actionsList1.add(assembleSetMplsTtlActionBuilder().build());
329
330         bucketB1.setAction(actionsList);
331
332         Bucket bucket1 = bucketB1.build(); // second bucket
333
334         bucketList.add(bucket1);
335
336         bucketsB.setBucket(bucketList);// List of bucket added to the Buckets
337         Buckets buckets = bucketsB.build();
338
339         addGroupBuilder.setBuckets(buckets);
340
341         GroupModInputBuilder outAddGroupInput = GroupConvertor.toGroupModInput(addGroupBuilder.build(), (short) 0X4,BigInteger.valueOf(1));
342
343         Assert.assertEquals(GroupModCommand.OFPGCADD, outAddGroupInput.getCommand());
344         Assert.assertEquals(GroupType.OFPGTALL, outAddGroupInput.getType());
345
346         Assert.assertEquals(10L, outAddGroupInput.getGroupId().getValue().longValue());
347
348         List<Action> outActionList = outAddGroupInput.getBucketsList().get(0).getAction();
349         for (int outItem = 0; outItem < outActionList.size(); outItem++) {
350             Action action = outActionList
351                     .get(outItem);
352             if (action instanceof GroupIdAction) {
353                 Assert.assertEquals((Long) 5L, ((GroupIdAction) action).getGroupId());
354
355             }
356
357         }
358
359         List<Action> outActionList1 = outAddGroupInput.getBucketsList().get(1).getAction();
360         for (int outItem = 0; outItem < outActionList1.size(); outItem++) {
361             Action action = outActionList1
362                     .get(outItem);
363             if (action instanceof GroupIdAction) {
364
365                 Assert.assertEquals((Long) 6L, ((GroupIdAction) action).getGroupId());
366
367             }
368
369         }
370
371     }
372 }