Fixup Augmentable and Identifiable methods changing
[openflowplugin.git] / test-provider / src / main / java / org / opendaylight / openflowplugin / test / OpenflowPluginBulkGroupTransactionProvider.java
1 /*
2  * Copyright (c) 2014, 2015 Ericsson, Inc. 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.test;
10
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.math.BigInteger;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.eclipse.osgi.framework.console.CommandInterpreter;
19 import org.eclipse.osgi.framework.console.CommandProvider;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
24 import org.opendaylight.controller.sal.binding.api.NotificationService;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.ControllerActionCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.CopyTtlInCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.CopyTtlOutCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DecMplsTtlCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DecNwTtlCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.GroupActionCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopMplsActionCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopPbbActionCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushMplsActionCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushPbbActionCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanIdActionCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.controller.action._case.ControllerActionBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.copy.ttl.in._case.CopyTtlInBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.copy.ttl.out._case.CopyTtlOutBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.dec.mpls.ttl._case.DecMplsTtlBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.dec.nw.ttl._case.DecNwTtl;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.dec.nw.ttl._case.DecNwTtlBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropAction;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropActionBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.group.action._case.GroupActionBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.pop.mpls.action._case.PopMplsActionBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.pop.pbb.action._case.PopPbbActionBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.pop.vlan.action._case.PopVlanActionBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.mpls.action._case.PushMplsActionBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.pbb.action._case.PushPbbActionBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.vlan.action._case.PushVlanActionBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.vlan.id.action._case.SetVlanIdActionBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.MeterCaseBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTableBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.meter._case.MeterBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.BucketsBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketKey;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.NodeErrorListener;
102 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
103 import org.osgi.framework.BundleContext;
104 import org.slf4j.Logger;
105 import org.slf4j.LoggerFactory;
106
107 @SuppressWarnings("checkstyle:MethodName")
108 public class OpenflowPluginBulkGroupTransactionProvider implements CommandProvider {
109     private static final Logger LOG = LoggerFactory.getLogger(OpenflowPluginBulkGroupTransactionProvider.class);
110     private DataBroker dataBroker;
111     private final BundleContext ctx;
112     private final String originalFlowName = "Foo";
113     private final NodeErrorListener nodeErrorListener = new NodeErrorListenerLoggingImpl();
114     private Node testNode12;
115     private final String originalGroupName = "Foo";
116     private NotificationService notificationService;
117
118     public OpenflowPluginBulkGroupTransactionProvider(BundleContext ctx) {
119         this.ctx = ctx;
120     }
121
122     public void onSessionInitiated(ProviderContext session) {
123         notificationService = session.getSALService(NotificationService.class);
124         notificationService.registerNotificationListener(nodeErrorListener);
125         dataBroker = session.getSALService(DataBroker.class);
126         ctx.registerService(CommandProvider.class.getName(), this, null);
127         createTestFlow(createTestNode(null), null, null);
128     }
129
130     private NodeBuilder createTestNode(String nodeId) {
131         if (nodeId == null) {
132             nodeId = OpenflowpluginTestActivator.NODE_ID;
133         }
134         NodeBuilder builder = new NodeBuilder();
135         builder.setId(new NodeId(nodeId));
136         builder.withKey(new NodeKey(builder.getId()));
137         return builder;
138     }
139
140     private void createTestNode() {
141         NodeBuilder builder = new NodeBuilder();
142         builder.setId(new NodeId(OpenflowpluginTestActivator.NODE_ID));
143         builder.withKey(new NodeKey(builder.getId()));
144         testNode12 = builder.build();
145     }
146
147     @Override
148     public String getHelp() {
149         return "No help";
150     }
151
152     private static MatchBuilder createMatch1() {
153         MatchBuilder match = new MatchBuilder();
154         Ipv4MatchBuilder ipv4Match = new Ipv4MatchBuilder();
155         Ipv4Prefix prefix = new Ipv4Prefix("10.0.0.1/24");
156         ipv4Match.setIpv4Destination(prefix);
157         Ipv4Match i4m = ipv4Match.build();
158         match.setLayer3Match(i4m);
159
160         EthernetMatchBuilder eth = new EthernetMatchBuilder();
161         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
162         ethTypeBuilder.setType(new EtherType(0x0800L));
163         eth.setEthernetType(ethTypeBuilder.build());
164         match.setEthernetMatch(eth.build());
165         return match;
166     }
167
168     private static InstructionsBuilder createDecNwTtlInstructions() {
169         DecNwTtlBuilder ta = new DecNwTtlBuilder();
170         DecNwTtl decNwTtl = ta.build();
171         ActionBuilder ab = new ActionBuilder();
172         ab.setAction(new DecNwTtlCaseBuilder().setDecNwTtl(decNwTtl).build());
173
174         // Add our drop action to a list
175         List<Action> actionList = new ArrayList<>();
176         actionList.add(ab.build());
177
178         // Create an Apply Action
179         ApplyActionsBuilder aab = new ApplyActionsBuilder();
180         aab.setAction(actionList);
181
182         // Wrap our Apply Action in an Instruction
183         InstructionBuilder ib = new InstructionBuilder();
184         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
185         ib.withKey(new InstructionKey(0));
186         ib.setOrder(0);
187
188         // Put our Instruction in a list of Instructions
189         InstructionsBuilder isb = new InstructionsBuilder();
190         List<Instruction> instructions = new ArrayList<>();
191         instructions.add(ib.build());
192         isb.setInstruction(instructions);
193         return isb;
194     }
195
196     private static MatchBuilder createMatch2() {
197         MatchBuilder match = new MatchBuilder();
198         Ipv4MatchBuilder ipv4Match = new Ipv4MatchBuilder();
199         Ipv4Prefix prefix = new Ipv4Prefix("10.0.0.1");
200         ipv4Match.setIpv4Source(prefix);
201         Ipv4Match i4m = ipv4Match.build();
202         match.setLayer3Match(i4m);
203
204         EthernetMatchBuilder eth = new EthernetMatchBuilder();
205         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
206         ethTypeBuilder.setType(new EtherType(0x0800L));
207         eth.setEthernetType(ethTypeBuilder.build());
208         match.setEthernetMatch(eth.build());
209         return match;
210     }
211
212     private static MatchBuilder createMatch3() {
213         MatchBuilder match = new MatchBuilder();
214         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
215         EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
216         ethSourceBuilder.setAddress(new MacAddress("00:00:00:00:00:01"));
217         ethernetMatch.setEthernetSource(ethSourceBuilder.build());
218         match.setEthernetMatch(ethernetMatch.build());
219
220         return match;
221     }
222
223     private static InstructionsBuilder createDropInstructions() {
224         DropActionBuilder dab = new DropActionBuilder();
225         DropAction dropAction = dab.build();
226         ActionBuilder ab = new ActionBuilder();
227         ab.setAction(new DropActionCaseBuilder().setDropAction(dropAction).build());
228
229         // Add our drop action to a list
230         List<Action> actionList = new ArrayList<>();
231         actionList.add(ab.build());
232
233         // Create an Apply Action
234         ApplyActionsBuilder aab = new ApplyActionsBuilder();
235         aab.setAction(actionList);
236
237         // Wrap our Apply Action in an Instruction
238         InstructionBuilder ib = new InstructionBuilder();
239         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
240
241         // Put our Instruction in a list of Instructions
242         InstructionsBuilder isb = new InstructionsBuilder();
243         List<Instruction> instructions = new ArrayList<>();
244         instructions.add(ib.build());
245         isb.setInstruction(instructions);
246         return isb;
247     }
248
249     private static MatchBuilder createEthernetMatch() {
250         EthernetMatchBuilder ethmatch = new EthernetMatchBuilder(); // ethernettype
251         // match
252         EthernetTypeBuilder ethtype = new EthernetTypeBuilder();
253         EtherType type = new EtherType(0x0800L);
254         ethmatch.setEthernetType(ethtype.setType(type).build());
255
256         EthernetDestinationBuilder ethdest = new EthernetDestinationBuilder(); // ethernet
257         // macaddress
258         // match
259         MacAddress macdest = new MacAddress("ff:ff:ff:ff:ff:ff");
260         ethdest.setAddress(macdest);
261         // ethdest.setMask(mask1);
262
263         ethmatch.setEthernetDestination(ethdest.build());
264
265         EthernetSourceBuilder ethsrc = new EthernetSourceBuilder();
266         MacAddress macsrc = new MacAddress("00:00:00:00:23:ae");
267         ethsrc.setAddress(macsrc);
268         // ethsrc.setMask(mask2);
269
270         ethmatch.setEthernetSource(ethsrc.build());
271         MatchBuilder match = new MatchBuilder();
272         match.setEthernetMatch(ethmatch.build());
273         return match;
274
275     }
276
277     private static InstructionsBuilder createMeterInstructions() {
278
279         MeterBuilder aab = new MeterBuilder();
280         aab.setMeterId(new MeterId(1L));
281
282         InstructionBuilder ib = new InstructionBuilder();
283         ib.setInstruction(new MeterCaseBuilder().setMeter(aab.build()).build());
284
285         // Put our Instruction in a list of Instructions
286         InstructionsBuilder isb = new InstructionsBuilder();
287         List<Instruction> instructions = new ArrayList<>();
288         instructions.add(ib.build());
289         isb.setInstruction(instructions);
290         return isb;
291     }
292
293     private static InstructionsBuilder createAppyActionInstruction() {
294
295         List<Action> actionList = new ArrayList<>();
296         ActionBuilder ab = new ActionBuilder();
297         ControllerActionBuilder controller = new ControllerActionBuilder();
298         controller.setMaxLength(5);
299         ab.setAction(new ControllerActionCaseBuilder().setControllerAction(controller.build()).build());
300         actionList.add(ab.build());
301         // Create an Apply Action
302         ApplyActionsBuilder aab = new ApplyActionsBuilder();
303         aab.setAction(actionList);
304
305         // Wrap our Apply Action in an Instruction
306         InstructionBuilder ib = new InstructionBuilder();
307         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
308
309         // Put our Instruction in a list of Instructions
310         InstructionsBuilder isb = new InstructionsBuilder();
311         List<Instruction> instructions = new ArrayList<>();
312         instructions.add(ib.build());
313         isb.setInstruction(instructions);
314         return isb;
315     }
316
317     private static InstructionsBuilder createAppyActionInstruction7() {
318
319         List<Action> actionList = new ArrayList<>();
320         ActionBuilder ab = new ActionBuilder();
321
322         SetVlanIdActionBuilder vl = new SetVlanIdActionBuilder();
323         VlanId vlanId = new VlanId(4012);
324         vl.setVlanId(vlanId);
325         ab.setAction(new SetVlanIdActionCaseBuilder().setSetVlanIdAction(vl.build()).build());
326         actionList.add(ab.build());
327         // Create an Apply Action
328         ApplyActionsBuilder aab = new ApplyActionsBuilder();
329         aab.setAction(actionList);
330
331         // Wrap our Apply Action in an Instruction
332         InstructionBuilder ib = new InstructionBuilder();
333         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
334
335         // Put our Instruction in a list of Instructions
336         InstructionsBuilder isb = new InstructionsBuilder();
337         List<Instruction> instructions = new ArrayList<>();
338         instructions.add(ib.build());
339         isb.setInstruction(instructions);
340         return isb;
341     }
342
343     private static InstructionsBuilder createAppyActionInstruction21() {
344
345         List<Action> actionList = new ArrayList<>();
346         ActionBuilder ab = new ActionBuilder();
347
348         PopVlanActionBuilder popVlanActionBuilder = new PopVlanActionBuilder();
349         ab.setAction(new PopVlanActionCaseBuilder().setPopVlanAction(popVlanActionBuilder.build()).build());
350         actionList.add(ab.build());
351
352         // Create an Apply Action
353         ApplyActionsBuilder aab = new ApplyActionsBuilder();
354         aab.setAction(actionList);
355
356         // Wrap our Apply Action in an Instruction
357         InstructionBuilder ib = new InstructionBuilder();
358         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
359
360         // Put our Instruction in a list of Instructions
361         InstructionsBuilder isb = new InstructionsBuilder();
362         List<Instruction> instructions = new ArrayList<>();
363         instructions.add(ib.build());
364         isb.setInstruction(instructions);
365         return isb;
366     }
367
368     private static InstructionsBuilder createAppyActionInstruction2() {
369
370         List<Action> actionList = new ArrayList<>();
371         ActionBuilder ab = new ActionBuilder();
372
373         PushMplsActionBuilder push = new PushMplsActionBuilder();
374         push.setEthernetType(0x8847);
375         ab.setAction(new PushMplsActionCaseBuilder().setPushMplsAction(push.build()).build());
376         actionList.add(ab.build());
377         // Create an Apply Action
378         ApplyActionsBuilder aab = new ApplyActionsBuilder();
379         aab.setAction(actionList);
380
381         // Wrap our Apply Action in an Instruction
382         InstructionBuilder ib = new InstructionBuilder();
383         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
384
385         // Put our Instruction in a list of Instructions
386         InstructionsBuilder isb = new InstructionsBuilder();
387         List<Instruction> instructions = new ArrayList<>();
388         instructions.add(ib.build());
389         isb.setInstruction(instructions);
390         return isb;
391     }
392
393     private static InstructionsBuilder createAppyActionInstruction3() {
394
395         List<Action> actionList = new ArrayList<>();
396         ActionBuilder ab = new ActionBuilder();
397
398         PushPbbActionBuilder pbb = new PushPbbActionBuilder();
399         pbb.setEthernetType(0x88E7);
400         ab.setAction(new PushPbbActionCaseBuilder().setPushPbbAction(pbb.build()).build());
401         actionList.add(ab.build());
402         // Create an Apply Action
403         ApplyActionsBuilder aab = new ApplyActionsBuilder();
404         aab.setAction(actionList);
405
406         // Wrap our Apply Action in an Instruction
407         InstructionBuilder ib = new InstructionBuilder();
408         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
409
410         // Put our Instruction in a list of Instructions
411         InstructionsBuilder isb = new InstructionsBuilder();
412         List<Instruction> instructions = new ArrayList<>();
413         instructions.add(ib.build());
414         isb.setInstruction(instructions);
415         return isb;
416     }
417
418     private static InstructionsBuilder createGotoTableInstructions() {
419
420         GoToTableBuilder aab = new GoToTableBuilder();
421         aab.setTableId((short) 2);
422
423         InstructionBuilder ib = new InstructionBuilder();
424         ib.setInstruction(new GoToTableCaseBuilder().setGoToTable(aab.build()).build());
425
426         // Put our Instruction in a list of Instructions
427         InstructionsBuilder isb = new InstructionsBuilder();
428         List<Instruction> instructions = new ArrayList<>();
429         instructions.add(ib.build());
430         isb.setInstruction(instructions);
431         return isb;
432     }
433
434     private FlowBuilder createTestFlow(NodeBuilder nodeBuilder, String flowTypeArg, String tableId) {
435
436         FlowBuilder flow = new FlowBuilder();
437         long id = 123;
438
439         String flowType = flowTypeArg;
440         if (flowType == null) {
441             flowType = "f1";
442         }
443
444         switch (flowType) {
445             case "f1":
446                 id += 1;
447                 flow.setMatch(createMatch1().build());
448                 flow.setInstructions(createDecNwTtlInstructions().build());
449                 break;
450             case "f2":
451                 id += 2;
452                 flow.setMatch(createMatch2().build());
453                 flow.setInstructions(createDropInstructions().build());
454                 break;
455             case "f3":
456                 id += 3;
457                 flow.setMatch(createMatch3().build());
458                 flow.setInstructions(createDropInstructions().build());
459                 break;
460             case "f4":
461                 id += 4;
462                 flow.setMatch(createEthernetMatch().build());
463                 flow.setInstructions(createDropInstructions().build());
464                 break;
465             case "f82":
466                 id += 1;
467                 flow.setMatch(createMatch1().build());
468                 flow.setInstructions(createDropInstructions().build());
469                 break;
470             case "f5":
471                 id += 5;
472                 flow.setMatch(createMatch1().build());
473                 flow.setInstructions(createAppyActionInstruction().build());
474                 break;
475             case "f6":
476                 id += 6;
477                 flow.setMatch(createMatch1().build());
478                 flow.setInstructions(createGotoTableInstructions().build());
479                 break;
480             case "f7":
481                 id += 7;
482                 flow.setMatch(createMatch1().build());
483                 flow.setInstructions(createMeterInstructions().build());
484                 break;
485             case "f8":
486                 id += 8;
487                 flow.setMatch(createMatch1().build());
488                 flow.setInstructions(createAppyActionInstruction7().build());
489                 break;
490             case "f9":
491                 id += 9;
492                 flow.setMatch(createMatch1().build());
493                 flow.setInstructions(createAppyActionInstruction2().build());
494                 break;
495             case "f10":
496                 id += 10;
497                 flow.setMatch(createMatch1().build());
498                 flow.setInstructions(createAppyActionInstruction3().build());
499                 break;
500             case "f14":
501                 id += 14;
502                 flow.setMatch(createMatch1().build());
503                 flow.setInstructions(createAppyActionInstruction7().build());
504                 break;
505             case "f29":
506                 id += 29;
507                 flow.setMatch(createMatch1().build());
508                 flow.setInstructions(createAppyActionInstruction21().build());
509                 break;
510
511             default:
512                 LOG.warn("flow type not understood: {}", flowType);
513         }
514
515         final FlowKey key = new FlowKey(new FlowId(Long.toString(id)));
516         if (null == flow.isBarrier()) {
517             flow.setBarrier(Boolean.FALSE);
518         }
519         // flow.setBufferId(12L);
520         BigInteger value = BigInteger.valueOf(10);
521         BigInteger outputPort = BigInteger.valueOf(4294967295L);
522         flow.setCookie(new FlowCookie(value));
523         flow.setCookieMask(new FlowCookie(value));
524         flow.setHardTimeout(0);
525         flow.setIdleTimeout(0);
526         flow.setInstallHw(false);
527         flow.setStrict(false);
528         flow.setContainerName(null);
529         flow.setFlags(new FlowModFlags(false, false, false, false, true));
530         flow.setId(new FlowId("12"));
531         flow.setTableId(getTableId(tableId));
532         flow.setOutGroup(4294967295L);
533         // set outport to OFPP_NONE (65535) to disable remove restriction for
534         // flow
535         flow.setOutPort(outputPort);
536
537         flow.withKey(key);
538         flow.setPriority(2);
539         flow.setFlowName(originalFlowName + "X" + flowType);
540         return flow;
541     }
542
543     private short getTableId(String tableId) {
544         short table = 2;
545         if (tableId == null) {
546             return table;
547         }
548
549         try {
550             table = Short.parseShort(tableId);
551         } catch (NumberFormatException ex) {
552             // ignore exception and continue with default value
553         }
554
555         return table;
556
557     }
558
559     public void _addGroups(CommandInterpreter ci) {
560         String nref = ci.nextArgument();
561
562         if (nref == null) {
563             ci.println("test node added");
564             createTestNode();
565         } else {
566             ci.println("User node added" + nref);
567             createUserNode(nref);
568         }
569         Integer count = Integer.parseInt(ci.nextArgument());
570         switch (count) {
571             case 1:
572                 GroupBuilder group = createTestGroup("a7", "g1", "add", "1");
573                 GroupBuilder group1 = createTestGroup("a3", "g1", "add", "2");
574                 writeGroup(ci, group.build(), group1.build());
575                 break;
576             case 2:
577                 GroupBuilder group2 = createTestGroup("a4", "g1", "add", "4");
578                 GroupBuilder group3 = createTestGroup("a5", "g1", "add", "5");
579                 writeGroup(ci, group2.build(), group3.build());
580                 break;
581             case 3:
582                 GroupBuilder group4 = createTestGroup("a6", "g1", "add", "6");
583                 GroupBuilder group5 = createTestGroup("a7", "g1", "add", "7");
584                 writeGroup(ci, group4.build(), group5.build());
585                 break;
586             case 4:
587                 // -ve
588                 GroupBuilder group6 = createTestGroup("a14", "g1", "add", "5");
589                 GroupBuilder group7 = createTestGroup("a3", "g1", "add", "6");
590                 writeGroup(ci, group6.build(), group7.build());
591                 break;
592             default:
593                 break;
594         }
595     }
596
597     private void createUserNode(String nodeRef) {
598         NodeBuilder builder = new NodeBuilder();
599         builder.setId(new NodeId(nodeRef));
600         builder.withKey(new NodeKey(builder.getId()));
601         testNode12 = builder.build();
602     }
603
604     public void _modifyGroups(CommandInterpreter ci) {
605         String nref = ci.nextArgument();
606
607         if (nref == null) {
608             ci.println("test node added");
609             createTestNode();
610         } else {
611             ci.println("User node added" + nref);
612             createUserNode(nref);
613         }
614         Integer count = Integer.parseInt(ci.nextArgument());
615         switch (count) {
616             case 1:
617                 GroupBuilder group = createTestGroup("a4", "g1", "modify", "1");
618                 GroupBuilder group1 = createTestGroup("a5", "g1", "modify", "2");
619                 writeGroup(ci, group.build(), group1.build());
620                 break;
621             case 2:
622                 GroupBuilder group2 = createTestGroup("a1", "g1", "modify", "4");
623                 GroupBuilder group3 = createTestGroup("a2", "g1", "modify", "5");
624                 writeGroup(ci, group2.build(), group3.build());
625                 break;
626             case 3:
627                 GroupBuilder group4 = createTestGroup("a9", "g1", "modify", "6");
628                 GroupBuilder group5 = createTestGroup("a10", "g1", "modify", "7");
629                 writeGroup(ci, group4.build(), group5.build());
630                 break;
631
632             case 4:
633                 GroupBuilder group6 = createTestGroup("a6", "g1", "modify", "5");
634                 GroupBuilder group7 = createTestGroup("a29", "g1", "modify", "6");
635                 writeGroup(ci, group6.build(), group7.build());
636                 break;
637             default:
638                 break;
639         }
640     }
641
642     private InstanceIdentifier<Node> nodeToInstanceId(Node node) {
643         return InstanceIdentifier.create(Nodes.class).child(Node.class, node.key());
644     }
645
646     public void _removeGroups(CommandInterpreter ci) {
647         String nref = ci.nextArgument();
648
649         if (nref == null) {
650             ci.println("test node added");
651             createTestNode();
652         } else {
653             ci.println("User node added" + nref);
654             createUserNode(nref);
655         }
656
657         Integer count = Integer.parseInt(ci.nextArgument());
658         switch (count) {
659             case 1:
660                 GroupBuilder group = createTestGroup("a2", "g1", "remove", "1");
661                 GroupBuilder group1 = createTestGroup("a3", "g1", "remove", "2");
662                 deleteGroup(ci, group.build(), group1.build());
663                 break;
664             case 2:
665                 GroupBuilder group2 = createTestGroup("a4", "g1", "remove", "4");
666                 GroupBuilder group3 = createTestGroup("a5", "g1", "remove", "5");
667                 deleteGroup(ci, group2.build(), group3.build());
668                 break;
669             case 3:
670                 GroupBuilder group4 = createTestGroup("a6", "g1", "remove", "6");
671                 GroupBuilder group5 = createTestGroup("a7", "g1", "remove", "7");
672                 deleteGroup(ci, group4.build(), group5.build());
673                 break;
674             case 4:
675                 GroupBuilder group6 = createTestGroup("a14", "g1", "remove", "5");
676                 GroupBuilder group7 = createTestGroup("a3", "g1", "remove", "6");
677                 deleteGroup(ci, group6.build(), group7.build());
678                 break;
679             case 5:
680                 GroupBuilder group8 = createTestGroup("a4", "g1", "modify", "1");
681                 GroupBuilder group9 = createTestGroup("a5", "g1", "modify", "2");
682                 writeGroup(ci, group8.build(), group9.build());
683                 break;
684             case 6:
685                 GroupBuilder group10 = createTestGroup("a1", "g1", "modify", "4");
686                 GroupBuilder group11 = createTestGroup("a2", "g1", "modify", "5");
687                 writeGroup(ci, group10.build(), group11.build());
688                 break;
689             case 7:
690                 GroupBuilder group12 = createTestGroup("a9", "g1", "modify", "6");
691                 GroupBuilder group13 = createTestGroup("a10", "g1", "modify", "7");
692                 writeGroup(ci, group12.build(), group13.build());
693                 break;
694
695             case 8:
696                 GroupBuilder group14 = createTestGroup("a6", "g1", "modify", "5");
697                 GroupBuilder group15 = createTestGroup("a29", "g1", "modify", "6");
698                 writeGroup(ci, group14.build(), group15.build());
699                 break;
700             default:
701                 break;
702         }
703     }
704
705     private void writeGroup(final CommandInterpreter ci, Group group, Group group1) {
706         ReadWriteTransaction modification = dataBroker.newReadWriteTransaction();
707
708         InstanceIdentifier<Group> path1 = InstanceIdentifier.create(Nodes.class)
709                 .child(Node.class, testNode12.key()).augmentation(FlowCapableNode.class)
710                 .child(Group.class, new GroupKey(group.getGroupId()));
711         modification.merge(LogicalDatastoreType.CONFIGURATION, nodeToInstanceId(testNode12), testNode12, true);
712         modification.merge(LogicalDatastoreType.CONFIGURATION, path1, group, true);
713
714         InstanceIdentifier<Group> path2 = InstanceIdentifier.create(Nodes.class)
715                 .child(Node.class, testNode12.key()).augmentation(FlowCapableNode.class)
716                 .child(Group.class, new GroupKey(group1.getGroupId()));
717         modification.merge(LogicalDatastoreType.CONFIGURATION, nodeToInstanceId(testNode12), testNode12, true);
718         modification.merge(LogicalDatastoreType.CONFIGURATION, path2, group1, true);
719         ListenableFuture<Void> commitFuture = modification.submit();
720         Futures.addCallback(commitFuture, new FutureCallback<Void>() {
721             @Override
722             public void onSuccess(Void notUsed) {
723                 ci.println("Status of Group Data Loaded Transaction: success.");
724             }
725
726             @Override
727             public void onFailure(Throwable throwable) {
728                 ci.println(String.format("Status of Group Data Loaded Transaction : failure. Reason : %s", throwable));
729             }
730         }, MoreExecutors.directExecutor());
731     }
732
733     private void deleteGroup(final CommandInterpreter ci, Group group, Group group1) {
734         ReadWriteTransaction modification = dataBroker.newReadWriteTransaction();
735         InstanceIdentifier<Group> path1 = InstanceIdentifier.create(Nodes.class)
736                 .child(Node.class, testNode12.key()).augmentation(FlowCapableNode.class)
737                 .child(Group.class, new GroupKey(group.getGroupId()));
738         modification.delete(LogicalDatastoreType.OPERATIONAL, path1);
739         modification.delete(LogicalDatastoreType.CONFIGURATION, path1);
740         InstanceIdentifier<Group> path2 = InstanceIdentifier.create(Nodes.class)
741                 .child(Node.class, testNode12.key()).augmentation(FlowCapableNode.class)
742                 .child(Group.class, new GroupKey(group1.getGroupId()));
743         modification.delete(LogicalDatastoreType.OPERATIONAL, path2);
744         modification.delete(LogicalDatastoreType.CONFIGURATION, path2);
745         ListenableFuture<Void> commitFuture = modification.submit();
746         Futures.addCallback(commitFuture, new FutureCallback<Void>() {
747             @Override
748             public void onSuccess(Void notUsed) {
749                 ci.println("Status of Group Data Loaded Transaction: success.");
750             }
751
752             @Override
753             public void onFailure(Throwable throwable) {
754                 ci.println(String.format("Status of Group Data Loaded Transaction : failure. Reason : %s", throwable));
755             }
756         }, MoreExecutors.directExecutor());
757     }
758
759     private GroupBuilder createTestGroup(String actionType, String groupType, String groupmod, String strId) {
760         // Sample data , committing to DataStore
761
762         GroupBuilder group = new GroupBuilder();
763         BucketBuilder bucket = new BucketBuilder();
764         bucket.setBucketId(new BucketId((long) 12));
765         bucket.withKey(new BucketKey(new BucketId((long) 12)));
766
767         if (groupType == null) {
768             groupType = "g1";
769         }
770         if (actionType == null) {
771             actionType = "a1";
772         }
773
774         switch (groupType) {
775             case "g1":
776                 group.setGroupType(GroupTypes.GroupSelect);
777                 break;
778             case "g2":
779                 group.setGroupType(GroupTypes.GroupAll);
780                 break;
781             case "g3":
782                 group.setGroupType(GroupTypes.GroupIndirect);
783                 break;
784             case "g4":
785                 group.setGroupType(GroupTypes.GroupFf);
786                 break;
787             default:
788                 break;
789         }
790
791         switch (actionType) {
792             case "a1":
793                 bucket.setAction(createPopVlanAction());
794                 break;
795             case "a2":
796                 bucket.setAction(createPushVlanAction());
797                 break;
798             case "a3":
799                 bucket.setAction(createPushMplsAction());
800                 break;
801             case "a4":
802                 bucket.setAction(createPopMplsAction());
803                 break;
804             case "a5":
805                 bucket.setAction(createPopPbbAction());
806                 break;
807             case "a6":
808             case "a7":
809                 bucket.setAction(createPushPbbAction());
810                 break;
811             case "a8":
812                 bucket.setAction(createCopyTtlInAction());
813                 break;
814             case "a9":
815                 bucket.setAction(createCopyTtlOutAction());
816                 break;
817             case "a10":
818                 bucket.setAction(createDecMplsTtlAction());
819                 break;
820             case "a14":
821                 bucket.setAction(createGroupAction());
822                 break;
823             case "a29":
824                 bucket.setAction(createNonAppyPushVlanAction());
825                 break;
826             default:
827                 break;
828         }
829
830         if ("add".equals(groupmod)) {
831             bucket.setWatchGroup((long) 14);
832             bucket.setWatchPort((long) 1234);
833             bucket.setWeight(50);
834         } else {
835             bucket.setWatchGroup((long) 13);
836             bucket.setWatchPort((long) 134);
837             bucket.setWeight(30);
838         }
839
840         long id = Long.parseLong(strId);
841         GroupKey key = new GroupKey(new GroupId(id));
842         group.withKey(key);
843         // group.setInstall(false);
844         group.setGroupId(new GroupId(id));
845         group.setGroupName(originalGroupName);
846         group.setBarrier(false);
847         BucketsBuilder value = new BucketsBuilder();
848         List<Bucket> value1 = new ArrayList<>();
849         value1.add(bucket.build());
850         value.setBucket(value1);
851         group.setBuckets(value.build());
852         return group;
853     }
854
855     private List<Action> createPopVlanAction() {
856         PopVlanActionBuilder vlanAction = new PopVlanActionBuilder();
857         ActionBuilder action = new ActionBuilder();
858         action.setAction(new PopVlanActionCaseBuilder().setPopVlanAction(vlanAction.build()).build());
859         action.withKey(new ActionKey(0));
860         List<Action> actions = new ArrayList<>();
861         actions.add(action.build());
862         return actions;
863     }
864
865     private List<Action> createPushVlanAction() {
866         PushVlanActionBuilder vlan = new PushVlanActionBuilder();
867         vlan.setEthernetType(0x8100);
868         VlanId vlanId = new VlanId(2);
869         vlan.setVlanId(vlanId);
870         ActionBuilder action = new ActionBuilder();
871         action.setAction(new PushVlanActionCaseBuilder().setPushVlanAction(vlan.build()).build());
872         List<Action> actions = new ArrayList<>();
873         actions.add(action.build());
874         return actions;
875     }
876
877     private List<Action> createPushMplsAction() {
878         PushMplsActionBuilder push = new PushMplsActionBuilder();
879         push.setEthernetType(0x8847);
880         ActionBuilder action = new ActionBuilder();
881         action.setAction(new PushMplsActionCaseBuilder().setPushMplsAction(push.build()).build());
882         List<Action> actions = new ArrayList<>();
883         actions.add(action.build());
884         return actions;
885     }
886
887     private List<Action> createPopMplsAction() {
888         PopMplsActionBuilder popMplsActionBuilder = new PopMplsActionBuilder();
889         popMplsActionBuilder.setEthernetType(0XB);
890         ActionBuilder action = new ActionBuilder();
891         action.setAction(new PopMplsActionCaseBuilder().setPopMplsAction(popMplsActionBuilder.build()).build());
892         List<Action> actions = new ArrayList<>();
893         actions.add(action.build());
894         return actions;
895     }
896
897     private List<Action> createPopPbbAction() {
898         PopPbbActionBuilder popPbbActionBuilder = new PopPbbActionBuilder();
899         ActionBuilder action = new ActionBuilder();
900         action.setAction(new PopPbbActionCaseBuilder().setPopPbbAction(popPbbActionBuilder.build()).build());
901         List<Action> actions = new ArrayList<>();
902         actions.add(action.build());
903         return actions;
904     }
905
906     private List<Action> createPushPbbAction() {
907         PushPbbActionBuilder pbb = new PushPbbActionBuilder();
908         pbb.setEthernetType(0x88E7);
909         ActionBuilder action = new ActionBuilder();
910         action.setAction(new PushPbbActionCaseBuilder().setPushPbbAction(pbb.build()).build());
911         List<Action> actions = new ArrayList<>();
912         actions.add(action.build());
913         return actions;
914     }
915
916     private List<Action> createCopyTtlInAction() {
917         CopyTtlInBuilder ttlin = new CopyTtlInBuilder();
918         ActionBuilder action = new ActionBuilder();
919         action.setAction(new CopyTtlInCaseBuilder().setCopyTtlIn(ttlin.build()).build());
920         List<Action> actions = new ArrayList<>();
921         actions.add(action.build());
922         return actions;
923     }
924
925     private List<Action> createCopyTtlOutAction() {
926         CopyTtlOutBuilder ttlout = new CopyTtlOutBuilder();
927         ActionBuilder action = new ActionBuilder();
928         action.setAction(new CopyTtlOutCaseBuilder().setCopyTtlOut(ttlout.build()).build());
929         List<Action> actions = new ArrayList<>();
930         actions.add(action.build());
931         return actions;
932     }
933
934     private List<Action> createDecMplsTtlAction() {
935         DecMplsTtlBuilder mpls = new DecMplsTtlBuilder();
936         ActionBuilder action = new ActionBuilder();
937         action.setAction(new DecMplsTtlCaseBuilder().setDecMplsTtl(mpls.build()).build());
938         List<Action> actions = new ArrayList<>();
939         actions.add(action.build());
940         return actions;
941     }
942
943     private List<Action> createGroupAction() {
944
945         GroupActionBuilder groupActionB = new GroupActionBuilder();
946         groupActionB.setGroupId(1L);
947         groupActionB.setGroup("0");
948         ActionBuilder action = new ActionBuilder();
949         action.setAction(new GroupActionCaseBuilder().setGroupAction(groupActionB.build()).build());
950         action.withKey(new ActionKey(0));
951         List<Action> actions = new ArrayList<>();
952         actions.add(action.build());
953         return actions;
954     }
955
956     private static List<Action> createNonAppyPushVlanAction() {
957
958         ActionBuilder ab = new ActionBuilder();
959
960         GroupActionBuilder groupActionB = new GroupActionBuilder();
961         groupActionB.setGroupId(1L);
962         groupActionB.setGroup("0");
963         ab.setAction(new GroupActionCaseBuilder().setGroupAction(groupActionB.build()).build());
964
965         List<Action> actionList = new ArrayList<>();
966         actionList.add(ab.build());
967
968         return actionList;
969     }
970 }