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