Merge "Making changes to LBaaSHandler and Activator to add logic for LB Neutron calls."
[ovsdb.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / OF13Provider.java
1 /**
2  * Copyright (C) 2013 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, Brent Salisbury, Dave Tucker
9  */
10 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13;
11
12 import com.google.common.base.Optional;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
20 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
21 import org.opendaylight.controller.sal.core.Node;
22 import org.opendaylight.controller.sal.utils.HexEncode;
23 import org.opendaylight.controller.sal.utils.Status;
24 import org.opendaylight.controller.sal.utils.StatusCode;
25 import org.opendaylight.ovsdb.lib.notation.Row;
26 import org.opendaylight.ovsdb.lib.notation.UUID;
27 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
28 import org.opendaylight.ovsdb.openstack.netvirt.NetworkHandler;
29 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
30 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProvider;
31 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
32 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
33 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
34 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
35 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
36 import org.opendaylight.ovsdb.plugin.api.StatusWithUuid;
37 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
38 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
39 import org.opendaylight.ovsdb.schema.openvswitch.Port;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.GroupActionCase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.GroupActionCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCase;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.group.action._case.GroupActionBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.Buckets;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.BucketsBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketKey;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
86 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
87
88 import com.google.common.base.Preconditions;
89 import com.google.common.collect.Lists;
90 import com.google.common.collect.Maps;
91 import org.slf4j.Logger;
92 import org.slf4j.LoggerFactory;
93
94 import java.math.BigInteger;
95 import java.net.InetAddress;
96 import java.util.List;
97 import java.util.Map;
98 import java.util.Set;
99 import java.util.concurrent.ExecutionException;
100
101 /**
102  * Open vSwitch OpenFlow 1.3 Networking Provider for OpenStack Neutron
103  */
104 public class OF13Provider implements NetworkingProvider {
105     private static final Logger logger = LoggerFactory.getLogger(OF13Provider.class);
106     private DataBroker dataBroker;
107     private static final short TABLE_0_DEFAULT_INGRESS = 0;
108     private static final short TABLE_1_ISOLATE_TENANT = 10;
109     private static final short TABLE_2_LOCAL_FORWARD = 20;
110     private static final String OPENFLOW = "openflow:";
111     private static Long groupId = 1L;
112
113     private volatile org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService configurationService;
114     private volatile BridgeConfigurationManager bridgeConfigurationManager;
115     private volatile TenantNetworkManager tenantNetworkManager;
116     private volatile OvsdbConfigurationService ovsdbConfigurationService;
117     private volatile OvsdbConnectionService connectionService;
118     private volatile MdsalConsumer mdsalConsumer;
119
120     public static final String NAME = "OF13Provider";
121
122     public OF13Provider(){
123
124     }
125
126     @Override
127     public String getName() {
128         return NAME;
129     }
130
131     @Override
132     public boolean supportsServices() {
133         return true;
134     }
135
136     @Override
137     public boolean hasPerTenantTunneling() {
138         return false;
139     }
140
141     private Status getTunnelReadinessStatus (Node node, String tunnelKey) {
142         InetAddress srcTunnelEndPoint = configurationService.getTunnelEndPoint(node);
143         if (srcTunnelEndPoint == null) {
144             logger.error("Tunnel Endpoint not configured for Node {}", node);
145             return new Status(StatusCode.NOTFOUND, "Tunnel Endpoint not configured for "+ node);
146         }
147
148         if (!bridgeConfigurationManager.isNodeNeutronReady(node)) {
149             logger.error(node+" is not Overlay ready");
150             return new Status(StatusCode.NOTACCEPTABLE, node+" is not Overlay ready");
151         }
152
153         if (!tenantNetworkManager.isTenantNetworkPresentInNode(node, tunnelKey)) {
154             logger.debug(node+" has no VM corresponding to segment "+ tunnelKey);
155             return new Status(StatusCode.NOTACCEPTABLE, node+" has no VM corresponding to segment "+ tunnelKey);
156         }
157         return new Status(StatusCode.SUCCESS);
158     }
159
160     private String getTunnelName(String tunnelType, InetAddress dst) {
161         return tunnelType+"-"+dst.getHostAddress();
162     }
163
164     private boolean isTunnelPresent(Node node, String tunnelName, String bridgeUUID) throws Exception {
165         Preconditions.checkNotNull(ovsdbConfigurationService);
166         Row bridgeRow = ovsdbConfigurationService
167                 .getRow(node, ovsdbConfigurationService.getTableName(node, Bridge.class), bridgeUUID);
168         Bridge bridge = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeRow);
169         if (bridge != null) {
170             Set<UUID> ports = bridge.getPortsColumn().getData();
171             for (UUID portUUID : ports) {
172                 Row portRow = ovsdbConfigurationService
173                         .getRow(node, ovsdbConfigurationService.getTableName(node, Port.class), portUUID.toString());
174                 Port port = ovsdbConfigurationService.getTypedRow(node, Port.class, portRow);
175                 if (port != null && tunnelName.equalsIgnoreCase(port.getName())) return true;
176             }
177         }
178         return false;
179     }
180
181     private String getPortUuid(Node node, String name, String bridgeUUID) throws Exception {
182         Preconditions.checkNotNull(ovsdbConfigurationService);
183         Row bridgeRow = ovsdbConfigurationService
184                 .getRow(node, ovsdbConfigurationService.getTableName(node, Bridge.class), bridgeUUID);
185         Bridge bridge = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeRow);
186         if (bridge != null) {
187             Set<UUID> ports = bridge.getPortsColumn().getData();
188             for (UUID portUUID : ports) {
189                 Row portRow = ovsdbConfigurationService
190                         .getRow(node, ovsdbConfigurationService.getTableName(node, Port.class), portUUID.toString());
191                 Port port = ovsdbConfigurationService.getTypedRow(node, Port.class, portRow);
192                 if (port != null && name.equalsIgnoreCase(port.getName())) return portUUID.toString();
193             }
194         }
195         return null;
196     }
197
198     private Status addTunnelPort (Node node, String tunnelType, InetAddress src, InetAddress dst) {
199         Preconditions.checkNotNull(ovsdbConfigurationService);
200         try {
201             String bridgeUUID = null;
202             String tunnelBridgeName = configurationService.getIntegrationBridgeName();
203             Map<String, Row> bridgeTable = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Bridge.class));
204             if (bridgeTable != null) {
205                 for (String uuid : bridgeTable.keySet()) {
206                     Bridge bridge = ovsdbConfigurationService.getTypedRow(node,Bridge.class, bridgeTable.get(uuid));
207                     if (bridge.getName().equals(tunnelBridgeName)) {
208                         bridgeUUID = uuid;
209                         break;
210                     }
211                 }
212             }
213             if (bridgeUUID == null) {
214                 logger.error("Could not find Bridge {} in {}", tunnelBridgeName, node);
215                 return new Status(StatusCode.NOTFOUND, "Could not find "+tunnelBridgeName+" in "+node);
216             }
217             String portName = getTunnelName(tunnelType, dst);
218
219             if (this.isTunnelPresent(node, portName, bridgeUUID)) {
220                 logger.trace("Tunnel {} is present in {} of {}", portName, tunnelBridgeName, node);
221                 return new Status(StatusCode.SUCCESS);
222             }
223
224             Port tunnelPort = ovsdbConfigurationService.createTypedRow(node, Port.class);
225             tunnelPort.setName(portName);
226             StatusWithUuid statusWithUuid = ovsdbConfigurationService
227                     .insertRow(node, ovsdbConfigurationService.getTableName(node, Port.class), bridgeUUID, tunnelPort.getRow());
228             if (!statusWithUuid.isSuccess()) {
229                 logger.error("Failed to insert Tunnel port {} in {}", portName, bridgeUUID);
230                 return statusWithUuid;
231             }
232
233             String tunnelPortUUID = statusWithUuid.getUuid().toString();
234             String interfaceUUID = null;
235             int timeout = 6;
236             while ((interfaceUUID == null) && (timeout > 0)) {
237                 Row portRow = ovsdbConfigurationService
238                         .getRow(node, ovsdbConfigurationService.getTableName(node, Port.class), tunnelPortUUID);
239                 tunnelPort = ovsdbConfigurationService.getTypedRow(node, Port.class, portRow);
240                 Set<UUID> interfaces = tunnelPort.getInterfacesColumn().getData();
241                 if (interfaces == null || interfaces.size() == 0) {
242                     // Wait for the OVSDB update to sync up the Local cache.
243                     Thread.sleep(500);
244                     timeout--;
245                     continue;
246                 }
247                 interfaceUUID = interfaces.toArray()[0].toString();
248                 Row intfRow = ovsdbConfigurationService
249                         .getRow(node, ovsdbConfigurationService.getTableName(node, Interface.class), interfaceUUID);
250                 Interface intf = ovsdbConfigurationService.getTypedRow(node, Interface.class, intfRow);
251                 if (intf == null) interfaceUUID = null;
252             }
253
254             if (interfaceUUID == null) {
255                 logger.error("Cannot identify Tunnel Interface for port {}/{}", portName, tunnelPortUUID);
256                 return new Status(StatusCode.INTERNALERROR);
257             }
258
259             Interface tunInterface = ovsdbConfigurationService.createTypedRow(node, Interface.class);
260             tunInterface.setType(tunnelType);
261             Map<String, String> options = Maps.newHashMap();
262             options.put("key", "flow");
263             options.put("local_ip", src.getHostAddress());
264             options.put("remote_ip", dst.getHostAddress());
265             tunInterface.setOptions(options);
266             Status status = ovsdbConfigurationService
267                     .updateRow(node, ovsdbConfigurationService.getTableName(node, Interface.class), tunnelPortUUID, interfaceUUID, tunInterface.getRow());
268             logger.debug("Tunnel {} add status : {}", tunInterface, status);
269             return status;
270         } catch (Exception e) {
271             logger.error("Exception in addTunnelPort", e);
272             return new Status(StatusCode.INTERNALERROR);
273         }
274     }
275
276     /* delete port from ovsdb port table */
277     private Status deletePort(Node node, String bridgeName, String portName) {
278         Preconditions.checkNotNull(ovsdbConfigurationService);
279         try {
280             String bridgeUUID = null;
281             Map<String, Row> bridgeTable = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Bridge.class));
282             if (bridgeTable != null) {
283                 for (String uuid : bridgeTable.keySet()) {
284                     Bridge bridge = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeTable.get(uuid));
285                     if (bridge.getName().equals(bridgeName)) {
286                         bridgeUUID = uuid;
287                         break;
288                     }
289                 }
290             }
291             if (bridgeUUID == null) {
292                 logger.debug("Could not find Bridge {} in {}", bridgeName, node);
293                 return new Status(StatusCode.SUCCESS);
294             }
295
296             String portUUID = this.getPortUuid(node, portName, bridgeUUID);
297             Status status = new Status(StatusCode.SUCCESS);
298             if (portUUID != null) {
299                status = ovsdbConfigurationService
300                        .deleteRow(node, ovsdbConfigurationService.getTableName(node, Port.class), portUUID);
301                if (!status.isSuccess()) {
302                    logger.error("Failed to delete port {} in {} status : {}", portName, bridgeUUID,
303                                 status);
304                    return status;
305                }
306                logger.debug("Port {} delete status : {}", portName, status);
307             }
308             return status;
309         } catch (Exception e) {
310             logger.error("Exception in deletePort", e);
311             return new Status(StatusCode.INTERNALERROR);
312         }
313     }
314
315     private Status deleteTunnelPort(Node node, String tunnelType, InetAddress src, InetAddress dst) {
316         String tunnelBridgeName = configurationService.getIntegrationBridgeName();
317         String portName = getTunnelName(tunnelType, dst);
318         return deletePort(node, tunnelBridgeName, portName);
319     }
320
321     private Status deletePhysicalPort(Node node, String phyIntfName) {
322         String intBridgeName = configurationService.getIntegrationBridgeName();
323         return deletePort(node, intBridgeName, phyIntfName);
324     }
325
326     private void programLocalBridgeRules(Node node, Long dpid, String segmentationId, String attachedMac, long localPort) {
327          /*
328          * Table(0) Rule #3
329          * ----------------
330          * Match: VM sMac and Local Ingress Port
331          * Action:Action: Set Tunnel ID and GOTO Local Table (5)
332          */
333
334          handleLocalInPort(dpid, TABLE_0_DEFAULT_INGRESS, TABLE_1_ISOLATE_TENANT, segmentationId, localPort, attachedMac, true);
335
336         /*
337          * Table(0) Rule #4
338          * ----------------
339          * Match: Drop any remaining Ingress Local VM Packets
340          * Action: Drop w/ a low priority
341          */
342
343          handleDropSrcIface(dpid, localPort, true);
344
345          /*
346           * Table(2) Rule #1
347           * ----------------
348           * Match: Match TunID and Destination DL/dMAC Addr
349           * Action: Output Port
350           * table=2,tun_id=0x5,dl_dst=00:00:00:00:00:01 actions=output:2
351           */
352
353          handleLocalUcastOut(dpid, TABLE_2_LOCAL_FORWARD, segmentationId, localPort, attachedMac, true);
354
355          /*
356           * Table(2) Rule #2
357           * ----------------
358           * Match: Tunnel ID and dMAC (::::FF:FF)
359           * table=2,priority=16384,tun_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
360           * actions=output:2,3,4,5
361           */
362
363           handleLocalBcastOut(dpid, TABLE_2_LOCAL_FORWARD, segmentationId, localPort, true);
364
365           /*
366            * TODO : Optimize the following 2 writes to be restricted only for the very first port known in a segment.
367            */
368           /*
369            * Table(1) Rule #3
370            * ----------------
371            * Match:  Any remaining Ingress Local VM Packets
372            * Action: Drop w/ a low priority
373            * -------------------------------------------
374            * table=1,priority=8192,tun_id=0x5 actions=goto_table:2
375            */
376
377            handleTunnelMiss(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD, segmentationId, true);
378
379           /*
380            * Table(2) Rule #3
381            * ----------------
382            * Match: Any Remaining Flows w/a TunID
383            * Action: Drop w/ a low priority
384            * table=2,priority=8192,tun_id=0x5 actions=drop
385            */
386
387            handleLocalTableMiss(dpid, TABLE_2_LOCAL_FORWARD, segmentationId, true);
388     }
389
390     private void removeLocalBridgeRules(Node node, Long dpid, String segmentationId, String attachedMac, long localPort) {
391         /*
392          * Table(0) Rule #3
393          * ----------------
394          * Match: VM sMac and Local Ingress Port
395          * Action:Action: Set Tunnel ID and GOTO Local Table (5)
396          */
397
398          handleLocalInPort(dpid, TABLE_0_DEFAULT_INGRESS, TABLE_1_ISOLATE_TENANT, segmentationId, localPort, attachedMac, false);
399
400         /*
401          * Table(0) Rule #4
402          * ----------------
403          * Match: Drop any remaining Ingress Local VM Packets
404          * Action: Drop w/ a low priority
405          */
406
407          handleDropSrcIface(dpid, localPort, false);
408
409          /*
410           * Table(2) Rule #1
411           * ----------------
412           * Match: Match TunID and Destination DL/dMAC Addr
413           * Action: Output Port
414           * table=2,tun_id=0x5,dl_dst=00:00:00:00:00:01 actions=output:2
415           */
416
417          handleLocalUcastOut(dpid, TABLE_2_LOCAL_FORWARD, segmentationId, localPort, attachedMac, false);
418
419          /*
420           * Table(2) Rule #2
421           * ----------------
422           * Match: Tunnel ID and dMAC (::::FF:FF)
423           * table=2,priority=16384,tun_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
424           * actions=output:2,3,4,5
425           */
426
427           handleLocalBcastOut(dpid, TABLE_2_LOCAL_FORWARD, segmentationId, localPort, false);
428     }
429
430     private void programLocalIngressTunnelBridgeRules(Node node, Long dpid, String segmentationId, String attachedMac, long tunnelOFPort, long localPort) {
431         /*
432          * Table(0) Rule #2
433          * ----------------
434          * Match: Ingress Port, Tunnel ID
435          * Action: GOTO Local Table (20)
436          */
437
438          handleTunnelIn(dpid, TABLE_0_DEFAULT_INGRESS, TABLE_2_LOCAL_FORWARD, segmentationId, tunnelOFPort, true);
439
440          /*
441           * Table(1) Rule #2
442           * ----------------
443           * Match: Match Tunnel ID and L2 ::::FF:FF Flooding
444           * Action: Flood to selected destination TEPs
445           * -------------------------------------------
446           * table=1,priority=16384,tun_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
447           * actions=output:10,output:11,goto_table:2
448           */
449
450          handleTunnelFloodOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD, segmentationId, tunnelOFPort, true);
451
452     }
453
454     private void programRemoteEgressTunnelBridgeRules(Node node, Long dpid, String segmentationId, String attachedMac, long tunnelOFPort, long localPort) {
455         /*
456          * Table(1) Rule #1
457          * ----------------
458          * Match: Drop any remaining Ingress Local VM Packets
459          * Action: Drop w/ a low priority
460          * -------------------------------------------
461          * table=1,tun_id=0x5,dl_dst=00:00:00:00:00:08 \
462          * actions=output:11,goto_table:2
463          */
464
465         handleTunnelOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD, segmentationId, tunnelOFPort, attachedMac, true);
466     }
467
468     private void removeRemoteEgressTunnelBridgeRules(Node node, Long dpid, String segmentationId, String attachedMac, long tunnelOFPort, long localPort) {
469         /*
470          * Table(1) Rule #1
471          * ----------------
472          * Match: Drop any remaining Ingress Local VM Packets
473          * Action: Drop w/ a low priority
474          * -------------------------------------------
475          * table=1,tun_id=0x5,dl_dst=00:00:00:00:00:08 \
476          * actions=output:11,goto_table:2
477          */
478
479         handleTunnelOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD, segmentationId, tunnelOFPort, attachedMac, false);
480     }
481
482     /* Remove tunnel rules if last node in this tenant network */
483     private void removePerTunnelRules(Node node, Long dpid, String segmentationId, long tunnelOFPort) {
484         /*
485          * TODO : Optimize the following 2 writes to be restricted only for the very first port known in a segment.
486          */
487         /*
488          * Table(1) Rule #3
489          * ----------------
490          * Match:  Any remaining Ingress Local VM Packets
491          * Action: Drop w/ a low priority
492          * -------------------------------------------
493          * table=1,priority=8192,tun_id=0x5 actions=goto_table:2
494          */
495
496         handleTunnelMiss(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD, segmentationId, false);
497
498         /*
499          * Table(2) Rule #3
500          * ----------------
501          * Match: Any Remaining Flows w/a TunID
502          * Action: Drop w/ a low priority
503          * table=2,priority=8192,tun_id=0x5 actions=drop
504          */
505
506         handleLocalTableMiss(dpid, TABLE_2_LOCAL_FORWARD, segmentationId, false);
507
508         /*
509          * Table(0) Rule #2
510          * ----------------
511          * Match: Ingress Port, Tunnel ID
512          * Action: GOTO Local Table (10)
513          */
514
515         handleTunnelIn(dpid, TABLE_0_DEFAULT_INGRESS, TABLE_2_LOCAL_FORWARD, segmentationId, tunnelOFPort, false);
516
517          /*
518           * Table(1) Rule #2
519           * ----------------
520           * Match: Match Tunnel ID and L2 ::::FF:FF Flooding
521           * Action: Flood to selected destination TEPs
522           * -------------------------------------------
523           * table=1,priority=16384,tun_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
524           * actions=output:10,output:11,goto_table:2
525           */
526
527         handleTunnelFloodOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD, segmentationId, tunnelOFPort, false);
528     }
529
530     private void programLocalVlanRules(Node node, Long dpid, String segmentationId, String attachedMac, long localPort) {
531         /*
532          * Table(0) Rule #1
533          * ----------------
534          * Match: VM sMac and Local Ingress Port
535          * Action: Set VLAN ID and GOTO Local Table 1
536          */
537
538         handleLocalInPortSetVlan(dpid, TABLE_0_DEFAULT_INGRESS,
539                 TABLE_1_ISOLATE_TENANT, segmentationId, localPort,
540                 attachedMac, true);
541
542         /*
543          * Table(0) Rule #3
544          * ----------------
545          * Match: Drop any remaining Ingress Local VM Packets
546          * Action: Drop w/ a low priority
547          */
548
549         handleDropSrcIface(dpid, localPort, true);
550
551         /*
552          * Table(2) Rule #1
553          * ----------------
554          * Match: Match VLAN ID and Destination DL/dMAC Addr
555          * Action: strip vlan, output to local port
556          * Example: table=2,vlan_id=0x5,dl_dst=00:00:00:00:00:01 actions= strip vlan, output:2
557          */
558
559         handleLocalVlanUcastOut(dpid, TABLE_2_LOCAL_FORWARD, segmentationId,
560                 localPort, attachedMac, true);
561
562         /*
563          * Table(2) Rule #2
564          * ----------------
565          * Match: VLAN ID and dMAC (::::FF:FF)
566          * Action: strip vlan, output to all local ports in this vlan
567          * Example: table=2,priority=16384,vlan_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
568          * actions= strip_vlan, output:2,3,4,5
569          */
570
571         handleLocalVlanBcastOut(dpid, TABLE_2_LOCAL_FORWARD, segmentationId,
572                 localPort, true);
573
574          /*
575           * Table(2) Rule #3
576           * ----------------
577           * Match: Any Remaining Flows w/a VLAN ID
578           * Action: Drop w/ a low priority
579           * Example: table=2,priority=8192,vlan_id=0x5 actions=drop
580           */
581
582           handleLocalVlanTableMiss(dpid, TABLE_2_LOCAL_FORWARD, segmentationId,
583                                    true);
584    }
585
586     private void removeLocalVlanRules(Node node, Long dpid,
587                                       String segmentationId, String attachedMac,
588                                       long localPort) {
589         /*
590          * Table(0) Rule #1
591          * ----------------
592          * Match: VM sMac and Local Ingress Port
593          * Action: Set VLAN ID and GOTO Local Table 1
594          */
595
596         handleLocalInPortSetVlan(dpid, TABLE_0_DEFAULT_INGRESS,
597                 TABLE_1_ISOLATE_TENANT, segmentationId, localPort,
598                 attachedMac, false);
599
600         /*
601          * Table(0) Rule #3
602          * ----------------
603          * Match: Drop any remaining Ingress Local VM Packets
604          * Action: Drop w/ a low priority
605          */
606
607         handleDropSrcIface(dpid, localPort, false);
608
609         /*
610          * Table(2) Rule #1
611          * ----------------
612          * Match: Match VLAN ID and Destination DL/dMAC Addr
613          * Action: strip vlan, output to local port
614          * Example: table=2,vlan_id=0x5,dl_dst=00:00:00:00:00:01 actions= strip vlan, output:2
615          */
616
617         handleLocalVlanUcastOut(dpid, TABLE_2_LOCAL_FORWARD, segmentationId,
618                 localPort, attachedMac, false);
619
620         /*
621          * Table(2) Rule #2
622          * ----------------
623          * Match: VLAN ID and dMAC (::::FF:FF)
624          * Action: strip vlan, output to all local ports in this vlan
625          * Example: table=2,priority=16384,vlan_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
626          * actions= strip_vlan, output:2,3,4,5
627          */
628
629         handleLocalVlanBcastOut(dpid, TABLE_2_LOCAL_FORWARD, segmentationId,
630                 localPort, false);
631    }
632
633    private void programLocalIngressVlanRules(Node node, Long dpid, String segmentationId, String attachedMac, long ethPort) {
634        /*
635         * Table(0) Rule #2
636         * ----------------
637         * Match: Ingress port = physical interface, Vlan ID
638         * Action: GOTO Local Table 2
639         */
640
641        handleVlanIn(dpid, TABLE_0_DEFAULT_INGRESS, TABLE_2_LOCAL_FORWARD,
642                     segmentationId, ethPort, true);
643
644         /*
645          * Table(1) Rule #2
646          * ----------------
647          * Match: Match VLAN ID and L2 ::::FF:FF Flooding
648          * Action: Flood to local and remote VLAN members
649          * -------------------------------------------
650          * Example: table=1,priority=16384,vlan_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
651          * actions=output:10 (eth port),goto_table:2
652          */
653
654         handleVlanFloodOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD,
655                            segmentationId, ethPort, true);
656    }
657
658    private void programRemoteEgressVlanRules(Node node, Long dpid, String segmentationId, String attachedMac, long ethPort) {
659        /*
660         * Table(1) Rule #1
661         * ----------------
662         * Match: Destination MAC is local VM MAC and vlan id
663         * Action: go to table 2
664         * -------------------------------------------
665         * Example: table=1,vlan_id=0x5,dl_dst=00:00:00:00:00:08 \
666         * actions=goto_table:2
667         */
668
669        handleVlanOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD,
670                      segmentationId, ethPort, attachedMac, true);
671
672        /*
673         * Table(1) Rule #3
674         * ----------------
675         * Match:  VLAN ID
676         * Action: Go to table 2
677         * -------------------------------------------
678         * Example: table=1,priority=8192,vlan_id=0x5 actions=output:1,goto_table:2
679         */
680
681        handleVlanMiss(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD,
682                       segmentationId, ethPort, true);
683    }
684
685    private void removeRemoteEgressVlanRules(Node node, Long dpid, String segmentationId, String attachedMac, long ethPort) {
686        /*
687         * Table(1) Rule #1
688         * ----------------
689         * Match: Destination MAC is local VM MAC and vlan id
690         * Action: go to table 2
691         * -------------------------------------------
692         * Example: table=1,vlan_id=0x5,dl_dst=00:00:00:00:00:08 \
693         * actions=goto_table:2
694         */
695
696        handleVlanOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD,
697                      segmentationId, ethPort, attachedMac, false);
698    }
699
700    private void removePerVlanRules(Node node, Long dpid, String segmentationId, long ethPort) {
701        /*
702         * Table(2) Rule #3
703         * ----------------
704         * Match: Any Remaining Flows w/a VLAN ID
705         * Action: Drop w/ a low priority
706         * Example: table=2,priority=8192,vlan_id=0x5 actions=drop
707         */
708
709         handleLocalVlanTableMiss(dpid, TABLE_2_LOCAL_FORWARD, segmentationId,
710                                  false);
711
712         /*
713          * Table(0) Rule #2
714          * ----------------
715          * Match: Ingress port = physical interface, Vlan ID
716          * Action: GOTO Local Table 2
717          */
718
719         handleVlanIn(dpid, TABLE_0_DEFAULT_INGRESS, TABLE_2_LOCAL_FORWARD,
720                      segmentationId, ethPort, false);
721
722          /*
723           * Table(1) Rule #2
724           * ----------------
725           * Match: Match VLAN ID and L2 ::::FF:FF Flooding
726           * Action: Flood to local and remote VLAN members
727           * -------------------------------------------
728           * Example: table=1,priority=16384,vlan_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
729           * actions=output:10 (eth port),goto_table:2
730           */
731
732          handleVlanFloodOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD,
733                             segmentationId, ethPort, false);
734
735          /*
736           * Table(1) Rule #3
737           * ----------------
738           * Match:  VLAN ID
739           * Action: Go to table 2
740           * -------------------------------------------
741           * Example: table=1,priority=8192,vlan_id=0x5 actions=output:1,goto_table:2
742           */
743
744          handleVlanMiss(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD,
745                         segmentationId, ethPort, false);
746    }
747     private Long getDpid (Node node, String bridgeUuid) {
748         Preconditions.checkNotNull(ovsdbConfigurationService);
749         try {
750             Row bridgeRow =  ovsdbConfigurationService
751                     .getRow(node, ovsdbConfigurationService.getTableName(node, Bridge.class), bridgeUuid);
752             Bridge bridge = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeRow);
753             Set<String> dpids = bridge.getDatapathIdColumn().getData();
754             if (dpids == null || dpids.size() == 0) return 0L;
755             return HexEncode.stringToLong((String) dpids.toArray()[0]);
756         } catch (Exception e) {
757             logger.error("Error finding Bridge's OF DPID", e);
758             return 0L;
759         }
760     }
761
762     private Long getIntegrationBridgeOFDPID (Node node) {
763         try {
764             String bridgeName = configurationService.getIntegrationBridgeName();
765             String brIntId = this.getInternalBridgeUUID(node, bridgeName);
766             if (brIntId == null) {
767                 logger.error("Unable to spot Bridge Identifier for {} in {}", bridgeName, node);
768                 return 0L;
769             }
770
771             return getDpid(node, brIntId);
772         } catch (Exception e) {
773             logger.error("Error finding Integration Bridge's OF DPID", e);
774             return 0L;
775         }
776     }
777
778     private Long getExternalBridgeDpid (Node node) {
779         try {
780             String bridgeName = configurationService.getExternalBridgeName();
781             String brUuid = this.getInternalBridgeUUID(node, bridgeName);
782             if (brUuid == null) {
783                 logger.error("Unable to spot Bridge Identifier for {} in {}", bridgeName, node);
784                 return 0L;
785             }
786
787             return getDpid(node, brUuid);
788         } catch (Exception e) {
789             logger.error("Error finding External Bridge's OF DPID", e);
790             return 0L;
791         }
792     }
793
794     private void programLocalRules (String networkType, String segmentationId, Node node, Interface intf) {
795         try {
796             Long dpid = this.getIntegrationBridgeOFDPID(node);
797             if (dpid == 0L) {
798                 logger.debug("Openflow Datapath-ID not set for the integration bridge in {}", node);
799                 return;
800             }
801
802             Set<Long> of_ports = intf.getOpenFlowPortColumn().getData();
803             if (of_ports == null || of_ports.size() <= 0) {
804                 logger.debug("Could NOT Identify OF value for port {} on {}", intf.getName(), node);
805                 return;
806             }
807             long localPort = (Long)of_ports.toArray()[0];
808
809             Map<String, String> externalIds = intf.getExternalIdsColumn().getData();
810             if (externalIds == null) {
811                 logger.error("No external_ids seen in {}", intf);
812                 return;
813             }
814
815             String attachedMac = externalIds.get(Constants.EXTERNAL_ID_VM_MAC);
816             if (attachedMac == null) {
817                 logger.error("No AttachedMac seen in {}", intf);
818                 return;
819             }
820
821             /* Program local rules based on network type */
822             if (networkType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
823                 logger.debug("Program local vlan rules for interface {}", intf.getName());
824                 programLocalVlanRules(node, dpid, segmentationId, attachedMac, localPort);
825             } else if (networkType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE) ||
826                        networkType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN)) {
827                 logger.debug("Program local bridge rules for interface {}", intf.getName());
828                 programLocalBridgeRules(node, dpid, segmentationId, attachedMac, localPort);
829             }
830         } catch (Exception e) {
831             logger.error("Exception in programming Local Rules for "+intf+" on "+node, e);
832         }
833     }
834
835     private void removeLocalRules (String networkType, String segmentationId, Node node, Interface intf) {
836         try {
837             Long dpid = this.getIntegrationBridgeOFDPID(node);
838             if (dpid == 0L) {
839                 logger.debug("Openflow Datapath-ID not set for the integration bridge in {}", node);
840                 return;
841             }
842
843             Set<Long> of_ports = intf.getOpenFlowPortColumn().getData();
844             if (of_ports == null || of_ports.size() <= 0) {
845                 logger.debug("Could NOT Identify OF value for port {} on {}", intf.getName(), node);
846                 return;
847             }
848             long localPort = (Long)of_ports.toArray()[0];
849
850             Map<String, String> externalIds = intf.getExternalIdsColumn().getData();
851             if (externalIds == null) {
852                 logger.error("No external_ids seen in {}", intf);
853                 return;
854             }
855
856             String attachedMac = externalIds.get(Constants.EXTERNAL_ID_VM_MAC);
857             if (attachedMac == null) {
858                 logger.error("No AttachedMac seen in {}", intf);
859                 return;
860             }
861
862             /* Program local rules based on network type */
863             if (networkType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
864                 logger.debug("Remove local vlan rules for interface {}", intf.getName());
865                 removeLocalVlanRules(node, dpid, segmentationId, attachedMac, localPort);
866             } else if (networkType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE) ||
867                        networkType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN)) {
868                 logger.debug("Remove local bridge rules for interface {}", intf.getName());
869                 removeLocalBridgeRules(node, dpid, segmentationId, attachedMac, localPort);
870             }
871         } catch (Exception e) {
872             logger.error("Exception in removing Local Rules for "+intf+" on "+node, e);
873         }
874     }
875
876     private void programTunnelRules (String tunnelType, String segmentationId, InetAddress dst, Node node,
877             Interface intf, boolean local) {
878
879         Preconditions.checkNotNull(ovsdbConfigurationService);
880
881         try {
882
883             Long dpid = this.getIntegrationBridgeOFDPID(node);
884             if (dpid == 0L) {
885                 logger.debug("Openflow Datapath-ID not set for the integration bridge in {}", node);
886                 return;
887             }
888
889             Set<Long> of_ports = intf.getOpenFlowPortColumn().getData();
890             if (of_ports == null || of_ports.size() <= 0) {
891                 logger.debug("Could NOT Identify OF value for port {} on {}", intf.getName(), node);
892                 return;
893             }
894             long localPort = (Long)of_ports.toArray()[0];
895
896             Map<String, String> externalIds = intf.getExternalIdsColumn().getData();
897             if (externalIds == null) {
898                 logger.error("No external_ids seen in {}", intf);
899                 return;
900             }
901
902             String attachedMac = externalIds.get(Constants.EXTERNAL_ID_VM_MAC);
903             if (attachedMac == null) {
904                 logger.error("No AttachedMac seen in {}", intf);
905                 return;
906             }
907
908             Map<String, Row> intfs = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Interface.class));
909             if (intfs != null) {
910                 for (Row row : intfs.values()) {
911                     Interface tunIntf = ovsdbConfigurationService.getTypedRow(node, Interface.class, row);
912                     if (tunIntf.getName().equals(this.getTunnelName(tunnelType, dst))) {
913                         of_ports = tunIntf.getOpenFlowPortColumn().getData();
914                         if (of_ports == null || of_ports.size() <= 0) {
915                             logger.debug("Could NOT Identify Tunnel port {} on {}", tunIntf.getName(), node);
916                             continue;
917                         }
918                         long tunnelOFPort = (Long)of_ports.toArray()[0];
919
920                         if (tunnelOFPort == -1) {
921                             logger.error("Could NOT Identify Tunnel port {} -> OF ({}) on {}", tunIntf.getName(), tunnelOFPort, node);
922                             return;
923                         }
924                         logger.debug("Identified Tunnel port {} -> OF ({}) on {}", tunIntf.getName(), tunnelOFPort, node);
925
926                         if (!local) {
927                             programRemoteEgressTunnelBridgeRules(node, dpid, segmentationId, attachedMac, tunnelOFPort, localPort);
928                         }
929                         logger.trace("program local ingress tunnel rules: node" + node.getNodeIDString() + " intf " + intf.getName());
930                         if (local) {
931                             programLocalIngressTunnelBridgeRules(node, dpid, segmentationId, attachedMac, tunnelOFPort, localPort);
932                         }
933                         return;
934                     }
935                 }
936             }
937         } catch (Exception e) {
938             logger.error("", e);
939         }
940     }
941
942     private void removeTunnelRules (String tunnelType, String segmentationId, InetAddress dst, Node node,
943             Interface intf, boolean local, boolean isLastInstanceOnNode) {
944
945         Preconditions.checkNotNull(ovsdbConfigurationService);
946         try {
947
948             Long dpid = this.getIntegrationBridgeOFDPID(node);
949             if (dpid == 0L) {
950                 logger.debug("Openflow Datapath-ID not set for the integration bridge in {}", node);
951                 return;
952             }
953
954             Set<Long> of_ports = intf.getOpenFlowPortColumn().getData();
955             if (of_ports == null || of_ports.size() <= 0) {
956                 logger.error("Could NOT Identify OF value for port {} on {}", intf.getName(), node);
957                 return;
958             }
959             long localPort = (Long)of_ports.toArray()[0];
960
961             Map<String, String> externalIds = intf.getExternalIdsColumn().getData();
962             if (externalIds == null) {
963                 logger.error("No external_ids seen in {}", intf);
964                 return;
965             }
966
967             String attachedMac = externalIds.get(Constants.EXTERNAL_ID_VM_MAC);
968             if (attachedMac == null) {
969                 logger.error("No AttachedMac seen in {}", intf);
970                 return;
971             }
972
973             Map<String, Row> intfs = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Interface.class));
974             if (intfs != null) {
975                 for (Row row : intfs.values()) {
976                     Interface tunIntf = ovsdbConfigurationService.getTypedRow(node, Interface.class, row);
977                     if (tunIntf.getName().equals(this.getTunnelName(tunnelType, dst))) {
978                         of_ports = tunIntf.getOpenFlowPortColumn().getData();
979                         if (of_ports == null || of_ports.size() <= 0) {
980                             logger.error("Could NOT Identify Tunnel port {} on {}", tunIntf.getName(), node);
981                             continue;
982                         }
983                         long tunnelOFPort = (Long)of_ports.toArray()[0];
984
985                         if (tunnelOFPort == -1) {
986                             logger.error("Could NOT Identify Tunnel port {} -> OF ({}) on {}", tunIntf.getName(), tunnelOFPort, node);
987                             return;
988                         }
989                         logger.debug("Identified Tunnel port {} -> OF ({}) on {}", tunIntf.getName(), tunnelOFPort, node);
990
991                         if (!local) {
992                             removeRemoteEgressTunnelBridgeRules(node, dpid, segmentationId, attachedMac, tunnelOFPort, localPort);
993                         }
994                         if (local && isLastInstanceOnNode) {
995                             removePerTunnelRules(node, dpid, segmentationId, tunnelOFPort);
996                         }
997                         return;
998                     }
999                 }
1000             }
1001         } catch (Exception e) {
1002             logger.error("", e);
1003         }
1004     }
1005
1006     private void programVlanRules (NeutronNetwork network, Node node, Interface intf) {
1007         Preconditions.checkNotNull(ovsdbConfigurationService);
1008         logger.debug("Program vlan rules for interface {}", intf.getName());
1009         try {
1010
1011             Long dpid = this.getIntegrationBridgeOFDPID(node);
1012             if (dpid == 0L) {
1013                 logger.debug("Openflow Datapath-ID not set for the integration bridge in {}", node);
1014                 return;
1015             }
1016
1017             Set<Long> of_ports = intf.getOpenFlowPortColumn().getData();
1018             int timeout = 6;
1019             while ((of_ports == null) && (timeout > 0)) {
1020                 of_ports = intf.getOpenFlowPortColumn().getData();
1021                 if (of_ports == null || of_ports.size() <= 0) {
1022                     // Wait for the OVSDB update to sync up the Local cache.
1023                     Thread.sleep(500);
1024                     timeout--;
1025                 }
1026             }
1027             if (of_ports == null || of_ports.size() <= 0) {
1028                 logger.error("Could NOT Identify OF value for port {} on {}", intf.getName(), node);
1029                 return;
1030             }
1031
1032             Map<String, String> externalIds = intf.getExternalIdsColumn().getData();
1033             if (externalIds == null) {
1034                 logger.error("No external_ids seen in {}", intf);
1035                 return;
1036             }
1037
1038             String attachedMac = externalIds.get(Constants.EXTERNAL_ID_VM_MAC);
1039             if (attachedMac == null) {
1040                 logger.error("No AttachedMac seen in {}", intf);
1041                 return;
1042             }
1043
1044             Map<String, Row> intfs = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Interface.class));
1045             if (intfs != null) {
1046                 for (Row row : intfs.values()) {
1047                     Interface ethIntf = ovsdbConfigurationService.getTypedRow(node, Interface.class, row);
1048                     if (ethIntf.getName().equalsIgnoreCase(bridgeConfigurationManager.getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork()))) {
1049                         of_ports = ethIntf.getOpenFlowPortColumn().getData();
1050                         timeout = 6;
1051                         while ((of_ports == null) && (timeout > 0)) {
1052                             of_ports = ethIntf.getOpenFlowPortColumn().getData();
1053                             if (of_ports == null || of_ports.size() <= 0) {
1054                                 // Wait for the OVSDB update to sync up the Local cache.
1055                                 Thread.sleep(500);
1056                                 timeout--;
1057                             }
1058                         }
1059
1060                         if (of_ports == null || of_ports.size() <= 0) {
1061                             logger.error("Could NOT Identify eth port {} on {}", ethIntf.getName(), node);
1062                             continue;
1063                         }
1064                         long ethOFPort = (Long)of_ports.toArray()[0];
1065
1066                         if (ethOFPort == -1) {
1067                             logger.error("Could NOT Identify eth port {} -> OF ({}) on {}", ethIntf.getName(), ethOFPort, node);
1068                             throw new Exception("port number < 0");
1069                         }
1070                         logger.debug("Identified eth port {} -> OF ({}) on {}", ethIntf.getName(), ethOFPort, node);
1071
1072                         programRemoteEgressVlanRules(node, dpid, network.getProviderSegmentationID(), attachedMac, ethOFPort);
1073                         programLocalIngressVlanRules(node, dpid, network.getProviderSegmentationID(), attachedMac, ethOFPort);
1074                         return;
1075                     }
1076                 }
1077             }
1078         } catch (Exception e) {
1079             logger.error("", e);
1080         }
1081     }
1082
1083     private void removeVlanRules (NeutronNetwork network, Node node,
1084                       Interface intf, boolean isLastInstanceOnNode) {
1085         Preconditions.checkNotNull(ovsdbConfigurationService);
1086         logger.debug("Remove vlan rules for interface {}", intf.getName());
1087
1088         try {
1089
1090             Long dpid = this.getIntegrationBridgeOFDPID(node);
1091             if (dpid == 0L) {
1092                 logger.debug("Openflow Datapath-ID not set for the integration bridge in {}", node);
1093                 return;
1094             }
1095
1096             Set<Long> of_ports = intf.getOpenFlowPortColumn().getData();
1097             if (of_ports == null || of_ports.size() <= 0) {
1098                 logger.error("Could NOT Identify OF value for port {} on {}", intf.getName(), node);
1099                 return;
1100             }
1101
1102             Map<String, String> externalIds = intf.getExternalIdsColumn().getData();
1103             if (externalIds == null) {
1104                 logger.error("No external_ids seen in {}", intf);
1105                 return;
1106             }
1107
1108             String attachedMac = externalIds.get(Constants.EXTERNAL_ID_VM_MAC);
1109             if (attachedMac == null) {
1110                 logger.error("No AttachedMac seen in {}", intf);
1111                 return;
1112             }
1113
1114             Map<String, Row> intfs = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Interface.class));
1115             if (intfs != null) {
1116                 for (Row row : intfs.values()) {
1117                     Interface ethIntf = ovsdbConfigurationService.getTypedRow(node, Interface.class, row);
1118                     if (ethIntf.getName().equalsIgnoreCase(bridgeConfigurationManager.getPhysicalInterfaceName(node,
1119                                                                    network.getProviderPhysicalNetwork()))) {
1120                         of_ports = ethIntf.getOpenFlowPortColumn().getData();
1121                         if (of_ports == null || of_ports.size() <= 0) {
1122                             logger.error("Could NOT Identify eth port {} on {}", ethIntf.getName(), node);
1123                             continue;
1124                         }
1125                         long ethOFPort = (Long)of_ports.toArray()[0];
1126
1127                         if (ethOFPort == -1) {
1128                             logger.error("Could NOT Identify eth port {} -> OF ({}) on {}", ethIntf.getName(), ethOFPort, node);
1129                             throw new Exception("port number < 0");
1130                         }
1131                         logger.debug("Identified eth port {} -> OF ({}) on {}", ethIntf.getName(), ethOFPort, node);
1132
1133                         removeRemoteEgressVlanRules(node, dpid, network.getProviderSegmentationID(), attachedMac, ethOFPort);
1134                         if (isLastInstanceOnNode) {
1135                             removePerVlanRules(node, dpid, network.getProviderSegmentationID(), ethOFPort);
1136                         }
1137                         return;
1138                     }
1139                 }
1140             }
1141         } catch (Exception e) {
1142             logger.error("", e);
1143         }
1144     }
1145
1146     @Override
1147     public Status handleInterfaceUpdate(NeutronNetwork network, Node srcNode, Interface intf) {
1148         Preconditions.checkNotNull(connectionService);
1149         List<Node> nodes = connectionService.getNodes();
1150         nodes.remove(srcNode);
1151         this.programLocalRules(network.getProviderNetworkType(), network.getProviderSegmentationID(), srcNode, intf);
1152
1153         if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
1154             this.programVlanRules(network, srcNode, intf);
1155         } else if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)
1156                    || network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN)){
1157             for (Node dstNode : nodes) {
1158                 InetAddress src = configurationService.getTunnelEndPoint(srcNode);
1159                 InetAddress dst = configurationService.getTunnelEndPoint(dstNode);
1160                 Status status = addTunnelPort(srcNode, network.getProviderNetworkType(), src, dst);
1161                 if (status.isSuccess()) {
1162                     this.programTunnelRules(network.getProviderNetworkType(), network.getProviderSegmentationID(), dst, srcNode, intf, true);
1163                 }
1164                 addTunnelPort(dstNode, network.getProviderNetworkType(), dst, src);
1165                 if (status.isSuccess()) {
1166                     this.programTunnelRules(network.getProviderNetworkType(), network.getProviderSegmentationID(), src, dstNode, intf, false);
1167                 }
1168             }
1169         }
1170
1171         return new Status(StatusCode.SUCCESS);
1172     }
1173
1174     private Status triggerInterfaceUpdates(Node node) {
1175         Preconditions.checkNotNull(ovsdbConfigurationService);
1176         try {
1177             Map<String, Row> intfs = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Interface.class));
1178             if (intfs != null) {
1179                 for (Row row : intfs.values()) {
1180                     Interface intf = ovsdbConfigurationService.getTypedRow(node, Interface.class, row);
1181                     NeutronNetwork network = tenantNetworkManager.getTenantNetwork(intf);
1182                     logger.debug("Trigger Interface update for {}", intf);
1183                     if (network != null) {
1184                         this.handleInterfaceUpdate(network, node, intf);
1185                     }
1186                 }
1187             }
1188         } catch (Exception e) {
1189             logger.error("Error Triggering the lost interface updates for "+ node, e);
1190             return new Status(StatusCode.INTERNALERROR, e.getLocalizedMessage());
1191         }
1192         return new Status(StatusCode.SUCCESS);
1193     }
1194     @Override
1195     public Status handleInterfaceUpdate(String tunnelType, String tunnelKey) {
1196         // TODO Auto-generated method stub
1197         return null;
1198     }
1199
1200     @Override
1201     public Status handleInterfaceDelete(String tunnelType, NeutronNetwork network, Node srcNode, Interface intf,
1202             boolean isLastInstanceOnNode) {
1203         Preconditions.checkNotNull(connectionService);
1204         Status status = new Status(StatusCode.SUCCESS);
1205         List<Node> nodes = connectionService.getNodes();
1206         nodes.remove(srcNode);
1207
1208         logger.info("Delete intf " + intf.getName() + " isLastInstanceOnNode " + isLastInstanceOnNode);
1209         List<String> phyIfName = bridgeConfigurationManager.getAllPhysicalInterfaceNames(srcNode);
1210         if (intf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN)
1211             || intf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)) {
1212             /* Delete tunnel port */
1213             try {
1214                 Map<String, String> options = intf.getOptionsColumn().getData();
1215                 InetAddress src = InetAddress.getByName(options.get("local_ip"));
1216                 InetAddress dst = InetAddress.getByName(options.get("remote_ip"));
1217                 status = deleteTunnelPort(srcNode, intf.getTypeColumn().getData(), src, dst);
1218             } catch (Exception e) {
1219                 logger.error(e.getMessage(), e);
1220             }
1221         } else if (phyIfName.contains(intf.getName())) {
1222             deletePhysicalPort(srcNode, intf.getName());
1223         } else {
1224             /* delete all other interfaces */
1225             this.removeLocalRules(network.getProviderNetworkType(), network.getProviderSegmentationID(),
1226                     srcNode, intf);
1227
1228             if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
1229                 this.removeVlanRules(network, srcNode,
1230                                  intf, isLastInstanceOnNode);
1231             } else if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)
1232                    || network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN)) {
1233
1234                 for (Node dstNode : nodes) {
1235                     InetAddress src = configurationService.getTunnelEndPoint(srcNode);
1236                     InetAddress dst = configurationService.getTunnelEndPoint(dstNode);
1237                     logger.info("Remove tunnel rules for interface " + intf.getName() + " on srcNode " + srcNode.getNodeIDString());
1238                     this.removeTunnelRules(tunnelType, network.getProviderSegmentationID(),
1239                                            dst, srcNode, intf, true, isLastInstanceOnNode);
1240                     logger.info("Remove tunnel rules for interface " + intf.getName() + " on dstNode " + dstNode.getNodeIDString());
1241                     this.removeTunnelRules(tunnelType, network.getProviderSegmentationID(),
1242                                            src, dstNode, intf, false, isLastInstanceOnNode);
1243                 }
1244             }
1245         }
1246         return status;
1247     }
1248
1249     @Override
1250     public void initializeFlowRules(Node node) {
1251         this.initializeFlowRules(node, configurationService.getIntegrationBridgeName());
1252         this.initializeFlowRules(node, configurationService.getExternalBridgeName());
1253         this.triggerInterfaceUpdates(node);
1254     }
1255
1256     private void initializeFlowRules(Node node, String bridgeName) {
1257         String bridgeUuid = this.getInternalBridgeUUID(node, bridgeName);
1258         if (bridgeUuid == null) {
1259             return;
1260         }
1261
1262         Long dpid = getDpid(node, bridgeUuid);
1263
1264         if (dpid == 0L) {
1265             logger.debug("Openflow Datapath-ID not set for the integration bridge in {}", node);
1266             return;
1267         }
1268
1269         /*
1270          * Table(0) Rule #1
1271          * ----------------
1272          * Match: LLDP (0x88CCL)
1273          * Action: Packet_In to Controller Reserved Port
1274          */
1275
1276          writeLLDPRule(dpid);
1277          if (bridgeName.equals(configurationService.getExternalBridgeName())) {
1278              writeNormalRule(dpid);
1279          }
1280     }
1281
1282     /*
1283     * Create an LLDP Flow Rule to encapsulate into
1284     * a packet_in that is sent to the controller
1285     * for topology handling.
1286     * Match: Ethertype 0x88CCL
1287     * Action: Punt to Controller in a Packet_In msg
1288     */
1289
1290     private void writeLLDPRule(Long dpidLong) {
1291
1292         String nodeName = OPENFLOW + dpidLong;
1293         EtherType etherType = new EtherType(0x88CCL);
1294
1295         MatchBuilder matchBuilder = new MatchBuilder();
1296         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
1297         FlowBuilder flowBuilder = new FlowBuilder();
1298
1299         // Create Match(es) and Set them in the FlowBuilder Object
1300         flowBuilder.setMatch(MatchUtils.createEtherTypeMatch(matchBuilder, etherType).build());
1301
1302         // Create the OF Actions and Instructions
1303         InstructionBuilder ib = new InstructionBuilder();
1304         InstructionsBuilder isb = new InstructionsBuilder();
1305
1306         // Instructions List Stores Individual Instructions
1307         List<Instruction> instructions = Lists.newArrayList();
1308
1309         // Call the InstructionBuilder Methods Containing Actions
1310         InstructionUtils.createSendToControllerInstructions(ib);
1311         ib.setOrder(0);
1312         ib.setKey(new InstructionKey(0));
1313         instructions.add(ib.build());
1314
1315         // Add InstructionBuilder to the Instruction(s)Builder List
1316         isb.setInstruction(instructions);
1317
1318         // Add InstructionsBuilder to FlowBuilder
1319         flowBuilder.setInstructions(isb.build());
1320
1321         String flowId = "LLDP";
1322         flowBuilder.setId(new FlowId(flowId));
1323         FlowKey key = new FlowKey(new FlowId(flowId));
1324         flowBuilder.setBarrier(true);
1325         flowBuilder.setTableId((short) 0);
1326         flowBuilder.setKey(key);
1327         flowBuilder.setFlowName(flowId);
1328         flowBuilder.setHardTimeout(0);
1329         flowBuilder.setIdleTimeout(0);
1330         writeFlow(flowBuilder, nodeBuilder);
1331     }
1332
1333     /*
1334     * Create a NORMAL Table Miss Flow Rule
1335     * Match: any
1336     * Action: forward to NORMAL pipeline
1337     */
1338
1339     private void writeNormalRule(Long dpidLong) {
1340
1341         String nodeName = OPENFLOW + dpidLong;
1342
1343         MatchBuilder matchBuilder = new MatchBuilder();
1344         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
1345         FlowBuilder flowBuilder = new FlowBuilder();
1346
1347         // Create the OF Actions and Instructions
1348         InstructionBuilder ib = new InstructionBuilder();
1349         InstructionsBuilder isb = new InstructionsBuilder();
1350
1351         // Instructions List Stores Individual Instructions
1352         List<Instruction> instructions = Lists.newArrayList();
1353
1354         // Call the InstructionBuilder Methods Containing Actions
1355         InstructionUtils.createNormalInstructions(ib);
1356         ib.setOrder(0);
1357         ib.setKey(new InstructionKey(0));
1358         instructions.add(ib.build());
1359
1360         // Add InstructionBuilder to the Instruction(s)Builder List
1361         isb.setInstruction(instructions);
1362
1363         // Add InstructionsBuilder to FlowBuilder
1364         flowBuilder.setInstructions(isb.build());
1365
1366         String flowId = "NORMAL";
1367         flowBuilder.setId(new FlowId(flowId));
1368         FlowKey key = new FlowKey(new FlowId(flowId));
1369         flowBuilder.setMatch(matchBuilder.build());
1370         flowBuilder.setPriority(0);
1371         flowBuilder.setBarrier(true);
1372         flowBuilder.setTableId((short) 0);
1373         flowBuilder.setKey(key);
1374         flowBuilder.setFlowName(flowId);
1375         flowBuilder.setHardTimeout(0);
1376         flowBuilder.setIdleTimeout(0);
1377         writeFlow(flowBuilder, nodeBuilder);
1378     }
1379
1380     /*
1381      * (Table:0) Ingress Tunnel Traffic
1382      * Match: OpenFlow InPort and Tunnel ID
1383      * Action: GOTO Local Table (10)
1384      * table=0,tun_id=0x5,in_port=10, actions=goto_table:2
1385      */
1386
1387     private void handleTunnelIn(Long dpidLong, Short writeTable,
1388                                 Short goToTableId, String segmentationId,
1389                                 Long ofPort, boolean write) {
1390
1391         String nodeName = OPENFLOW + dpidLong;
1392
1393         BigInteger tunnelId = new BigInteger(segmentationId);
1394         MatchBuilder matchBuilder = new MatchBuilder();
1395         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
1396         FlowBuilder flowBuilder = new FlowBuilder();
1397
1398         // Create Match(es) and Set them in the FlowBuilder Object
1399         flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, tunnelId).build());
1400         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ofPort).build());
1401
1402         if (write) {
1403             // Create the OF Actions and Instructions
1404             InstructionBuilder ib = new InstructionBuilder();
1405             InstructionsBuilder isb = new InstructionsBuilder();
1406
1407             // Instructions List Stores Individual Instructions
1408             List<Instruction> instructions = Lists.newArrayList();
1409
1410             // Call the InstructionBuilder Methods Containing Actions
1411             InstructionUtils.createGotoTableInstructions(ib, goToTableId);
1412             ib.setOrder(0);
1413             ib.setKey(new InstructionKey(0));
1414             instructions.add(ib.build());
1415
1416             // Add InstructionBuilder to the Instruction(s)Builder List
1417             isb.setInstruction(instructions);
1418
1419             // Add InstructionsBuilder to FlowBuilder
1420             flowBuilder.setInstructions(isb.build());
1421         }
1422
1423         String flowId = "TunnelIn_"+segmentationId+"_"+ofPort;
1424         // Add Flow Attributes
1425         flowBuilder.setId(new FlowId(flowId));
1426         FlowKey key = new FlowKey(new FlowId(flowId));
1427         flowBuilder.setStrict(true);
1428         flowBuilder.setBarrier(false);
1429         flowBuilder.setTableId(writeTable);
1430         flowBuilder.setKey(key);
1431         flowBuilder.setFlowName(flowId);
1432         flowBuilder.setHardTimeout(0);
1433         flowBuilder.setIdleTimeout(0);
1434
1435         if (write) {
1436             writeFlow(flowBuilder, nodeBuilder);
1437         } else {
1438             removeFlow(flowBuilder, nodeBuilder);
1439         }
1440     }
1441
1442     /*
1443      * (Table:0) Ingress VLAN Traffic
1444      * Match: OpenFlow InPort and vlan ID
1445      * Action: GOTO Local Table (20)
1446      * table=0,vlan_id=0x5,in_port=10, actions=goto_table:2
1447      */
1448
1449     private void handleVlanIn(Long dpidLong, Short writeTable, Short goToTableId,
1450                       String segmentationId,  Long ethPort, boolean write) {
1451
1452         String nodeName = OPENFLOW + dpidLong;
1453
1454         MatchBuilder matchBuilder = new MatchBuilder();
1455         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
1456         FlowBuilder flowBuilder = new FlowBuilder();
1457
1458         // Create Match(es) and Set them in the FlowBuilder Object
1459         flowBuilder.setMatch(
1460                 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)))
1461                         .build())
1462                 .setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ethPort)
1463                         .build());
1464
1465         if (write) {
1466             // Create the OF Actions and Instructions
1467             InstructionBuilder ib = new InstructionBuilder();
1468             InstructionsBuilder isb = new InstructionsBuilder();
1469
1470             // Instructions List Stores Individual Instructions
1471             List<Instruction> instructions = Lists.newArrayList();
1472
1473             // Call the InstructionBuilder Methods Containing Actions
1474             InstructionUtils.createGotoTableInstructions(ib, goToTableId);
1475             ib.setOrder(0);
1476             ib.setKey(new InstructionKey(0));
1477             instructions.add(ib.build());
1478
1479             // Add InstructionBuilder to the Instruction(s)Builder List
1480             isb.setInstruction(instructions);
1481
1482             // Add InstructionsBuilder to FlowBuilder
1483             flowBuilder.setInstructions(isb.build());
1484         }
1485
1486         String flowId = "VlanIn_"+segmentationId+"_"+ethPort;
1487         // Add Flow Attributes
1488         flowBuilder.setId(new FlowId(flowId));
1489         FlowKey key = new FlowKey(new FlowId(flowId));
1490         flowBuilder.setStrict(true);
1491         flowBuilder.setBarrier(false);
1492         flowBuilder.setTableId(writeTable);
1493         flowBuilder.setKey(key);
1494         flowBuilder.setFlowName(flowId);
1495         flowBuilder.setHardTimeout(0);
1496         flowBuilder.setIdleTimeout(0);
1497         if (write) {
1498             writeFlow(flowBuilder, nodeBuilder);
1499         } else {
1500             removeFlow(flowBuilder, nodeBuilder);
1501         }
1502     }
1503
1504    /*
1505     * (Table:0) Egress VM Traffic Towards TEP
1506     * Match: Destination Ethernet Addr and OpenFlow InPort
1507     * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
1508     * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
1509     * actions=set_field:5->tun_id,goto_table=1"
1510     */
1511
1512     private void handleLocalInPort(Long dpidLong, Short writeTable, Short goToTableId,
1513                            String segmentationId, Long inPort, String attachedMac,
1514                            boolean write) {
1515
1516         String nodeName = OPENFLOW + dpidLong;
1517
1518         MatchBuilder matchBuilder = new MatchBuilder();
1519         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
1520         FlowBuilder flowBuilder = new FlowBuilder();
1521
1522         // Create the OF Match using MatchBuilder
1523         flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
1524         // TODO Broken In_Port Match
1525         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
1526
1527         String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
1528         // Add Flow Attributes
1529         flowBuilder.setId(new FlowId(flowId));
1530         FlowKey key = new FlowKey(new FlowId(flowId));
1531         flowBuilder.setStrict(true);
1532         flowBuilder.setBarrier(false);
1533         flowBuilder.setTableId(writeTable);
1534         flowBuilder.setKey(key);
1535         flowBuilder.setFlowName(flowId);
1536         flowBuilder.setHardTimeout(0);
1537         flowBuilder.setIdleTimeout(0);
1538
1539         if (write) {
1540             // Instantiate the Builders for the OF Actions and Instructions
1541             InstructionBuilder ib = new InstructionBuilder();
1542             InstructionsBuilder isb = new InstructionsBuilder();
1543
1544             // Instructions List Stores Individual Instructions
1545             List<Instruction> instructions = Lists.newArrayList();
1546
1547             // GOTO Instructions Need to be added first to the List
1548             InstructionUtils.createGotoTableInstructions(ib, goToTableId);
1549             ib.setOrder(0);
1550             ib.setKey(new InstructionKey(0));
1551             instructions.add(ib.build());
1552             // TODO Broken SetTunID
1553             InstructionUtils.createSetTunnelIdInstructions(ib, new BigInteger(segmentationId));
1554             ib.setOrder(1);
1555             ib.setKey(new InstructionKey(1));
1556             instructions.add(ib.build());
1557
1558             // Add InstructionBuilder to the Instruction(s)Builder List
1559             isb.setInstruction(instructions);
1560
1561             // Add InstructionsBuilder to FlowBuilder
1562             flowBuilder.setInstructions(isb.build());
1563
1564             writeFlow(flowBuilder, nodeBuilder);
1565         } else {
1566             removeFlow(flowBuilder, nodeBuilder);
1567         }
1568     }
1569
1570     /*
1571      * (Table:0) Egress VM Traffic Towards TEP
1572      * Match: Source Ethernet Addr and OpenFlow InPort
1573      * Instruction: Set VLANID and GOTO Table Egress (n)
1574      * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
1575      * actions=push_vlan, set_field:5->vlan_id,goto_table=1"
1576      */
1577
1578      private void handleLocalInPortSetVlan(Long dpidLong, Short writeTable,
1579                                   Short goToTableId, String segmentationId,
1580                                   Long inPort, String attachedMac,
1581                                   boolean write) {
1582
1583          String nodeName = OPENFLOW + dpidLong;
1584
1585          MatchBuilder matchBuilder = new MatchBuilder();
1586          NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
1587          FlowBuilder flowBuilder = new FlowBuilder();
1588
1589          // Create the OF Match using MatchBuilder
1590          flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
1591          flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
1592
1593          String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
1594          // Add Flow Attributes
1595          flowBuilder.setId(new FlowId(flowId));
1596          FlowKey key = new FlowKey(new FlowId(flowId));
1597          flowBuilder.setStrict(true);
1598          flowBuilder.setBarrier(false);
1599          flowBuilder.setTableId(writeTable);
1600          flowBuilder.setKey(key);
1601          flowBuilder.setFlowName(flowId);
1602          flowBuilder.setHardTimeout(0);
1603          flowBuilder.setIdleTimeout(0);
1604
1605          if (write) {
1606              // Instantiate the Builders for the OF Actions and Instructions
1607              InstructionBuilder ib = new InstructionBuilder();
1608              InstructionsBuilder isb = new InstructionsBuilder();
1609
1610              // Instructions List Stores Individual Instructions
1611              List<Instruction> instructions = Lists.newArrayList();
1612
1613              // GOTO Instructions Need to be added first to the List
1614              InstructionUtils.createGotoTableInstructions(ib, goToTableId);
1615              ib.setOrder(0);
1616              ib.setKey(new InstructionKey(0));
1617              instructions.add(ib.build());
1618              // Set VLAN ID Instruction
1619              InstructionUtils.createSetVlanInstructions(ib, new VlanId(Integer.valueOf(segmentationId)));
1620              ib.setOrder(1);
1621              ib.setKey(new InstructionKey(1));
1622              instructions.add(ib.build());
1623
1624              // Add InstructionBuilder to the Instruction(s)Builder List
1625              isb.setInstruction(instructions);
1626
1627              // Add InstructionsBuilder to FlowBuilder
1628              flowBuilder.setInstructions(isb.build());
1629
1630              writeFlow(flowBuilder, nodeBuilder);
1631          } else {
1632              removeFlow(flowBuilder, nodeBuilder);
1633          }
1634      }
1635
1636     /*
1637      * (Table:0) Drop frames source from a VM that do not
1638      * match the associated MAC address of the local VM.
1639      * Match: Low priority anything not matching the VM SMAC
1640      * Instruction: Drop
1641      * table=0,priority=16384,in_port=1 actions=drop"
1642      */
1643
1644     private void handleDropSrcIface(Long dpidLong, Long inPort, boolean write) {
1645
1646         String nodeName = OPENFLOW + dpidLong;
1647
1648         MatchBuilder matchBuilder = new MatchBuilder();
1649         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
1650         FlowBuilder flowBuilder = new FlowBuilder();
1651
1652         // Create the OF Match using MatchBuilder
1653         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
1654
1655         if (write) {
1656             // Instantiate the Builders for the OF Actions and Instructions
1657             InstructionBuilder ib = new InstructionBuilder();
1658             InstructionsBuilder isb = new InstructionsBuilder();
1659
1660             // Instructions List Stores Individual Instructions
1661             List<Instruction> instructions = Lists.newArrayList();
1662
1663             // Call the InstructionBuilder Methods Containing Actions
1664             InstructionUtils.createDropInstructions(ib);
1665             ib.setOrder(0);
1666             ib.setKey(new InstructionKey(0));
1667             instructions.add(ib.build());
1668
1669             // Add InstructionBuilder to the Instruction(s)Builder List
1670             isb.setInstruction(instructions);
1671
1672             // Add InstructionsBuilder to FlowBuilder
1673             flowBuilder.setInstructions(isb.build());
1674         }
1675
1676         String flowId = "DropFilter_"+inPort;
1677         // Add Flow Attributes
1678         flowBuilder.setId(new FlowId(flowId));
1679         FlowKey key = new FlowKey(new FlowId(flowId));
1680         flowBuilder.setStrict(true);
1681         flowBuilder.setBarrier(false);
1682         flowBuilder.setTableId(TABLE_0_DEFAULT_INGRESS);
1683         flowBuilder.setKey(key);
1684         flowBuilder.setFlowName(flowId);
1685         flowBuilder.setPriority(8192);
1686         flowBuilder.setHardTimeout(0);
1687         flowBuilder.setIdleTimeout(0);
1688         if (write) {
1689             writeFlow(flowBuilder, nodeBuilder);
1690         } else {
1691             removeFlow(flowBuilder, nodeBuilder);
1692         }
1693     }
1694
1695    /*
1696     * (Table:1) Egress Tunnel Traffic
1697     * Match: Destination Ethernet Addr and Local InPort
1698     * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
1699     * table=1,tun_id=0x5,dl_dst=00:00:00:00:00:08 \
1700     * actions=output:10,goto_table:2"
1701     */
1702     private void handleTunnelOut(Long dpidLong, Short writeTable,
1703                          Short goToTableId, String segmentationId,
1704                          Long OFPortOut, String attachedMac,
1705                          boolean write) {
1706
1707         String nodeName = OPENFLOW + dpidLong;
1708
1709         MatchBuilder matchBuilder = new MatchBuilder();
1710         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
1711         FlowBuilder flowBuilder = new FlowBuilder();
1712
1713         // Create the OF Match using MatchBuilder
1714         flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId)).build());
1715         flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress(attachedMac), null).build());
1716
1717         String flowId = "TunnelOut_"+segmentationId+"_"+OFPortOut+"_"+attachedMac;
1718         // Add Flow Attributes
1719         flowBuilder.setId(new FlowId(flowId));
1720         FlowKey key = new FlowKey(new FlowId(flowId));
1721         flowBuilder.setStrict(true);
1722         flowBuilder.setBarrier(false);
1723         flowBuilder.setTableId(writeTable);
1724         flowBuilder.setKey(key);
1725         flowBuilder.setFlowName(flowId);
1726         flowBuilder.setHardTimeout(0);
1727         flowBuilder.setIdleTimeout(0);
1728
1729         if (write) {
1730             // Instantiate the Builders for the OF Actions and Instructions
1731             InstructionBuilder ib = new InstructionBuilder();
1732             InstructionsBuilder isb = new InstructionsBuilder();
1733
1734             // Instructions List Stores Individual Instructions
1735             List<Instruction> instructions = Lists.newArrayList();
1736
1737             // GOTO Instructions
1738             InstructionUtils.createGotoTableInstructions(ib, goToTableId);
1739             ib.setOrder(0);
1740             ib.setKey(new InstructionKey(0));
1741             instructions.add(ib.build());
1742             // Set the Output Port/Iface
1743             InstructionUtils.createOutputPortInstructions(ib, dpidLong, OFPortOut);
1744             ib.setOrder(1);
1745             ib.setKey(new InstructionKey(1));
1746             instructions.add(ib.build());
1747
1748             // Add InstructionBuilder to the Instruction(s)Builder List
1749             isb.setInstruction(instructions);
1750
1751             // Add InstructionsBuilder to FlowBuilder
1752             flowBuilder.setInstructions(isb.build());
1753
1754             writeFlow(flowBuilder, nodeBuilder);
1755         } else {
1756             removeFlow(flowBuilder, nodeBuilder);
1757         }
1758     }
1759
1760     /*
1761      * (Table:1) Egress VLAN Traffic
1762      * Match: Destination Ethernet Addr and VLAN id
1763      * Instruction: GOTO Table Table 2
1764      * table=1,vlan_id=0x5,dl_dst=00:00:00:00:00:08 \
1765      * actions= goto_table:2"
1766      */
1767
1768      private void handleVlanOut(Long dpidLong, Short writeTable,
1769                         Short goToTableId, String segmentationId,
1770                         Long ethPort, String attachedMac, boolean write) {
1771
1772          String nodeName = OPENFLOW + dpidLong;
1773
1774          MatchBuilder matchBuilder = new MatchBuilder();
1775          NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
1776          FlowBuilder flowBuilder = new FlowBuilder();
1777
1778          // Create the OF Match using MatchBuilder
1779          flowBuilder.setMatch(
1780                  MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId))).build());
1781          flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress(attachedMac), null).build());
1782
1783          String flowId = "VlanOut_"+segmentationId+"_"+ethPort+"_"+attachedMac;
1784          // Add Flow Attributes
1785          flowBuilder.setId(new FlowId(flowId));
1786          FlowKey key = new FlowKey(new FlowId(flowId));
1787          flowBuilder.setStrict(true);
1788          flowBuilder.setBarrier(false);
1789          flowBuilder.setTableId(writeTable);
1790          flowBuilder.setKey(key);
1791          flowBuilder.setFlowName(flowId);
1792          flowBuilder.setHardTimeout(0);
1793          flowBuilder.setIdleTimeout(0);
1794
1795          if (write) {
1796              // Instantiate the Builders for the OF Actions and Instructions
1797              InstructionBuilder ib = new InstructionBuilder();
1798              InstructionsBuilder isb = new InstructionsBuilder();
1799
1800              // Instructions List Stores Individual Instructions
1801              List<Instruction> instructions = Lists.newArrayList();
1802
1803              // GOTO Instructions
1804              InstructionUtils.createGotoTableInstructions(ib, goToTableId);
1805              ib.setOrder(0);
1806              ib.setKey(new InstructionKey(0));
1807              instructions.add(ib.build());
1808
1809              // Add InstructionBuilder to the Instruction(s)Builder List
1810              isb.setInstruction(instructions);
1811
1812              // Add InstructionsBuilder to FlowBuilder
1813              flowBuilder.setInstructions(isb.build());
1814
1815              writeFlow(flowBuilder, nodeBuilder);
1816          } else {
1817              removeFlow(flowBuilder, nodeBuilder);
1818          }
1819      }
1820
1821        /*
1822     * (Table:1) Egress Tunnel Traffic
1823     * Match: Destination Ethernet Addr and Local InPort
1824     * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
1825     * table=1,priority=16384,tun_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
1826     * actions=output:10,output:11,goto_table:2
1827     */
1828
1829     private void handleTunnelFloodOut(Long dpidLong, Short writeTable,
1830                              Short localTable, String segmentationId,
1831                              Long OFPortOut, boolean write) {
1832
1833         String nodeName = OPENFLOW + dpidLong;
1834
1835         MatchBuilder matchBuilder = new MatchBuilder();
1836         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
1837         FlowBuilder flowBuilder = new FlowBuilder();
1838
1839         // Create the OF Match using MatchBuilder
1840         // Match TunnelID
1841         flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId)).build());
1842         // Match DMAC
1843
1844         flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress("01:00:00:00:00:00"),
1845                 new MacAddress("01:00:00:00:00:00")).build());
1846
1847         String flowId = "TunnelFloodOut_"+segmentationId;
1848         // Add Flow Attributes
1849         flowBuilder.setId(new FlowId(flowId));
1850         FlowKey key = new FlowKey(new FlowId(flowId));
1851         flowBuilder.setBarrier(true);
1852         flowBuilder.setTableId(writeTable);
1853         flowBuilder.setKey(key);
1854         flowBuilder.setPriority(16384);
1855         flowBuilder.setFlowName(flowId);
1856         flowBuilder.setHardTimeout(0);
1857         flowBuilder.setIdleTimeout(0);
1858
1859         Flow flow = this.getFlow(flowBuilder, nodeBuilder);
1860         // Instantiate the Builders for the OF Actions and Instructions
1861         InstructionBuilder ib = new InstructionBuilder();
1862         InstructionsBuilder isb = new InstructionsBuilder();
1863         List<Instruction> instructions = Lists.newArrayList();
1864         List<Instruction> existingInstructions = null;
1865         if (flow != null) {
1866             Instructions ins = flow.getInstructions();
1867             if (ins != null) {
1868                 existingInstructions = ins.getInstruction();
1869             }
1870         }
1871
1872         if (write) {
1873             // GOTO Instruction
1874             InstructionUtils.createGotoTableInstructions(ib, localTable);
1875             ib.setOrder(0);
1876             ib.setKey(new InstructionKey(0));
1877             instructions.add(ib.build());
1878             // Set the Output Port/Iface
1879             //createOutputGroupInstructions(nodeBuilder, ib, dpidLong, OFPortOut, existingInstructions);
1880             createOutputPortInstructions(ib, dpidLong, OFPortOut, existingInstructions);
1881             ib.setOrder(1);
1882             ib.setKey(new InstructionKey(1));
1883             instructions.add(ib.build());
1884
1885             // Add InstructionBuilder to the Instruction(s)Builder List
1886             isb.setInstruction(instructions);
1887
1888             // Add InstructionsBuilder to FlowBuilder
1889             flowBuilder.setInstructions(isb.build());
1890
1891             writeFlow(flowBuilder, nodeBuilder);
1892         } else {
1893             /* remove port from action list */
1894             boolean flowRemove = InstructionUtils.removeOutputPortFromInstructions(ib, dpidLong,
1895                     OFPortOut, existingInstructions);
1896             if (flowRemove) {
1897                 /* if all port are removed, remove the flow too. */
1898                 removeFlow(flowBuilder, nodeBuilder);
1899             } else {
1900                 /* Install instruction with new output port list*/
1901                 ib.setOrder(0);
1902                 ib.setKey(new InstructionKey(0));
1903                 instructions.add(ib.build());
1904
1905                 // Add InstructionBuilder to the Instruction(s)Builder List
1906                 isb.setInstruction(instructions);
1907
1908                 // Add InstructionsBuilder to FlowBuilder
1909                 flowBuilder.setInstructions(isb.build());
1910             }
1911         }
1912     }
1913
1914     /*
1915      * (Table:1) Egress VLAN Traffic
1916      * Match: Destination Ethernet Addr and VLAN id
1917      * Instruction: GOTO table 2 and Output port eth interface
1918      * Example: table=1,priority=16384,vlan_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
1919      * actions=output:eth1,goto_table:2
1920      */
1921
1922      private void handleVlanFloodOut(Long dpidLong, Short writeTable,
1923                            Short localTable, String segmentationId,
1924                            Long ethPort, boolean write) {
1925
1926          String nodeName = OPENFLOW + dpidLong;
1927
1928          MatchBuilder matchBuilder = new MatchBuilder();
1929          NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
1930          FlowBuilder flowBuilder = new FlowBuilder();
1931
1932          // Create the OF Match using MatchBuilder
1933          // Match Vlan ID
1934          flowBuilder.setMatch(
1935                  MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId))).build());
1936          // Match DMAC
1937          flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress("01:00:00:00:00:00"),
1938                  new MacAddress("01:00:00:00:00:00")).build());
1939
1940          String flowId = "VlanFloodOut_"+segmentationId;
1941          // Add Flow Attributes
1942          flowBuilder.setId(new FlowId(flowId));
1943          FlowKey key = new FlowKey(new FlowId(flowId));
1944          flowBuilder.setBarrier(true);
1945          flowBuilder.setTableId(writeTable);
1946          flowBuilder.setKey(key);
1947          flowBuilder.setPriority(16384);
1948          flowBuilder.setFlowName(flowId);
1949          flowBuilder.setHardTimeout(0);
1950          flowBuilder.setIdleTimeout(0);
1951
1952          //ToDo: Is there something to be done with result of the call to getFlow?
1953
1954          Flow flow = this.getFlow(flowBuilder, nodeBuilder);
1955          // Instantiate the Builders for the OF Actions and Instructions
1956          InstructionBuilder ib = new InstructionBuilder();
1957          InstructionsBuilder isb = new InstructionsBuilder();
1958          List<Instruction> instructions = Lists.newArrayList();
1959
1960          if (write) {
1961              // GOTO Instruction
1962              InstructionUtils.createGotoTableInstructions(ib, localTable);
1963              ib.setOrder(0);
1964              ib.setKey(new InstructionKey(0));
1965              instructions.add(ib.build());
1966              // Set the Output Port/Iface
1967              InstructionUtils.createOutputPortInstructions(ib, dpidLong, ethPort);
1968              ib.setOrder(1);
1969              ib.setKey(new InstructionKey(1));
1970              instructions.add(ib.build());
1971
1972              // Add InstructionBuilder to the Instruction(s)Builder List
1973              isb.setInstruction(instructions);
1974
1975              // Add InstructionsBuilder to FlowBuilder
1976              flowBuilder.setInstructions(isb.build());
1977
1978              writeFlow(flowBuilder, nodeBuilder);
1979          } else {
1980              removeFlow(flowBuilder, nodeBuilder);
1981          }
1982      }
1983
1984    /*
1985     * (Table:1) Table Drain w/ Catch All
1986     * Match: Tunnel ID
1987     * Action: GOTO Local Table (10)
1988     * table=2,priority=8192,tun_id=0x5 actions=drop
1989     */
1990
1991     private void handleTunnelMiss(Long dpidLong, Short writeTable,
1992                           Short goToTableId, String segmentationId,
1993                           boolean write) {
1994
1995         String nodeName = OPENFLOW + dpidLong;
1996
1997         MatchBuilder matchBuilder = new MatchBuilder();
1998         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
1999         FlowBuilder flowBuilder = new FlowBuilder();
2000
2001         // Create Match(es) and Set them in the FlowBuilder Object
2002         flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId)).build());
2003
2004         if (write) {
2005             // Create the OF Actions and Instructions
2006             InstructionBuilder ib = new InstructionBuilder();
2007             InstructionsBuilder isb = new InstructionsBuilder();
2008
2009             // Instructions List Stores Individual Instructions
2010             List<Instruction> instructions = Lists.newArrayList();
2011
2012             // Call the InstructionBuilder Methods Containing Actions
2013             InstructionUtils.createGotoTableInstructions(ib, goToTableId);
2014             ib.setOrder(0);
2015             ib.setKey(new InstructionKey(0));
2016             instructions.add(ib.build());
2017
2018             // Add InstructionBuilder to the Instruction(s)Builder List
2019             isb.setInstruction(instructions);
2020
2021             // Add InstructionsBuilder to FlowBuilder
2022             flowBuilder.setInstructions(isb.build());
2023         }
2024
2025         String flowId = "TunnelMiss_"+segmentationId;
2026         // Add Flow Attributes
2027         flowBuilder.setId(new FlowId(flowId));
2028         FlowKey key = new FlowKey(new FlowId(flowId));
2029         flowBuilder.setStrict(true);
2030         flowBuilder.setBarrier(false);
2031         flowBuilder.setTableId(writeTable);
2032         flowBuilder.setKey(key);
2033         flowBuilder.setPriority(8192);
2034         flowBuilder.setFlowName(flowId);
2035         flowBuilder.setHardTimeout(0);
2036         flowBuilder.setIdleTimeout(0);
2037         if (write) {
2038             writeFlow(flowBuilder, nodeBuilder);
2039         } else {
2040             removeFlow(flowBuilder, nodeBuilder);
2041         }
2042     }
2043
2044
2045     /*
2046      * (Table:1) Table Drain w/ Catch All
2047      * Match: Vlan ID
2048      * Action: Output port eth interface
2049      * table=1,priority=8192,vlan_id=0x5 actions= output port:eth1
2050      */
2051
2052      private void handleVlanMiss(Long dpidLong, Short writeTable,
2053                          Short goToTableId, String segmentationId,
2054                          Long ethPort, boolean write) {
2055
2056          String nodeName = OPENFLOW + dpidLong;
2057
2058          MatchBuilder matchBuilder = new MatchBuilder();
2059          NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
2060          FlowBuilder flowBuilder = new FlowBuilder();
2061
2062          // Create Match(es) and Set them in the FlowBuilder Object
2063          flowBuilder.setMatch(
2064                  MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId))).build());
2065
2066          if (write) {
2067              // Create the OF Actions and Instructions
2068              InstructionBuilder ib = new InstructionBuilder();
2069              InstructionsBuilder isb = new InstructionsBuilder();
2070
2071              // Instructions List Stores Individual Instructions
2072              List<Instruction> instructions = Lists.newArrayList();
2073
2074              // Call the InstructionBuilder Methods Containing Actions
2075              //createGotoTableInstructions(ib, goToTableId);
2076              //ib.setOrder(0);
2077              //ib.setKey(new InstructionKey(0));
2078              //instructions.add(ib.build());
2079              // Set the Output Port/Iface
2080              InstructionUtils.createOutputPortInstructions(ib, dpidLong, ethPort);
2081              ib.setOrder(0);
2082              ib.setKey(new InstructionKey(1));
2083              instructions.add(ib.build());
2084
2085              // Add InstructionBuilder to the Instruction(s)Builder List
2086              isb.setInstruction(instructions);
2087
2088              // Add InstructionsBuilder to FlowBuilder
2089              flowBuilder.setInstructions(isb.build());
2090          }
2091
2092          String flowId = "VlanMiss_"+segmentationId;
2093          // Add Flow Attributes
2094          flowBuilder.setId(new FlowId(flowId));
2095          FlowKey key = new FlowKey(new FlowId(flowId));
2096          flowBuilder.setStrict(true);
2097          flowBuilder.setBarrier(false);
2098          flowBuilder.setTableId(writeTable);
2099          flowBuilder.setKey(key);
2100          flowBuilder.setPriority(8192);
2101          flowBuilder.setFlowName(flowId);
2102          flowBuilder.setHardTimeout(0);
2103          flowBuilder.setIdleTimeout(0);
2104          if (write) {
2105              writeFlow(flowBuilder, nodeBuilder);
2106          } else {
2107              removeFlow(flowBuilder, nodeBuilder);
2108          }
2109      }
2110
2111     /*
2112      * (Table:1) Local Broadcast Flood
2113      * Match: Tunnel ID and dMAC
2114      * Action: Output Port
2115      * table=2,tun_id=0x5,dl_dst=00:00:00:00:00:01 actions=output:2
2116      */
2117
2118     private void handleLocalUcastOut(Long dpidLong, Short writeTable,
2119                              String segmentationId, Long localPort,
2120                              String attachedMac, boolean write) {
2121
2122         String nodeName = OPENFLOW + dpidLong;
2123
2124         MatchBuilder matchBuilder = new MatchBuilder();
2125         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
2126         FlowBuilder flowBuilder = new FlowBuilder();
2127
2128         // Create the OF Match using MatchBuilder
2129         flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId)).build());
2130         flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress(attachedMac), null).build());
2131
2132         String flowId = "UcastOut_"+segmentationId+"_"+localPort+"_"+attachedMac;
2133         // Add Flow Attributes
2134         flowBuilder.setId(new FlowId(flowId));
2135         FlowKey key = new FlowKey(new FlowId(flowId));
2136         flowBuilder.setStrict(true);
2137         flowBuilder.setBarrier(false);
2138         flowBuilder.setTableId(writeTable);
2139         flowBuilder.setKey(key);
2140         flowBuilder.setFlowName(flowId);
2141         flowBuilder.setHardTimeout(0);
2142         flowBuilder.setIdleTimeout(0);
2143
2144         if (write) {
2145             // Instantiate the Builders for the OF Actions and Instructions
2146             InstructionBuilder ib = new InstructionBuilder();
2147             InstructionsBuilder isb = new InstructionsBuilder();
2148
2149             // Instructions List Stores Individual Instructions
2150             List<Instruction> instructions = Lists.newArrayList();
2151
2152             // Set the Output Port/Iface
2153             InstructionUtils.createOutputPortInstructions(ib, dpidLong, localPort);
2154             ib.setOrder(0);
2155             ib.setKey(new InstructionKey(0));
2156             instructions.add(ib.build());
2157
2158             // Add InstructionBuilder to the Instruction(s)Builder List
2159             isb.setInstruction(instructions);
2160
2161             // Add InstructionsBuilder to FlowBuilder
2162             flowBuilder.setInstructions(isb.build());
2163             writeFlow(flowBuilder, nodeBuilder);
2164         } else {
2165             removeFlow(flowBuilder, nodeBuilder);
2166         }
2167     }
2168
2169     /*
2170      * (Table:2) Local VLAN unicast
2171      * Match: VLAN ID and dMAC
2172      * Action: Output Port
2173      * table=2,vlan_id=0x5,dl_dst=00:00:00:00:00:01 actions=output:2
2174      */
2175
2176     private void handleLocalVlanUcastOut(Long dpidLong, Short writeTable,
2177                                  String segmentationId, Long localPort,
2178                                  String attachedMac, boolean write) {
2179
2180         String nodeName = OPENFLOW + dpidLong;
2181
2182         MatchBuilder matchBuilder = new MatchBuilder();
2183         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
2184         FlowBuilder flowBuilder = new FlowBuilder();
2185
2186         // Create the OF Match using MatchBuilder
2187         flowBuilder.setMatch(
2188                 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId))).build());
2189         flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress(attachedMac), null).build());
2190
2191         String flowId = "VlanUcastOut_"+segmentationId+"_"+localPort+"_"+attachedMac;
2192         // Add Flow Attributes
2193         flowBuilder.setId(new FlowId(flowId));
2194         FlowKey key = new FlowKey(new FlowId(flowId));
2195         flowBuilder.setStrict(true);
2196         flowBuilder.setBarrier(false);
2197         flowBuilder.setTableId(writeTable);
2198         flowBuilder.setKey(key);
2199         flowBuilder.setFlowName(flowId);
2200         flowBuilder.setHardTimeout(0);
2201         flowBuilder.setIdleTimeout(0);
2202
2203         if (write) {
2204             // Instantiate the Builders for the OF Actions and Instructions
2205             InstructionBuilder ib = new InstructionBuilder();
2206             InstructionsBuilder isb = new InstructionsBuilder();
2207
2208             // Instructions List Stores Individual Instructions
2209             List<Instruction> instructions = Lists.newArrayList();
2210             List<Instruction> instructions_tmp = Lists.newArrayList();
2211
2212             /* Strip vlan and store to tmp instruction space*/
2213             InstructionUtils.createPopVlanInstructions(ib);
2214             ib.setOrder(0);
2215             ib.setKey(new InstructionKey(0));
2216             instructions_tmp.add(ib.build());
2217
2218             // Set the Output Port/Iface
2219             ib = new InstructionBuilder();
2220             InstructionUtils.addOutputPortInstructions(ib, dpidLong, localPort, instructions_tmp);
2221             ib.setOrder(0);
2222             ib.setKey(new InstructionKey(0));
2223             instructions.add(ib.build());
2224
2225             // Add InstructionBuilder to the Instruction(s)Builder List
2226             isb.setInstruction(instructions);
2227
2228             // Add InstructionsBuilder to FlowBuilder
2229             flowBuilder.setInstructions(isb.build());
2230             writeFlow(flowBuilder, nodeBuilder);
2231         } else {
2232             removeFlow(flowBuilder, nodeBuilder);
2233         }
2234     }
2235
2236     /*
2237      * (Table:2) Local Broadcast Flood
2238      * Match: Tunnel ID and dMAC (::::FF:FF)
2239      * table=2,priority=16384,tun_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
2240      * actions=output:2,3,4,5
2241      */
2242
2243     private void handleLocalBcastOut(Long dpidLong, Short writeTable,
2244                              String segmentationId, Long localPort,
2245                              boolean write) {
2246
2247         String nodeName = OPENFLOW + dpidLong;
2248
2249         MatchBuilder matchBuilder = new MatchBuilder();
2250         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
2251         FlowBuilder flowBuilder = new FlowBuilder();
2252
2253         // Create the OF Match using MatchBuilder
2254         flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId)).build());
2255         flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress("01:00:00:00:00:00"),
2256                 new MacAddress("01:00:00:00:00:00")).build());
2257
2258         String flowId = "BcastOut_"+segmentationId;
2259         // Add Flow Attributes
2260         flowBuilder.setId(new FlowId(flowId));
2261         FlowKey key = new FlowKey(new FlowId(flowId));
2262         flowBuilder.setStrict(true);
2263         flowBuilder.setBarrier(false);
2264         flowBuilder.setTableId(writeTable);
2265         flowBuilder.setKey(key);
2266         flowBuilder.setPriority(16384);
2267         flowBuilder.setFlowName(flowId);
2268         flowBuilder.setHardTimeout(0);
2269         flowBuilder.setIdleTimeout(0);
2270         Flow flow = this.getFlow(flowBuilder, nodeBuilder);
2271         // Instantiate the Builders for the OF Actions and Instructions
2272         InstructionBuilder ib = new InstructionBuilder();
2273         InstructionsBuilder isb = new InstructionsBuilder();
2274         List<Instruction> instructions = Lists.newArrayList();
2275         List<Instruction> existingInstructions = null;
2276         if (flow != null) {
2277             Instructions ins = flow.getInstructions();
2278             if (ins != null) {
2279                 existingInstructions = ins.getInstruction();
2280             }
2281         }
2282
2283         if (write) {
2284             // Create output port list
2285             createOutputPortInstructions(ib, dpidLong, localPort, existingInstructions);
2286             ib.setOrder(0);
2287             ib.setKey(new InstructionKey(0));
2288             instructions.add(ib.build());
2289
2290             // Add InstructionBuilder to the Instruction(s)Builder List
2291             isb.setInstruction(instructions);
2292
2293             // Add InstructionsBuilder to FlowBuilder
2294             flowBuilder.setInstructions(isb.build());
2295
2296             writeFlow(flowBuilder, nodeBuilder);
2297         } else {
2298             boolean flowRemove = InstructionUtils.removeOutputPortFromInstructions(ib, dpidLong, localPort,
2299                     existingInstructions);
2300             if (flowRemove) {
2301                 /* if all ports are removed, remove flow */
2302                 removeFlow(flowBuilder, nodeBuilder);
2303             } else {
2304                 /* Install instruction with new output port list*/
2305                 ib.setOrder(0);
2306                 ib.setKey(new InstructionKey(0));
2307                 instructions.add(ib.build());
2308
2309                 // Add InstructionBuilder to the Instruction(s)Builder List
2310                 isb.setInstruction(instructions);
2311
2312                 // Add InstructionsBuilder to FlowBuilder
2313                 flowBuilder.setInstructions(isb.build());
2314
2315                 writeFlow(flowBuilder, nodeBuilder);
2316             }
2317         }
2318     }
2319
2320     /*
2321      * (Table:2) Local VLAN Broadcast Flood
2322      * Match: vlan ID and dMAC (::::FF:FF)
2323      * table=2,priority=16384,vlan_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
2324      * actions=strip_vlan, output:2,3,4,5
2325      */
2326
2327     private void handleLocalVlanBcastOut(Long dpidLong, Short writeTable,
2328                                  String segmentationId, Long localPort,
2329                                  boolean write) {
2330
2331         String nodeName = OPENFLOW + dpidLong;
2332
2333         MatchBuilder matchBuilder = new MatchBuilder();
2334         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
2335         FlowBuilder flowBuilder = new FlowBuilder();
2336
2337         // Create the OF Match using MatchBuilder
2338         flowBuilder.setMatch(
2339                 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId))).build());
2340         flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress("01:00:00:00:00:00"),
2341                 new MacAddress("01:00:00:00:00:00")).build());
2342
2343         String flowId = "VlanBcastOut_"+segmentationId;
2344         // Add Flow Attributes
2345         flowBuilder.setId(new FlowId(flowId));
2346         FlowKey key = new FlowKey(new FlowId(flowId));
2347         flowBuilder.setStrict(true);
2348         flowBuilder.setBarrier(false);
2349         flowBuilder.setTableId(writeTable);
2350         flowBuilder.setKey(key);
2351         flowBuilder.setPriority(16384);
2352         flowBuilder.setFlowName(flowId);
2353         flowBuilder.setHardTimeout(0);
2354         flowBuilder.setIdleTimeout(0);
2355         Flow flow = this.getFlow(flowBuilder, nodeBuilder);
2356         // Instantiate the Builders for the OF Actions and Instructions
2357         InstructionBuilder ib = new InstructionBuilder();
2358         InstructionsBuilder isb = new InstructionsBuilder();
2359         List<Instruction> instructions = Lists.newArrayList();
2360         List<Instruction> existingInstructions = null;
2361         boolean add_pop_vlan = true;
2362         if (flow != null) {
2363             Instructions ins = flow.getInstructions();
2364             if (ins != null) {
2365                 existingInstructions = ins.getInstruction();
2366             }
2367         }
2368
2369         if (write) {
2370             if (existingInstructions != null) {
2371                 /* Check if pop vlan is already the first action in action list */
2372                 List<Action> existingActions;
2373                 for (Instruction in : existingInstructions) {
2374                     if (in.getInstruction() instanceof ApplyActionsCase) {
2375                         existingActions = (((ApplyActionsCase)
2376                                 in.getInstruction()).getApplyActions().getAction());
2377                         if (existingActions.get(0).getAction() instanceof PopVlanActionCase) {
2378                             add_pop_vlan = false;
2379                             break;
2380                         }
2381                     }
2382                 }
2383             } else {
2384                 existingInstructions = Lists.newArrayList();
2385             }
2386
2387             if (add_pop_vlan) {
2388                 /* pop vlan */
2389                 InstructionUtils.createPopVlanInstructions(ib);
2390                 ib.setOrder(0);
2391                 ib.setKey(new InstructionKey(0));
2392                 existingInstructions.add(ib.build());
2393                 ib = new InstructionBuilder();
2394             }
2395
2396             // Create port list
2397             //createOutputGroupInstructions(nodeBuilder, ib, dpidLong, localPort, existingInstructions);
2398             createOutputPortInstructions(ib, dpidLong, localPort, existingInstructions);
2399             ib.setOrder(0);
2400             ib.setKey(new InstructionKey(0));
2401             instructions.add(ib.build());
2402
2403             // Add InstructionBuilder to the Instruction(s)Builder List
2404             isb.setInstruction(instructions);
2405
2406             // Add InstructionsBuilder to FlowBuilder
2407             flowBuilder.setInstructions(isb.build());
2408
2409             writeFlow(flowBuilder, nodeBuilder);
2410         } else {
2411             //boolean flowRemove = removeOutputPortFromGroup(nodeBuilder, ib, dpidLong,
2412             //                     localPort, existingInstructions);
2413             boolean flowRemove = InstructionUtils.removeOutputPortFromInstructions(ib, dpidLong,
2414                     localPort, existingInstructions);
2415             if (flowRemove) {
2416                 /* if all ports are removed, remove flow */
2417                 removeFlow(flowBuilder, nodeBuilder);
2418             } else {
2419                 /* Install instruction with new output port list*/
2420                 ib.setOrder(0);
2421                 ib.setKey(new InstructionKey(0));
2422                 instructions.add(ib.build());
2423
2424                 // Add InstructionBuilder to the Instruction(s)Builder List
2425                 isb.setInstruction(instructions);
2426
2427                 // Add InstructionsBuilder to FlowBuilder
2428                 flowBuilder.setInstructions(isb.build());
2429                 writeFlow(flowBuilder, nodeBuilder);
2430             }
2431         }
2432     }
2433
2434     /*
2435      * (Table:1) Local Table Miss
2436      * Match: Any Remaining Flows w/a TunID
2437      * Action: Drop w/ a low priority
2438      * table=2,priority=8192,tun_id=0x5 actions=drop
2439      */
2440
2441     private void handleLocalTableMiss(Long dpidLong, Short writeTable,
2442                              String segmentationId, boolean write) {
2443
2444         String nodeName = OPENFLOW + dpidLong;
2445
2446         MatchBuilder matchBuilder = new MatchBuilder();
2447         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
2448         FlowBuilder flowBuilder = new FlowBuilder();
2449
2450         // Create Match(es) and Set them in the FlowBuilder Object
2451         flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId)).build());
2452
2453         if (write) {
2454             // Create the OF Actions and Instructions
2455             InstructionBuilder ib = new InstructionBuilder();
2456             InstructionsBuilder isb = new InstructionsBuilder();
2457
2458             // Instructions List Stores Individual Instructions
2459             List<Instruction> instructions = Lists.newArrayList();
2460
2461             // Call the InstructionBuilder Methods Containing Actions
2462             InstructionUtils.createDropInstructions(ib);
2463             ib.setOrder(0);
2464             ib.setKey(new InstructionKey(0));
2465             instructions.add(ib.build());
2466
2467             // Add InstructionBuilder to the Instruction(s)Builder List
2468             isb.setInstruction(instructions);
2469
2470             // Add InstructionsBuilder to FlowBuilder
2471             flowBuilder.setInstructions(isb.build());
2472         }
2473
2474         String flowId = "LocalTableMiss_"+segmentationId;
2475         // Add Flow Attributes
2476         flowBuilder.setId(new FlowId(flowId));
2477         FlowKey key = new FlowKey(new FlowId(flowId));
2478         flowBuilder.setStrict(true);
2479         flowBuilder.setBarrier(false);
2480         flowBuilder.setTableId(writeTable);
2481         flowBuilder.setKey(key);
2482         flowBuilder.setPriority(8192);
2483         flowBuilder.setFlowName(flowId);
2484         flowBuilder.setHardTimeout(0);
2485         flowBuilder.setIdleTimeout(0);
2486         if (write) {
2487             writeFlow(flowBuilder, nodeBuilder);
2488         } else {
2489             removeFlow(flowBuilder, nodeBuilder);
2490         }
2491     }
2492
2493     /*
2494      * (Table:1) Local Table Miss
2495      * Match: Any Remaining Flows w/a VLAN ID
2496      * Action: Drop w/ a low priority
2497      * table=2,priority=8192,vlan_id=0x5 actions=drop
2498      */
2499
2500     private void handleLocalVlanTableMiss(Long dpidLong, Short writeTable,
2501                                   String segmentationId, boolean write) {
2502
2503         String nodeName = OPENFLOW + dpidLong;
2504
2505         MatchBuilder matchBuilder = new MatchBuilder();
2506         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
2507         FlowBuilder flowBuilder = new FlowBuilder();
2508
2509         // Create Match(es) and Set them in the FlowBuilder Object
2510         flowBuilder.setMatch(
2511                 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId))).build());
2512
2513         if (write) {
2514             // Create the OF Actions and Instructions
2515             InstructionBuilder ib = new InstructionBuilder();
2516             InstructionsBuilder isb = new InstructionsBuilder();
2517
2518             // Instructions List Stores Individual Instructions
2519             List<Instruction> instructions = Lists.newArrayList();
2520
2521             // Call the InstructionBuilder Methods Containing Actions
2522             InstructionUtils.createDropInstructions(ib);
2523             ib.setOrder(0);
2524             ib.setKey(new InstructionKey(0));
2525             instructions.add(ib.build());
2526
2527             // Add InstructionBuilder to the Instruction(s)Builder List
2528             isb.setInstruction(instructions);
2529
2530             // Add InstructionsBuilder to FlowBuilder
2531             flowBuilder.setInstructions(isb.build());
2532         }
2533
2534         String flowId = "LocalTableMiss_"+segmentationId;
2535         // Add Flow Attributes
2536         flowBuilder.setId(new FlowId(flowId));
2537         FlowKey key = new FlowKey(new FlowId(flowId));
2538         flowBuilder.setStrict(true);
2539         flowBuilder.setBarrier(false);
2540         flowBuilder.setTableId(writeTable);
2541         flowBuilder.setKey(key);
2542         flowBuilder.setPriority(8192);
2543         flowBuilder.setFlowName(flowId);
2544         flowBuilder.setHardTimeout(0);
2545         flowBuilder.setIdleTimeout(0);
2546         if (write) {
2547             writeFlow(flowBuilder, nodeBuilder);
2548         } else {
2549             removeFlow(flowBuilder, nodeBuilder);
2550         }
2551     }
2552
2553     private Group getGroup(GroupBuilder groupBuilder, NodeBuilder nodeBuilder) {
2554         Preconditions.checkNotNull(mdsalConsumer);
2555         if (mdsalConsumer == null) {
2556             logger.error("ERROR finding MDSAL Service. Its possible that writeFlow is called too soon ?");
2557             return null;
2558         }
2559
2560         dataBroker = mdsalConsumer.getDataBroker();
2561         if (dataBroker == null) {
2562             logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
2563             return null;
2564         }
2565
2566         InstanceIdentifier<Group> path1 = InstanceIdentifier.builder(Nodes.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory
2567                 .rev130819.nodes.Node.class, nodeBuilder.getKey()).augmentation(FlowCapableNode.class).child(Group.class,
2568                 new GroupKey(groupBuilder.getGroupId())).build();
2569         ReadOnlyTransaction readTx = dataBroker.newReadOnlyTransaction();
2570         try {
2571             Optional<Group> data = readTx.read(LogicalDatastoreType.CONFIGURATION, path1).get();
2572             if (data.isPresent()) {
2573                 return data.get();
2574             }
2575         } catch (InterruptedException|ExecutionException e) {
2576             logger.error(e.getMessage(), e);
2577         }
2578
2579         logger.debug("Cannot find data for Group " + groupBuilder.getGroupName());
2580         return null;
2581     }
2582
2583     private void writeGroup(GroupBuilder groupBuilder, NodeBuilder nodeBuilder) {
2584         Preconditions.checkNotNull(mdsalConsumer);
2585         if (mdsalConsumer == null) {
2586             logger.error("ERROR finding MDSAL Service. Its possible that writeFlow is called too soon ?");
2587             return;
2588         }
2589
2590         dataBroker = mdsalConsumer.getDataBroker();
2591         if (dataBroker == null) {
2592             logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
2593             return;
2594         }
2595
2596         ReadWriteTransaction modification = dataBroker.newReadWriteTransaction();
2597         InstanceIdentifier<Group> path1 = InstanceIdentifier.builder(Nodes.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory
2598                 .rev130819.nodes.Node.class, nodeBuilder.getKey()).augmentation(FlowCapableNode.class).child(Group.class,
2599                 new GroupKey(groupBuilder.getGroupId())).build();
2600         modification.put(LogicalDatastoreType.CONFIGURATION, path1, groupBuilder.build(), true /*createMissingParents*/);
2601
2602         CheckedFuture<Void, TransactionCommitFailedException> commitFuture = modification.submit();
2603         try {
2604             commitFuture.get();  // TODO: Make it async (See bug 1362)
2605             logger.debug("Transaction success for write of Group "+groupBuilder.getGroupName());
2606         } catch (InterruptedException|ExecutionException e) {
2607             logger.error(e.getMessage(), e);
2608         }
2609     }
2610
2611     private void removeGroup(GroupBuilder groupBuilder, NodeBuilder nodeBuilder) {
2612         Preconditions.checkNotNull(mdsalConsumer);
2613         if (mdsalConsumer == null) {
2614             logger.error("ERROR finding MDSAL Service. Its possible that writeFlow is called too soon ?");
2615             return;
2616         }
2617
2618         dataBroker = mdsalConsumer.getDataBroker();
2619         if (dataBroker == null) {
2620             logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
2621             return;
2622         }
2623
2624         WriteTransaction modification = dataBroker.newWriteOnlyTransaction();
2625         InstanceIdentifier<Group> path1 = InstanceIdentifier.builder(Nodes.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory
2626                 .rev130819.nodes.Node.class, nodeBuilder.getKey()).augmentation(FlowCapableNode.class).child(Group.class,
2627                 new GroupKey(groupBuilder.getGroupId())).build();
2628         modification.delete(LogicalDatastoreType.CONFIGURATION, path1);
2629         CheckedFuture<Void, TransactionCommitFailedException> commitFuture = modification.submit();
2630
2631         try {
2632             commitFuture.get();  // TODO: Make it async (See bug 1362)
2633             logger.debug("Transaction success for deletion of Group "+groupBuilder.getGroupName());
2634         } catch (InterruptedException|ExecutionException e) {
2635             logger.error(e.getMessage(), e);
2636         }
2637     }
2638     private Flow getFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
2639         Preconditions.checkNotNull(mdsalConsumer);
2640         if (mdsalConsumer == null) {
2641             logger.error("ERROR finding MDSAL Service. Its possible that writeFlow is called too soon ?");
2642             return null;
2643         }
2644
2645         dataBroker = mdsalConsumer.getDataBroker();
2646         if (dataBroker == null) {
2647             logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
2648             return null;
2649         }
2650
2651         InstanceIdentifier<Flow> path1 = InstanceIdentifier.builder(Nodes.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory
2652                 .rev130819.nodes.Node.class, nodeBuilder.getKey()).augmentation(FlowCapableNode.class).child(Table.class,
2653                 new TableKey(flowBuilder.getTableId())).child(Flow.class, flowBuilder.getKey()).build();
2654
2655         ReadOnlyTransaction readTx = dataBroker.newReadOnlyTransaction();
2656         try {
2657             Optional<Flow> data = readTx.read(LogicalDatastoreType.CONFIGURATION, path1).get();
2658             if (data.isPresent()) {
2659                 return data.get();
2660             }
2661         } catch (InterruptedException|ExecutionException e) {
2662             logger.error(e.getMessage(), e);
2663         }
2664
2665         logger.debug("Cannot find data for Flow " + flowBuilder.getFlowName());
2666         return null;
2667     }
2668
2669     private void writeFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
2670         Preconditions.checkNotNull(mdsalConsumer);
2671         if (mdsalConsumer == null) {
2672             logger.error("ERROR finding MDSAL Service. Its possible that writeFlow is called too soon ?");
2673             return;
2674         }
2675
2676         dataBroker = mdsalConsumer.getDataBroker();
2677         if (dataBroker == null) {
2678             logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
2679             return;
2680         }
2681
2682         ReadWriteTransaction modification = dataBroker.newReadWriteTransaction();
2683         InstanceIdentifier<Flow> path1 = InstanceIdentifier.builder(Nodes.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory
2684                 .rev130819.nodes.Node.class, nodeBuilder.getKey()).augmentation(FlowCapableNode.class).child(Table.class,
2685                 new TableKey(flowBuilder.getTableId())).child(Flow.class, flowBuilder.getKey()).build();
2686
2687         //modification.put(LogicalDatastoreType.OPERATIONAL, path1, flowBuilder.build());
2688         modification.put(LogicalDatastoreType.CONFIGURATION, path1, flowBuilder.build(), true /*createMissingParents*/);
2689
2690
2691         CheckedFuture<Void, TransactionCommitFailedException> commitFuture = modification.submit();
2692         try {
2693             commitFuture.get();  // TODO: Make it async (See bug 1362)
2694             logger.debug("Transaction success for write of Flow "+flowBuilder.getFlowName());
2695         } catch (InterruptedException|ExecutionException e) {
2696             logger.error(e.getMessage(), e);
2697         }
2698     }
2699
2700     private void removeFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
2701         Preconditions.checkNotNull(mdsalConsumer);
2702         if (mdsalConsumer == null) {
2703             logger.error("ERROR finding MDSAL Service.");
2704             return;
2705         }
2706
2707         dataBroker = mdsalConsumer.getDataBroker();
2708         if (dataBroker == null) {
2709             logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
2710             return;
2711         }
2712
2713         WriteTransaction modification = dataBroker.newWriteOnlyTransaction();
2714         InstanceIdentifier<Flow> path1 = InstanceIdentifier.builder(Nodes.class)
2715                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory
2716                 .rev130819.nodes.Node.class, nodeBuilder.getKey())
2717                 .augmentation(FlowCapableNode.class).child(Table.class,
2718                 new TableKey(flowBuilder.getTableId())).child(Flow.class, flowBuilder.getKey()).build();
2719         //modification.delete(LogicalDatastoreType.OPERATIONAL, nodeBuilderToInstanceId(nodeBuilder));
2720         //modification.delete(LogicalDatastoreType.OPERATIONAL, path1);
2721         //modification.delete(LogicalDatastoreType.CONFIGURATION, nodeBuilderToInstanceId(nodeBuilder));
2722         modification.delete(LogicalDatastoreType.CONFIGURATION, path1);
2723
2724         CheckedFuture<Void, TransactionCommitFailedException> commitFuture = modification.submit();
2725         try {
2726             commitFuture.get();  // TODO: Make it async (See bug 1362)
2727             logger.debug("Transaction success for deletion of Flow "+flowBuilder.getFlowName());
2728         } catch (InterruptedException|ExecutionException e) {
2729             logger.error(e.getMessage(), e);
2730         }
2731     }
2732
2733     /**
2734      * Create Output Port Group Instruction
2735      *
2736      * @param ib       Map InstructionBuilder without any instructions
2737      * @param dpidLong Long the datapath ID of a switch/node
2738      * @param port     Long representing a port on a switch/node
2739      * @return ib InstructionBuilder Map with instructions
2740      */
2741     protected InstructionBuilder createOutputPortInstructions(InstructionBuilder ib,
2742                                                               Long dpidLong, Long port ,
2743                                                               List<Instruction> instructions) {
2744         NodeConnectorId ncid = new NodeConnectorId(OPENFLOW + dpidLong + ":" + port);
2745         logger.debug("createOutputPortInstructions() Node Connector ID is - Type=openflow: DPID={} port={} existingInstructions={}", dpidLong, port, instructions);
2746
2747         List<Action> actionList = Lists.newArrayList();
2748         ActionBuilder ab = new ActionBuilder();
2749
2750         List<Action> existingActions;
2751         if (instructions != null) {
2752             for (Instruction in : instructions) {
2753                 if (in.getInstruction() instanceof ApplyActionsCase) {
2754                     existingActions = (((ApplyActionsCase) in.getInstruction()).getApplyActions().getAction());
2755                     actionList.addAll(existingActions);
2756                 }
2757             }
2758         }
2759         /* Create output action for this port*/
2760         OutputActionBuilder oab = new OutputActionBuilder();
2761         oab.setOutputNodeConnector(ncid);
2762         ab.setAction(new OutputActionCaseBuilder().setOutputAction(oab.build()).build());
2763         boolean addNew = true;
2764
2765         /* Find the group action and get the group */
2766         for (Action action : actionList) {
2767             if (action.getAction() instanceof OutputActionCase) {
2768                 OutputActionCase opAction = (OutputActionCase)action.getAction();
2769                 /* If output port action already in the action list of one of the buckets, skip */
2770                 if (opAction.getOutputAction().getOutputNodeConnector().equals(new Uri(ncid))) {
2771                     addNew = false;
2772                     break;
2773                 }
2774             }
2775         }
2776         if (addNew) {
2777             ab.setOrder(actionList.size());
2778             ab.setKey(new ActionKey(actionList.size()));
2779             actionList.add(ab.build());
2780         }
2781         // Create an Apply Action
2782         ApplyActionsBuilder aab = new ApplyActionsBuilder();
2783         aab.setAction(actionList);
2784         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
2785         logger.debug("createOutputPortInstructions() : applyAction {}", aab.build());
2786         return ib;
2787     }
2788
2789     /**
2790      * Create Output Port Group Instruction
2791      *
2792      * @param ib       Map InstructionBuilder without any instructions
2793      * @param dpidLong Long the datapath ID of a switch/node
2794      * @param port     Long representing a port on a switch/node
2795      * @return ib InstructionBuilder Map with instructions
2796      */
2797     protected InstructionBuilder createOutputGroupInstructions(NodeBuilder nodeBuilder,
2798                                                                InstructionBuilder ib,
2799                                                                Long dpidLong, Long port ,
2800                                                                List<Instruction> instructions) {
2801         NodeConnectorId ncid = new NodeConnectorId(OPENFLOW + dpidLong + ":" + port);
2802         logger.debug("createOutputGroupInstructions() Node Connector ID is - Type=openflow: DPID={} port={} existingInstructions={}", dpidLong, port, instructions);
2803
2804         List<Action> actionList = Lists.newArrayList();
2805         ActionBuilder ab = new ActionBuilder();
2806
2807         List<Action> existingActions;
2808         if (instructions != null) {
2809             for (Instruction in : instructions) {
2810                 if (in.getInstruction() instanceof ApplyActionsCase) {
2811                     existingActions = (((ApplyActionsCase) in.getInstruction()).getApplyActions().getAction());
2812                     actionList.addAll(existingActions);
2813                 }
2814             }
2815         }
2816
2817         GroupBuilder groupBuilder = new GroupBuilder();
2818         Group group = null;
2819
2820         /* Create output action for this port*/
2821         OutputActionBuilder oab = new OutputActionBuilder();
2822         oab.setOutputNodeConnector(ncid);
2823         ab.setAction(new OutputActionCaseBuilder().setOutputAction(oab.build()).build());
2824         logger.debug("createOutputGroupInstructions(): output action {}", ab.build());
2825         boolean addNew = true;
2826         boolean groupActionAdded = false;
2827
2828         /* Find the group action and get the group */
2829         for (Action action : actionList) {
2830             if (action.getAction() instanceof GroupActionCase) {
2831                 groupActionAdded = true;
2832                 GroupActionCase groupAction = (GroupActionCase) action.getAction();
2833                 Long id = groupAction.getGroupAction().getGroupId();
2834                 String groupName = groupAction.getGroupAction().getGroup();
2835                 GroupKey key = new GroupKey(new GroupId(id));
2836
2837                 groupBuilder.setGroupId(new GroupId(id));
2838                 groupBuilder.setGroupName(groupName);
2839                 groupBuilder.setGroupType(GroupTypes.GroupAll);
2840                 groupBuilder.setKey(key);
2841                 group = getGroup(groupBuilder, nodeBuilder);
2842                 logger.debug("createOutputGroupInstructions: group {}", group);
2843                 break;
2844             }
2845         }
2846
2847         logger.debug("createOutputGroupInstructions: groupActionAdded {}", groupActionAdded);
2848         if (groupActionAdded) {
2849             /* modify the action bucket in group */
2850             groupBuilder = new GroupBuilder(group);
2851             Buckets buckets = groupBuilder.getBuckets();
2852             for (Bucket bucket : buckets.getBucket()) {
2853                 List<Action> bucketActions = bucket.getAction();
2854                 logger.debug("createOutputGroupInstructions: bucketActions {}", bucketActions);
2855                 for (Action action : bucketActions) {
2856                     if (action.getAction() instanceof OutputActionCase) {
2857                         OutputActionCase opAction = (OutputActionCase)action.getAction();
2858                         /* If output port action already in the action list of one of the buckets, skip */
2859                         if (opAction.getOutputAction().getOutputNodeConnector().equals(new Uri(ncid))) {
2860                             addNew = false;
2861                             break;
2862                         }
2863                     }
2864                 }
2865             }
2866             logger.debug("createOutputGroupInstructions: addNew {}", addNew);
2867             if (addNew) {
2868                 /* the new output action is not in the bucket, add to bucket */
2869                 if (!buckets.getBucket().isEmpty()) {
2870                     Bucket bucket = buckets.getBucket().get(0);
2871                     List<Action> bucketActionList = Lists.newArrayList();
2872                     bucketActionList.addAll(bucket.getAction());
2873                     /* set order for new action and add to action list */
2874                     ab.setOrder(bucketActionList.size());
2875                     ab.setKey(new ActionKey(bucketActionList.size()));
2876                     bucketActionList.add(ab.build());
2877
2878                     /* set bucket and buckets list. Reset groupBuilder with new buckets.*/
2879                     BucketsBuilder bucketsBuilder = new BucketsBuilder();
2880                     List<Bucket> bucketList = Lists.newArrayList();
2881                     BucketBuilder bucketBuilder = new BucketBuilder();
2882                     bucketBuilder.setBucketId(new BucketId((long) 1));
2883                     bucketBuilder.setKey(new BucketKey(new BucketId((long) 1)));
2884                     bucketBuilder.setAction(bucketActionList);
2885                     bucketList.add(bucketBuilder.build());
2886                     bucketsBuilder.setBucket(bucketList);
2887                     groupBuilder.setBuckets(bucketsBuilder.build());
2888                     logger.debug("createOutputGroupInstructions: bucketList {}", bucketList);
2889                 }
2890             }
2891         } else {
2892             /* create group */
2893             groupBuilder = new GroupBuilder();
2894             groupBuilder.setGroupType(GroupTypes.GroupAll);
2895             groupBuilder.setGroupId(new GroupId(groupId));
2896             groupBuilder.setKey(new GroupKey(new GroupId(groupId)));
2897             groupBuilder.setGroupName("Output port group " + groupId);
2898             groupBuilder.setBarrier(false);
2899
2900             BucketsBuilder bucketBuilder = new BucketsBuilder();
2901             List<Bucket> bucketList = Lists.newArrayList();
2902             BucketBuilder bucket = new BucketBuilder();
2903             bucket.setBucketId(new BucketId((long) 1));
2904             bucket.setKey(new BucketKey(new BucketId((long) 1)));
2905
2906             /* put output action to the bucket */
2907             List<Action> bucketActionList = Lists.newArrayList();
2908             /* set order for new action and add to action list */
2909             ab.setOrder(bucketActionList.size());
2910             ab.setKey(new ActionKey(bucketActionList.size()));
2911             bucketActionList.add(ab.build());
2912
2913             bucket.setAction(bucketActionList);
2914             bucketList.add(bucket.build());
2915             bucketBuilder.setBucket(bucketList);
2916             groupBuilder.setBuckets(bucketBuilder.build());
2917
2918             /* Add new group action */
2919             GroupActionBuilder groupActionB = new GroupActionBuilder();
2920             groupActionB.setGroupId(groupId);
2921             groupActionB.setGroup("Output port group " + groupId);
2922             ab = new ActionBuilder();
2923             ab.setAction(new GroupActionCaseBuilder().setGroupAction(groupActionB.build()).build());
2924             ab.setOrder(actionList.size());
2925             ab.setKey(new ActionKey(actionList.size()));
2926             actionList.add(ab.build());
2927
2928             groupId++;
2929         }
2930         logger.debug("createOutputGroupInstructions: group {}", groupBuilder.build());
2931         logger.debug("createOutputGroupInstructions: actionList {}", actionList);
2932
2933         if (addNew) {
2934             /* rewrite the group to group table */
2935             writeGroup(groupBuilder, nodeBuilder);
2936         }
2937
2938         // Create an Apply Action
2939         ApplyActionsBuilder aab = new ApplyActionsBuilder();
2940         aab.setAction(actionList);
2941         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
2942
2943         return ib;
2944     }
2945
2946     /**
2947      * Remove Output Port from action list in group bucket
2948      *
2949      * @param ib       Map InstructionBuilder without any instructions
2950      * @param dpidLong Long the datapath ID of a switch/node
2951      * @param port     Long representing a port on a switch/node
2952      * @return ib InstructionBuilder Map with instructions
2953      */
2954     protected boolean removeOutputPortFromGroup(NodeBuilder nodeBuilder, InstructionBuilder ib,
2955                                 Long dpidLong, Long port , List<Instruction> instructions) {
2956
2957         NodeConnectorId ncid = new NodeConnectorId(OPENFLOW + dpidLong + ":" + port);
2958         logger.debug("removeOutputPortFromGroup() Node Connector ID is - Type=openflow: DPID={} port={} existingInstructions={}", dpidLong, port, instructions);
2959
2960         List<Action> actionList = Lists.newArrayList();
2961         ActionBuilder ab;
2962
2963         List<Action> existingActions;
2964         if (instructions != null) {
2965             for (Instruction in : instructions) {
2966                 if (in.getInstruction() instanceof ApplyActionsCase) {
2967                     existingActions = (((ApplyActionsCase) in.getInstruction()).getApplyActions().getAction());
2968                     actionList.addAll(existingActions);
2969                     break;
2970                 }
2971             }
2972         }
2973
2974         GroupBuilder groupBuilder = new GroupBuilder();
2975         Group group = null;
2976         boolean groupActionAdded = false;
2977         /* Find the group action and get the group */
2978         for (Action action : actionList) {
2979             if (action.getAction() instanceof GroupActionCase) {
2980                 groupActionAdded = true;
2981                 GroupActionCase groupAction = (GroupActionCase) action.getAction();
2982                 Long id = groupAction.getGroupAction().getGroupId();
2983                 String groupName = groupAction.getGroupAction().getGroup();
2984                 GroupKey key = new GroupKey(new GroupId(id));
2985
2986                 groupBuilder.setGroupId(new GroupId(id));
2987                 groupBuilder.setGroupName(groupName);
2988                 groupBuilder.setGroupType(GroupTypes.GroupAll);
2989                 groupBuilder.setKey(key);
2990                 group = getGroup(groupBuilder, nodeBuilder);
2991                 break;
2992             }
2993         }
2994
2995         if (groupActionAdded) {
2996             /* modify the action bucket in group */
2997             groupBuilder = new GroupBuilder(group);
2998             Buckets buckets = groupBuilder.getBuckets();
2999             List<Action> bucketActions = Lists.newArrayList();
3000             for (Bucket bucket : buckets.getBucket()) {
3001                 int index = 0;
3002                 boolean isPortDeleted = false;
3003                 bucketActions = bucket.getAction();
3004                 for (Action action : bucketActions) {
3005                     if (action.getAction() instanceof OutputActionCase) {
3006                         OutputActionCase opAction = (OutputActionCase)action.getAction();
3007                         if (opAction.getOutputAction().getOutputNodeConnector().equals(new Uri(ncid))) {
3008                             /* Find the output port in action list and remove */
3009                             index = bucketActions.indexOf(action);
3010                             bucketActions.remove(action);
3011                             isPortDeleted = true;
3012                             break;
3013                         }
3014                     }
3015                 }
3016                 if (isPortDeleted && !bucketActions.isEmpty()) {
3017                     for (int i = index; i< bucketActions.size(); i++) {
3018                         Action action = bucketActions.get(i);
3019                         if (action.getOrder() != i) {
3020                             /* Shift the action order */
3021                             ab = new ActionBuilder();
3022                             ab.setAction(action.getAction());
3023                             ab.setOrder(i);
3024                             ab.setKey(new ActionKey(i));
3025                             Action actionNewOrder = ab.build();
3026                             bucketActions.remove(action);
3027                             bucketActions.add(i, actionNewOrder);
3028                         }
3029                     }
3030
3031                 } else if (bucketActions.isEmpty()) {
3032                     /* remove bucket with empty action list */
3033                     buckets.getBucket().remove(bucket);
3034                     break;
3035                 }
3036             }
3037             if (!buckets.getBucket().isEmpty()) {
3038                 /* rewrite the group to group table */
3039                 /* set bucket and buckets list. Reset groupBuilder with new buckets.*/
3040                 BucketsBuilder bucketsBuilder = new BucketsBuilder();
3041                 List<Bucket> bucketList = Lists.newArrayList();
3042                 BucketBuilder bucketBuilder = new BucketBuilder();
3043                 bucketBuilder.setBucketId(new BucketId((long) 1));
3044                 bucketBuilder.setKey(new BucketKey(new BucketId((long) 1)));
3045                 bucketBuilder.setAction(bucketActions);
3046                 bucketList.add(bucketBuilder.build());
3047                 bucketsBuilder.setBucket(bucketList);
3048                 groupBuilder.setBuckets(bucketsBuilder.build());
3049                 logger.debug("removeOutputPortFromGroup: bucketList {}", bucketList);
3050
3051                 writeGroup(groupBuilder, nodeBuilder);
3052                 ApplyActionsBuilder aab = new ApplyActionsBuilder();
3053                 aab.setAction(actionList);
3054                 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
3055                 return false;
3056             } else {
3057                 /* remove group with empty bucket. return true to delete flow */
3058                 removeGroup(groupBuilder, nodeBuilder);
3059                 return true;
3060             }
3061         } else {
3062             /* no group for port list. flow can be removed */
3063             return true;
3064         }
3065     }
3066
3067     @Override
3068     public void initializeOFFlowRules(Node openflowNode) {
3069         Preconditions.checkNotNull(connectionService);
3070         List<Node> ovsNodes = connectionService.getNodes();
3071         if (ovsNodes == null) return;
3072         for (Node ovsNode : ovsNodes) {
3073             Long brIntDpid = this.getIntegrationBridgeOFDPID(ovsNode);
3074             Long brExDpid = this.getExternalBridgeDpid(ovsNode);
3075             logger.debug("Compare openflowNode to OVS node {} vs {} and {}", openflowNode.getID(), brIntDpid, brExDpid);
3076             String openflowID = openflowNode.getID().toString();
3077             if (openflowID.contains(brExDpid.toString())) {
3078                 this.initializeFlowRules(ovsNode, configurationService.getExternalBridgeName());
3079                 this.triggerInterfaceUpdates(ovsNode);
3080             }
3081             if (openflowID.contains(brIntDpid.toString())) {
3082                 this.initializeFlowRules(ovsNode, configurationService.getIntegrationBridgeName());
3083                 this.triggerInterfaceUpdates(ovsNode);
3084             }
3085         }
3086     }
3087
3088     public static NodeBuilder createNodeBuilder(String nodeId) {
3089         NodeBuilder builder = new NodeBuilder();
3090         builder.setId(new NodeId(nodeId));
3091         builder.setKey(new NodeKey(builder.getId()));
3092         return builder;
3093     }
3094
3095     private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node> nodeBuilderToInstanceId(NodeBuilder
3096                                                                                                                                              node) {
3097         return InstanceIdentifier.builder(Nodes.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class,
3098                 node.getKey()).toInstance();
3099     }
3100
3101     private String getInternalBridgeUUID (Node node, String bridgeName) {
3102         Preconditions.checkNotNull(ovsdbConfigurationService);
3103         try {
3104             Map<String, Row> bridgeTable = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Bridge.class));
3105             if (bridgeTable == null) return null;
3106             for (String key : bridgeTable.keySet()) {
3107                 Bridge bridge = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeTable.get(key));
3108                 if (bridge.getName().equals(bridgeName)) return key;
3109             }
3110         } catch (Exception e) {
3111             logger.error("Error getting Bridge Identifier for {} / {}", node, bridgeName, e);
3112         }
3113         return null;
3114     }
3115 }