Bug 2013 Vlan flows needs merging from traditional rules to a single L2Fwding Service
[netvirt.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 java.net.InetAddress;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16 import java.util.concurrent.ExecutionException;
17
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
21 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
24 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
25 import org.opendaylight.controller.networkconfig.neutron.NeutronSecurityGroup;
26 import org.opendaylight.controller.sal.core.Node;
27 import org.opendaylight.controller.sal.utils.HexEncode;
28 import org.opendaylight.controller.sal.utils.Status;
29 import org.opendaylight.controller.sal.utils.StatusCode;
30 import org.opendaylight.ovsdb.lib.notation.Row;
31 import org.opendaylight.ovsdb.lib.notation.UUID;
32 import org.opendaylight.ovsdb.openstack.netvirt.NetworkHandler;
33 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
34 import org.opendaylight.ovsdb.openstack.netvirt.api.ClassifierProvider;
35 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
36 import org.opendaylight.ovsdb.openstack.netvirt.api.EgressAclProvider;
37 import org.opendaylight.ovsdb.openstack.netvirt.api.IngressAclProvider;
38 import org.opendaylight.ovsdb.openstack.netvirt.api.L2ForwardingProvider;
39 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProvider;
40 import org.opendaylight.ovsdb.openstack.netvirt.api.SecurityServicesManager;
41 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
42 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
43 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
44 import org.opendaylight.ovsdb.plugin.api.StatusWithUuid;
45 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
46 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
47 import org.opendaylight.ovsdb.schema.openvswitch.Port;
48 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.GroupActionCase;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.GroupActionCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.group.action._case.GroupActionBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.Buckets;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.BucketsBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketKey;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
90 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
91 import org.slf4j.Logger;
92 import org.slf4j.LoggerFactory;
93
94 import com.google.common.base.Optional;
95 import com.google.common.base.Preconditions;
96 import com.google.common.collect.Lists;
97 import com.google.common.collect.Maps;
98 import com.google.common.util.concurrent.CheckedFuture;
99
100 /**
101  * Open vSwitch OpenFlow 1.3 Networking Provider for OpenStack Neutron
102  */
103 public class OF13Provider implements NetworkingProvider {
104     private static final Logger logger = LoggerFactory.getLogger(OF13Provider.class);
105     private DataBroker dataBroker;
106     private static final short TABLE_0_DEFAULT_INGRESS = 0;
107     private static final short TABLE_1_ISOLATE_TENANT = 10;
108     private static final short TABLE_2_LOCAL_FORWARD = 20;
109     private static final String OPENFLOW = "openflow:";
110     private static Long groupId = 1L;
111
112     private volatile org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService configurationService;
113     private volatile BridgeConfigurationManager bridgeConfigurationManager;
114     private volatile TenantNetworkManager tenantNetworkManager;
115     private volatile OvsdbConfigurationService ovsdbConfigurationService;
116     private volatile OvsdbConnectionService connectionService;
117     private volatile MdsalConsumer mdsalConsumer;
118     private volatile SecurityServicesManager securityServicesManager;
119     private volatile IngressAclProvider ingressAclProvider;
120     private volatile EgressAclProvider egressAclProvider;
121     private volatile ClassifierProvider classifierProvider;
122     private volatile L2ForwardingProvider l2ForwardingProvider;
123
124     public static final String NAME = "OF13Provider";
125
126     public OF13Provider(){
127
128     }
129
130     @Override
131     public String getName() {
132         return NAME;
133     }
134
135     @Override
136     public boolean supportsServices() {
137         return true;
138     }
139
140     @Override
141     public boolean hasPerTenantTunneling() {
142         return false;
143     }
144
145     private Status getTunnelReadinessStatus (Node node, String tunnelKey) {
146         InetAddress srcTunnelEndPoint = configurationService.getTunnelEndPoint(node);
147         if (srcTunnelEndPoint == null) {
148             logger.error("Tunnel Endpoint not configured for Node {}", node);
149             return new Status(StatusCode.NOTFOUND, "Tunnel Endpoint not configured for "+ node);
150         }
151
152         if (!bridgeConfigurationManager.isNodeNeutronReady(node)) {
153             logger.error(node+" is not Overlay ready");
154             return new Status(StatusCode.NOTACCEPTABLE, node+" is not Overlay ready");
155         }
156
157         if (!tenantNetworkManager.isTenantNetworkPresentInNode(node, tunnelKey)) {
158             logger.debug(node+" has no VM corresponding to segment "+ tunnelKey);
159             return new Status(StatusCode.NOTACCEPTABLE, node+" has no VM corresponding to segment "+ tunnelKey);
160         }
161         return new Status(StatusCode.SUCCESS);
162     }
163
164     private String getTunnelName(String tunnelType, InetAddress dst) {
165         return tunnelType+"-"+dst.getHostAddress();
166     }
167
168     private boolean isTunnelPresent(Node node, String tunnelName, String bridgeUUID) throws Exception {
169         Preconditions.checkNotNull(ovsdbConfigurationService);
170         Row bridgeRow = ovsdbConfigurationService
171                 .getRow(node, ovsdbConfigurationService.getTableName(node, Bridge.class), bridgeUUID);
172         Bridge bridge = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeRow);
173         if (bridge != null) {
174             Set<UUID> ports = bridge.getPortsColumn().getData();
175             for (UUID portUUID : ports) {
176                 Row portRow = ovsdbConfigurationService
177                         .getRow(node, ovsdbConfigurationService.getTableName(node, Port.class), portUUID.toString());
178                 Port port = ovsdbConfigurationService.getTypedRow(node, Port.class, portRow);
179                 if (port != null && tunnelName.equalsIgnoreCase(port.getName())) return true;
180             }
181         }
182         return false;
183     }
184
185     private String getPortUuid(Node node, String name, String bridgeUUID) throws Exception {
186         Preconditions.checkNotNull(ovsdbConfigurationService);
187         Row bridgeRow = ovsdbConfigurationService
188                 .getRow(node, ovsdbConfigurationService.getTableName(node, Bridge.class), bridgeUUID);
189         Bridge bridge = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeRow);
190         if (bridge != null) {
191             Set<UUID> ports = bridge.getPortsColumn().getData();
192             for (UUID portUUID : ports) {
193                 Row portRow = ovsdbConfigurationService
194                         .getRow(node, ovsdbConfigurationService.getTableName(node, Port.class), portUUID.toString());
195                 Port port = ovsdbConfigurationService.getTypedRow(node, Port.class, portRow);
196                 if (port != null && name.equalsIgnoreCase(port.getName())) return portUUID.toString();
197             }
198         }
199         return null;
200     }
201
202     private Status addTunnelPort (Node node, String tunnelType, InetAddress src, InetAddress dst) {
203         Preconditions.checkNotNull(ovsdbConfigurationService);
204         try {
205             String bridgeUUID = null;
206             String tunnelBridgeName = configurationService.getIntegrationBridgeName();
207             Map<String, Row> bridgeTable = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Bridge.class));
208             if (bridgeTable != null) {
209                 for (String uuid : bridgeTable.keySet()) {
210                     Bridge bridge = ovsdbConfigurationService.getTypedRow(node,Bridge.class, bridgeTable.get(uuid));
211                     if (bridge.getName().equals(tunnelBridgeName)) {
212                         bridgeUUID = uuid;
213                         break;
214                     }
215                 }
216             }
217             if (bridgeUUID == null) {
218                 logger.error("Could not find Bridge {} in {}", tunnelBridgeName, node);
219                 return new Status(StatusCode.NOTFOUND, "Could not find "+tunnelBridgeName+" in "+node);
220             }
221             String portName = getTunnelName(tunnelType, dst);
222
223             if (this.isTunnelPresent(node, portName, bridgeUUID)) {
224                 logger.trace("Tunnel {} is present in {} of {}", portName, tunnelBridgeName, node);
225                 return new Status(StatusCode.SUCCESS);
226             }
227
228             Port tunnelPort = ovsdbConfigurationService.createTypedRow(node, Port.class);
229             tunnelPort.setName(portName);
230             StatusWithUuid statusWithUuid = ovsdbConfigurationService
231                     .insertRow(node, ovsdbConfigurationService.getTableName(node, Port.class), bridgeUUID, tunnelPort.getRow());
232             if (!statusWithUuid.isSuccess()) {
233                 logger.error("Failed to insert Tunnel port {} in {}", portName, bridgeUUID);
234                 return statusWithUuid;
235             }
236
237             String tunnelPortUUID = statusWithUuid.getUuid().toString();
238             String interfaceUUID = null;
239             int timeout = 6;
240             while ((interfaceUUID == null) && (timeout > 0)) {
241                 Row portRow = ovsdbConfigurationService
242                         .getRow(node, ovsdbConfigurationService.getTableName(node, Port.class), tunnelPortUUID);
243                 tunnelPort = ovsdbConfigurationService.getTypedRow(node, Port.class, portRow);
244                 Set<UUID> interfaces = tunnelPort.getInterfacesColumn().getData();
245                 if (interfaces == null || interfaces.size() == 0) {
246                     // Wait for the OVSDB update to sync up the Local cache.
247                     Thread.sleep(500);
248                     timeout--;
249                     continue;
250                 }
251                 interfaceUUID = interfaces.toArray()[0].toString();
252                 Row intfRow = ovsdbConfigurationService
253                         .getRow(node, ovsdbConfigurationService.getTableName(node, Interface.class), interfaceUUID);
254                 Interface intf = ovsdbConfigurationService.getTypedRow(node, Interface.class, intfRow);
255                 if (intf == null) interfaceUUID = null;
256             }
257
258             if (interfaceUUID == null) {
259                 logger.error("Cannot identify Tunnel Interface for port {}/{}", portName, tunnelPortUUID);
260                 return new Status(StatusCode.INTERNALERROR);
261             }
262
263             Interface tunInterface = ovsdbConfigurationService.createTypedRow(node, Interface.class);
264             tunInterface.setType(tunnelType);
265             Map<String, String> options = Maps.newHashMap();
266             options.put("key", "flow");
267             options.put("local_ip", src.getHostAddress());
268             options.put("remote_ip", dst.getHostAddress());
269             tunInterface.setOptions(options);
270             Status status = ovsdbConfigurationService
271                     .updateRow(node, ovsdbConfigurationService.getTableName(node, Interface.class), tunnelPortUUID, interfaceUUID, tunInterface.getRow());
272             logger.debug("Tunnel {} add status : {}", tunInterface, status);
273             return status;
274         } catch (Exception e) {
275             logger.error("Exception in addTunnelPort", e);
276             return new Status(StatusCode.INTERNALERROR);
277         }
278     }
279
280     /* delete port from ovsdb port table */
281     private Status deletePort(Node node, String bridgeName, String portName) {
282         Preconditions.checkNotNull(ovsdbConfigurationService);
283         try {
284             String bridgeUUID = null;
285             Map<String, Row> bridgeTable = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Bridge.class));
286             if (bridgeTable != null) {
287                 for (String uuid : bridgeTable.keySet()) {
288                     Bridge bridge = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeTable.get(uuid));
289                     if (bridge.getName().equals(bridgeName)) {
290                         bridgeUUID = uuid;
291                         break;
292                     }
293                 }
294             }
295             if (bridgeUUID == null) {
296                 logger.debug("Could not find Bridge {} in {}", bridgeName, node);
297                 return new Status(StatusCode.SUCCESS);
298             }
299
300             String portUUID = this.getPortUuid(node, portName, bridgeUUID);
301             Status status = new Status(StatusCode.SUCCESS);
302             if (portUUID != null) {
303                 status = ovsdbConfigurationService
304                         .deleteRow(node, ovsdbConfigurationService.getTableName(node, Port.class), portUUID);
305                 if (!status.isSuccess()) {
306                     logger.error("Failed to delete port {} in {} status : {}", portName, bridgeUUID,
307                             status);
308                     return status;
309                 }
310                 logger.debug("Port {} delete status : {}", portName, status);
311             }
312             return status;
313         } catch (Exception e) {
314             logger.error("Exception in deletePort", e);
315             return new Status(StatusCode.INTERNALERROR);
316         }
317     }
318
319     private Status deleteTunnelPort(Node node, String tunnelType, InetAddress src, InetAddress dst) {
320         String tunnelBridgeName = configurationService.getIntegrationBridgeName();
321         String portName = getTunnelName(tunnelType, dst);
322         return deletePort(node, tunnelBridgeName, portName);
323     }
324
325     private Status deletePhysicalPort(Node node, String phyIntfName) {
326         String intBridgeName = configurationService.getIntegrationBridgeName();
327         return deletePort(node, intBridgeName, phyIntfName);
328     }
329
330     private void programLocalBridgeRules(Node node, Long dpid, String segmentationId, String attachedMac, long localPort) {
331         /*
332          * Table(0) Rule #3
333          * ----------------
334          * Match: VM sMac and Local Ingress Port
335          * Action:Action: Set Tunnel ID and GOTO Local Table (5)
336          */
337
338         handleLocalInPort(dpid, TABLE_0_DEFAULT_INGRESS, TABLE_1_ISOLATE_TENANT, segmentationId, localPort, attachedMac, true);
339
340         /*
341          * Table(0) Rule #4
342          * ----------------
343          * Match: Drop any remaining Ingress Local VM Packets
344          * Action: Drop w/ a low priority
345          */
346
347         handleDropSrcIface(dpid, localPort, true);
348
349         /*
350          * Table(2) Rule #1
351          * ----------------
352          * Match: Match TunID and Destination DL/dMAC Addr
353          * Action: Output Port
354          * table=2,tun_id=0x5,dl_dst=00:00:00:00:00:01 actions=output:2
355          */
356
357         handleLocalUcastOut(dpid, TABLE_2_LOCAL_FORWARD, segmentationId, localPort, attachedMac, true);
358
359         /*
360          * Table(2) Rule #2
361          * ----------------
362          * Match: Tunnel ID and dMAC (::::FF:FF)
363          * table=2,priority=16384,tun_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
364          * actions=output:2,3,4,5
365          */
366
367         handleLocalBcastOut(dpid, TABLE_2_LOCAL_FORWARD, segmentationId, localPort, true);
368         handleTunnelFloodOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD, segmentationId, localPort, true);
369
370         /*
371          * TODO : Optimize the following 2 writes to be restricted only for the very first port known in a segment.
372          */
373         /*
374          * Table(1) Rule #3
375          * ----------------
376          * Match:  Any remaining Ingress Local VM Packets
377          * Action: Drop w/ a low priority
378          * -------------------------------------------
379          * table=1,priority=8192,tun_id=0x5 actions=goto_table:2
380          */
381
382         handleTunnelMiss(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD, segmentationId, true);
383
384         /*
385          * Table(2) Rule #3
386          * ----------------
387          * Match: Any Remaining Flows w/a TunID
388          * Action: Drop w/ a low priority
389          * table=2,priority=8192,tun_id=0x5 actions=drop
390          */
391
392         handleLocalTableMiss(dpid, TABLE_2_LOCAL_FORWARD, segmentationId, true);
393     }
394
395     private void removeLocalBridgeRules(Node node, Long dpid, String segmentationId, String attachedMac, long localPort) {
396         /*
397          * Table(0) Rule #3
398          * ----------------
399          * Match: VM sMac and Local Ingress Port
400          * Action:Action: Set Tunnel ID and GOTO Local Table (5)
401          */
402
403         handleLocalInPort(dpid, TABLE_0_DEFAULT_INGRESS, TABLE_1_ISOLATE_TENANT, segmentationId, localPort, attachedMac, false);
404
405         /*
406          * Table(0) Rule #4
407          * ----------------
408          * Match: Drop any remaining Ingress Local VM Packets
409          * Action: Drop w/ a low priority
410          */
411
412         handleDropSrcIface(dpid, localPort, false);
413
414         /*
415          * Table(2) Rule #1
416          * ----------------
417          * Match: Match TunID and Destination DL/dMAC Addr
418          * Action: Output Port
419          * table=2,tun_id=0x5,dl_dst=00:00:00:00:00:01 actions=output:2
420          */
421
422         handleLocalUcastOut(dpid, TABLE_2_LOCAL_FORWARD, segmentationId, localPort, attachedMac, false);
423
424         /*
425          * Table(2) Rule #2
426          * ----------------
427          * Match: Tunnel ID and dMAC (::::FF:FF)
428          * table=2,priority=16384,tun_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
429          * actions=output:2,3,4,5
430          */
431
432         handleLocalBcastOut(dpid, TABLE_2_LOCAL_FORWARD, segmentationId, localPort, false);
433         handleTunnelFloodOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD, segmentationId, localPort, false);
434     }
435
436     private void programLocalIngressTunnelBridgeRules(Node node, Long dpid, String segmentationId, String attachedMac, long tunnelOFPort, long localPort) {
437         /*
438          * Table(0) Rule #2
439          * ----------------
440          * Match: Ingress Port, Tunnel ID
441          * Action: GOTO Local Table (20)
442          */
443
444         handleTunnelIn(dpid, TABLE_0_DEFAULT_INGRESS, TABLE_2_LOCAL_FORWARD, segmentationId, tunnelOFPort, true);
445
446         /*
447          * Table(1) Rule #2
448          * ----------------
449          * Match: Match Tunnel ID and L2 ::::FF:FF Flooding
450          * Action: Flood to selected destination TEPs
451          * -------------------------------------------
452          * table=1,priority=16384,tun_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
453          * actions=output:10,output:11,goto_table:2
454          */
455
456         handleTunnelFloodOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD, segmentationId, tunnelOFPort, true);
457
458     }
459
460     private void programRemoteEgressTunnelBridgeRules(Node node, Long dpid, String segmentationId, String attachedMac, long tunnelOFPort, long localPort) {
461         /*
462          * Table(1) Rule #1
463          * ----------------
464          * Match: Drop any remaining Ingress Local VM Packets
465          * Action: Drop w/ a low priority
466          * -------------------------------------------
467          * table=1,tun_id=0x5,dl_dst=00:00:00:00:00:08 \
468          * actions=output:11,goto_table:2
469          */
470
471         handleTunnelOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD, segmentationId, tunnelOFPort, attachedMac, true);
472     }
473
474     private void removeRemoteEgressTunnelBridgeRules(Node node, Long dpid, String segmentationId, String attachedMac, long tunnelOFPort, long localPort) {
475         /*
476          * Table(1) Rule #1
477          * ----------------
478          * Match: Drop any remaining Ingress Local VM Packets
479          * Action: Drop w/ a low priority
480          * -------------------------------------------
481          * table=1,tun_id=0x5,dl_dst=00:00:00:00:00:08 \
482          * actions=output:11,goto_table:2
483          */
484
485         handleTunnelOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD, segmentationId, tunnelOFPort, attachedMac, false);
486     }
487
488     /* Remove tunnel rules if last node in this tenant network */
489     private void removePerTunnelRules(Node node, Long dpid, String segmentationId, long tunnelOFPort) {
490         /*
491          * TODO : Optimize the following 2 writes to be restricted only for the very first port known in a segment.
492          */
493         /*
494          * Table(1) Rule #3
495          * ----------------
496          * Match:  Any remaining Ingress Local VM Packets
497          * Action: Drop w/ a low priority
498          * -------------------------------------------
499          * table=1,priority=8192,tun_id=0x5 actions=goto_table:2
500          */
501
502         handleTunnelMiss(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD, segmentationId, false);
503
504         /*
505          * Table(2) Rule #3
506          * ----------------
507          * Match: Any Remaining Flows w/a TunID
508          * Action: Drop w/ a low priority
509          * table=2,priority=8192,tun_id=0x5 actions=drop
510          */
511
512         handleLocalTableMiss(dpid, TABLE_2_LOCAL_FORWARD, segmentationId, false);
513
514         /*
515          * Table(0) Rule #2
516          * ----------------
517          * Match: Ingress Port, Tunnel ID
518          * Action: GOTO Local Table (10)
519          */
520
521         handleTunnelIn(dpid, TABLE_0_DEFAULT_INGRESS, TABLE_2_LOCAL_FORWARD, segmentationId, tunnelOFPort, false);
522
523         /*
524          * Table(1) Rule #2
525          * ----------------
526          * Match: Match Tunnel ID and L2 ::::FF:FF Flooding
527          * Action: Flood to selected destination TEPs
528          * -------------------------------------------
529          * table=1,priority=16384,tun_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
530          * actions=output:10,output:11,goto_table:2
531          */
532
533         handleTunnelFloodOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD, segmentationId, tunnelOFPort, false);
534     }
535
536     private void programLocalVlanRules(Node node, Long dpid, String segmentationId, String attachedMac, long localPort) {
537         /*
538          * Table(0) Rule #1
539          * ----------------
540          * Tag traffic coming from the local port and vm srcmac
541          * Match: VM sMac and Local Ingress Port
542          * Action: Set VLAN ID and GOTO Local Table 1
543          */
544
545         handleLocalInPortSetVlan(dpid, TABLE_0_DEFAULT_INGRESS,
546                 TABLE_1_ISOLATE_TENANT, segmentationId, localPort,
547                 attachedMac, true);
548
549         /*
550          * Table(0) Rule #3
551          * ----------------
552          * Drop all other traffic coming from the local port
553          * Match: Drop any remaining Ingress Local VM Packets
554          * Action: Drop w/ a low priority
555          */
556
557         handleDropSrcIface(dpid, localPort, true);
558
559         /*
560          * Table(2) Rule #1
561          * ----------------
562          * Forward unicast traffic destined to the local port after stripping tag
563          * Match: Match VLAN ID and Destination DL/dMAC Addr
564          * Action: strip vlan, output to local port
565          * Example: table=2,vlan_id=0x5,dl_dst=00:00:00:00:00:01 actions= strip vlan, output:2
566          */
567
568         handleLocalVlanUcastOut(dpid, TABLE_2_LOCAL_FORWARD, segmentationId,
569                 localPort, attachedMac, true);
570
571         /*
572          * Table(2) Rule #2
573          * ----------------
574          * Match: VLAN ID and dMAC (::::FF:FF)
575          * Action: strip vlan, output to all local ports in this vlan
576          * Example: table=2,priority=16384,vlan_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
577          * actions= strip_vlan, output:2,3,4,5
578          */
579
580         //handleLocalVlanBcastOut(dpid, TABLE_2_LOCAL_FORWARD, segmentationId,
581         //        localPort, ethPort, true);
582         //handleVlanFloodOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD,
583         //        segmentationId, localPort, ethport, true);
584
585         /*
586          * Table(2) Rule #3
587          * ----------------
588          * Match: Any Remaining Flows w/a VLAN ID
589          * Action: Drop w/ a low priority
590          * Example: table=2,priority=8192,vlan_id=0x5 actions=drop
591          */
592
593         //handleLocalVlanTableMiss(dpid, TABLE_2_LOCAL_FORWARD, segmentationId,
594         //        true);
595     }
596
597     private void removeLocalVlanRules(Node node, Long dpid,
598                                       String segmentationId, String attachedMac, long localPort) {
599         /*
600          * Table(0) Rule #1
601          * ----------------
602          * Match: VM sMac and Local Ingress Port
603          * Action: Set VLAN ID and GOTO Local Table 1
604          */
605
606         handleLocalInPortSetVlan(dpid, TABLE_0_DEFAULT_INGRESS,
607                 TABLE_1_ISOLATE_TENANT, segmentationId, localPort,
608                 attachedMac, false);
609
610         /*
611          * Table(0) Rule #3
612          * ----------------
613          * Match: Drop any remaining Ingress Local VM Packets
614          * Action: Drop w/ a low priority
615          */
616
617         handleDropSrcIface(dpid, localPort, false);
618
619         /*
620          * Table(2) Rule #1
621          * ----------------
622          * Match: Match VLAN ID and Destination DL/dMAC Addr
623          * Action: strip vlan, output to local port
624          * Example: table=2,vlan_id=0x5,dl_dst=00:00:00:00:00:01 actions= strip vlan, output:2
625          */
626
627         handleLocalVlanUcastOut(dpid, TABLE_2_LOCAL_FORWARD, segmentationId,
628                 localPort, attachedMac, false);
629
630         /*
631          * Table(2) Rule #2
632          * ----------------
633          * Match: VLAN ID and dMAC (::::FF:FF)
634          * Action: strip vlan, output to all local ports in this vlan
635          * Example: table=2,priority=16384,vlan_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
636          * actions= strip_vlan, output:2,3,4,5
637          */
638
639         //handleLocalVlanBcastOut(dpid, TABLE_2_LOCAL_FORWARD, segmentationId,
640         //        localPort, ethPort, false);
641         //handleVlanFloodOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD,
642         //        segmentationId, localPort, false);
643
644     }
645
646     private void programLocalIngressVlanRules(Node node, Long dpid, String segmentationId, String attachedMac,
647                                               long localPort, long ethPort) {
648         /*
649          * Table(0) Rule #2
650          * ----------------
651          * Match: Ingress port = physical interface, Vlan ID
652          * Action: GOTO Local Table 2
653          */
654
655         handleVlanIn(dpid, TABLE_0_DEFAULT_INGRESS, TABLE_2_LOCAL_FORWARD,
656                 segmentationId, ethPort, true);
657
658         /*
659          * Table(1) Rule #2
660          * ----------------
661          * Match: Match VLAN ID and L2 ::::FF:FF Flooding
662          * Action: Flood to local and remote VLAN members
663          * -------------------------------------------
664          * Example: table=1,priority=16384,vlan_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
665          * actions=output:10 (eth port),goto_table:2
666          * table=110, priority=16384,dl_vlan=2001,dl_dst=01:00:00:00:00:00/01:00:00:00:00:00 actions=output:2,pop_vlan,output:1,output:3,output:4
667          */
668
669         handleLocalVlanBcastOut(dpid, TABLE_2_LOCAL_FORWARD, segmentationId, localPort, ethPort, true);
670
671         /*
672          * Table(1) Rule #2
673          * ----------------
674          * Match: Match VLAN ID and L2 ::::FF:FF Flooding
675          * Action: Flood to local and remote VLAN members
676          * -------------------------------------------
677          * Example: table=1,priority=16384,vlan_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
678          * actions=output:10 (eth port),goto_table:2
679          */
680
681         //handleVlanFloodOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD,
682         //        segmentationId, ethPort, true);
683     }
684
685     private void programRemoteEgressVlanRules(Node node, Long dpid, String segmentationId,
686                                               String attachedMac, long ethPort) {
687         /*
688          * Table(1) Rule #1
689          * ----------------
690          * Match: Destination MAC is local VM MAC and vlan id
691          * Action: go to table 2
692          * -------------------------------------------
693          * Example: table=1,vlan_id=0x5,dl_dst=00:00:00:00:00:08 \
694          * actions=goto_table:2
695          */
696
697         //handleVlanOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD,
698         //        segmentationId, ethPort, attachedMac, true);
699
700         /*
701          * Table(1) Rule #3
702          * ----------------
703          * Match:  VLAN ID
704          * Action: Go to table 2
705          * -------------------------------------------
706          * Example: table=1,priority=8192,vlan_id=0x5 actions=output:1,goto_table:2
707          * table=110,priority=8192,dl_vlan=2001 actions=output:2
708          */
709
710         handleVlanMiss(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD, segmentationId, ethPort, true);
711     }
712
713     private void removeRemoteEgressVlanRules(Node node, Long dpid, String segmentationId,
714                                              String attachedMac, long localPort, long ethPort) {
715         /*
716          * Table(1) Rule #1
717          * ----------------
718          * Match: Destination MAC is local VM MAC and vlan id
719          * Action: go to table 2
720          * -------------------------------------------
721          * Example: table=1,vlan_id=0x5,dl_dst=00:00:00:00:00:08 \
722          * actions=goto_table:2
723          */
724
725         //handleVlanOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD,
726         //        segmentationId, ethPort, attachedMac, false);
727
728         /*
729          * Table(1) Rule #2
730          * ----------------
731          * Match: Match VLAN ID and L2 ::::FF:FF Flooding
732          * Action: Flood to local and remote VLAN members
733          * -------------------------------------------
734          * Example: table=1,priority=16384,vlan_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
735          * actions=output:10 (eth port),goto_table:2
736          * table=110, priority=16384,dl_vlan=2001,dl_dst=01:00:00:00:00:00/01:00:00:00:00:00 actions=output:2,pop_vlan,output:1,output:3,output:4
737          */
738
739         handleLocalVlanBcastOut(dpid, TABLE_2_LOCAL_FORWARD, segmentationId, localPort, ethPort, false);
740     }
741
742     private void removePerVlanRules(Node node, Long dpid, String segmentationId, long localPort, long ethPort) {
743         /*
744          * Table(2) Rule #3
745          * ----------------
746          * Match: Any Remaining Flows w/a VLAN ID
747          * Action: Drop w/ a low priority
748          * Example: table=2,priority=8192,vlan_id=0x5 actions=drop
749          */
750
751         //handleLocalVlanTableMiss(dpid, TABLE_2_LOCAL_FORWARD, segmentationId, false);
752
753         /*
754          * Table(0) Rule #2
755          * ----------------
756          * Match: Ingress port = physical interface, Vlan ID
757          * Action: GOTO Local Table 2
758          */
759
760         handleVlanIn(dpid, TABLE_0_DEFAULT_INGRESS, TABLE_2_LOCAL_FORWARD, segmentationId, ethPort, false);
761
762         /*
763          * Table(1) Rule #2
764          * ----------------
765          * Match: Match VLAN ID and L2 ::::FF:FF Flooding
766          * Action: Flood to local and remote VLAN members
767          * -------------------------------------------
768          * Example: table=1,priority=16384,vlan_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
769          * actions=output:10 (eth port),goto_table:2
770          * table=110, priority=16384,dl_vlan=2001,dl_dst=01:00:00:00:00:00/01:00:00:00:00:00 actions=output:2,pop_vlan,output:1,output:3,output:4
771          */
772
773         //handleLocalVlanBcastOut(dpid, TABLE_2_LOCAL_FORWARD, segmentationId, localPort, ethPort, false);
774
775         /*
776          * Table(1) Rule #2
777          * ----------------
778          * Match: Match VLAN ID and L2 ::::FF:FF Flooding
779          * Action: Flood to local and remote VLAN members
780          * -------------------------------------------
781          * Example: table=1,priority=16384,vlan_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
782          * actions=output:10 (eth port),goto_table:2
783          */
784
785         //handleVlanFloodOut(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD,
786         //        segmentationId, ethPort, false);
787
788         /*
789          * Table(1) Rule #3
790          * ----------------
791          * Match:  VLAN ID
792          * Action: Go to table 2
793          * -------------------------------------------
794          * Example: table=1,priority=8192,vlan_id=0x5 actions=output:1,goto_table:2
795          * table=110,priority=8192,dl_vlan=2001 actions=output:2
796          */
797
798         handleVlanMiss(dpid, TABLE_1_ISOLATE_TENANT, TABLE_2_LOCAL_FORWARD, segmentationId, ethPort, false);
799     }
800
801     private Long getDpid (Node node, String bridgeUuid) {
802         Preconditions.checkNotNull(ovsdbConfigurationService);
803         try {
804             Row bridgeRow =  ovsdbConfigurationService
805                     .getRow(node, ovsdbConfigurationService.getTableName(node, Bridge.class), bridgeUuid);
806             Bridge bridge = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeRow);
807             Set<String> dpids = bridge.getDatapathIdColumn().getData();
808             if (dpids == null || dpids.size() == 0) return 0L;
809             return HexEncode.stringToLong((String) dpids.toArray()[0]);
810         } catch (Exception e) {
811             logger.error("Error finding Bridge's OF DPID", e);
812             return 0L;
813         }
814     }
815
816     private Long getIntegrationBridgeOFDPID (Node node) {
817         try {
818             String bridgeName = configurationService.getIntegrationBridgeName();
819             String brIntId = this.getInternalBridgeUUID(node, bridgeName);
820             if (brIntId == null) {
821                 logger.error("Unable to spot Bridge Identifier for {} in {}", bridgeName, node);
822                 return 0L;
823             }
824
825             return getDpid(node, brIntId);
826         } catch (Exception e) {
827             logger.error("Error finding Integration Bridge's OF DPID", e);
828             return 0L;
829         }
830     }
831
832     private Long getExternalBridgeDpid (Node node) {
833         try {
834             String bridgeName = configurationService.getExternalBridgeName();
835             String brUuid = this.getInternalBridgeUUID(node, bridgeName);
836             if (brUuid == null) {
837                 logger.error("Unable to spot Bridge Identifier for {} in {}", bridgeName, node);
838                 return 0L;
839             }
840
841             return getDpid(node, brUuid);
842         } catch (Exception e) {
843             logger.error("Error finding External Bridge's OF DPID", e);
844             return 0L;
845         }
846     }
847
848     private void programLocalRules (String networkType, String segmentationId, Node node, Interface intf) {
849         try {
850             Long dpid = this.getIntegrationBridgeOFDPID(node);
851             if (dpid == 0L) {
852                 logger.debug("Openflow Datapath-ID not set for the integration bridge in {}", node);
853                 return;
854             }
855
856             Set<Long> of_ports = intf.getOpenFlowPortColumn().getData();
857             if (of_ports == null || of_ports.size() <= 0) {
858                 logger.debug("Could NOT Identify OF value for port {} on {}", intf.getName(), node);
859                 return;
860             }
861             long localPort = (Long)of_ports.toArray()[0];
862
863             Map<String, String> externalIds = intf.getExternalIdsColumn().getData();
864             if (externalIds == null) {
865                 logger.error("No external_ids seen in {}", intf);
866                 return;
867             }
868
869             String attachedMac = externalIds.get(Constants.EXTERNAL_ID_VM_MAC);
870             if (attachedMac == null) {
871                 logger.error("No AttachedMac seen in {}", intf);
872                 return;
873             }
874
875             /* Program local rules based on network type */
876             if (networkType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
877                 logger.debug("Program local vlan rules for interface {}", intf.getName());
878                 programLocalVlanRules(node, dpid, segmentationId, attachedMac, localPort);
879             }
880             /* If the network type is tunnel based (VXLAN/GRRE/etc) with Neutron Port Security ACLs */
881             if ((networkType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE) || networkType.equalsIgnoreCase
882                     (NetworkHandler.NETWORK_TYPE_VXLAN)) && securityServicesManager.isPortSecurityReady(intf)) {
883                 logger.debug("Neutron port has a Port Security Group");
884                 /* Retrieve the security group UUID from the Neutron Port */
885                 NeutronSecurityGroup securityGroupInPort = securityServicesManager.getSecurityGroupInPort(intf);
886                 logger.debug("Program Local rules for networkType: {} does contain a Port Security Group: {} " +
887                         "to be installed on DPID: {}", networkType, securityGroupInPort, dpid);
888                 ingressAclProvider.programPortSecurityACL(node, dpid, segmentationId, attachedMac, localPort,
889                         securityGroupInPort);
890                 egressAclProvider.programPortSecurityACL(node, dpid, segmentationId, attachedMac, localPort,
891                         securityGroupInPort);
892             }
893             if (networkType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE) ||
894                     networkType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN)) {
895                 logger.debug("Program local bridge rules for interface {}", intf.getName());
896                 programLocalBridgeRules(node, dpid, segmentationId, attachedMac, localPort);
897             }
898         } catch (Exception e) {
899             logger.error("Exception in programming Local Rules for "+intf+" on "+node, e);
900         }
901     }
902
903     private void removeLocalRules (String networkType, String segmentationId, Node node, Interface intf) {
904         try {
905             Long dpid = this.getIntegrationBridgeOFDPID(node);
906             if (dpid == 0L) {
907                 logger.debug("Openflow Datapath-ID not set for the integration bridge in {}", node);
908                 return;
909             }
910
911             Set<Long> of_ports = intf.getOpenFlowPortColumn().getData();
912             if (of_ports == null || of_ports.size() <= 0) {
913                 logger.debug("Could NOT Identify OF value for port {} on {}", intf.getName(), node);
914                 return;
915             }
916             long localPort = (Long)of_ports.toArray()[0];
917
918             Map<String, String> externalIds = intf.getExternalIdsColumn().getData();
919             if (externalIds == null) {
920                 logger.error("No external_ids seen in {}", intf);
921                 return;
922             }
923
924             String attachedMac = externalIds.get(Constants.EXTERNAL_ID_VM_MAC);
925             if (attachedMac == null) {
926                 logger.error("No AttachedMac seen in {}", intf);
927                 return;
928             }
929
930             /* Program local rules based on network type */
931             if (networkType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
932                 logger.debug("Remove local vlan rules for interface {}", intf.getName());
933                 removeLocalVlanRules(node, dpid, segmentationId, attachedMac, localPort);
934             } else if (networkType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE) ||
935                     networkType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN)) {
936                 logger.debug("Remove local bridge rules for interface {}", intf.getName());
937                 removeLocalBridgeRules(node, dpid, segmentationId, attachedMac, localPort);
938             }
939         } catch (Exception e) {
940             logger.error("Exception in removing Local Rules for "+intf+" on "+node, e);
941         }
942     }
943
944     private void programTunnelRules (String tunnelType, String segmentationId, InetAddress dst, Node node,
945             Interface intf, boolean local) {
946
947         Preconditions.checkNotNull(ovsdbConfigurationService);
948
949         try {
950
951             Long dpid = this.getIntegrationBridgeOFDPID(node);
952             if (dpid == 0L) {
953                 logger.debug("Openflow Datapath-ID not set for the integration bridge in {}", node);
954                 return;
955             }
956
957             Set<Long> of_ports = intf.getOpenFlowPortColumn().getData();
958             if (of_ports == null || of_ports.size() <= 0) {
959                 logger.debug("Could NOT Identify OF value for port {} on {}", intf.getName(), node);
960                 return;
961             }
962             long localPort = (Long)of_ports.toArray()[0];
963
964             Map<String, String> externalIds = intf.getExternalIdsColumn().getData();
965             if (externalIds == null) {
966                 logger.error("No external_ids seen in {}", intf);
967                 return;
968             }
969
970             String attachedMac = externalIds.get(Constants.EXTERNAL_ID_VM_MAC);
971             if (attachedMac == null) {
972                 logger.error("No AttachedMac seen in {}", intf);
973                 return;
974             }
975
976             Map<String, Row> intfs = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Interface.class));
977             if (intfs != null) {
978                 for (Row row : intfs.values()) {
979                     Interface tunIntf = ovsdbConfigurationService.getTypedRow(node, Interface.class, row);
980                     if (tunIntf.getName().equals(this.getTunnelName(tunnelType, dst))) {
981                         of_ports = tunIntf.getOpenFlowPortColumn().getData();
982                         if (of_ports == null || of_ports.size() <= 0) {
983                             logger.debug("Could NOT Identify Tunnel port {} on {}", tunIntf.getName(), node);
984                             continue;
985                         }
986                         long tunnelOFPort = (Long)of_ports.toArray()[0];
987
988                         if (tunnelOFPort == -1) {
989                             logger.error("Could NOT Identify Tunnel port {} -> OF ({}) on {}", tunIntf.getName(), tunnelOFPort, node);
990                             return;
991                         }
992                         logger.debug("Identified Tunnel port {} -> OF ({}) on {}", tunIntf.getName(), tunnelOFPort, node);
993
994                         if (!local) {
995                             programRemoteEgressTunnelBridgeRules(node, dpid, segmentationId, attachedMac, tunnelOFPort, localPort);
996                         }
997                         logger.trace("program local ingress tunnel rules: node" + node.getNodeIDString() + " intf " + intf.getName());
998                         if (local) {
999                             programLocalIngressTunnelBridgeRules(node, dpid, segmentationId, attachedMac, tunnelOFPort, localPort);
1000                         }
1001                         return;
1002                     }
1003                 }
1004             }
1005         } catch (Exception e) {
1006             logger.error("", e);
1007         }
1008     }
1009
1010     private void removeTunnelRules (String tunnelType, String segmentationId, InetAddress dst, Node node,
1011             Interface intf, boolean local, boolean isLastInstanceOnNode) {
1012
1013         Preconditions.checkNotNull(ovsdbConfigurationService);
1014         try {
1015
1016             Long dpid = this.getIntegrationBridgeOFDPID(node);
1017             if (dpid == 0L) {
1018                 logger.debug("Openflow Datapath-ID not set for the integration bridge in {}", node);
1019                 return;
1020             }
1021
1022             Set<Long> of_ports = intf.getOpenFlowPortColumn().getData();
1023             if (of_ports == null || of_ports.size() <= 0) {
1024                 logger.error("Could NOT Identify OF value for port {} on {}", intf.getName(), node);
1025                 return;
1026             }
1027             long localPort = (Long)of_ports.toArray()[0];
1028
1029             Map<String, String> externalIds = intf.getExternalIdsColumn().getData();
1030             if (externalIds == null) {
1031                 logger.error("No external_ids seen in {}", intf);
1032                 return;
1033             }
1034
1035             String attachedMac = externalIds.get(Constants.EXTERNAL_ID_VM_MAC);
1036             if (attachedMac == null) {
1037                 logger.error("No AttachedMac seen in {}", intf);
1038                 return;
1039             }
1040
1041             Map<String, Row> intfs = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Interface.class));
1042             if (intfs != null) {
1043                 for (Row row : intfs.values()) {
1044                     Interface tunIntf = ovsdbConfigurationService.getTypedRow(node, Interface.class, row);
1045                     if (tunIntf.getName().equals(this.getTunnelName(tunnelType, dst))) {
1046                         of_ports = tunIntf.getOpenFlowPortColumn().getData();
1047                         if (of_ports == null || of_ports.size() <= 0) {
1048                             logger.error("Could NOT Identify Tunnel port {} on {}", tunIntf.getName(), node);
1049                             continue;
1050                         }
1051                         long tunnelOFPort = (Long)of_ports.toArray()[0];
1052
1053                         if (tunnelOFPort == -1) {
1054                             logger.error("Could NOT Identify Tunnel port {} -> OF ({}) on {}", tunIntf.getName(), tunnelOFPort, node);
1055                             return;
1056                         }
1057                         logger.debug("Identified Tunnel port {} -> OF ({}) on {}", tunIntf.getName(), tunnelOFPort, node);
1058
1059                         if (!local) {
1060                             removeRemoteEgressTunnelBridgeRules(node, dpid, segmentationId, attachedMac, tunnelOFPort, localPort);
1061                         }
1062                         if (local && isLastInstanceOnNode) {
1063                             removePerTunnelRules(node, dpid, segmentationId, tunnelOFPort);
1064                         }
1065                         return;
1066                     }
1067                 }
1068             }
1069         } catch (Exception e) {
1070             logger.error("", e);
1071         }
1072     }
1073
1074     private void programVlanRules (NeutronNetwork network, Node node, Interface intf) {
1075         Preconditions.checkNotNull(ovsdbConfigurationService);
1076         logger.debug("Program vlan rules for interface {}", intf.getName());
1077         try {
1078
1079             Long dpid = this.getIntegrationBridgeOFDPID(node);
1080             if (dpid == 0L) {
1081                 logger.debug("Openflow Datapath-ID not set for the integration bridge in {}", node);
1082                 return;
1083             }
1084
1085             Set<Long> of_ports = intf.getOpenFlowPortColumn().getData();
1086             int timeout = 6;
1087             while ((of_ports == null) && (timeout > 0)) {
1088                 of_ports = intf.getOpenFlowPortColumn().getData();
1089                 if (of_ports == null || of_ports.size() <= 0) {
1090                     // Wait for the OVSDB update to sync up the Local cache.
1091                     Thread.sleep(500);
1092                     timeout--;
1093                 }
1094             }
1095             if (of_ports == null || of_ports.size() <= 0) {
1096                 logger.error("Could NOT Identify OF value for port {} on {}", intf.getName(), node);
1097                 return;
1098             }
1099             long localPort = (Long)of_ports.toArray()[0];
1100
1101             Map<String, String> externalIds = intf.getExternalIdsColumn().getData();
1102             if (externalIds == null) {
1103                 logger.error("No external_ids seen in {}", intf);
1104                 return;
1105             }
1106
1107             String attachedMac = externalIds.get(Constants.EXTERNAL_ID_VM_MAC);
1108             if (attachedMac == null) {
1109                 logger.error("No AttachedMac seen in {}", intf);
1110                 return;
1111             }
1112
1113             Map<String, Row> intfs = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Interface.class));
1114             if (intfs != null) {
1115                 for (Row row : intfs.values()) {
1116                     Interface ethIntf = ovsdbConfigurationService.getTypedRow(node, Interface.class, row);
1117                     if (ethIntf.getName().equalsIgnoreCase(bridgeConfigurationManager.getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork()))) {
1118                         of_ports = ethIntf.getOpenFlowPortColumn().getData();
1119                         timeout = 6;
1120                         while ((of_ports == null) && (timeout > 0)) {
1121                             of_ports = ethIntf.getOpenFlowPortColumn().getData();
1122                             if (of_ports == null || of_ports.size() <= 0) {
1123                                 // Wait for the OVSDB update to sync up the Local cache.
1124                                 Thread.sleep(500);
1125                                         timeout--;
1126                             }
1127                         }
1128
1129                         if (of_ports == null || of_ports.size() <= 0) {
1130                             logger.error("Could NOT Identify eth port {} on {}", ethIntf.getName(), node);
1131                             continue;
1132                         }
1133                         long ethOFPort = (Long)of_ports.toArray()[0];
1134
1135                         if (ethOFPort == -1) {
1136                             logger.error("Could NOT Identify eth port {} -> OF ({}) on {}", ethIntf.getName(), ethOFPort, node);
1137                             throw new Exception("port number < 0");
1138                         }
1139                         logger.debug("Identified eth port {} -> OF ({}) on {}", ethIntf.getName(), ethOFPort, node);
1140                         // TODO: add logic to only add rule on remote nodes
1141                         programRemoteEgressVlanRules(node, dpid, network.getProviderSegmentationID(), attachedMac, ethOFPort);
1142                         programLocalIngressVlanRules(node, dpid, network.getProviderSegmentationID(), attachedMac, localPort, ethOFPort);
1143                         return;
1144                     }
1145                 }
1146             }
1147         } catch (Exception e) {
1148             logger.error("", e);
1149         }
1150     }
1151
1152     private void removeVlanRules (NeutronNetwork network, Node node,
1153             Interface intf, boolean isLastInstanceOnNode) {
1154         Preconditions.checkNotNull(ovsdbConfigurationService);
1155         logger.debug("Remove vlan rules for interface {}", intf.getName());
1156
1157         try {
1158
1159             Long dpid = this.getIntegrationBridgeOFDPID(node);
1160             if (dpid == 0L) {
1161                 logger.debug("Openflow Datapath-ID not set for the integration bridge in {}", node);
1162                 return;
1163             }
1164
1165             Set<Long> of_ports = intf.getOpenFlowPortColumn().getData();
1166             if (of_ports == null || of_ports.size() <= 0) {
1167                 logger.error("Could NOT Identify OF value for port {} on {}", intf.getName(), node);
1168                 return;
1169             }
1170             long localPort = (Long)of_ports.toArray()[0];
1171
1172             Map<String, String> externalIds = intf.getExternalIdsColumn().getData();
1173             if (externalIds == null) {
1174                 logger.error("No external_ids seen in {}", intf);
1175                 return;
1176             }
1177
1178             String attachedMac = externalIds.get(Constants.EXTERNAL_ID_VM_MAC);
1179             if (attachedMac == null) {
1180                 logger.error("No AttachedMac seen in {}", intf);
1181                 return;
1182             }
1183
1184             Map<String, Row> intfs = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Interface.class));
1185             if (intfs != null) {
1186                 for (Row row : intfs.values()) {
1187                     Interface ethIntf = ovsdbConfigurationService.getTypedRow(node, Interface.class, row);
1188                     if (ethIntf.getName().equalsIgnoreCase(bridgeConfigurationManager.getPhysicalInterfaceName(node,
1189                             network.getProviderPhysicalNetwork()))) {
1190                         of_ports = ethIntf.getOpenFlowPortColumn().getData();
1191                         if (of_ports == null || of_ports.size() <= 0) {
1192                             logger.error("Could NOT Identify eth port {} on {}", ethIntf.getName(), node);
1193                             continue;
1194                         }
1195                         long ethOFPort = (Long)of_ports.toArray()[0];
1196
1197                         if (ethOFPort == -1) {
1198                             logger.error("Could NOT Identify eth port {} -> OF ({}) on {}", ethIntf.getName(), ethOFPort, node);
1199                             throw new Exception("port number < 0");
1200                         }
1201                         logger.debug("Identified eth port {} -> OF ({}) on {}", ethIntf.getName(), ethOFPort, node);
1202
1203                         removeRemoteEgressVlanRules(node, dpid, network.getProviderSegmentationID(), attachedMac, localPort, ethOFPort);
1204                         if (isLastInstanceOnNode) {
1205                             removePerVlanRules(node, dpid, network.getProviderSegmentationID(), localPort, ethOFPort);
1206                         }
1207                         return;
1208                     }
1209                 }
1210             }
1211         } catch (Exception e) {
1212             logger.error("", e);
1213         }
1214     }
1215
1216     @Override
1217     public Status handleInterfaceUpdate(NeutronNetwork network, Node srcNode, Interface intf) {
1218         Preconditions.checkNotNull(connectionService);
1219         List<Node> nodes = connectionService.getNodes();
1220         nodes.remove(srcNode);
1221         this.programLocalRules(network.getProviderNetworkType(), network.getProviderSegmentationID(), srcNode, intf);
1222
1223         if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
1224             this.programVlanRules(network, srcNode, intf);
1225         } else if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)
1226                 || network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN)){
1227             for (Node dstNode : nodes) {
1228                 InetAddress src = configurationService.getTunnelEndPoint(srcNode);
1229                 InetAddress dst = configurationService.getTunnelEndPoint(dstNode);
1230                 Status status = addTunnelPort(srcNode, network.getProviderNetworkType(), src, dst);
1231                 if (status.isSuccess()) {
1232                     this.programTunnelRules(network.getProviderNetworkType(), network.getProviderSegmentationID(), dst, srcNode, intf, true);
1233                 }
1234                 addTunnelPort(dstNode, network.getProviderNetworkType(), dst, src);
1235                 if (status.isSuccess()) {
1236                     this.programTunnelRules(network.getProviderNetworkType(), network.getProviderSegmentationID(), src, dstNode, intf, false);
1237                 }
1238             }
1239         }
1240
1241         return new Status(StatusCode.SUCCESS);
1242     }
1243
1244     private Status triggerInterfaceUpdates(Node node) {
1245         Preconditions.checkNotNull(ovsdbConfigurationService);
1246         try {
1247             Map<String, Row> intfs = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Interface.class));
1248             if (intfs != null) {
1249                 for (Row row : intfs.values()) {
1250                     Interface intf = ovsdbConfigurationService.getTypedRow(node, Interface.class, row);
1251                     NeutronNetwork network = tenantNetworkManager.getTenantNetwork(intf);
1252                     logger.debug("Trigger Interface update for {}", intf);
1253                     if (network != null) {
1254                         this.handleInterfaceUpdate(network, node, intf);
1255                     }
1256                 }
1257             }
1258         } catch (Exception e) {
1259             logger.error("Error Triggering the lost interface updates for "+ node, e);
1260             return new Status(StatusCode.INTERNALERROR, e.getLocalizedMessage());
1261         }
1262         return new Status(StatusCode.SUCCESS);
1263     }
1264     @Override
1265     public Status handleInterfaceUpdate(String tunnelType, String tunnelKey) {
1266         // TODO Auto-generated method stub
1267         return null;
1268     }
1269
1270     @Override
1271     public Status handleInterfaceDelete(String tunnelType, NeutronNetwork network, Node srcNode, Interface intf,
1272             boolean isLastInstanceOnNode) {
1273         Preconditions.checkNotNull(connectionService);
1274         Status status = new Status(StatusCode.SUCCESS);
1275         List<Node> nodes = connectionService.getNodes();
1276         nodes.remove(srcNode);
1277
1278         logger.info("Delete intf " + intf.getName() + " isLastInstanceOnNode " + isLastInstanceOnNode);
1279         List<String> phyIfName = bridgeConfigurationManager.getAllPhysicalInterfaceNames(srcNode);
1280         if (intf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN)
1281                 || intf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)) {
1282             /* Delete tunnel port */
1283             try {
1284                 Map<String, String> options = intf.getOptionsColumn().getData();
1285                 InetAddress src = InetAddress.getByName(options.get("local_ip"));
1286                 InetAddress dst = InetAddress.getByName(options.get("remote_ip"));
1287                 status = deleteTunnelPort(srcNode, intf.getTypeColumn().getData(), src, dst);
1288             } catch (Exception e) {
1289                 logger.error(e.getMessage(), e);
1290             }
1291         } else if (phyIfName.contains(intf.getName())) {
1292             deletePhysicalPort(srcNode, intf.getName());
1293         } else {
1294             /* delete all other interfaces */
1295             this.removeLocalRules(network.getProviderNetworkType(), network.getProviderSegmentationID(),
1296                     srcNode, intf);
1297
1298             if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
1299                 this.removeVlanRules(network, srcNode,
1300                         intf, isLastInstanceOnNode);
1301             } else if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)
1302                     || network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN)) {
1303
1304                 for (Node dstNode : nodes) {
1305                     InetAddress src = configurationService.getTunnelEndPoint(srcNode);
1306                     InetAddress dst = configurationService.getTunnelEndPoint(dstNode);
1307                     logger.info("Remove tunnel rules for interface " + intf.getName() + " on srcNode " + srcNode.getNodeIDString());
1308                     this.removeTunnelRules(tunnelType, network.getProviderSegmentationID(),
1309                             dst, srcNode, intf, true, isLastInstanceOnNode);
1310                     logger.info("Remove tunnel rules for interface " + intf.getName() + " on dstNode " + dstNode.getNodeIDString());
1311                     this.removeTunnelRules(tunnelType, network.getProviderSegmentationID(),
1312                             src, dstNode, intf, false, isLastInstanceOnNode);
1313                 }
1314             }
1315         }
1316         return status;
1317     }
1318
1319     @Override
1320     public void initializeFlowRules(Node node) {
1321         this.initializeFlowRules(node, configurationService.getIntegrationBridgeName());
1322         this.initializeFlowRules(node, configurationService.getExternalBridgeName());
1323         this.triggerInterfaceUpdates(node);
1324     }
1325
1326     private void initializeFlowRules(Node node, String bridgeName) {
1327         String bridgeUuid = this.getInternalBridgeUUID(node, bridgeName);
1328         if (bridgeUuid == null) {
1329             return;
1330         }
1331
1332         Long dpid = getDpid(node, bridgeUuid);
1333
1334         if (dpid == 0L) {
1335             logger.debug("Openflow Datapath-ID not set for the integration bridge in {}", node);
1336             return;
1337         }
1338
1339         /*
1340          * Table(0) Rule #1
1341          * ----------------
1342          * Match: LLDP (0x88CCL)
1343          * Action: Packet_In to Controller Reserved Port
1344          */
1345
1346         writeLLDPRule(dpid);
1347         if (bridgeName.equals(configurationService.getExternalBridgeName())) {
1348             writeNormalRule(dpid);
1349         }
1350     }
1351
1352     /*
1353      * Create an LLDP Flow Rule to encapsulate into
1354      * a packet_in that is sent to the controller
1355      * for topology handling.
1356      * Match: Ethertype 0x88CCL
1357      * Action: Punt to Controller in a Packet_In msg
1358      */
1359
1360     private void writeLLDPRule(Long dpidLong) {
1361         classifierProvider.programLLDPPuntRule(dpidLong);
1362     }
1363
1364     /*
1365      * Create a NORMAL Table Miss Flow Rule
1366      * Match: any
1367      * Action: forward to NORMAL pipeline
1368      */
1369
1370     private void writeNormalRule(Long dpidLong) {
1371
1372         String nodeName = OPENFLOW + dpidLong;
1373
1374         MatchBuilder matchBuilder = new MatchBuilder();
1375         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
1376         FlowBuilder flowBuilder = new FlowBuilder();
1377
1378         // Create the OF Actions and Instructions
1379         InstructionBuilder ib = new InstructionBuilder();
1380         InstructionsBuilder isb = new InstructionsBuilder();
1381
1382         // Instructions List Stores Individual Instructions
1383         List<Instruction> instructions = Lists.newArrayList();
1384
1385         // Call the InstructionBuilder Methods Containing Actions
1386         InstructionUtils.createNormalInstructions(ib);
1387         ib.setOrder(0);
1388         ib.setKey(new InstructionKey(0));
1389         instructions.add(ib.build());
1390
1391         // Add InstructionBuilder to the Instruction(s)Builder List
1392         isb.setInstruction(instructions);
1393
1394         // Add InstructionsBuilder to FlowBuilder
1395         flowBuilder.setInstructions(isb.build());
1396
1397         String flowId = "NORMAL";
1398         flowBuilder.setId(new FlowId(flowId));
1399         FlowKey key = new FlowKey(new FlowId(flowId));
1400         flowBuilder.setMatch(matchBuilder.build());
1401         flowBuilder.setPriority(0);
1402         flowBuilder.setBarrier(true);
1403         flowBuilder.setTableId((short) 0);
1404         flowBuilder.setKey(key);
1405         flowBuilder.setFlowName(flowId);
1406         flowBuilder.setHardTimeout(0);
1407         flowBuilder.setIdleTimeout(0);
1408         writeFlow(flowBuilder, nodeBuilder);
1409     }
1410
1411     /*
1412      * (Table:0) Ingress Tunnel Traffic
1413      * Match: OpenFlow InPort and Tunnel ID
1414      * Action: GOTO Local Table (10)
1415      * table=0,tun_id=0x5,in_port=10, actions=goto_table:2
1416      */
1417
1418     private void handleTunnelIn(Long dpidLong, Short writeTable,
1419             Short goToTableId, String segmentationId,
1420             Long ofPort, boolean write) {
1421         classifierProvider.programTunnelIn(dpidLong, segmentationId, ofPort, write);
1422     }
1423
1424     /*
1425      * (Table:0) Ingress VLAN Traffic
1426      * Match: OpenFlow InPort and vlan ID
1427      * Action: GOTO Local Table (20)
1428      * table=0,vlan_id=0x5,in_port=10, actions=goto_table:2
1429      */
1430
1431     private void handleVlanIn(Long dpidLong, Short writeTable, Short goToTableId,
1432             String segmentationId,  Long ethPort, boolean write) {
1433         classifierProvider.programVlanIn(dpidLong, segmentationId, ethPort, write);
1434     }
1435
1436     /*
1437      * (Table:0) Egress VM Traffic Towards TEP
1438      * Match: Destination Ethernet Addr and OpenFlow InPort
1439      * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
1440      * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
1441      * actions=set_field:5->tun_id,goto_table=1"
1442      */
1443
1444     private void handleLocalInPort(Long dpidLong, Short writeTable, Short goToTableId,
1445             String segmentationId, Long inPort, String attachedMac,
1446             boolean write) {
1447         classifierProvider.programLocalInPort(dpidLong, segmentationId, inPort, attachedMac, write);
1448     }
1449
1450     /*
1451      * (Table:0) Egress VM Traffic Towards TEP
1452      * Match: Source Ethernet Addr and OpenFlow InPort
1453      * Instruction: Set VLANID and GOTO Table Egress (n)
1454      * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
1455      * actions=push_vlan, set_field:5->vlan_id,goto_table=1"
1456      */
1457
1458     private void handleLocalInPortSetVlan(Long dpidLong, Short writeTable,
1459             Short goToTableId, String segmentationId,
1460             Long inPort, String attachedMac,
1461             boolean write) {
1462         classifierProvider.programLocalInPortSetVlan(dpidLong, segmentationId, inPort, attachedMac, write);
1463     }
1464
1465     /*
1466      * (Table:0) Drop frames source from a VM that do not
1467      * match the associated MAC address of the local VM.
1468      * Match: Low priority anything not matching the VM SMAC
1469      * Instruction: Drop
1470      * table=0,priority=16384,in_port=1 actions=drop"
1471      */
1472
1473     private void handleDropSrcIface(Long dpidLong, Long inPort, boolean write) {
1474         classifierProvider.programDropSrcIface(dpidLong, inPort, write);
1475     }
1476
1477     /*
1478      * (Table:1) Egress Tunnel Traffic
1479      * Match: Destination Ethernet Addr and Local InPort
1480      * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
1481      * table=1,tun_id=0x5,dl_dst=00:00:00:00:00:08 \
1482      * actions=output:10,goto_table:2"
1483      */
1484     private void handleTunnelOut(Long dpidLong, Short writeTable,
1485             Short goToTableId, String segmentationId,
1486             Long OFPortOut, String attachedMac,
1487             boolean write) {
1488         l2ForwardingProvider.programTunnelOut(dpidLong, segmentationId, OFPortOut, attachedMac, write);
1489     }
1490
1491     /*
1492      * (Table:1) Egress VLAN Traffic
1493      * Match: Destination Ethernet Addr and VLAN id
1494      * Instruction: GOTO Table Table 2
1495      * table=1,vlan_id=0x5,dl_dst=00:00:00:00:00:08 \
1496      * actions= goto_table:2"
1497      */
1498
1499     private void handleVlanOut(Long dpidLong, Short writeTable,
1500             Short goToTableId, String segmentationId,
1501             Long ethPort, String attachedMac, boolean write) {
1502         l2ForwardingProvider.programVlanOut(dpidLong, segmentationId, ethPort, attachedMac, write);
1503     }
1504
1505     /*
1506      * (Table:1) Egress Tunnel Traffic
1507      * Match: Destination Ethernet Addr and Local InPort
1508      * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
1509      * table=1,priority=16384,tun_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
1510      * actions=output:10,output:11,goto_table:2
1511      */
1512
1513     private void handleTunnelFloodOut(Long dpidLong, Short writeTable,
1514             Short localTable, String segmentationId,
1515             Long OFPortOut, boolean write) {
1516         l2ForwardingProvider.programTunnelFloodOut(dpidLong, segmentationId, OFPortOut, write);
1517     }
1518
1519     /*
1520      * (Table:1) Egress VLAN Traffic
1521      * Match: Destination Ethernet Addr and VLAN id
1522      * Instruction: GOTO table 2 and Output port eth interface
1523      * Example: table=1,priority=16384,vlan_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
1524      * actions=output:eth1,goto_table:2
1525      */
1526
1527     private void handleVlanFloodOut(Long dpidLong, Short writeTable,
1528             Short localTable, String segmentationId,
1529             Long localPort, Long ethPort, boolean write) {
1530         //l2ForwardingProvider.programVlanFloodOut(dpidLong, segmentationId, localPort, ethPort, write);
1531     }
1532
1533     /*
1534      * (Table:1) Table Drain w/ Catch All
1535      * Match: Tunnel ID
1536      * Action: GOTO Local Table (10)
1537      * table=2,priority=8192,tun_id=0x5 actions=drop
1538      */
1539
1540     private void handleTunnelMiss(Long dpidLong, Short writeTable,
1541             Short goToTableId, String segmentationId,
1542             boolean write) {
1543         l2ForwardingProvider.programTunnelMiss(dpidLong, segmentationId, write);
1544     }
1545
1546
1547     /*
1548      * (Table:1) Table Drain w/ Catch All
1549      * Match: Vlan ID
1550      * Action: Output port eth interface
1551      * table=1,priority=8192,vlan_id=0x5 actions= output port:eth1
1552      * table=110,priority=8192,dl_vlan=2001 actions=output:2
1553      */
1554
1555     private void handleVlanMiss(Long dpidLong, Short writeTable,
1556             Short goToTableId, String segmentationId,
1557             Long ethPort, boolean write) {
1558         l2ForwardingProvider.programVlanMiss(dpidLong, segmentationId, ethPort, write);
1559     }
1560
1561     /*
1562      * (Table:1) Local Broadcast Flood
1563      * Match: Tunnel ID and dMAC
1564      * Action: Output Port
1565      * table=2,tun_id=0x5,dl_dst=00:00:00:00:00:01 actions=output:2
1566      */
1567
1568     private void handleLocalUcastOut(Long dpidLong, Short writeTable,
1569             String segmentationId, Long localPort,
1570             String attachedMac, boolean write) {
1571         l2ForwardingProvider.programLocalUcastOut(dpidLong, segmentationId, localPort, attachedMac, write);
1572     }
1573
1574     /*
1575      * (Table:2) Local VLAN unicast
1576      * Match: VLAN ID and dMAC
1577      * Action: Output Port
1578      * table=2,vlan_id=0x5,dl_dst=00:00:00:00:00:01 actions=output:2
1579      */
1580
1581     private void handleLocalVlanUcastOut(Long dpidLong, Short writeTable,
1582             String segmentationId, Long localPort,
1583             String attachedMac, boolean write) {
1584         l2ForwardingProvider.programLocalVlanUcastOut(dpidLong, segmentationId, localPort, attachedMac, write);
1585     }
1586
1587     /*
1588      * (Table:2) Local Broadcast Flood
1589      * Match: Tunnel ID and dMAC (::::FF:FF)
1590      * table=2,priority=16384,tun_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
1591      * actions=output:2,3,4,5
1592      */
1593
1594     private void handleLocalBcastOut(Long dpidLong, Short writeTable,
1595             String segmentationId, Long localPort,
1596             boolean write) {
1597         l2ForwardingProvider.programLocalBcastOut(dpidLong, segmentationId, localPort, write);
1598     }
1599
1600     /*
1601      * (Table:2) Local VLAN Broadcast Flood
1602      * Match: vlan ID and dMAC (::::FF:FF)
1603      * table=2,priority=16384,vlan_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
1604      * actions=strip_vlan, output:2,3,4,5
1605      * table=110,dl_vlan=2001,dl_dst=01:00:00:00:00:00/01:00:00:00:00:00 actions=output:2,pop_vlan,output:1,output:3,output:4
1606      */
1607
1608     private void handleLocalVlanBcastOut(Long dpidLong, Short writeTable, String segmentationId,
1609                                          Long localPort, Long ethPort, boolean write) {
1610         l2ForwardingProvider.programLocalVlanBcastOut(dpidLong, segmentationId, localPort, ethPort, write);
1611     }
1612
1613     /*
1614      * (Table:1) Local Table Miss
1615      * Match: Any Remaining Flows w/a TunID
1616      * Action: Drop w/ a low priority
1617      * table=2,priority=8192,tun_id=0x5 actions=drop
1618      */
1619
1620     private void handleLocalTableMiss(Long dpidLong, Short writeTable,
1621             String segmentationId, boolean write) {
1622         l2ForwardingProvider.programLocalTableMiss(dpidLong, segmentationId, write);
1623     }
1624
1625     /*
1626      * (Table:1) Local Table Miss
1627      * Match: Any Remaining Flows w/a VLAN ID
1628      * Action: Drop w/ a low priority
1629      * table=2,priority=8192,vlan_id=0x5 actions=drop
1630      */
1631
1632     private void handleLocalVlanTableMiss(Long dpidLong, Short writeTable,
1633             String segmentationId, boolean write) {
1634         l2ForwardingProvider.programLocalVlanTableMiss(dpidLong, segmentationId, write);
1635     }
1636
1637     private Group getGroup(GroupBuilder groupBuilder, NodeBuilder nodeBuilder) {
1638         Preconditions.checkNotNull(mdsalConsumer);
1639         if (mdsalConsumer == null) {
1640             logger.error("ERROR finding MDSAL Service. Its possible that writeFlow is called too soon ?");
1641             return null;
1642         }
1643
1644         dataBroker = mdsalConsumer.getDataBroker();
1645         if (dataBroker == null) {
1646             logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
1647             return null;
1648         }
1649
1650         InstanceIdentifier<Group> path1 = InstanceIdentifier.builder(Nodes.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory
1651                 .rev130819.nodes.Node.class, nodeBuilder.getKey()).augmentation(FlowCapableNode.class).child(Group.class,
1652                         new GroupKey(groupBuilder.getGroupId())).build();
1653         ReadOnlyTransaction readTx = dataBroker.newReadOnlyTransaction();
1654         try {
1655             Optional<Group> data = readTx.read(LogicalDatastoreType.CONFIGURATION, path1).get();
1656             if (data.isPresent()) {
1657                 return data.get();
1658             }
1659         } catch (InterruptedException|ExecutionException e) {
1660             logger.error(e.getMessage(), e);
1661         }
1662
1663         logger.debug("Cannot find data for Group " + groupBuilder.getGroupName());
1664         return null;
1665     }
1666
1667     private void writeGroup(GroupBuilder groupBuilder, NodeBuilder nodeBuilder) {
1668         Preconditions.checkNotNull(mdsalConsumer);
1669         if (mdsalConsumer == null) {
1670             logger.error("ERROR finding MDSAL Service. Its possible that writeFlow is called too soon ?");
1671             return;
1672         }
1673
1674         dataBroker = mdsalConsumer.getDataBroker();
1675         if (dataBroker == null) {
1676             logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
1677             return;
1678         }
1679
1680         ReadWriteTransaction modification = dataBroker.newReadWriteTransaction();
1681         InstanceIdentifier<Group> path1 = InstanceIdentifier.builder(Nodes.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory
1682                 .rev130819.nodes.Node.class, nodeBuilder.getKey()).augmentation(FlowCapableNode.class).child(Group.class,
1683                         new GroupKey(groupBuilder.getGroupId())).build();
1684         modification.put(LogicalDatastoreType.CONFIGURATION, path1, groupBuilder.build(), true /*createMissingParents*/);
1685
1686         CheckedFuture<Void, TransactionCommitFailedException> commitFuture = modification.submit();
1687         try {
1688             commitFuture.get();  // TODO: Make it async (See bug 1362)
1689             logger.debug("Transaction success for write of Group "+groupBuilder.getGroupName());
1690         } catch (InterruptedException|ExecutionException e) {
1691             logger.error(e.getMessage(), e);
1692         }
1693     }
1694
1695     private void removeGroup(GroupBuilder groupBuilder, NodeBuilder nodeBuilder) {
1696         Preconditions.checkNotNull(mdsalConsumer);
1697         if (mdsalConsumer == null) {
1698             logger.error("ERROR finding MDSAL Service. Its possible that writeFlow is called too soon ?");
1699             return;
1700         }
1701
1702         dataBroker = mdsalConsumer.getDataBroker();
1703         if (dataBroker == null) {
1704             logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
1705             return;
1706         }
1707
1708         WriteTransaction modification = dataBroker.newWriteOnlyTransaction();
1709         InstanceIdentifier<Group> path1 = InstanceIdentifier.builder(Nodes.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory
1710                 .rev130819.nodes.Node.class, nodeBuilder.getKey()).augmentation(FlowCapableNode.class).child(Group.class,
1711                         new GroupKey(groupBuilder.getGroupId())).build();
1712         modification.delete(LogicalDatastoreType.CONFIGURATION, path1);
1713         CheckedFuture<Void, TransactionCommitFailedException> commitFuture = modification.submit();
1714
1715         try {
1716             commitFuture.get();  // TODO: Make it async (See bug 1362)
1717             logger.debug("Transaction success for deletion of Group "+groupBuilder.getGroupName());
1718         } catch (InterruptedException|ExecutionException e) {
1719             logger.error(e.getMessage(), e);
1720         }
1721     }
1722     private Flow getFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
1723         Preconditions.checkNotNull(mdsalConsumer);
1724         if (mdsalConsumer == null) {
1725             logger.error("ERROR finding MDSAL Service. Its possible that writeFlow is called too soon ?");
1726             return null;
1727         }
1728
1729         dataBroker = mdsalConsumer.getDataBroker();
1730         if (dataBroker == null) {
1731             logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
1732             return null;
1733         }
1734
1735         InstanceIdentifier<Flow> path1 = InstanceIdentifier.builder(Nodes.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory
1736                 .rev130819.nodes.Node.class, nodeBuilder.getKey()).augmentation(FlowCapableNode.class).child(Table.class,
1737                         new TableKey(flowBuilder.getTableId())).child(Flow.class, flowBuilder.getKey()).build();
1738
1739         ReadOnlyTransaction readTx = dataBroker.newReadOnlyTransaction();
1740         try {
1741             Optional<Flow> data = readTx.read(LogicalDatastoreType.CONFIGURATION, path1).get();
1742             if (data.isPresent()) {
1743                 return data.get();
1744             }
1745         } catch (InterruptedException|ExecutionException e) {
1746             logger.error(e.getMessage(), e);
1747         }
1748
1749         logger.debug("Cannot find data for Flow " + flowBuilder.getFlowName());
1750         return null;
1751     }
1752
1753     private void writeFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
1754         Preconditions.checkNotNull(mdsalConsumer);
1755         if (mdsalConsumer == null) {
1756             logger.error("ERROR finding MDSAL Service. Its possible that writeFlow is called too soon ?");
1757             return;
1758         }
1759
1760         dataBroker = mdsalConsumer.getDataBroker();
1761         if (dataBroker == null) {
1762             logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
1763             return;
1764         }
1765
1766         ReadWriteTransaction modification = dataBroker.newReadWriteTransaction();
1767         InstanceIdentifier<Flow> path1 = InstanceIdentifier.builder(Nodes.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory
1768                 .rev130819.nodes.Node.class, nodeBuilder.getKey()).augmentation(FlowCapableNode.class).child(Table.class,
1769                         new TableKey(flowBuilder.getTableId())).child(Flow.class, flowBuilder.getKey()).build();
1770
1771         //modification.put(LogicalDatastoreType.OPERATIONAL, path1, flowBuilder.build());
1772         modification.put(LogicalDatastoreType.CONFIGURATION, path1, flowBuilder.build(), true /*createMissingParents*/);
1773
1774
1775         CheckedFuture<Void, TransactionCommitFailedException> commitFuture = modification.submit();
1776         try {
1777             commitFuture.get();  // TODO: Make it async (See bug 1362)
1778             logger.debug("Transaction success for write of Flow "+flowBuilder.getFlowName());
1779         } catch (InterruptedException|ExecutionException e) {
1780             logger.error(e.getMessage(), e);
1781         }
1782     }
1783
1784     private void removeFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
1785         Preconditions.checkNotNull(mdsalConsumer);
1786         if (mdsalConsumer == null) {
1787             logger.error("ERROR finding MDSAL Service.");
1788             return;
1789         }
1790
1791         dataBroker = mdsalConsumer.getDataBroker();
1792         if (dataBroker == null) {
1793             logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
1794             return;
1795         }
1796
1797         WriteTransaction modification = dataBroker.newWriteOnlyTransaction();
1798         InstanceIdentifier<Flow> path1 = InstanceIdentifier.builder(Nodes.class)
1799                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory
1800                         .rev130819.nodes.Node.class, nodeBuilder.getKey())
1801                         .augmentation(FlowCapableNode.class).child(Table.class,
1802                                 new TableKey(flowBuilder.getTableId())).child(Flow.class, flowBuilder.getKey()).build();
1803         //modification.delete(LogicalDatastoreType.OPERATIONAL, nodeBuilderToInstanceId(nodeBuilder));
1804         //modification.delete(LogicalDatastoreType.OPERATIONAL, path1);
1805         //modification.delete(LogicalDatastoreType.CONFIGURATION, nodeBuilderToInstanceId(nodeBuilder));
1806         modification.delete(LogicalDatastoreType.CONFIGURATION, path1);
1807
1808         CheckedFuture<Void, TransactionCommitFailedException> commitFuture = modification.submit();
1809         try {
1810             commitFuture.get();  // TODO: Make it async (See bug 1362)
1811             logger.debug("Transaction success for deletion of Flow "+flowBuilder.getFlowName());
1812         } catch (InterruptedException|ExecutionException e) {
1813             logger.error(e.getMessage(), e);
1814         }
1815     }
1816
1817     /**
1818      * Create Output Port Group Instruction
1819      *
1820      * @param ib       Map InstructionBuilder without any instructions
1821      * @param dpidLong Long the datapath ID of a switch/node
1822      * @param port     Long representing a port on a switch/node
1823      * @return ib InstructionBuilder Map with instructions
1824      */
1825     protected InstructionBuilder createOutputGroupInstructions(NodeBuilder nodeBuilder,
1826             InstructionBuilder ib,
1827             Long dpidLong, Long port ,
1828             List<Instruction> instructions) {
1829         NodeConnectorId ncid = new NodeConnectorId(OPENFLOW + dpidLong + ":" + port);
1830         logger.debug("createOutputGroupInstructions() Node Connector ID is - Type=openflow: DPID={} port={} existingInstructions={}", dpidLong, port, instructions);
1831
1832         List<Action> actionList = Lists.newArrayList();
1833         ActionBuilder ab = new ActionBuilder();
1834
1835         List<Action> existingActions;
1836         if (instructions != null) {
1837             for (Instruction in : instructions) {
1838                 if (in.getInstruction() instanceof ApplyActionsCase) {
1839                     existingActions = (((ApplyActionsCase) in.getInstruction()).getApplyActions().getAction());
1840                     actionList.addAll(existingActions);
1841                 }
1842             }
1843         }
1844
1845         GroupBuilder groupBuilder = new GroupBuilder();
1846         Group group = null;
1847
1848         /* Create output action for this port*/
1849         OutputActionBuilder oab = new OutputActionBuilder();
1850         oab.setOutputNodeConnector(ncid);
1851         ab.setAction(new OutputActionCaseBuilder().setOutputAction(oab.build()).build());
1852         logger.debug("createOutputGroupInstructions(): output action {}", ab.build());
1853         boolean addNew = true;
1854         boolean groupActionAdded = false;
1855
1856         /* Find the group action and get the group */
1857         for (Action action : actionList) {
1858             if (action.getAction() instanceof GroupActionCase) {
1859                 groupActionAdded = true;
1860                 GroupActionCase groupAction = (GroupActionCase) action.getAction();
1861                 Long id = groupAction.getGroupAction().getGroupId();
1862                 String groupName = groupAction.getGroupAction().getGroup();
1863                 GroupKey key = new GroupKey(new GroupId(id));
1864
1865                 groupBuilder.setGroupId(new GroupId(id));
1866                 groupBuilder.setGroupName(groupName);
1867                 groupBuilder.setGroupType(GroupTypes.GroupAll);
1868                 groupBuilder.setKey(key);
1869                 group = getGroup(groupBuilder, nodeBuilder);
1870                 logger.debug("createOutputGroupInstructions: group {}", group);
1871                 break;
1872             }
1873         }
1874
1875         logger.debug("createOutputGroupInstructions: groupActionAdded {}", groupActionAdded);
1876         if (groupActionAdded) {
1877             /* modify the action bucket in group */
1878             groupBuilder = new GroupBuilder(group);
1879             Buckets buckets = groupBuilder.getBuckets();
1880             for (Bucket bucket : buckets.getBucket()) {
1881                 List<Action> bucketActions = bucket.getAction();
1882                 logger.debug("createOutputGroupInstructions: bucketActions {}", bucketActions);
1883                 for (Action action : bucketActions) {
1884                     if (action.getAction() instanceof OutputActionCase) {
1885                         OutputActionCase opAction = (OutputActionCase)action.getAction();
1886                         /* If output port action already in the action list of one of the buckets, skip */
1887                         if (opAction.getOutputAction().getOutputNodeConnector().equals(new Uri(ncid))) {
1888                             addNew = false;
1889                             break;
1890                         }
1891                     }
1892                 }
1893             }
1894             logger.debug("createOutputGroupInstructions: addNew {}", addNew);
1895             if (addNew) {
1896                 /* the new output action is not in the bucket, add to bucket */
1897                 if (!buckets.getBucket().isEmpty()) {
1898                     Bucket bucket = buckets.getBucket().get(0);
1899                     List<Action> bucketActionList = Lists.newArrayList();
1900                     bucketActionList.addAll(bucket.getAction());
1901                     /* set order for new action and add to action list */
1902                     ab.setOrder(bucketActionList.size());
1903                     ab.setKey(new ActionKey(bucketActionList.size()));
1904                     bucketActionList.add(ab.build());
1905
1906                     /* set bucket and buckets list. Reset groupBuilder with new buckets.*/
1907                     BucketsBuilder bucketsBuilder = new BucketsBuilder();
1908                     List<Bucket> bucketList = Lists.newArrayList();
1909                     BucketBuilder bucketBuilder = new BucketBuilder();
1910                     bucketBuilder.setBucketId(new BucketId((long) 1));
1911                     bucketBuilder.setKey(new BucketKey(new BucketId((long) 1)));
1912                     bucketBuilder.setAction(bucketActionList);
1913                     bucketList.add(bucketBuilder.build());
1914                     bucketsBuilder.setBucket(bucketList);
1915                     groupBuilder.setBuckets(bucketsBuilder.build());
1916                     logger.debug("createOutputGroupInstructions: bucketList {}", bucketList);
1917                 }
1918             }
1919         } else {
1920             /* create group */
1921             groupBuilder = new GroupBuilder();
1922             groupBuilder.setGroupType(GroupTypes.GroupAll);
1923             groupBuilder.setGroupId(new GroupId(groupId));
1924             groupBuilder.setKey(new GroupKey(new GroupId(groupId)));
1925             groupBuilder.setGroupName("Output port group " + groupId);
1926             groupBuilder.setBarrier(false);
1927
1928             BucketsBuilder bucketBuilder = new BucketsBuilder();
1929             List<Bucket> bucketList = Lists.newArrayList();
1930             BucketBuilder bucket = new BucketBuilder();
1931             bucket.setBucketId(new BucketId((long) 1));
1932             bucket.setKey(new BucketKey(new BucketId((long) 1)));
1933
1934             /* put output action to the bucket */
1935             List<Action> bucketActionList = Lists.newArrayList();
1936             /* set order for new action and add to action list */
1937             ab.setOrder(bucketActionList.size());
1938             ab.setKey(new ActionKey(bucketActionList.size()));
1939             bucketActionList.add(ab.build());
1940
1941             bucket.setAction(bucketActionList);
1942             bucketList.add(bucket.build());
1943             bucketBuilder.setBucket(bucketList);
1944             groupBuilder.setBuckets(bucketBuilder.build());
1945
1946             /* Add new group action */
1947             GroupActionBuilder groupActionB = new GroupActionBuilder();
1948             groupActionB.setGroupId(groupId);
1949             groupActionB.setGroup("Output port group " + groupId);
1950             ab = new ActionBuilder();
1951             ab.setAction(new GroupActionCaseBuilder().setGroupAction(groupActionB.build()).build());
1952             ab.setOrder(actionList.size());
1953             ab.setKey(new ActionKey(actionList.size()));
1954             actionList.add(ab.build());
1955
1956             groupId++;
1957         }
1958         logger.debug("createOutputGroupInstructions: group {}", groupBuilder.build());
1959         logger.debug("createOutputGroupInstructions: actionList {}", actionList);
1960
1961         if (addNew) {
1962             /* rewrite the group to group table */
1963             writeGroup(groupBuilder, nodeBuilder);
1964         }
1965
1966         // Create an Apply Action
1967         ApplyActionsBuilder aab = new ApplyActionsBuilder();
1968         aab.setAction(actionList);
1969         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
1970
1971         return ib;
1972     }
1973
1974     /**
1975      * Remove Output Port from action list in group bucket
1976      *
1977      * @param ib       Map InstructionBuilder without any instructions
1978      * @param dpidLong Long the datapath ID of a switch/node
1979      * @param port     Long representing a port on a switch/node
1980      * @return ib InstructionBuilder Map with instructions
1981      */
1982     protected boolean removeOutputPortFromGroup(NodeBuilder nodeBuilder, InstructionBuilder ib,
1983             Long dpidLong, Long port , List<Instruction> instructions) {
1984
1985         NodeConnectorId ncid = new NodeConnectorId(OPENFLOW + dpidLong + ":" + port);
1986         logger.debug("removeOutputPortFromGroup() Node Connector ID is - Type=openflow: DPID={} port={} existingInstructions={}", dpidLong, port, instructions);
1987
1988         List<Action> actionList = Lists.newArrayList();
1989         ActionBuilder ab;
1990
1991         List<Action> existingActions;
1992         if (instructions != null) {
1993             for (Instruction in : instructions) {
1994                 if (in.getInstruction() instanceof ApplyActionsCase) {
1995                     existingActions = (((ApplyActionsCase) in.getInstruction()).getApplyActions().getAction());
1996                     actionList.addAll(existingActions);
1997                     break;
1998                 }
1999             }
2000         }
2001
2002         GroupBuilder groupBuilder = new GroupBuilder();
2003         Group group = null;
2004         boolean groupActionAdded = false;
2005         /* Find the group action and get the group */
2006         for (Action action : actionList) {
2007             if (action.getAction() instanceof GroupActionCase) {
2008                 groupActionAdded = true;
2009                 GroupActionCase groupAction = (GroupActionCase) action.getAction();
2010                 Long id = groupAction.getGroupAction().getGroupId();
2011                 String groupName = groupAction.getGroupAction().getGroup();
2012                 GroupKey key = new GroupKey(new GroupId(id));
2013
2014                 groupBuilder.setGroupId(new GroupId(id));
2015                 groupBuilder.setGroupName(groupName);
2016                 groupBuilder.setGroupType(GroupTypes.GroupAll);
2017                 groupBuilder.setKey(key);
2018                 group = getGroup(groupBuilder, nodeBuilder);
2019                 break;
2020             }
2021         }
2022
2023         if (groupActionAdded) {
2024             /* modify the action bucket in group */
2025             groupBuilder = new GroupBuilder(group);
2026             Buckets buckets = groupBuilder.getBuckets();
2027             List<Action> bucketActions = Lists.newArrayList();
2028             for (Bucket bucket : buckets.getBucket()) {
2029                 int index = 0;
2030                 boolean isPortDeleted = false;
2031                 bucketActions = bucket.getAction();
2032                 for (Action action : bucketActions) {
2033                     if (action.getAction() instanceof OutputActionCase) {
2034                         OutputActionCase opAction = (OutputActionCase)action.getAction();
2035                         if (opAction.getOutputAction().getOutputNodeConnector().equals(new Uri(ncid))) {
2036                             /* Find the output port in action list and remove */
2037                             index = bucketActions.indexOf(action);
2038                             bucketActions.remove(action);
2039                             isPortDeleted = true;
2040                             break;
2041                         }
2042                     }
2043                 }
2044                 if (isPortDeleted && !bucketActions.isEmpty()) {
2045                     for (int i = index; i< bucketActions.size(); i++) {
2046                         Action action = bucketActions.get(i);
2047                         if (action.getOrder() != i) {
2048                             /* Shift the action order */
2049                             ab = new ActionBuilder();
2050                             ab.setAction(action.getAction());
2051                             ab.setOrder(i);
2052                             ab.setKey(new ActionKey(i));
2053                             Action actionNewOrder = ab.build();
2054                             bucketActions.remove(action);
2055                             bucketActions.add(i, actionNewOrder);
2056                         }
2057                     }
2058
2059                 } else if (bucketActions.isEmpty()) {
2060                     /* remove bucket with empty action list */
2061                     buckets.getBucket().remove(bucket);
2062                     break;
2063                 }
2064             }
2065             if (!buckets.getBucket().isEmpty()) {
2066                 /* rewrite the group to group table */
2067                 /* set bucket and buckets list. Reset groupBuilder with new buckets.*/
2068                 BucketsBuilder bucketsBuilder = new BucketsBuilder();
2069                 List<Bucket> bucketList = Lists.newArrayList();
2070                 BucketBuilder bucketBuilder = new BucketBuilder();
2071                 bucketBuilder.setBucketId(new BucketId((long) 1));
2072                 bucketBuilder.setKey(new BucketKey(new BucketId((long) 1)));
2073                 bucketBuilder.setAction(bucketActions);
2074                 bucketList.add(bucketBuilder.build());
2075                 bucketsBuilder.setBucket(bucketList);
2076                 groupBuilder.setBuckets(bucketsBuilder.build());
2077                 logger.debug("removeOutputPortFromGroup: bucketList {}", bucketList);
2078
2079                 writeGroup(groupBuilder, nodeBuilder);
2080                 ApplyActionsBuilder aab = new ApplyActionsBuilder();
2081                 aab.setAction(actionList);
2082                 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
2083                 return false;
2084             } else {
2085                 /* remove group with empty bucket. return true to delete flow */
2086                 removeGroup(groupBuilder, nodeBuilder);
2087                 return true;
2088             }
2089         } else {
2090             /* no group for port list. flow can be removed */
2091             return true;
2092         }
2093     }
2094
2095     @Override
2096     public void initializeOFFlowRules(Node openflowNode) {
2097         Preconditions.checkNotNull(connectionService);
2098         List<Node> ovsNodes = connectionService.getNodes();
2099         if (ovsNodes == null) return;
2100         for (Node ovsNode : ovsNodes) {
2101             Long brIntDpid = this.getIntegrationBridgeOFDPID(ovsNode);
2102             Long brExDpid = this.getExternalBridgeDpid(ovsNode);
2103             logger.debug("Compare openflowNode to OVS node {} vs {} and {}", openflowNode.getID(), brIntDpid, brExDpid);
2104             String openflowID = openflowNode.getID().toString();
2105             if (openflowID.contains(brExDpid.toString())) {
2106                 this.initializeFlowRules(ovsNode, configurationService.getExternalBridgeName());
2107                 this.triggerInterfaceUpdates(ovsNode);
2108             }
2109             if (openflowID.contains(brIntDpid.toString())) {
2110                 this.initializeFlowRules(ovsNode, configurationService.getIntegrationBridgeName());
2111                 this.triggerInterfaceUpdates(ovsNode);
2112             }
2113         }
2114     }
2115
2116     public static NodeBuilder createNodeBuilder(String nodeId) {
2117         NodeBuilder builder = new NodeBuilder();
2118         builder.setId(new NodeId(nodeId));
2119         builder.setKey(new NodeKey(builder.getId()));
2120         return builder;
2121     }
2122
2123     private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node> nodeBuilderToInstanceId(NodeBuilder
2124             node) {
2125         return InstanceIdentifier.builder(Nodes.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class,
2126                 node.getKey()).toInstance();
2127     }
2128
2129     private String getInternalBridgeUUID (Node node, String bridgeName) {
2130         Preconditions.checkNotNull(ovsdbConfigurationService);
2131         try {
2132             Map<String, Row> bridgeTable = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Bridge.class));
2133             if (bridgeTable == null) return null;
2134             for (String key : bridgeTable.keySet()) {
2135                 Bridge bridge = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeTable.get(key));
2136                 if (bridge.getName().equals(bridgeName)) return key;
2137             }
2138         } catch (Exception e) {
2139             logger.error("Error getting Bridge Identifier for {} / {}", node, bridgeName, e);
2140         }
2141         return null;
2142     }
2143 }