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