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