Merge remote-tracking branch 'liblldp/master'
[openflowplugin.git] / test-provider / src / main / java / org / opendaylight / openflowplugin / test / OpenflowPluginBulkTransactionProvider.java
1 /*
2  * Copyright (c) 2014, 2015 Ericsson, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowplugin.test;
10
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.math.BigInteger;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.eclipse.osgi.framework.console.CommandInterpreter;
19 import org.eclipse.osgi.framework.console.CommandProvider;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
25 import org.opendaylight.controller.sal.binding.api.NotificationService;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6FlowLabel;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.ControllerActionCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.CopyTtlInCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DecNwTtlCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.FloodAllActionCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.GroupActionCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushMplsActionCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushPbbActionCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlSrcActionCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwDstActionCaseBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcActionCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwTosActionCaseBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanIdActionCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanPcpActionCaseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SwPathActionCaseBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.controller.action._case.ControllerActionBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.copy.ttl.in._case.CopyTtlInBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.dec.nw.ttl._case.DecNwTtl;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.dec.nw.ttl._case.DecNwTtlBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropAction;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropActionBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.flood.all.action._case.FloodAllActionBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.group.action._case.GroupActionBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.mpls.action._case.PushMplsActionBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.pbb.action._case.PushPbbActionBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.dl.src.action._case.SetDlSrcActionBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.nw.dst.action._case.SetNwDstActionBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.nw.src.action._case.SetNwSrcActionBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.nw.tos.action._case.SetNwTosActionBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.vlan.id.action._case.SetVlanIdActionBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.vlan.pcp.action._case.SetVlanPcpActionBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.sw.path.action._case.SwPathActionBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4Builder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.MeterCaseBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTableBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.meter._case.MeterBuilder;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6ExtHeaderBuilder;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6LabelBuilder;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv6MatchBuilder;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.NodeErrorListener;
115 import org.opendaylight.yangtools.concepts.Registration;
116 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
117 import org.osgi.framework.BundleContext;
118 import org.slf4j.Logger;
119 import org.slf4j.LoggerFactory;
120
121 public class OpenflowPluginBulkTransactionProvider implements CommandProvider {
122
123     private static final Logger LOG = LoggerFactory.getLogger(OpenflowPluginBulkTransactionProvider.class);
124     private DataBroker dataBroker;
125     private final BundleContext ctx;
126     private NodeBuilder testNode;
127     private ProviderContext pc;
128     private FlowBuilder testFlow;
129     private final String originalFlowName = "Foo";
130     private final NodeErrorListener nodeErrorListener = new NodeErrorListenerLoggingImpl();
131     private Registration listener1Reg;
132     private Registration listener2Reg;
133     private Node testNode12;
134     private final String originalGroupName = "Foo";
135     private static NotificationService notificationService;
136
137     public OpenflowPluginBulkTransactionProvider(BundleContext ctx) {
138         this.ctx = ctx;
139     }
140
141     public void onSessionInitiated(ProviderContext session) {
142         pc = session;
143         notificationService = session.getSALService(NotificationService.class);
144         listener2Reg = notificationService.registerNotificationListener(nodeErrorListener);
145         dataBroker = session.getSALService(DataBroker.class);
146         ctx.registerService(CommandProvider.class.getName(), this, null);
147         createTestFlow(createTestNode(null), null, null);
148     }
149
150     private NodeBuilder createTestNode(String nodeId) {
151         if (nodeId == null) {
152             nodeId = OpenflowpluginTestActivator.NODE_ID;
153         }
154         NodeRef nodeOne = createNodeRef(nodeId);
155         NodeBuilder builder = new NodeBuilder();
156         builder.setId(new NodeId(nodeId));
157         builder.setKey(new NodeKey(builder.getId()));
158         testNode = builder;
159         return builder;
160     }
161
162     private static NodeRef createNodeRef(String string) {
163         NodeKey key = new NodeKey(new NodeId(string));
164         InstanceIdentifier<Node> path = InstanceIdentifier.create(Nodes.class).child(Node.class, key);
165
166         return new NodeRef(path);
167     }
168
169     @Override
170     public String getHelp() {
171         return "No help";
172     }
173
174     private FlowBuilder createTestFlow(NodeBuilder nodeBuilder, String flowTypeArg, String tableId) {
175
176         FlowBuilder flow = new FlowBuilder();
177         long id = 123;
178
179         String flowType = flowTypeArg;
180         if (flowType == null) {
181             flowType = "f1";
182         }
183
184         switch (flowType) {
185             case "f1":
186                 id += 1;
187                 flow.setMatch(createMatch1().build());
188                 flow.setInstructions(createDecNwTtlInstructions().build());
189                 break;
190             case "f2":
191                 id += 2;
192                 flow.setMatch(createMatch2().build());
193                 flow.setInstructions(createDropInstructions().build());
194                 break;
195             case "f3":
196                 id += 3;
197                 flow.setMatch(createMatch3().build());
198                 flow.setInstructions(createDropInstructions().build());
199                 break;
200             case "f4":
201                 id += 4;
202                 flow.setMatch(createEthernetMatch().build());
203                 flow.setInstructions(createDropInstructions().build());
204                 break;
205             case "f5":
206                 id += 5;
207                 flow.setMatch(createMatch1().build());
208                 flow.setInstructions(createAppyActionInstruction().build());
209                 break;
210             case "f6":
211                 id += 6;
212                 flow.setMatch(createMatch1().build());
213                 flow.setInstructions(createGotoTableInstructions().build());
214                 break;
215             case "f7":
216                 id += 7;
217                 flow.setMatch(createMatch1().build());
218                 flow.setInstructions(createMeterInstructions().build());
219                 break;
220             case "f8":
221                 id += 8;
222                 flow.setMatch(createMatch1().build());
223                 flow.setInstructions(createAppyActionInstruction7().build());
224                 break;
225             case "f9":
226                 id += 9;
227                 flow.setMatch(createMatch1().build());
228                 flow.setInstructions(createAppyActionInstruction2().build());
229                 break;
230             case "f10":
231                 id += 10;
232                 flow.setMatch(createMatch1().build());
233                 flow.setInstructions(createAppyActionInstruction3().build());
234                 break;
235             case "f23":
236                 id += 23;
237                 flow.setMatch(createMatch1().build());
238                 flow.setInstructions(createAppyActionInstruction16().build());
239                 break;
240             case "f230":
241                 id += 23;
242                 flow.setMatch(createMatch1().build());
243                 flow.setInstructions(createAppyActionInstruction160().build());
244                 break;
245             case "f34":
246                 id += 34;
247                 flow.setMatch(createMatch1().build());
248                 flow.setInstructions(createAppyActionInstruction26().build());
249                 break;
250             case "f35":
251                 id += 35;
252                 flow.setMatch(createMatch1().build());
253                 flow.setInstructions(createAppyActionInstruction27().build());
254                 break;
255             case "f36":
256                 id += 36;
257                 flow.setMatch(createMatch1().build());
258                 flow.setInstructions(createAppyActionInstruction28().build());
259                 break;
260             case "f42":
261                 id += 42;
262                 flow.setMatch(createMatch1().build());
263                 flow.setInstructions(createAppyActionInstruction34().build());
264                 break;
265             case "f43":
266                 id += 43;
267                 flow.setMatch(createICMPv6Match().build());
268                 flow.setInstructions(createDecNwTtlInstructions().build());
269                 break;
270             case "f44":
271                 id += 44;
272                 flow.setMatch(createInphyportMatch(nodeBuilder.getId()).build());
273                 flow.setInstructions(createDropInstructions().build());
274                 break;
275             case "f45":
276                 id += 45;
277                 flow.setMatch(createMetadataMatch().build());
278                 flow.setInstructions(createDropInstructions().build());
279                 break;
280             case "f46":
281                 id += 46;
282                 flow.setMatch(createL3IPv6Match().build());
283                 flow.setInstructions(createDecNwTtlInstructions().build());
284                 break;
285             case "f81":
286                 id += 81;
287                 flow.setMatch(createLLDPMatch().build());
288                 flow.setInstructions(createSentToControllerInstructions().build());
289                 break;
290
291             case "f82":
292                 id += 1;
293                 flow.setMatch(createMatch1().build());
294                 flow.setInstructions(createDropInstructions().build());
295                 break;
296             case "f83":
297                 id += 2;
298                 flow.setMatch(createMatch2().build());
299                 flow.setInstructions(createDecNwTtlInstructions().build());
300                 break;
301             case "f84":
302                 id += 3;
303                 flow.setMatch(createMatch3().build());
304                 flow.setInstructions(createDecNwTtlInstructions().build());
305                 break;
306             case "f85":
307                 id += 4;
308                 flow.setMatch(createEthernetMatch().build());
309                 flow.setInstructions(createMeterInstructions().build());
310                 break;
311             case "f86":
312                 id += 6;
313                 flow.setMatch(createMatch1().build());
314                 flow.setInstructions(createDecNwTtlInstructions().build());
315                 break;
316             case "f87":
317                 id += 12;
318                 flow.setMatch(createMatch1().build());
319                 flow.setInstructions(createAppyActionInstruction7().build());
320                 break;
321             case "f88":
322                 id += 13;
323                 flow.setMatch(createEthernetMatch().build());
324                 flow.setInstructions(createAppyActionInstruction6().build());
325                 break;
326             case "f89":
327                 id += 14;
328                 flow.setMatch(createEthernetMatch().build());
329                 flow.setInstructions(createAppyActionInstruction7().build());
330                 break;
331             case "f90":
332                 id += 15;
333                 flow.setMatch(createMatch1().build());
334                 flow.setInstructions(createAppyActionInstruction9().build());
335                 break;
336             case "f91":
337                 id += 7;
338                 flow.setMatch(createMatch1().build());
339                 flow.setInstructions(createAppyActionInstruction9().build());
340                 break;
341             case "f92":
342                 id += 8;
343                 flow.setMatch(createMatch1().build());
344                 flow.setInstructions(createAppyActionInstruction6().build());
345                 break;
346             case "f93":
347                 id += 9;
348                 flow.setMatch(createMatch1().build());
349                 flow.setInstructions(createDecNwTtlInstructions().build());
350                 break;
351             case "f94":
352                 id += 10;
353                 flow.setMatch(createMatch1().build());
354                 flow.setInstructions(createDecNwTtlInstructions().build());
355                 break;
356             case "f95":
357                 id += 42;
358                 flow.setMatch(createMatch1().build());
359                 flow.setInstructions(createDecNwTtlInstructions().build());
360                 break;
361             case "f96":
362                 id += 43;
363                 flow.setMatch(createICMPv6Match().build());
364                 flow.setInstructions(createDropInstructions().build());
365                 break;
366             case "f97":
367                 id += 44;
368                 flow.setMatch(createInphyportMatch(nodeBuilder.getId()).build());
369                 flow.setInstructions(createMeterInstructions().build());
370                 break;
371             case "f98":
372                 id += 45;
373                 flow.setMatch(createMetadataMatch().build());
374                 flow.setInstructions(createAppyActionInstruction6().build());
375                 break;
376             case "f99":
377                 id += 34;
378                 flow.setMatch(createMatch1().build());
379                 flow.setInstructions(createAppyActionInstruction6().build());
380                 break;
381             case "f100":
382                 id += 35;
383                 flow.setMatch(createMatch1().build());
384                 flow.setInstructions(createAppyActionInstruction7().build());
385                 break;
386             case "f101":
387                 id += 36;
388                 flow.setMatch(createMatch1().build());
389                 flow.setInstructions(createAppyActionInstruction8().build());
390                 break;
391             case "f700":
392                 id += 3;
393                 flow.setMatch(createMatch3().build());
394                 flow.setInstructions(createMeterInstructions().build());
395                 break;
396             case "f800":
397                 id += 8;
398                 flow.setMatch(createMatch1000().build());
399                 flow.setInstructions(createAppyActionInstruction6().build());
400                 break;
401             case "f900":
402                 id += 5;
403                 flow.setMatch(createMatch1000().build());
404                 flow.setInstructions(createAppyActionInstruction2().build());
405                 break;
406             case "f1000":
407                 id += 10;
408                 flow.setMatch(createMatch1000().build());
409                 flow.setInstructions(createAppyActionInstruction3().build());
410                 break;
411             default:
412                 LOG.warn("flow type not understood: {}", flowType);
413         }
414
415         FlowKey key = new FlowKey(new FlowId(Long.toString(id)));
416         if (null == flow.isBarrier()) {
417             flow.setBarrier(Boolean.FALSE);
418         }
419         // flow.setBufferId(12L);
420         BigInteger value = BigInteger.valueOf(10);
421         BigInteger outputPort = BigInteger.valueOf(4294967295L);
422         flow.setCookie(new FlowCookie(value));
423         flow.setCookieMask(new FlowCookie(value));
424         flow.setHardTimeout(0);
425         flow.setIdleTimeout(0);
426         flow.setInstallHw(false);
427         flow.setStrict(false);
428         flow.setContainerName(null);
429         flow.setFlags(new FlowModFlags(false, false, false, false, true));
430         flow.setId(new FlowId("12"));
431         flow.setTableId(getTableId(tableId));
432         flow.setOutGroup(4294967295L);
433         // set outport to OFPP_NONE (65535) to disable remove restriction for
434         // flow
435         flow.setOutPort(outputPort);
436
437         flow.setKey(key);
438         flow.setPriority(2);
439         flow.setFlowName(originalFlowName + "X" + flowType);
440         testFlow = flow;
441         return flow;
442     }
443
444     private short getTableId(String tableId) {
445         short table = 2;
446         try {
447             table = Short.parseShort(tableId);
448         } catch (Exception ex) {
449             // ignore exception and continue with default value
450         }
451
452         return table;
453
454     }
455
456     private void createTestNode() {
457         NodeRef nodeOne = createNodeRef(OpenflowpluginTestActivator.NODE_ID);
458         NodeBuilder builder = new NodeBuilder();
459         builder.setId(new NodeId(OpenflowpluginTestActivator.NODE_ID));
460         builder.setKey(new NodeKey(builder.getId()));
461         testNode12 = builder.build();
462     }
463
464     public void _addFlows(CommandInterpreter ci) {
465         NodeBuilder tn = createTestNode(ci.nextArgument());
466         String flowtype = ci.nextArgument();
467         Integer flowcnt = Integer.parseInt(flowtype);
468         FlowBuilder tf;
469         FlowBuilder tf1;
470         FlowBuilder tf2;
471         FlowBuilder tf3;
472         switch (flowcnt) {
473             case 1:
474                 tf = createTestFlow(tn, "f1", "10");
475                 tf1 = createTestFlow(tn, "f2", "11");
476                 tf2 = createTestFlow(tn, "f3", "12");
477                 tf3 = createTestFlow(tn, "f4", "13");
478                 break;
479             case 2:
480                 tf = createTestFlow(tn, "f3", "3");
481                 tf1 = createTestFlow(tn, "f4", "4");
482                 tf2 = createTestFlow(tn, "f5", "5");
483                 tf3 = createTestFlow(tn, "f6", "6");
484                 break;
485             case 3:
486                 tf = createTestFlow(tn, "f7", "7");
487                 tf1 = createTestFlow(tn, "f8", "8");
488                 tf2 = createTestFlow(tn, "f9", "9");
489                 tf3 = createTestFlow(tn, "f10", "10");
490                 break;
491             case 4:
492                 // -ve scenario
493                 tf = createTestFlow(tn, "f23", "3");
494                 tf1 = createTestFlow(tn, "f34", "4");
495                 tf2 = createTestFlow(tn, "f35", "5");
496                 tf3 = createTestFlow(tn, "f36", "6");
497                 break;
498             case 5:
499                 // +ve scenario
500                 // modify case 6 -ve
501                 tf = createTestFlow(tn, "f230", "3");
502                 tf1 = createTestFlow(tn, "f34", "4");
503                 tf2 = createTestFlow(tn, "f35", "5");
504                 tf3 = createTestFlow(tn, "f36", "6");
505                 break;
506
507             default:
508                 tf = createTestFlow(tn, "f42", "42");
509                 tf1 = createTestFlow(tn, "f43", "43");
510                 tf2 = createTestFlow(tn, "f44", "44");
511                 tf3 = createTestFlow(tn, "f45", "45");
512
513         }
514         writeFlow(ci, tf, tf1, tf2, tf3, tn);
515     }
516
517     private InstanceIdentifier<Node> nodeBuilderToInstanceId(NodeBuilder node) {
518         return InstanceIdentifier.create(Nodes.class).child(Node.class, node.getKey());
519     }
520
521     public void _modifyFlows(CommandInterpreter ci) {
522         NodeBuilder tn = createTestNode(ci.nextArgument());
523         String flowtype = ci.nextArgument();
524         Integer flowcnt = Integer.parseInt(flowtype);
525         FlowBuilder tf;
526         FlowBuilder tf1;
527         FlowBuilder tf2;
528         FlowBuilder tf3;
529         switch (flowcnt) {
530             case 1:
531                 tf = createTestFlow(tn, "f82", "10");
532                 tf1 = createTestFlow(tn, "f83", "11");
533                 tf2 = createTestFlow(tn, "f84", "12");
534                 tf3 = createTestFlow(tn, "f85", "13");
535                 break;
536             case 2:
537                 tf = createTestFlow(tn, "f700", "3");
538                 tf1 = createTestFlow(tn, "f4", "4");
539                 tf2 = createTestFlow(tn, "f900", "5");
540                 tf3 = createTestFlow(tn, "f86", "6");
541                 break;
542             case 3:
543                 // +
544                 tf = createTestFlow(tn, "f91", "7");
545                 tf1 = createTestFlow(tn, "f92", "8");
546                 tf2 = createTestFlow(tn, "f93", "9");
547                 tf3 = createTestFlow(tn, "f94", "10");
548                 break;
549             case 4:
550                 // +ve scenario
551                 tf = createTestFlow(tn, "f230", "3");
552                 tf1 = createTestFlow(tn, "f99", "4");
553                 tf2 = createTestFlow(tn, "f100", "5");
554                 tf3 = createTestFlow(tn, "f101", "6");
555                 break;
556             case 5:
557                 // -
558                 tf = createTestFlow(tn, "f23", "3");
559                 tf1 = createTestFlow(tn, "f99", "4");
560                 tf2 = createTestFlow(tn, "f100", "5");
561                 tf3 = createTestFlow(tn, "f101", "6");
562                 break;
563
564             default:
565                 tf = createTestFlow(tn, "f87", "12");
566                 tf1 = createTestFlow(tn, "f88", "13");
567                 tf2 = createTestFlow(tn, "f89", "14");
568                 tf3 = createTestFlow(tn, "f90", "15");
569
570         }
571
572         writeFlow(ci, tf, tf1, tf2, tf3, tn);
573
574     }
575
576     public void _removeFlows(final CommandInterpreter ci) {
577         ReadWriteTransaction modification = dataBroker.newReadWriteTransaction();
578         NodeBuilder tn = createTestNode(ci.nextArgument());
579         String flowtype = ci.nextArgument();
580         Integer flowcnt = Integer.parseInt(flowtype);
581         FlowBuilder tf = null;
582         FlowBuilder tf1 = null;
583         FlowBuilder tf2 = null;
584         FlowBuilder tf3 = null;
585         switch (flowcnt) {
586             case 1:
587                 // add case 1
588                 tf = createTestFlow(tn, "f1", "10");
589                 tf1 = createTestFlow(tn, "f2", "11");
590                 tf2 = createTestFlow(tn, "f3", "12");
591                 tf3 = createTestFlow(tn, "f4", "13");
592                 break;
593             case 2:
594                 // modify case 1
595                 tf = createTestFlow(tn, "f82", "10");
596                 tf1 = createTestFlow(tn, "f83", "11");
597                 tf2 = createTestFlow(tn, "f84", "12");
598                 tf3 = createTestFlow(tn, "f85", "13");
599                 break;
600             case 3:
601                 // add case 2
602                 tf = createTestFlow(tn, "f3", "3");
603                 tf1 = createTestFlow(tn, "f4", "4");
604                 tf2 = createTestFlow(tn, "f5", "5");
605                 tf3 = createTestFlow(tn, "f6", "6");
606                 break;
607             case 4:
608                 // modify case 2
609                 tf = createTestFlow(tn, "f700", "3");
610                 tf1 = createTestFlow(tn, "f4", "4");
611                 tf2 = createTestFlow(tn, "f900", "5");
612                 tf3 = createTestFlow(tn, "f86", "6");
613                 break;
614             case 5:
615                 // add case 3
616                 tf = createTestFlow(tn, "f7", "7");
617                 tf1 = createTestFlow(tn, "f8", "8");
618                 tf2 = createTestFlow(tn, "f9", "9");
619                 tf3 = createTestFlow(tn, "f10", "10");
620                 break;
621             case 6:
622                 // modify case 3
623                 tf = createTestFlow(tn, "f91", "7");
624                 tf1 = createTestFlow(tn, "f92", "8");
625                 tf2 = createTestFlow(tn, "f93", "9");
626                 tf3 = createTestFlow(tn, "f94", "10");
627                 break;
628             case 7:
629                 // -ve scenario
630                 tf = createTestFlow(tn, "f23", "3");
631                 tf1 = createTestFlow(tn, "f34", "4");
632                 tf2 = createTestFlow(tn, "f35", "5");
633                 tf3 = createTestFlow(tn, "f36", "6");
634                 break;
635             case 8:
636                 // +ve scenario
637                 // modify case 6 -ve
638                 tf = createTestFlow(tn, "f23", "3");
639                 tf1 = createTestFlow(tn, "f99", "4");
640                 tf2 = createTestFlow(tn, "f100", "5");
641                 tf3 = createTestFlow(tn, "f101", "6");
642                 break;
643             case 9:
644                 // modify case 6
645                 tf = createTestFlow(tn, "f700", "7");
646                 tf1 = createTestFlow(tn, "f230", "23");
647                 tf2 = createTestFlow(tn, "f900", "9");
648                 tf3 = createTestFlow(tn, "f1000", "10");
649                 break;
650         }
651
652         InstanceIdentifier<Flow> path1 = InstanceIdentifier.create(Nodes.class).child(Node.class, tn.getKey())
653                 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tf.getTableId()))
654                 .child(Flow.class, tf.getKey());
655         modification.delete(LogicalDatastoreType.OPERATIONAL, path1);
656         modification.delete(LogicalDatastoreType.CONFIGURATION, nodeBuilderToInstanceId(tn));
657         modification.delete(LogicalDatastoreType.CONFIGURATION, path1);
658         InstanceIdentifier<Flow> path2 = InstanceIdentifier.create(Nodes.class).child(Node.class, tn.getKey())
659                 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tf1.getTableId()))
660                 .child(Flow.class, tf1.getKey());
661         modification.delete(LogicalDatastoreType.OPERATIONAL, path2);
662         modification.delete(LogicalDatastoreType.CONFIGURATION, nodeBuilderToInstanceId(tn));
663         modification.delete(LogicalDatastoreType.CONFIGURATION, path2);
664
665         InstanceIdentifier<Flow> path3 = InstanceIdentifier.create(Nodes.class).child(Node.class, tn.getKey())
666                 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tf2.getTableId()))
667                 .child(Flow.class, tf2.getKey());
668         modification.delete(LogicalDatastoreType.OPERATIONAL, path3);
669         modification.delete(LogicalDatastoreType.CONFIGURATION, nodeBuilderToInstanceId(tn));
670         modification.delete(LogicalDatastoreType.CONFIGURATION, path3);
671         InstanceIdentifier<Flow> path4 = InstanceIdentifier.create(Nodes.class).child(Node.class, tn.getKey())
672                 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tf3.getTableId()))
673                 .child(Flow.class, tf3.getKey());
674         modification.delete(LogicalDatastoreType.OPERATIONAL, path4);
675         modification.delete(LogicalDatastoreType.CONFIGURATION, nodeBuilderToInstanceId(tn));
676         modification.delete(LogicalDatastoreType.CONFIGURATION, path4);
677         CheckedFuture<Void, TransactionCommitFailedException> commitFuture = modification.submit();
678         Futures.addCallback(commitFuture, new FutureCallback<Void>() {
679             @Override
680             public void onSuccess(Void aVoid) {
681                 ci.println("Status of Group Data Loaded Transaction: success.");
682             }
683
684             @Override
685             public void onFailure(Throwable throwable) {
686                 LOG.error(throwable.getMessage(), throwable);
687                 ci.println(String.format("Status of Group Data Loaded Transaction : failure. Reason : %s", throwable));
688             }
689         }, MoreExecutors.directExecutor());
690
691     }
692
693     private void writeFlow(final CommandInterpreter ci, FlowBuilder flow, FlowBuilder flow1, FlowBuilder flow2,
694                            FlowBuilder flow3, NodeBuilder nodeBuilder) {
695         ReadWriteTransaction modification = dataBroker.newReadWriteTransaction();
696         InstanceIdentifier<Flow> path1 = InstanceIdentifier.create(Nodes.class)
697                 .child(Node.class, nodeBuilder.getKey()).augmentation(FlowCapableNode.class)
698                 .child(Table.class, new TableKey(flow.getTableId())).child(Flow.class, flow.getKey());
699         modification.merge(LogicalDatastoreType.OPERATIONAL, nodeBuilderToInstanceId(nodeBuilder), nodeBuilder.build(), true);
700         modification.merge(LogicalDatastoreType.OPERATIONAL, path1, flow.build(), true);
701         modification.merge(LogicalDatastoreType.CONFIGURATION, nodeBuilderToInstanceId(nodeBuilder), nodeBuilder.build(), true);
702         modification.merge(LogicalDatastoreType.CONFIGURATION, path1, flow.build(), true);
703         InstanceIdentifier<Flow> path2 = InstanceIdentifier.create(Nodes.class)
704                 .child(Node.class, nodeBuilder.getKey()).augmentation(FlowCapableNode.class)
705                 .child(Table.class, new TableKey(flow1.getTableId())).child(Flow.class, flow1.getKey());
706         modification.merge(LogicalDatastoreType.OPERATIONAL, nodeBuilderToInstanceId(nodeBuilder), nodeBuilder.build(), true);
707         modification.merge(LogicalDatastoreType.OPERATIONAL, path2, flow1.build(), true);
708         modification.merge(LogicalDatastoreType.CONFIGURATION, nodeBuilderToInstanceId(nodeBuilder), nodeBuilder.build(), true);
709         modification.merge(LogicalDatastoreType.CONFIGURATION, path2, flow1.build(), true);
710
711         InstanceIdentifier<Flow> path3 = InstanceIdentifier.create(Nodes.class)
712                 .child(Node.class, nodeBuilder.getKey()).augmentation(FlowCapableNode.class)
713                 .child(Table.class, new TableKey(flow2.getTableId())).child(Flow.class, flow2.getKey());
714         modification.merge(LogicalDatastoreType.OPERATIONAL, nodeBuilderToInstanceId(nodeBuilder), nodeBuilder.build(), true);
715         modification.merge(LogicalDatastoreType.OPERATIONAL, path3, flow2.build(), true);
716         modification.merge(LogicalDatastoreType.CONFIGURATION, nodeBuilderToInstanceId(nodeBuilder), nodeBuilder.build(), true);
717         modification.merge(LogicalDatastoreType.CONFIGURATION, path3, flow2.build(), true);
718
719         InstanceIdentifier<Flow> path4 = InstanceIdentifier.create(Nodes.class)
720                 .child(Node.class, nodeBuilder.getKey()).augmentation(FlowCapableNode.class)
721                 .child(Table.class, new TableKey(flow3.getTableId())).child(Flow.class, flow3.getKey());
722         modification.merge(LogicalDatastoreType.OPERATIONAL, nodeBuilderToInstanceId(nodeBuilder), nodeBuilder.build(), true);
723         modification.merge(LogicalDatastoreType.OPERATIONAL, path4, flow3.build(), true);
724         modification.merge(LogicalDatastoreType.CONFIGURATION, nodeBuilderToInstanceId(nodeBuilder), nodeBuilder.build(), true);
725         modification.merge(LogicalDatastoreType.CONFIGURATION, path4, flow3.build(), true);
726         CheckedFuture<Void, TransactionCommitFailedException> commitFuture = modification.submit();
727         Futures.addCallback(commitFuture, new FutureCallback<Void>() {
728             @Override
729             public void onSuccess(Void aVoid) {
730                 ci.println("Status of Group Data Loaded Transaction: success.");
731             }
732
733             @Override
734             public void onFailure(Throwable throwable) {
735                 LOG.error(throwable.getMessage(), throwable);
736                 ci.println(String.format("Status of Group Data Loaded Transaction : failure. Reason : %s", throwable));
737             }
738         }, MoreExecutors.directExecutor());
739     }
740
741     /**
742      * @return
743      */
744     private static InstructionsBuilder createDecNwTtlInstructions() {
745         DecNwTtlBuilder ta = new DecNwTtlBuilder();
746         DecNwTtl decNwTtl = ta.build();
747         ActionBuilder ab = new ActionBuilder();
748         ab.setAction(new DecNwTtlCaseBuilder().setDecNwTtl(decNwTtl).build());
749
750         // Add our drop action to a list
751         List<Action> actionList = new ArrayList<Action>();
752         actionList.add(ab.build());
753
754         // Create an Apply Action
755         ApplyActionsBuilder aab = new ApplyActionsBuilder();
756         aab.setAction(actionList);
757
758         // Wrap our Apply Action in an Instruction
759         InstructionBuilder ib = new InstructionBuilder();
760         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
761         ib.setKey(new InstructionKey(0));
762         ib.setOrder(0);
763
764         // Put our Instruction in a list of Instructions
765         InstructionsBuilder isb = new InstructionsBuilder();
766         List<Instruction> instructions = new ArrayList<Instruction>();
767         instructions.add(ib.build());
768         isb.setInstruction(instructions);
769         return isb;
770     }
771
772     /**
773      * @return
774      */
775     private static InstructionsBuilder createMeterInstructions() {
776
777         MeterBuilder aab = new MeterBuilder();
778         aab.setMeterId(new MeterId(1L));
779
780         InstructionBuilder ib = new InstructionBuilder();
781         ib.setInstruction(new MeterCaseBuilder().setMeter(aab.build()).build());
782
783         // Put our Instruction in a list of Instructions
784         InstructionsBuilder isb = new InstructionsBuilder();
785         List<Instruction> instructions = new ArrayList<Instruction>();
786         instructions.add(ib.build());
787         isb.setInstruction(instructions);
788         return isb;
789     }
790
791     private static InstructionsBuilder createGotoTableInstructions() {
792
793         GoToTableBuilder aab = new GoToTableBuilder();
794         aab.setTableId((short) 2);
795
796         InstructionBuilder ib = new InstructionBuilder();
797         ib.setInstruction(new GoToTableCaseBuilder().setGoToTable(aab.build()).build());
798
799         // Put our Instruction in a list of Instructions
800         InstructionsBuilder isb = new InstructionsBuilder();
801         List<Instruction> instructions = new ArrayList<Instruction>();
802         instructions.add(ib.build());
803         isb.setInstruction(instructions);
804         return isb;
805     }
806
807     private static InstructionsBuilder createDropInstructions() {
808         DropActionBuilder dab = new DropActionBuilder();
809         DropAction dropAction = dab.build();
810         ActionBuilder ab = new ActionBuilder();
811         ab.setAction(new DropActionCaseBuilder().setDropAction(dropAction).build());
812
813         // Add our drop action to a list
814         List<Action> actionList = new ArrayList<Action>();
815         actionList.add(ab.build());
816
817         // Create an Apply Action
818         ApplyActionsBuilder aab = new ApplyActionsBuilder();
819         aab.setAction(actionList);
820
821         // Wrap our Apply Action in an Instruction
822         InstructionBuilder ib = new InstructionBuilder();
823         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
824
825         // Put our Instruction in a list of Instructions
826         InstructionsBuilder isb = new InstructionsBuilder();
827         List<Instruction> instructions = new ArrayList<Instruction>();
828         instructions.add(ib.build());
829         isb.setInstruction(instructions);
830         return isb;
831     }
832
833     private static InstructionsBuilder createAppyActionInstruction() {
834
835         List<Action> actionList = new ArrayList<Action>();
836         ActionBuilder ab = new ActionBuilder();
837         ControllerActionBuilder controller = new ControllerActionBuilder();
838         controller.setMaxLength(5);
839         ab.setAction(new ControllerActionCaseBuilder().setControllerAction(controller.build()).build());
840         actionList.add(ab.build());
841         // Create an Apply Action
842         ApplyActionsBuilder aab = new ApplyActionsBuilder();
843         aab.setAction(actionList);
844
845         // Wrap our Apply Action in an Instruction
846         InstructionBuilder ib = new InstructionBuilder();
847         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
848
849         // Put our Instruction in a list of Instructions
850         InstructionsBuilder isb = new InstructionsBuilder();
851         List<Instruction> instructions = new ArrayList<Instruction>();
852         instructions.add(ib.build());
853         isb.setInstruction(instructions);
854         return isb;
855     }
856
857     private static InstructionsBuilder createSentToControllerInstructions() {
858         List<Action> actionList = new ArrayList<Action>();
859         ActionBuilder ab = new ActionBuilder();
860
861         OutputActionBuilder output = new OutputActionBuilder();
862         output.setMaxLength(0xffff);
863         Uri value = new Uri(OutputPortValues.CONTROLLER.toString());
864         output.setOutputNodeConnector(value);
865         ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
866         ab.setOrder(0);
867         ab.setKey(new ActionKey(0));
868         actionList.add(ab.build());
869         // Create an Apply Action
870         ApplyActionsBuilder aab = new ApplyActionsBuilder();
871         aab.setAction(actionList);
872
873         // Wrap our Apply Action in an Instruction
874         InstructionBuilder ib = new InstructionBuilder();
875         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
876         ib.setOrder(0);
877         ib.setKey(new InstructionKey(0));
878
879         // Put our Instruction in a list of Instructions
880         InstructionsBuilder isb = new InstructionsBuilder();
881         List<Instruction> instructions = new ArrayList<Instruction>();
882         instructions.add(ib.build());
883         isb.setInstruction(instructions);
884         return isb;
885     }
886
887     private static InstructionsBuilder createAppyActionInstruction2() {
888
889         List<Action> actionList = new ArrayList<Action>();
890         ActionBuilder ab = new ActionBuilder();
891
892         PushMplsActionBuilder push = new PushMplsActionBuilder();
893         push.setEthernetType(0x8847);
894         ab.setAction(new PushMplsActionCaseBuilder().setPushMplsAction(push.build()).build());
895         actionList.add(ab.build());
896         // Create an Apply Action
897         ApplyActionsBuilder aab = new ApplyActionsBuilder();
898         aab.setAction(actionList);
899
900         // Wrap our Apply Action in an Instruction
901         InstructionBuilder ib = new InstructionBuilder();
902         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
903
904         // Put our Instruction in a list of Instructions
905         InstructionsBuilder isb = new InstructionsBuilder();
906         List<Instruction> instructions = new ArrayList<Instruction>();
907         instructions.add(ib.build());
908         isb.setInstruction(instructions);
909         return isb;
910     }
911
912     private static InstructionsBuilder createAppyActionInstruction3() {
913
914         List<Action> actionList = new ArrayList<Action>();
915         ActionBuilder ab = new ActionBuilder();
916
917         PushPbbActionBuilder pbb = new PushPbbActionBuilder();
918         pbb.setEthernetType(0x88E7);
919         ab.setAction(new PushPbbActionCaseBuilder().setPushPbbAction(pbb.build()).build());
920         actionList.add(ab.build());
921         // Create an Apply Action
922         ApplyActionsBuilder aab = new ApplyActionsBuilder();
923         aab.setAction(actionList);
924
925         // Wrap our Apply Action in an Instruction
926         InstructionBuilder ib = new InstructionBuilder();
927         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
928
929         // Put our Instruction in a list of Instructions
930         InstructionsBuilder isb = new InstructionsBuilder();
931         List<Instruction> instructions = new ArrayList<Instruction>();
932         instructions.add(ib.build());
933         isb.setInstruction(instructions);
934         return isb;
935     }
936
937     private static InstructionsBuilder createAppyActionInstruction6() {
938
939         List<Action> actionList = new ArrayList<Action>();
940         ActionBuilder ab = new ActionBuilder();
941
942         SetDlSrcActionBuilder src = new SetDlSrcActionBuilder();
943         src.setAddress(new MacAddress("00:05:b9:7c:81:5f"));
944         ab.setAction(new SetDlSrcActionCaseBuilder().setSetDlSrcAction(src.build()).build());
945         actionList.add(ab.build());
946
947         // Create an Apply Action
948         ApplyActionsBuilder aab = new ApplyActionsBuilder();
949         aab.setAction(actionList);
950
951         // Wrap our Apply Action in an Instruction
952         InstructionBuilder ib = new InstructionBuilder();
953         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
954
955         // Put our Instruction in a list of Instructions
956         InstructionsBuilder isb = new InstructionsBuilder();
957         List<Instruction> instructions = new ArrayList<Instruction>();
958         instructions.add(ib.build());
959         isb.setInstruction(instructions);
960         return isb;
961     }
962
963     private static InstructionsBuilder createAppyActionInstruction7() {
964
965         List<Action> actionList = new ArrayList<Action>();
966         ActionBuilder ab = new ActionBuilder();
967
968         SetVlanIdActionBuilder vl = new SetVlanIdActionBuilder();
969         VlanId a = new VlanId(4012);
970         vl.setVlanId(a);
971         ab.setAction(new SetVlanIdActionCaseBuilder().setSetVlanIdAction(vl.build()).build());
972         actionList.add(ab.build());
973         // Create an Apply Action
974         ApplyActionsBuilder aab = new ApplyActionsBuilder();
975         aab.setAction(actionList);
976
977         // Wrap our Apply Action in an Instruction
978         InstructionBuilder ib = new InstructionBuilder();
979         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
980
981         // Put our Instruction in a list of Instructions
982         InstructionsBuilder isb = new InstructionsBuilder();
983         List<Instruction> instructions = new ArrayList<Instruction>();
984         instructions.add(ib.build());
985         isb.setInstruction(instructions);
986         return isb;
987     }
988
989     private static InstructionsBuilder createAppyActionInstruction8() {
990
991         List<Action> actionList = new ArrayList<Action>();
992         ActionBuilder ab = new ActionBuilder();
993
994         SetVlanPcpActionBuilder pcp = new SetVlanPcpActionBuilder();
995         VlanPcp pcp1 = new VlanPcp((short) 2);
996         pcp.setVlanPcp(pcp1);
997         ab.setAction(new SetVlanPcpActionCaseBuilder().setSetVlanPcpAction(pcp.build()).build());
998         actionList.add(ab.build());
999         // Create an Apply Action
1000         ApplyActionsBuilder aab = new ApplyActionsBuilder();
1001         aab.setAction(actionList);
1002
1003         // Wrap our Apply Action in an Instruction
1004         InstructionBuilder ib = new InstructionBuilder();
1005         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
1006
1007         // Put our Instruction in a list of Instructions
1008         InstructionsBuilder isb = new InstructionsBuilder();
1009         List<Instruction> instructions = new ArrayList<Instruction>();
1010         instructions.add(ib.build());
1011         isb.setInstruction(instructions);
1012         return isb;
1013     }
1014
1015     private static InstructionsBuilder createAppyActionInstruction9() {
1016
1017         List<Action> actionList = new ArrayList<Action>();
1018         ActionBuilder ab = new ActionBuilder();
1019
1020         CopyTtlInBuilder ttlin = new CopyTtlInBuilder();
1021         ab.setAction(new CopyTtlInCaseBuilder().setCopyTtlIn(ttlin.build()).build());
1022         actionList.add(ab.build());
1023         // Create an Apply Action
1024         ApplyActionsBuilder aab = new ApplyActionsBuilder();
1025         aab.setAction(actionList);
1026
1027         // Wrap our Apply Action in an Instruction
1028         InstructionBuilder ib = new InstructionBuilder();
1029         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
1030
1031         // Put our Instruction in a list of Instructions
1032         InstructionsBuilder isb = new InstructionsBuilder();
1033         List<Instruction> instructions = new ArrayList<Instruction>();
1034         instructions.add(ib.build());
1035         isb.setInstruction(instructions);
1036         return isb;
1037     }
1038
1039     private static InstructionsBuilder createAppyActionInstruction16() {
1040
1041         List<Action> actionList = new ArrayList<Action>();
1042         ActionBuilder ab = new ActionBuilder();
1043
1044         GroupActionBuilder groupActionB = new GroupActionBuilder();
1045         groupActionB.setGroupId(1L);
1046         groupActionB.setGroup("0");
1047         ab.setAction(new GroupActionCaseBuilder().setGroupAction(groupActionB.build()).build());
1048         actionList.add(ab.build());
1049
1050         // Create an Apply Action
1051         ApplyActionsBuilder aab = new ApplyActionsBuilder();
1052         aab.setAction(actionList);
1053
1054         // Wrap our Apply Action in an Instruction
1055         InstructionBuilder ib = new InstructionBuilder();
1056         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
1057
1058         // Put our Instruction in a list of Instructions
1059         InstructionsBuilder isb = new InstructionsBuilder();
1060         List<Instruction> instructions = new ArrayList<Instruction>();
1061         instructions.add(ib.build());
1062         isb.setInstruction(instructions);
1063         return isb;
1064     }
1065
1066     private static InstructionsBuilder createAppyActionInstruction160() {
1067
1068         List<Action> actionList = new ArrayList<Action>();
1069         ActionBuilder ab = new ActionBuilder();
1070
1071         FloodAllActionBuilder fldall = new FloodAllActionBuilder();
1072         ab.setAction(new FloodAllActionCaseBuilder().setFloodAllAction(fldall.build()).build());
1073         actionList.add(ab.build());
1074         // Create an Apply Action
1075         ApplyActionsBuilder aab = new ApplyActionsBuilder();
1076         aab.setAction(actionList);
1077
1078         // Wrap our Apply Action in an Instruction
1079         InstructionBuilder ib = new InstructionBuilder();
1080         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
1081
1082         // Put our Instruction in a list of Instructions
1083         InstructionsBuilder isb = new InstructionsBuilder();
1084         List<Instruction> instructions = new ArrayList<Instruction>();
1085         instructions.add(ib.build());
1086         isb.setInstruction(instructions);
1087         return isb;
1088     }
1089
1090     private static InstructionsBuilder createAppyActionInstruction26() {
1091
1092         List<Action> actionList = new ArrayList<Action>();
1093         ActionBuilder ab = new ActionBuilder();
1094
1095         SetNwDstActionBuilder setNwDstActionBuilder = new SetNwDstActionBuilder();
1096         Ipv4Builder ipdst = new Ipv4Builder();
1097         Ipv4Prefix prefixdst = new Ipv4Prefix("10.0.0.21/24");
1098         ipdst.setIpv4Address(prefixdst);
1099         setNwDstActionBuilder.setAddress(ipdst.build());
1100         ab.setAction(new SetNwDstActionCaseBuilder().setSetNwDstAction(setNwDstActionBuilder.build()).build());
1101         actionList.add(ab.build());
1102
1103         // Create an Apply Action
1104         ApplyActionsBuilder aab = new ApplyActionsBuilder();
1105         aab.setAction(actionList);
1106
1107         // Wrap our Apply Action in an Instruction
1108         InstructionBuilder ib = new InstructionBuilder();
1109         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
1110
1111         // Put our Instruction in a list of Instructions
1112         InstructionsBuilder isb = new InstructionsBuilder();
1113         List<Instruction> instructions = new ArrayList<Instruction>();
1114         instructions.add(ib.build());
1115         isb.setInstruction(instructions);
1116         return isb;
1117     }
1118
1119     private static InstructionsBuilder createAppyActionInstruction27() {
1120
1121         List<Action> actionList = new ArrayList<Action>();
1122         ActionBuilder ab = new ActionBuilder();
1123
1124         SetNwSrcActionBuilder setNwsrcActionBuilder = new SetNwSrcActionBuilder();
1125         Ipv4Builder ipsrc = new Ipv4Builder();
1126         Ipv4Prefix prefixsrc = new Ipv4Prefix("10.0.23.21/24");
1127         ipsrc.setIpv4Address(prefixsrc);
1128         setNwsrcActionBuilder.setAddress(ipsrc.build());
1129         ab.setAction(new SetNwSrcActionCaseBuilder().setSetNwSrcAction(setNwsrcActionBuilder.build()).build());
1130         actionList.add(ab.build());
1131
1132         // Create an Apply Action
1133         ApplyActionsBuilder aab = new ApplyActionsBuilder();
1134         aab.setAction(actionList);
1135
1136         // Wrap our Apply Action in an Instruction
1137         InstructionBuilder ib = new InstructionBuilder();
1138         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
1139
1140         // Put our Instruction in a list of Instructions
1141         InstructionsBuilder isb = new InstructionsBuilder();
1142         List<Instruction> instructions = new ArrayList<Instruction>();
1143         instructions.add(ib.build());
1144         isb.setInstruction(instructions);
1145         return isb;
1146     }
1147
1148     private static InstructionsBuilder createAppyActionInstruction28() {
1149
1150         List<Action> actionList = new ArrayList<Action>();
1151         ActionBuilder ab = new ActionBuilder();
1152
1153         SetNwTosActionBuilder setNwTosActionBuilder = new SetNwTosActionBuilder();
1154         setNwTosActionBuilder.setTos(8);
1155         ab.setAction(new SetNwTosActionCaseBuilder().setSetNwTosAction(setNwTosActionBuilder.build()).build());
1156         actionList.add(ab.build());
1157         // Create an Apply Action
1158         ApplyActionsBuilder aab = new ApplyActionsBuilder();
1159         aab.setAction(actionList);
1160
1161         // Wrap our Apply Action in an Instruction
1162         InstructionBuilder ib = new InstructionBuilder();
1163         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
1164
1165         // Put our Instruction in a list of Instructions
1166         InstructionsBuilder isb = new InstructionsBuilder();
1167         List<Instruction> instructions = new ArrayList<Instruction>();
1168         instructions.add(ib.build());
1169         isb.setInstruction(instructions);
1170         return isb;
1171     }
1172
1173     private static InstructionsBuilder createAppyActionInstruction34() {
1174
1175         List<Action> actionList = new ArrayList<Action>();
1176         ActionBuilder ab = new ActionBuilder();
1177
1178         SwPathActionBuilder swPathAction = new SwPathActionBuilder();
1179         ab.setAction(new SwPathActionCaseBuilder().setSwPathAction(swPathAction.build()).build());
1180         actionList.add(ab.build());
1181
1182         // Create an Apply Action
1183         ApplyActionsBuilder aab = new ApplyActionsBuilder();
1184         aab.setAction(actionList);
1185
1186         // Wrap our Apply Action in an Instruction
1187         InstructionBuilder ib = new InstructionBuilder();
1188         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
1189
1190         // Put our Instruction in a list of Instructions
1191         InstructionsBuilder isb = new InstructionsBuilder();
1192         List<Instruction> instructions = new ArrayList<Instruction>();
1193         instructions.add(ib.build());
1194         isb.setInstruction(instructions);
1195         return isb;
1196     }
1197
1198     private static MatchBuilder createLLDPMatch() {
1199         MatchBuilder match = new MatchBuilder();
1200         EthernetMatchBuilder eth = new EthernetMatchBuilder();
1201         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1202         ethTypeBuilder.setType(new EtherType(0x88ccL));
1203         eth.setEthernetType(ethTypeBuilder.build());
1204         match.setEthernetMatch(eth.build());
1205         return match;
1206     }
1207
1208     /**
1209      * @return
1210      */
1211     private static MatchBuilder createMatch1() {
1212         MatchBuilder match = new MatchBuilder();
1213         Ipv4MatchBuilder ipv4Match = new Ipv4MatchBuilder();
1214         Ipv4Prefix prefix = new Ipv4Prefix("10.0.0.1/24");
1215         ipv4Match.setIpv4Destination(prefix);
1216         Ipv4Match i4m = ipv4Match.build();
1217         match.setLayer3Match(i4m);
1218
1219         EthernetMatchBuilder eth = new EthernetMatchBuilder();
1220         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1221         ethTypeBuilder.setType(new EtherType(0x0800L));
1222         eth.setEthernetType(ethTypeBuilder.build());
1223         match.setEthernetMatch(eth.build());
1224         return match;
1225     }
1226
1227     private static MatchBuilder createMatch1000() {
1228         MatchBuilder match = new MatchBuilder();
1229         Ipv4MatchBuilder ipv4Match = new Ipv4MatchBuilder();
1230         Ipv4Prefix prefix = new Ipv4Prefix("10.1.1.1/24");
1231         ipv4Match.setIpv4Destination(prefix);
1232         Ipv4Match i4m = ipv4Match.build();
1233         match.setLayer3Match(i4m);
1234
1235         EthernetMatchBuilder eth = new EthernetMatchBuilder();
1236         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1237         ethTypeBuilder.setType(new EtherType(0x0800L));
1238         eth.setEthernetType(ethTypeBuilder.build());
1239         match.setEthernetMatch(eth.build());
1240         return match;
1241     }
1242
1243     /**
1244      * @return
1245      */
1246     private static MatchBuilder createMatch2() {
1247         MatchBuilder match = new MatchBuilder();
1248         Ipv4MatchBuilder ipv4Match = new Ipv4MatchBuilder();
1249         Ipv4Prefix prefix = new Ipv4Prefix("10.0.0.1");
1250         ipv4Match.setIpv4Source(prefix);
1251         Ipv4Match i4m = ipv4Match.build();
1252         match.setLayer3Match(i4m);
1253
1254         EthernetMatchBuilder eth = new EthernetMatchBuilder();
1255         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1256         ethTypeBuilder.setType(new EtherType(0x0800L));
1257         eth.setEthernetType(ethTypeBuilder.build());
1258         match.setEthernetMatch(eth.build());
1259         return match;
1260     }
1261
1262     /**
1263      * @return
1264      */
1265     private static MatchBuilder createMatch3() {
1266         MatchBuilder match = new MatchBuilder();
1267         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
1268         EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
1269         ethSourceBuilder.setAddress(new MacAddress("00:00:00:00:00:01"));
1270         ethernetMatch.setEthernetSource(ethSourceBuilder.build());
1271         match.setEthernetMatch(ethernetMatch.build());
1272
1273         return match;
1274     }
1275
1276     private static MatchBuilder createMatch33() {
1277
1278         MatchBuilder match = new MatchBuilder();
1279         Ipv4MatchBuilder ipv4Match = new Ipv4MatchBuilder();
1280         Ipv4Prefix prefix = new Ipv4Prefix("10.0.0.10");
1281         ipv4Match.setIpv4Source(prefix);
1282         Ipv4Match i4m = ipv4Match.build();
1283         match.setLayer3Match(i4m);
1284
1285         EthernetMatchBuilder eth = new EthernetMatchBuilder();
1286         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1287         ethTypeBuilder.setType(new EtherType(0xfffeL));
1288         eth.setEthernetType(ethTypeBuilder.build());
1289         match.setEthernetMatch(eth.build());
1290         return match;
1291     }
1292
1293     private static MatchBuilder createInphyportMatch(NodeId nodeId) {
1294         MatchBuilder match = new MatchBuilder();
1295         match.setInPort(new NodeConnectorId(nodeId + ":202"));
1296         match.setInPhyPort(new NodeConnectorId(nodeId + ":10122"));
1297         return match;
1298     }
1299
1300     private static MatchBuilder createEthernetMatch() {
1301         MatchBuilder match = new MatchBuilder();
1302
1303         byte[] mask1 = new byte[]{(byte) -1, (byte) -1, 0, 0, 0, 0};
1304         byte[] mask2 = new byte[]{(byte) -1, (byte) -1, (byte) -1, 0, 0, 0};
1305
1306         EthernetMatchBuilder ethmatch = new EthernetMatchBuilder(); // ethernettype
1307         // match
1308         EthernetTypeBuilder ethtype = new EthernetTypeBuilder();
1309         EtherType type = new EtherType(0x0800L);
1310         ethmatch.setEthernetType(ethtype.setType(type).build());
1311
1312         EthernetDestinationBuilder ethdest = new EthernetDestinationBuilder(); // ethernet
1313         // macaddress
1314         // match
1315         MacAddress macdest = new MacAddress("ff:ff:ff:ff:ff:ff");
1316         ethdest.setAddress(macdest);
1317         // ethdest.setMask(mask1);
1318
1319         ethmatch.setEthernetDestination(ethdest.build());
1320
1321         EthernetSourceBuilder ethsrc = new EthernetSourceBuilder();
1322         MacAddress macsrc = new MacAddress("00:00:00:00:23:ae");
1323         ethsrc.setAddress(macsrc);
1324         // ethsrc.setMask(mask2);
1325
1326         ethmatch.setEthernetSource(ethsrc.build());
1327         match.setEthernetMatch(ethmatch.build());
1328         return match;
1329
1330     }
1331
1332     /**
1333      * @return
1334      */
1335     private static MatchBuilder createL3IPv6Match() {
1336         MatchBuilder match = new MatchBuilder();
1337
1338         EthernetMatchBuilder eth = new EthernetMatchBuilder();
1339         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1340         ethTypeBuilder.setType(new EtherType(0x86ddL));
1341         eth.setEthernetType(ethTypeBuilder.build());
1342         match.setEthernetMatch(eth.build());
1343
1344         Ipv6Prefix dstip6 = new Ipv6Prefix("2002::2/64");
1345         Ipv6Prefix srcip6 = new Ipv6Prefix("2001:0:0:0:0:0:0:1/56");
1346         Ipv6Address ndtarget = new Ipv6Address("2001:db8:0:1:fd97:f9f0:a810:782e");
1347         MacAddress ndsll = new MacAddress("c2:00:54:f5:00:00");
1348         MacAddress ndtll = new MacAddress("00:0c:29:0e:4c:67");
1349         Ipv6ExtHeaderBuilder nextheader = new Ipv6ExtHeaderBuilder();
1350         nextheader.setIpv6Exthdr(58);
1351         Ipv6LabelBuilder ipv6label = new Ipv6LabelBuilder();
1352         Ipv6FlowLabel label = new Ipv6FlowLabel(10028L);
1353         ipv6label.setIpv6Flabel(label);
1354         // ipv6label.setFlabelMask(new byte[] { 0, 1, -1, -1 });
1355
1356         Icmpv6MatchBuilder icmpv6match = new Icmpv6MatchBuilder(); // icmpv6
1357         // match
1358         icmpv6match.setIcmpv6Type((short) 135);
1359         icmpv6match.setIcmpv6Code((short) 0);
1360         match.setIcmpv6Match(icmpv6match.build());
1361
1362         Ipv6MatchBuilder ipv6match = new Ipv6MatchBuilder();
1363         // ipv6match.setIpv6Source(srcip6);
1364         // ipv6match.setIpv6Destination(dstip6);
1365         // ipv6match.setIpv6ExtHeader(nextheader.build());
1366         ipv6match.setIpv6NdSll(ndsll);
1367         ipv6match.setIpv6NdTll(ndtll);
1368         // ipv6match.setIpv6NdTarget(ndtarget);
1369         ipv6match.setIpv6Label(ipv6label.build());
1370
1371         match.setLayer3Match(ipv6match.build());
1372
1373         return match;
1374     }
1375
1376     /**
1377      * @return
1378      */
1379     private static MatchBuilder createICMPv6Match() {
1380
1381         MatchBuilder match = new MatchBuilder();
1382         EthernetMatchBuilder eth = new EthernetMatchBuilder();
1383         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1384         ethTypeBuilder.setType(new EtherType(0x86ddL));
1385         eth.setEthernetType(ethTypeBuilder.build());
1386         match.setEthernetMatch(eth.build());
1387
1388         IpMatchBuilder ipmatch = new IpMatchBuilder(); // ipv4 version
1389         ipmatch.setIpProtocol((short) 58);
1390         match.setIpMatch(ipmatch.build());
1391
1392         Icmpv6MatchBuilder icmpv6match = new Icmpv6MatchBuilder(); // icmpv6
1393         // match
1394         icmpv6match.setIcmpv6Type((short) 135);
1395         icmpv6match.setIcmpv6Code((short) 1);
1396         match.setIcmpv6Match(icmpv6match.build());
1397
1398         return match;
1399     }
1400
1401     /**
1402      * @return
1403      */
1404     private static MatchBuilder createMetadataMatch() {
1405         MatchBuilder match = new MatchBuilder();
1406         byte[] metamask = new byte[]{(byte) -1, (byte) -1, (byte) -1, 0, 0, 0, (byte) 1, (byte) 1};
1407         MetadataBuilder metadata = new MetadataBuilder(); // metadata match
1408         metadata.setMetadata(BigInteger.valueOf(500L));
1409         // metadata.setMetadataMask(metamask);
1410         match.setMetadata(metadata.build());
1411
1412         return match;
1413     }
1414
1415 }