General Sonar clean-up
[netvirt.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / services / L2ForwardingService.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
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  * Authors : Madhu Venugopal
9  */
10 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services;
11
12 import java.math.BigInteger;
13 import java.util.List;
14
15 import org.opendaylight.ovsdb.openstack.netvirt.api.L2ForwardingProvider;
16 import org.opendaylight.ovsdb.openstack.netvirt.providers.ConfigInterface;
17 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
18 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
19 import org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils;
20 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
21 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
47 import org.osgi.framework.BundleContext;
48 import org.osgi.framework.ServiceReference;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 import com.google.common.collect.Lists;
53
54 public class L2ForwardingService extends AbstractServiceInstance implements ConfigInterface, L2ForwardingProvider {
55     private static final Logger LOG = LoggerFactory.getLogger(L2ForwardingService.class);
56     public L2ForwardingService() {
57         super(Service.L2_FORWARDING);
58     }
59
60     public L2ForwardingService(Service service) {
61         super(service);
62     }
63
64     /*
65      * (Table:L2Forwarding) Local Unicast
66      * Match: Tunnel ID and dMAC
67      * Action: Output Port
68      * table=2,tun_id=0x5,dl_dst=00:00:00:00:00:01 actions=output:2 goto:<next-table>
69      */
70     @Override
71     public void programLocalUcastOut(Long dpidLong, String segmentationId,
72             Long localPort, String attachedMac, boolean write) {
73
74         String nodeName = OPENFLOW + dpidLong;
75
76         MatchBuilder matchBuilder = new MatchBuilder();
77         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
78         FlowBuilder flowBuilder = new FlowBuilder();
79
80         // Create the OF Match using MatchBuilder
81         flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId)).build());
82         flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress(attachedMac), null).build());
83
84         String flowId = "UcastOut_"+segmentationId+"_"+localPort+"_"+attachedMac;
85         // Add Flow Attributes
86         flowBuilder.setId(new FlowId(flowId));
87         FlowKey key = new FlowKey(new FlowId(flowId));
88         flowBuilder.setStrict(true);
89         flowBuilder.setBarrier(false);
90         flowBuilder.setTableId(getTable());
91         flowBuilder.setKey(key);
92         flowBuilder.setFlowName(flowId);
93         flowBuilder.setHardTimeout(0);
94         flowBuilder.setIdleTimeout(0);
95
96         if (write) {
97             // Instantiate the Builders for the OF Actions and Instructions
98             InstructionBuilder ib = new InstructionBuilder();
99             InstructionsBuilder isb = new InstructionsBuilder();
100
101             // Instructions List Stores Individual Instructions
102             List<Instruction> instructions = Lists.newArrayList();
103
104             // Set the Output Port/Iface
105             InstructionUtils.createOutputPortInstructions(ib, dpidLong, localPort);
106             ib.setOrder(0);
107             ib.setKey(new InstructionKey(0));
108             instructions.add(ib.build());
109
110             // Add InstructionBuilder to the Instruction(s)Builder List
111             isb.setInstruction(instructions);
112
113             // Add InstructionsBuilder to FlowBuilder
114             flowBuilder.setInstructions(isb.build());
115             writeFlow(flowBuilder, nodeBuilder);
116         } else {
117             removeFlow(flowBuilder, nodeBuilder);
118         }
119     }
120     /*
121      * (Table:2) Local VLAN unicast
122      * Match: VLAN ID and dMAC
123      * Action: Output Port
124      * table=2,vlan_id=0x5,dl_dst=00:00:00:00:00:01 actions=output:2
125      * table=110,dl_vlan=2001,dl_dst=fa:16:3e:a3:3b:cc actions=pop_vlan,output:1
126      */
127
128     @Override
129     public void programLocalVlanUcastOut (Long dpidLong, String segmentationId, Long localPort, String attachedMac, boolean write) {
130
131         String nodeName = OPENFLOW + dpidLong;
132
133         MatchBuilder matchBuilder = new MatchBuilder();
134         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
135         FlowBuilder flowBuilder = new FlowBuilder();
136
137         // Create the OF Match using MatchBuilder
138         flowBuilder.setMatch(
139                 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
140         flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress(attachedMac), null).build());
141
142         String flowId = "VlanUcastOut_"+segmentationId+"_"+localPort+"_"+attachedMac;
143         // Add Flow Attributes
144         flowBuilder.setId(new FlowId(flowId));
145         FlowKey key = new FlowKey(new FlowId(flowId));
146         flowBuilder.setStrict(true);
147         flowBuilder.setBarrier(false);
148         flowBuilder.setTableId(getTable());
149         flowBuilder.setKey(key);
150         flowBuilder.setFlowName(flowId);
151         flowBuilder.setHardTimeout(0);
152         flowBuilder.setIdleTimeout(0);
153
154         if (write) {
155             // Instantiate the Builders for the OF Actions and Instructions
156             InstructionBuilder ib = new InstructionBuilder();
157             InstructionsBuilder isb = new InstructionsBuilder();
158
159             // Instructions List Stores Individual Instructions
160             List<Instruction> instructions = Lists.newArrayList();
161             List<Instruction> instructions_tmp = Lists.newArrayList();
162
163             /* Strip vlan and store to tmp instruction space*/
164             InstructionUtils.createPopVlanInstructions(ib);
165             ib.setOrder(0);
166             ib.setKey(new InstructionKey(0));
167             instructions_tmp.add(ib.build());
168
169             // Set the Output Port/Iface
170             ib = new InstructionBuilder();
171             InstructionUtils.addOutputPortInstructions(ib, dpidLong, localPort, instructions_tmp);
172             ib.setOrder(1);
173             ib.setKey(new InstructionKey(0));
174             instructions.add(ib.build());
175
176             // Add InstructionBuilder to the Instruction(s)Builder List
177             isb.setInstruction(instructions);
178
179             // Add InstructionsBuilder to FlowBuilder
180             flowBuilder.setInstructions(isb.build());
181             writeFlow(flowBuilder, nodeBuilder);
182         } else {
183             removeFlow(flowBuilder, nodeBuilder);
184         }
185     }
186
187
188     /**
189      * Utility function used by the flooding logic to allow a flow to be resubmitted
190      * to the local port flooding rule, after being outputed to all available tunnel
191      * or VLAN egress ports.
192      */
193     private void appendResubmitLocalFlood(InstructionBuilder ib) {
194
195         //Update the ApplyActions instructions
196         ApplyActionsCase aac = (ApplyActionsCase) ib.getInstruction();
197         List<Action> actionList = aac.getApplyActions().getAction();
198
199         int index = actionList.size();
200         ActionBuilder ab = new ActionBuilder();
201         ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(ClassifierService.REG_FIELD).build(),
202                 BigInteger.valueOf(ClassifierService.REG_VALUE_FROM_REMOTE)));
203         ab.setOrder(index);
204         ab.setKey(new ActionKey(index));
205         actionList.add(ab.build());
206
207         index++;
208         ab = new ActionBuilder();
209         ab.setAction(ActionUtils.nxResubmitAction(null, this.getTable()));
210         ab.setOrder(index);
211         ab.setKey(new ActionKey(index));
212         actionList.add(ab.build());
213     }
214
215     /*
216      * (Table:2) Local Broadcast Flood
217      * Match: Tunnel ID and dMAC (::::FF:FF)
218      * table=2,priority=16384,tun_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
219      * actions=output:2,3,4,5
220      */
221
222     @Override
223     public void programLocalBcastOut(Long dpidLong, String segmentationId, Long localPort, boolean write) {
224
225         String nodeName = OPENFLOW + dpidLong;
226
227         MatchBuilder matchBuilder = new MatchBuilder();
228         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
229         FlowBuilder flowBuilder = new FlowBuilder();
230
231         // Create the OF Match using MatchBuilder
232         MatchUtils.addNxRegMatch(matchBuilder, new MatchUtils.RegMatch(ClassifierService.REG_FIELD, ClassifierService.REG_VALUE_FROM_REMOTE));
233         flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId)).build());
234         flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress("01:00:00:00:00:00"),
235                 new MacAddress("01:00:00:00:00:00")).build());
236
237         String flowId = "BcastOut_"+segmentationId;
238         // Add Flow Attributes
239         flowBuilder.setId(new FlowId(flowId));
240         FlowKey key = new FlowKey(new FlowId(flowId));
241         flowBuilder.setStrict(true);
242         flowBuilder.setBarrier(false);
243         flowBuilder.setTableId(getTable());
244         flowBuilder.setKey(key);
245         flowBuilder.setPriority(16384);
246         flowBuilder.setFlowName(flowId);
247         flowBuilder.setHardTimeout(0);
248         flowBuilder.setIdleTimeout(0);
249         Flow flow = this.getFlow(flowBuilder, nodeBuilder);
250         // Instantiate the Builders for the OF Actions and Instructions
251         InstructionBuilder ib = new InstructionBuilder();
252         InstructionsBuilder isb = new InstructionsBuilder();
253         List<Instruction> instructions = Lists.newArrayList();
254         List<Instruction> existingInstructions = null;
255         if (flow != null) {
256             Instructions ins = flow.getInstructions();
257             if (ins != null) {
258                 existingInstructions = ins.getInstruction();
259             }
260         }
261
262         if (write) {
263             // Create output port list
264             createOutputPortInstructions(ib, dpidLong, localPort, existingInstructions);
265             ib.setOrder(0);
266             ib.setKey(new InstructionKey(0));
267
268             /* Alternative method to address Bug 2004 is to make a call
269              * here to appendResubmitLocalFlood(ib) so that we send the
270              * flow back to the local flood rule.
271              */
272             instructions.add(ib.build());
273
274             // Add InstructionBuilder to the Instruction(s)Builder List
275             isb.setInstruction(instructions);
276
277             // Add InstructionsBuilder to FlowBuilder
278             flowBuilder.setInstructions(isb.build());
279
280             writeFlow(flowBuilder, nodeBuilder);
281         } else {
282             boolean flowRemove = InstructionUtils.removeOutputPortFromInstructions(ib, dpidLong, localPort,
283                     existingInstructions);
284             if (flowRemove) {
285                 /* if all ports are removed, remove flow */
286                 removeFlow(flowBuilder, nodeBuilder);
287             } else {
288                 /* Install instruction with new output port list*/
289                 ib.setOrder(0);
290                 ib.setKey(new InstructionKey(0));
291                 instructions.add(ib.build());
292
293                 // Add InstructionBuilder to the Instruction(s)Builder List
294                 isb.setInstruction(instructions);
295
296                 // Add InstructionsBuilder to FlowBuilder
297                 flowBuilder.setInstructions(isb.build());
298                 writeFlow(flowBuilder, nodeBuilder);
299             }
300         }
301     }
302
303     /*
304      * (Table:2) Local VLAN Broadcast Flood
305      * Match: vlan ID and dMAC (::::FF:FF)
306      * table=2,priority=16384,vlan_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
307      * actions=strip_vlan, output:2,3,4,5
308      * table=110,dl_vlan=2001,dl_dst=01:00:00:00:00:00/01:00:00:00:00:00 actions=output:2,pop_vlan,output:1,output:3,output:4
309      */
310
311     @Override
312     public void programLocalVlanBcastOut(Long dpidLong, String segmentationId,
313                                          Long localPort, Long ethPort, boolean write) {
314
315         String nodeName = OPENFLOW + dpidLong;
316
317         MatchBuilder matchBuilder = new MatchBuilder();
318         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
319         FlowBuilder flowBuilder = new FlowBuilder();
320
321         // Create the OF Match using MatchBuilder
322         flowBuilder.setMatch(
323                 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
324         flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress("01:00:00:00:00:00"),
325                 new MacAddress("01:00:00:00:00:00")).build());
326
327         String flowId = "VlanBcastOut_"+segmentationId+"_"+ethPort;
328         // Add Flow Attributes
329         flowBuilder.setId(new FlowId(flowId));
330         FlowKey key = new FlowKey(new FlowId(flowId));
331         flowBuilder.setStrict(true);
332         flowBuilder.setBarrier(false);
333         flowBuilder.setTableId(getTable());
334         flowBuilder.setKey(key);
335         flowBuilder.setPriority(16384);
336         flowBuilder.setFlowName(flowId);
337         flowBuilder.setHardTimeout(0);
338         flowBuilder.setIdleTimeout(0);
339         Flow flow = this.getFlow(flowBuilder, nodeBuilder);
340         // Instantiate the Builders for the OF Actions and Instructions
341         List<Instruction> existingInstructions = null;
342         if (flow != null) {
343             Instructions ins = flow.getInstructions();
344             if (ins != null) {
345                 existingInstructions = ins.getInstruction();
346             }
347         }
348
349         List<Instruction> instructions = Lists.newArrayList();
350         InstructionBuilder ib = new InstructionBuilder();
351         List<Action> actionList;
352         if (write) {
353             if (existingInstructions == null) {
354                 /* First time called there should be no instructions.
355                  * We can simply add the output:ethPort first, followed by
356                  * popVlan and then the local port. The next calls will append
357                  * the rest of the local ports.
358                  */
359                 ActionBuilder ab = new ActionBuilder();
360                 actionList = Lists.newArrayList();
361
362                 ab.setAction(ActionUtils.outputAction(new NodeConnectorId(nodeName + ":" + ethPort)));
363                 ab.setOrder(0);
364                 ab.setKey(new ActionKey(0));
365                 actionList.add(ab.build());
366
367                 ab.setAction(ActionUtils.popVlanAction());
368                 ab.setOrder(1);
369                 ab.setKey(new ActionKey(1));
370                 actionList.add(ab.build());
371
372                 ab.setAction(ActionUtils.outputAction(new NodeConnectorId(nodeName + ":" + localPort)));
373                 ab.setOrder(2);
374                 ab.setKey(new ActionKey(2));
375                 actionList.add(ab.build());
376             } else {
377                 /* Subsequent calls require appending any new local ports for this tenant. */
378                 Instruction in = existingInstructions.get(0);
379                 actionList = (((ApplyActionsCase) in.getInstruction()).getApplyActions().getAction());
380
381                 NodeConnectorId ncid = new NodeConnectorId(nodeName + ":" + localPort);
382                 boolean addNew = true;
383
384                 /* Check if the port is already in the output list */
385                 for (Action action : actionList) {
386                     if (action.getAction() instanceof OutputActionCase) {
387                         OutputActionCase opAction = (OutputActionCase) action.getAction();
388                         if (opAction.getOutputAction().getOutputNodeConnector().equals(new Uri(ncid))) {
389                             addNew = false;
390                             break;
391                         }
392                     }
393                 }
394
395                 if (addNew) {
396                     ActionBuilder ab = new ActionBuilder();
397
398                     ab.setAction(ActionUtils.outputAction(new NodeConnectorId(nodeName + ":" + localPort)));
399                     ab.setOrder(actionList.size());
400                     ab.setKey(new ActionKey(actionList.size()));
401                     actionList.add(ab.build());
402                 }
403             }
404
405             ApplyActionsBuilder aab = new ApplyActionsBuilder();
406             aab.setAction(actionList);
407             ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
408             ib.setOrder(0);
409             ib.setKey(new InstructionKey(0));
410             instructions.add(ib.build());
411
412             // Add InstructionBuilder to the Instruction(s)Builder List
413             InstructionsBuilder isb = new InstructionsBuilder();
414             isb.setInstruction(instructions);
415
416             // Add InstructionsBuilder to FlowBuilder
417             flowBuilder.setInstructions(isb.build());
418             writeFlow(flowBuilder, nodeBuilder);
419         } else {
420             //boolean flowRemove = removeOutputPortFromGroup(nodeBuilder, ib, dpidLong,
421             //                     localPort, existingInstructions);
422             boolean flowRemove = removeOutputPortFromInstructions(ib, dpidLong, localPort, ethPort,
423                     existingInstructions);
424             if (flowRemove) {
425                 /* if all ports are removed, remove flow */
426                 removeFlow(flowBuilder, nodeBuilder);
427             } else {
428                 /* Install instruction with new output port list*/
429                 ib.setOrder(0);
430                 ib.setKey(new InstructionKey(0));
431                 instructions.add(ib.build());
432
433                 // Add InstructionBuilder to the Instruction(s)Builder List
434                 InstructionsBuilder isb = new InstructionsBuilder();
435                 isb.setInstruction(instructions);
436
437                 // Add InstructionsBuilder to FlowBuilder
438                 flowBuilder.setInstructions(isb.build());
439                 writeFlow(flowBuilder, nodeBuilder);
440             }
441         }
442     }
443
444     private boolean removeOutputPortFromInstructions(InstructionBuilder ib, Long dpidLong, Long localPort,
445                                                      Long ethPort, List<Instruction> instructions) {
446         List<Action> actionList = Lists.newArrayList();
447         boolean removeFlow = true;
448
449         if (instructions != null) {
450             Instruction in = instructions.get(0);
451             List<Action> oldActionList = (((ApplyActionsCase) in.getInstruction()).getApplyActions().getAction());
452             NodeConnectorId ncid = new NodeConnectorId(OPENFLOW + dpidLong + ":" + localPort);
453             NodeConnectorId ncidEth = new NodeConnectorId(OPENFLOW + dpidLong + ":" + ethPort);
454
455             // Remove the port from the output list
456             ActionBuilder ab = new ActionBuilder();
457             int index = 2;
458             //for (ListIterator<Action> it = oldActionList.listIterator(oldActionList.size()); it.hasPrevious();) {
459             //    Action action = it.previous();
460             for (Action action : oldActionList) {
461                 if (action.getAction() instanceof OutputActionCase) {
462                     OutputActionCase opAction = (OutputActionCase) action.getAction();
463                     if (opAction.getOutputAction().getOutputNodeConnector().equals(new Uri(ncidEth))) {
464                         actionList.add(action);
465                     } else if (!opAction.getOutputAction().getOutputNodeConnector().equals(new Uri(ncid))) {
466                         ab.setAction(action.getAction());
467                         ab.setOrder(index);
468                         ab.setKey(new ActionKey(index));
469                         actionList.add(ab.build());
470                         index++;
471                     }
472                 } else {
473                     actionList.add(action);
474                 }
475             }
476             ApplyActionsBuilder aab = new ApplyActionsBuilder();
477             aab.setAction(actionList);
478             ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
479         }
480
481         if (actionList.size() > 2) {
482             // Add InstructionBuilder to the Instruction(s)Builder List
483             InstructionsBuilder isb = new InstructionsBuilder();
484             isb.setInstruction(instructions);
485             removeFlow = false;
486         }
487
488         return removeFlow;
489     }
490
491     /*
492      * (Table:1) Local Table Miss
493      * Match: Any Remaining Flows w/a TunID
494      * Action: Drop w/ a low priority
495      * table=2,priority=8192,tun_id=0x5 actions=drop
496      */
497
498     @Override
499     public void programLocalTableMiss(Long dpidLong, String segmentationId, boolean write) {
500
501         String nodeName = OPENFLOW + dpidLong;
502
503         MatchBuilder matchBuilder = new MatchBuilder();
504         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
505         FlowBuilder flowBuilder = new FlowBuilder();
506
507         // Create Match(es) and Set them in the FlowBuilder Object
508         flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId)).build());
509
510         if (write) {
511             // Create the OF Actions and Instructions
512             InstructionBuilder ib = new InstructionBuilder();
513             InstructionsBuilder isb = new InstructionsBuilder();
514
515             // Instructions List Stores Individual Instructions
516             List<Instruction> instructions = Lists.newArrayList();
517
518             // Call the InstructionBuilder Methods Containing Actions
519             InstructionUtils.createDropInstructions(ib);
520             ib.setOrder(0);
521             ib.setKey(new InstructionKey(0));
522             instructions.add(ib.build());
523
524             // Add InstructionBuilder to the Instruction(s)Builder List
525             isb.setInstruction(instructions);
526
527             // Add InstructionsBuilder to FlowBuilder
528             flowBuilder.setInstructions(isb.build());
529         }
530
531         String flowId = "LocalTableMiss_"+segmentationId;
532         // Add Flow Attributes
533         flowBuilder.setId(new FlowId(flowId));
534         FlowKey key = new FlowKey(new FlowId(flowId));
535         flowBuilder.setStrict(true);
536         flowBuilder.setBarrier(false);
537         flowBuilder.setTableId(getTable());
538         flowBuilder.setKey(key);
539         flowBuilder.setPriority(8192);
540         flowBuilder.setFlowName(flowId);
541         flowBuilder.setHardTimeout(0);
542         flowBuilder.setIdleTimeout(0);
543         if (write) {
544             writeFlow(flowBuilder, nodeBuilder);
545         } else {
546             removeFlow(flowBuilder, nodeBuilder);
547         }
548     }
549
550     /*
551      * (Table:1) Local Table Miss
552      * Match: Any Remaining Flows w/a VLAN ID
553      * Action: Drop w/ a low priority
554      * table=2,priority=8192,vlan_id=0x5 actions=drop
555      */
556
557     @Override
558     public void programLocalVlanTableMiss(Long dpidLong, String segmentationId, boolean write) {
559
560         String nodeName = OPENFLOW + dpidLong;
561
562         MatchBuilder matchBuilder = new MatchBuilder();
563         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
564         FlowBuilder flowBuilder = new FlowBuilder();
565
566         // Create Match(es) and Set them in the FlowBuilder Object
567         flowBuilder.setMatch(
568                 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
569
570         if (write) {
571             // Create the OF Actions and Instructions
572             InstructionBuilder ib = new InstructionBuilder();
573             InstructionsBuilder isb = new InstructionsBuilder();
574
575             // Instructions List Stores Individual Instructions
576             List<Instruction> instructions = Lists.newArrayList();
577
578             // Call the InstructionBuilder Methods Containing Actions
579             InstructionUtils.createDropInstructions(ib);
580             ib.setOrder(0);
581             ib.setKey(new InstructionKey(0));
582             instructions.add(ib.build());
583
584             // Add InstructionBuilder to the Instruction(s)Builder List
585             isb.setInstruction(instructions);
586
587             // Add InstructionsBuilder to FlowBuilder
588             flowBuilder.setInstructions(isb.build());
589         }
590
591         String flowId = "LocalTableMiss_"+segmentationId;
592         // Add Flow Attributes
593         flowBuilder.setId(new FlowId(flowId));
594         FlowKey key = new FlowKey(new FlowId(flowId));
595         flowBuilder.setStrict(true);
596         flowBuilder.setBarrier(false);
597         flowBuilder.setTableId(getTable());
598         flowBuilder.setKey(key);
599         flowBuilder.setPriority(8192);
600         flowBuilder.setFlowName(flowId);
601         flowBuilder.setHardTimeout(0);
602         flowBuilder.setIdleTimeout(0);
603         if (write) {
604             writeFlow(flowBuilder, nodeBuilder);
605         } else {
606             removeFlow(flowBuilder, nodeBuilder);
607         }
608     }
609
610
611     /*
612      * (Table:1) Egress Tunnel Traffic
613      * Match: Destination Ethernet Addr and Local InPort
614      * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
615      * table=1,tun_id=0x5,dl_dst=00:00:00:00:00:08 \
616      * actions=output:10,goto_table:2"
617      */
618     // TODO : Check on the reason why the original handleTunnelOut was chaining the traffic to table 2
619     @Override
620     public void programTunnelOut(Long dpidLong, String segmentationId, Long OFPortOut, String attachedMac, boolean write) {
621
622         String nodeName = OPENFLOW + dpidLong;
623
624         MatchBuilder matchBuilder = new MatchBuilder();
625         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
626         FlowBuilder flowBuilder = new FlowBuilder();
627
628         // Create the OF Match using MatchBuilder
629         flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId)).build());
630         flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress(attachedMac), null).build());
631
632         String flowId = "TunnelOut_"+segmentationId+"_"+OFPortOut+"_"+attachedMac;
633         // Add Flow Attributes
634         flowBuilder.setId(new FlowId(flowId));
635         FlowKey key = new FlowKey(new FlowId(flowId));
636         flowBuilder.setStrict(true);
637         flowBuilder.setBarrier(false);
638         flowBuilder.setTableId(getTable());
639         flowBuilder.setKey(key);
640         flowBuilder.setFlowName(flowId);
641         flowBuilder.setHardTimeout(0);
642         flowBuilder.setIdleTimeout(0);
643
644         if (write) {
645             // Instantiate the Builders for the OF Actions and Instructions
646             InstructionBuilder ib = new InstructionBuilder();
647             InstructionsBuilder isb = new InstructionsBuilder();
648
649             // Instructions List Stores Individual Instructions
650             List<Instruction> instructions = Lists.newArrayList();
651
652             // Set the Output Port/Iface
653             InstructionUtils.createOutputPortInstructions(ib, dpidLong, OFPortOut);
654             ib.setOrder(0);
655             ib.setKey(new InstructionKey(1));
656             instructions.add(ib.build());
657
658             // Add InstructionBuilder to the Instruction(s)Builder List
659             isb.setInstruction(instructions);
660
661             // Add InstructionsBuilder to FlowBuilder
662             flowBuilder.setInstructions(isb.build());
663
664             writeFlow(flowBuilder, nodeBuilder);
665         } else {
666             removeFlow(flowBuilder, nodeBuilder);
667         }
668     }
669
670     /*
671      * (Table:1) Egress VLAN Traffic
672      * Match: Destination Ethernet Addr and VLAN id
673      * Instruction: GOTO Table Table 2
674      * table=1,vlan_id=0x5,dl_dst=00:00:00:00:00:08 \
675      * actions= goto_table:2"
676      */
677     // TODO : Check on the reason why the original handleTunnelOut was chaining the traffic to table 2
678     @Override
679     public void programVlanOut(Long dpidLong, String segmentationId, Long ethPort, String attachedMac, boolean write) {
680
681         String nodeName = OPENFLOW + dpidLong;
682
683         MatchBuilder matchBuilder = new MatchBuilder();
684         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
685         FlowBuilder flowBuilder = new FlowBuilder();
686
687         // Create the OF Match using MatchBuilder
688         flowBuilder.setMatch(
689                 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
690         flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress(attachedMac), null).build());
691
692         String flowId = "VlanOut_"+segmentationId+"_"+ethPort+"_"+attachedMac;
693         // Add Flow Attributes
694         flowBuilder.setId(new FlowId(flowId));
695         FlowKey key = new FlowKey(new FlowId(flowId));
696         flowBuilder.setStrict(true);
697         flowBuilder.setBarrier(false);
698         flowBuilder.setTableId(getTable());
699         flowBuilder.setKey(key);
700         flowBuilder.setFlowName(flowId);
701         flowBuilder.setHardTimeout(0);
702         flowBuilder.setIdleTimeout(0);
703
704         if (write) {
705             // Instantiate the Builders for the OF Actions and Instructions
706             InstructionBuilder ib = new InstructionBuilder();
707             InstructionsBuilder isb = new InstructionsBuilder();
708
709             // Instructions List Stores Individual Instructions
710             List<Instruction> instructions = Lists.newArrayList();
711             InstructionUtils.createOutputPortInstructions(ib, dpidLong, ethPort);
712             ib.setOrder(0);
713             ib.setKey(new InstructionKey(1));
714             instructions.add(ib.build());
715
716             // Add InstructionBuilder to the Instruction(s)Builder List
717             isb.setInstruction(instructions);
718
719             // Add InstructionsBuilder to FlowBuilder
720             flowBuilder.setInstructions(isb.build());
721
722             writeFlow(flowBuilder, nodeBuilder);
723         } else {
724             removeFlow(flowBuilder, nodeBuilder);
725         }
726     }
727
728     /*
729      * (Table:1) Egress Tunnel Traffic
730      * Match: Destination Ethernet Addr and Local InPort
731      * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
732      * table=1,priority=16384,tun_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
733      * actions=output:10,output:11,goto_table:2
734      */
735     @Override
736     public void programTunnelFloodOut(Long dpidLong, String segmentationId, Long OFPortOut, boolean write) {
737
738         String nodeName = OPENFLOW + dpidLong;
739
740         MatchBuilder matchBuilder = new MatchBuilder();
741         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
742         FlowBuilder flowBuilder = new FlowBuilder();
743
744         // Create the OF Match using MatchBuilder
745         // Match TunnelID
746         MatchUtils.addNxRegMatch(matchBuilder, new MatchUtils.RegMatch(ClassifierService.REG_FIELD, ClassifierService.REG_VALUE_FROM_LOCAL));
747         flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId)).build());
748         // Match DMAC
749         flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress("01:00:00:00:00:00"),
750                 new MacAddress("01:00:00:00:00:00")).build());
751
752         String flowId = "TunnelFloodOut_"+segmentationId;
753         // Add Flow Attributes
754         flowBuilder.setId(new FlowId(flowId));
755         FlowKey key = new FlowKey(new FlowId(flowId));
756         flowBuilder.setBarrier(true);
757         flowBuilder.setTableId(getTable());
758         flowBuilder.setKey(key);
759         flowBuilder.setPriority(16383);  // FIXME: change it back to 16384 once bug 3005 is fixed.
760         flowBuilder.setFlowName(flowId);
761         flowBuilder.setHardTimeout(0);
762         flowBuilder.setIdleTimeout(0);
763
764         Flow flow = this.getFlow(flowBuilder, nodeBuilder);
765         // Instantiate the Builders for the OF Actions and Instructions
766         InstructionBuilder ib = new InstructionBuilder();
767         InstructionsBuilder isb = new InstructionsBuilder();
768         List<Instruction> instructions = Lists.newArrayList();
769         List<Instruction> existingInstructions = null;
770         if (flow != null) {
771             Instructions ins = flow.getInstructions();
772             if (ins != null) {
773                 existingInstructions = ins.getInstruction();
774             }
775         }
776
777         if (write) {
778             // Set the Output Port/Iface
779             //createOutputGroupInstructions(nodeBuilder, ib, dpidLong, OFPortOut, existingInstructions);
780             createOutputPortInstructions(ib, dpidLong, OFPortOut, existingInstructions);
781             ib.setOrder(0);
782             ib.setKey(new InstructionKey(0));
783             instructions.add(ib.build());
784
785             // Add InstructionBuilder to the Instruction(s)Builder List
786             isb.setInstruction(instructions);
787
788             // Add InstructionsBuilder to FlowBuilder
789             flowBuilder.setInstructions(isb.build());
790
791             writeFlow(flowBuilder, nodeBuilder);
792         } else {
793             /* remove port from action list */
794             boolean flowRemove = InstructionUtils.removeOutputPortFromInstructions(ib, dpidLong,
795                     OFPortOut, existingInstructions);
796             if (flowRemove) {
797                 /* if all port are removed, remove the flow too. */
798                 removeFlow(flowBuilder, nodeBuilder);
799             } else {
800                 /* Install instruction with new output port list*/
801                 ib.setOrder(0);
802                 ib.setKey(new InstructionKey(0));
803                 instructions.add(ib.build());
804
805                 // Add InstructionBuilder to the Instruction(s)Builder List
806                 isb.setInstruction(instructions);
807
808                 // Add InstructionsBuilder to FlowBuilder
809                 flowBuilder.setInstructions(isb.build());
810                 writeFlow(flowBuilder, nodeBuilder);
811             }
812         }
813     }
814
815     /*
816      * (Table:1) Egress VLAN Traffic
817      * Match: Destination Ethernet Addr and VLAN id
818      * Instruction: GOTO table 2 and Output port eth interface
819      * Example: table=1,priority=16384,vlan_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
820      * actions=output:eth1,goto_table:2
821      */
822
823     @Override
824     public void programVlanFloodOut(Long dpidLong, String segmentationId, Long ethPort, boolean write) {
825
826         String nodeName = OPENFLOW + dpidLong;
827
828         MatchBuilder matchBuilder = new MatchBuilder();
829         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
830         FlowBuilder flowBuilder = new FlowBuilder();
831
832         // Create the OF Match using MatchBuilder
833         // Match Vlan ID
834         flowBuilder.setMatch(
835                 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
836         // Match DMAC
837         flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress("01:00:00:00:00:00"),
838                 new MacAddress("01:00:00:00:00:00")).build());
839
840         String flowId = "VlanFloodOut_"+segmentationId+"_"+ethPort;
841         // Add Flow Attributes
842         flowBuilder.setId(new FlowId(flowId));
843         FlowKey key = new FlowKey(new FlowId(flowId));
844         flowBuilder.setBarrier(true);
845         flowBuilder.setTableId(getTable());
846         flowBuilder.setKey(key);
847         flowBuilder.setPriority(16384);
848         flowBuilder.setFlowName(flowId);
849         flowBuilder.setHardTimeout(0);
850         flowBuilder.setIdleTimeout(0);
851
852         //ToDo: Is there something to be done with result of the call to getFlow?
853
854         Flow flow = this.getFlow(flowBuilder, nodeBuilder);
855         // Instantiate the Builders for the OF Actions and Instructions
856         InstructionBuilder ib = new InstructionBuilder();
857         InstructionsBuilder isb = new InstructionsBuilder();
858         List<Instruction> instructions = Lists.newArrayList();
859
860         if (write) {
861             // Set the Output Port/Iface
862             InstructionUtils.createOutputPortInstructions(ib, dpidLong, ethPort);
863             ib.setOrder(0);
864             ib.setKey(new InstructionKey(0));
865             instructions.add(ib.build());
866
867             // Add InstructionBuilder to the Instruction(s)Builder List
868             isb.setInstruction(instructions);
869
870             // Add InstructionsBuilder to FlowBuilder
871             flowBuilder.setInstructions(isb.build());
872
873             writeFlow(flowBuilder, nodeBuilder);
874         } else {
875             removeFlow(flowBuilder, nodeBuilder);
876         }
877     }
878
879     /*
880      * (Table:1) Table Drain w/ Catch All
881      * Match: Tunnel ID
882      * Action: GOTO Local Table (10)
883      * table=2,priority=8192,tun_id=0x5 actions=drop
884      */
885     @Override
886     public void programTunnelMiss(Long dpidLong, String segmentationId, boolean write) {
887
888         String nodeName = OPENFLOW + dpidLong;
889
890         MatchBuilder matchBuilder = new MatchBuilder();
891         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
892         FlowBuilder flowBuilder = new FlowBuilder();
893
894         // Create Match(es) and Set them in the FlowBuilder Object
895         flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId)).build());
896
897         if (write) {
898             // Create the OF Actions and Instructions
899             InstructionsBuilder isb = new InstructionsBuilder();
900
901             // Instructions List Stores Individual Instructions
902             List<Instruction> instructions = Lists.newArrayList();
903
904             // Call the InstructionBuilder Methods Containing Actions
905             InstructionBuilder ib = this.getMutablePipelineInstructionBuilder();
906             ib.setOrder(0);
907             ib.setKey(new InstructionKey(0));
908             instructions.add(ib.build());
909
910             // Add InstructionBuilder to the Instruction(s)Builder List
911             isb.setInstruction(instructions);
912
913             // Add InstructionsBuilder to FlowBuilder
914             flowBuilder.setInstructions(isb.build());
915         }
916
917         String flowId = "TunnelMiss_"+segmentationId;
918         // Add Flow Attributes
919         flowBuilder.setId(new FlowId(flowId));
920         FlowKey key = new FlowKey(new FlowId(flowId));
921         flowBuilder.setStrict(true);
922         flowBuilder.setBarrier(false);
923         flowBuilder.setTableId(getTable());
924         flowBuilder.setKey(key);
925         flowBuilder.setPriority(8192);
926         flowBuilder.setFlowName(flowId);
927         flowBuilder.setHardTimeout(0);
928         flowBuilder.setIdleTimeout(0);
929         if (write) {
930             writeFlow(flowBuilder, nodeBuilder);
931         } else {
932             removeFlow(flowBuilder, nodeBuilder);
933         }
934     }
935
936     /*
937      * (Table:1) Table Drain w/ Catch All
938      * Match: Vlan ID
939      * Action: Output port eth interface
940      * table=1,priority=8192,vlan_id=0x5 actions= output port:eth1
941      * table=110,priority=8192,dl_vlan=2001 actions=output:2
942      */
943
944     @Override
945     public void programVlanMiss(Long dpidLong, String segmentationId, Long ethPort, boolean write) {
946
947         String nodeName = OPENFLOW + dpidLong;
948
949         MatchBuilder matchBuilder = new MatchBuilder();
950         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
951         FlowBuilder flowBuilder = new FlowBuilder();
952
953         // Create Match(es) and Set them in the FlowBuilder Object
954         flowBuilder.setMatch(
955                 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
956
957         if (write) {
958             // Create the OF Actions and Instructions
959             InstructionBuilder ib = new InstructionBuilder();
960             InstructionsBuilder isb = new InstructionsBuilder();
961
962             // Instructions List Stores Individual Instructions
963             List<Instruction> instructions = Lists.newArrayList();
964
965             // Set the Output Port/Iface
966             InstructionUtils.createOutputPortInstructions(ib, dpidLong, ethPort);
967             ib.setOrder(0);
968             ib.setKey(new InstructionKey(1));
969             instructions.add(ib.build());
970
971             // Add InstructionBuilder to the Instruction(s)Builder List
972             isb.setInstruction(instructions);
973
974             // Add InstructionsBuilder to FlowBuilder
975             flowBuilder.setInstructions(isb.build());
976         }
977
978         String flowId = "VlanMiss_"+segmentationId;
979         // Add Flow Attributes
980         flowBuilder.setId(new FlowId(flowId));
981         FlowKey key = new FlowKey(new FlowId(flowId));
982         flowBuilder.setStrict(true);
983         flowBuilder.setBarrier(false);
984         flowBuilder.setTableId(getTable());
985         flowBuilder.setKey(key);
986         flowBuilder.setPriority(8192);
987         flowBuilder.setFlowName(flowId);
988         flowBuilder.setHardTimeout(0);
989         flowBuilder.setIdleTimeout(0);
990         if (write) {
991             writeFlow(flowBuilder, nodeBuilder);
992         } else {
993             removeFlow(flowBuilder, nodeBuilder);
994         }
995     }
996
997     /**
998      * Create Output Port Group Instruction
999      *
1000      * @param ib       Map InstructionBuilder without any instructions
1001      * @param dpidLong Long the datapath ID of a switch/node
1002      * @param port     Long representing a port on a switch/node
1003      * @return ib InstructionBuilder Map with instructions
1004      */
1005     protected InstructionBuilder createOutputPortInstructions(InstructionBuilder ib,
1006             Long dpidLong, Long port ,
1007             List<Instruction> instructions) {
1008         NodeConnectorId ncid = new NodeConnectorId(OPENFLOW + dpidLong + ":" + port);
1009         LOG.debug("createOutputPortInstructions() Node Connector ID is - Type=openflow: DPID={} port={} existingInstructions={}", dpidLong, port, instructions);
1010
1011         List<Action> actionList = Lists.newArrayList();
1012         ActionBuilder ab = new ActionBuilder();
1013
1014         List<Action> existingActions;
1015         if (instructions != null && instructions.size() > 0) {
1016             /**
1017              * First instruction is the one containing the output ports.
1018              * So, only extract the actions from that.
1019              */
1020             Instruction in = instructions.get(0);
1021             if (in.getInstruction() instanceof ApplyActionsCase) {
1022                 existingActions = (((ApplyActionsCase) in.getInstruction()).getApplyActions().getAction());
1023                 // Only include output actions
1024                 for (Action action : existingActions) {
1025                     if (action.getAction() instanceof OutputActionCase) {
1026                         actionList.add(action);
1027                     }
1028                 }
1029             }
1030         }
1031         /* Create output action for this port*/
1032         OutputActionBuilder oab = new OutputActionBuilder();
1033         oab.setOutputNodeConnector(ncid);
1034         ab.setAction(new OutputActionCaseBuilder().setOutputAction(oab.build()).build());
1035         boolean addNew = true;
1036
1037         /* Find the group action and get the group */
1038         for (Action action : actionList) {
1039             OutputActionCase opAction = (OutputActionCase)action.getAction();
1040             /* If output port action already in the action list of one of the buckets, skip */
1041             if (opAction.getOutputAction().getOutputNodeConnector().equals(new Uri(ncid))) {
1042                 addNew = false;
1043                 break;
1044             }
1045         }
1046         if (addNew) {
1047             ab.setOrder(actionList.size());
1048             ab.setKey(new ActionKey(actionList.size()));
1049             actionList.add(ab.build());
1050         }
1051         // Create an Apply Action
1052         ApplyActionsBuilder aab = new ApplyActionsBuilder();
1053         aab.setAction(actionList);
1054         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
1055         LOG.debug("createOutputPortInstructions() : applyAction {}", aab.build());
1056         return ib;
1057     }
1058
1059     @Override
1060     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
1061         super.setDependencies(bundleContext.getServiceReference(L2ForwardingProvider.class.getName()), this);
1062     }
1063
1064     @Override
1065     public void setDependencies(Object impl) {
1066
1067     }
1068 }