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