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