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