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