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