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