Merge "Remove ComponentActivatorAbstractBase from plugin"
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / impl / BridgeConfigurationManagerImpl.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, Sam Hague
9  */
10 package org.opendaylight.ovsdb.openstack.netvirt.impl;
11
12 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
13 import org.opendaylight.controller.sal.core.Node;
14 import org.opendaylight.controller.sal.utils.Status;
15 import org.opendaylight.controller.sal.utils.StatusCode;
16 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
17 import org.opendaylight.ovsdb.lib.notation.Row;
18 import org.opendaylight.ovsdb.lib.notation.UUID;
19 import org.opendaylight.ovsdb.openstack.netvirt.NetworkHandler;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
23 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProviderManager;
24 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
25 import org.opendaylight.ovsdb.plugin.api.StatusWithUuid;
26 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
27 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
28 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
29 import org.opendaylight.ovsdb.schema.openvswitch.Port;
30
31 import com.google.common.base.Preconditions;
32 import com.google.common.collect.Lists;
33 import com.google.common.collect.Maps;
34 import org.apache.commons.lang3.tuple.ImmutablePair;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import java.util.HashSet;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Set;
42
43 public class BridgeConfigurationManagerImpl implements BridgeConfigurationManager {
44     static final Logger logger = LoggerFactory.getLogger(BridgeConfigurationManagerImpl.class);
45
46     // The implementation for each of these services is resolved by the OSGi Service Manager
47     private volatile ConfigurationService configurationService;
48     private volatile NetworkingProviderManager networkingProviderManager;
49     private volatile OvsdbConfigurationService ovsdbConfigurationService;
50
51     public BridgeConfigurationManagerImpl() {
52     }
53
54     @Override
55     public String getBridgeUuid(Node node, String bridgeName) {
56         Preconditions.checkNotNull(ovsdbConfigurationService);
57         try {
58              Map<String, Row> bridgeTable =
59                      ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Bridge.class));
60             if (bridgeTable == null) return null;
61             for (String key : bridgeTable.keySet()) {
62                 Bridge bridge = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeTable.get(key));
63                 if (bridge.getName().equals(bridgeName)) return key;
64             }
65         } catch (Exception e) {
66             logger.error("Error getting Bridge Identifier for {} / {}", node, bridgeName, e);
67         }
68         return null;
69     }
70
71     @Override
72     public boolean isNodeNeutronReady(Node node) {
73         Preconditions.checkNotNull(configurationService);
74         return this.getBridgeUuid(node, configurationService.getIntegrationBridgeName()) != null;
75     }
76
77     @Override
78     public boolean isNodeOverlayReady(Node node) {
79         Preconditions.checkNotNull(ovsdbConfigurationService);
80         return this.isNodeNeutronReady(node)
81                && this.getBridgeUuid(node, configurationService.getNetworkBridgeName()) != null;
82     }
83
84     @Override
85     public boolean isPortOnBridge (Node node, Bridge bridge, String portName) {
86         Preconditions.checkNotNull(ovsdbConfigurationService);
87         for (UUID portsUUID : bridge.getPortsColumn().getData()) {
88             try {
89                 Row portRow = ovsdbConfigurationService.getRow(node,
90                                                         ovsdbConfigurationService.getTableName(node, Port.class),
91                                                         portsUUID.toString());
92
93                 Port port = ovsdbConfigurationService.getTypedRow(node, Port.class, portRow);
94                 if ((port != null) && port.getName().equalsIgnoreCase(portName)) {
95                     return true;
96                 }
97             } catch (Exception e) {
98                 logger.error("Error getting port {} for bridge domain {}/{}", portsUUID, node, bridge.getName(), e);
99             }
100         }
101
102         return false;
103     }
104
105     @Override
106     public boolean isNodeTunnelReady(Node node) {
107         Preconditions.checkNotNull(configurationService);
108         Preconditions.checkNotNull(networkingProviderManager);
109
110         /* Is br-int created? */
111         Bridge intBridge = this.getBridge(node, configurationService.getIntegrationBridgeName());
112         if (intBridge == null) {
113             return false;
114         }
115
116         if (networkingProviderManager == null) {
117             logger.error("Provider Network Manager is not available");
118             return false;
119         }
120         if (networkingProviderManager.getProvider(node).hasPerTenantTunneling()) {
121             /* Is br-net created? */
122             Bridge netBridge = this.getBridge(node, configurationService.getNetworkBridgeName());
123             if (netBridge == null) {
124                 return false;
125             }
126
127             if (!isNetworkPatchCreated(node, intBridge, netBridge)) {
128                 return false;
129             }
130         }
131         return true;
132     }
133
134     @Override
135     public boolean isNodeVlanReady(Node node, NeutronNetwork network) {
136         Preconditions.checkNotNull(ovsdbConfigurationService);
137         Preconditions.checkNotNull(networkingProviderManager);
138
139         /* is br-int created */
140         Bridge intBridge = this.getBridge(node, configurationService.getIntegrationBridgeName());
141         if (intBridge == null) {
142             logger.trace("isNodeVlanReady: node: {}, br-int missing", node);
143             return false;
144         }
145
146         if (networkingProviderManager == null) {
147             logger.error("Provider Network Manager is not available");
148             return false;
149         }
150         if (networkingProviderManager.getProvider(node).hasPerTenantTunneling()) {
151             /* is br-net created? */
152             Bridge netBridge = this.getBridge(node, configurationService.getNetworkBridgeName());
153
154             if (netBridge == null) {
155                 logger.trace("isNodeVlanReady: node: {}, br-net missing", node);
156                 return false;
157             }
158
159             if (!isNetworkPatchCreated(node, intBridge, netBridge)) {
160                 logger.trace("isNodeVlanReady: node: {}, patch missing", node);
161                 return false;
162             }
163
164             /* Check if physical device is added to br-net. */
165             String phyNetName = this.getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork());
166             if (isPortOnBridge(node, netBridge, phyNetName)) {
167                 return true;
168             }
169         } else {
170             /* Check if physical device is added to br-int. */
171             String phyNetName = this.getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork());
172             if (isPortOnBridge(node, intBridge, phyNetName)) {
173                 return true;
174             }
175         }
176
177         logger.trace("isNodeVlanReady: node: {}, eth missing", node);
178         return false;
179     }
180
181     @Override
182     public void prepareNode(Node node) {
183         Preconditions.checkNotNull(networkingProviderManager);
184
185         try {
186             this.createIntegrationBridge(node);
187         } catch (Exception e) {
188             logger.error("Error creating Integration Bridge on " + node.toString(), e);
189             return;
190         }
191         if (networkingProviderManager == null) {
192             logger.error("Error creating internal network. Provider Network Manager unavailable");
193             return;
194         }
195         networkingProviderManager.getProvider(node).initializeFlowRules(node);
196     }
197
198     /*
199      * Check if the full network setup is available. If not, create it.
200      */
201     @Override
202     public boolean createLocalNetwork (Node node, NeutronNetwork network) {
203         boolean isCreated = false;
204         if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
205             if (!this.isNodeVlanReady(node, network)) {
206                 try {
207                     isCreated = this.createBridges(node, network);
208                 } catch (Exception e) {
209                     logger.error("Error creating internal net network " + node, e);
210                 }
211             } else {
212                 isCreated = true;
213             }
214         } else if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
215                    network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)) {
216             if (!this.isNodeTunnelReady(node)) {
217                 try {
218                     isCreated = this.createBridges(node, network);
219                 } catch (Exception e) {
220                     logger.error("Error creating internal net network " + node, e);
221                 }
222             } else {
223                 isCreated = true;
224             }
225         }
226         return isCreated;
227     }
228
229     @Override
230     public String getPhysicalInterfaceName (Node node, String physicalNetwork) {
231         String phyIf = null;
232         try {
233             Map<String, Row> ovsTable =
234                     ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
235
236             if (ovsTable == null) {
237                 logger.error("OpenVSwitch table is null for Node {} ", node);
238                 return null;
239             }
240
241             // Loop through all the Open_vSwitch rows looking for the first occurrence of other_config.
242             // The specification does not restrict the number of rows so we choose the first we find.
243             for (Row row : ovsTable.values()) {
244                 String providerMaps;
245                 OpenVSwitch ovsRow = ovsdbConfigurationService.getTypedRow(node, OpenVSwitch.class, row);
246                 Map<String, String> configs = ovsRow.getOtherConfigColumn().getData();
247
248                 if (configs == null) {
249                     logger.debug("OpenVSwitch table is null for Node {} ", node);
250                     continue;
251                 }
252
253                 providerMaps = configs.get(configurationService.getProviderMappingsKey());
254                 if (providerMaps == null) {
255                     providerMaps = configurationService.getDefaultProviderMapping();
256                 }
257
258                 if (providerMaps != null) {
259                     for (String map : providerMaps.split(",")) {
260                         String[] pair = map.split(":");
261                         if (pair[0].equals(physicalNetwork)) {
262                             phyIf = pair[1];
263                             break;
264                         }
265                     }
266                 }
267
268                 if (phyIf != null) {
269                     break;
270                 }
271             }
272         } catch (Exception e) {
273             logger.error("Unable to find physical interface for Node: {}, Network {}",
274                          node, physicalNetwork, e);
275         }
276
277         if (phyIf == null) {
278             logger.error("Physical interface not found for Node: {}, Network {}",
279                          node, physicalNetwork);
280         }
281
282         return phyIf;
283     }
284
285     @Override
286     public List<String> getAllPhysicalInterfaceNames(Node node) {
287         List<String> phyIfName = Lists.newArrayList();
288
289         try {
290             Map<String, Row> ovsTable =
291                     ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
292
293             if (ovsTable == null) {
294                 logger.error("OpenVSwitch table is null for Node {} ", node);
295                 return null;
296             }
297
298             // While there is only one entry in the HashMap, we can't access it by index...
299             for (Row row : ovsTable.values()) {
300                 String bridgeMaps;
301                 OpenVSwitch ovsRow = ovsdbConfigurationService.getTypedRow(node, OpenVSwitch.class, row);
302                 Map<String, String> configs = ovsRow.getOtherConfigColumn().getData();
303
304                 if (configs == null) {
305                     logger.debug("OpenVSwitch table is null for Node {} ", node);
306                     continue;
307                 }
308
309                 bridgeMaps = configs.get(configurationService.getProviderMappingsKey());
310                 if (bridgeMaps == null) {
311                     bridgeMaps = configurationService.getDefaultProviderMapping();
312                 }
313
314                 if (bridgeMaps != null) {
315                     for (String map : bridgeMaps.split(",")) {
316                         String[] pair = map.split(":");
317                         phyIfName.add(pair[1]);
318                     }
319                 }
320             }
321         } catch (Exception e) {
322             logger.error("Unable to find physical interface for Node: " + node, e);
323         }
324
325         logger.debug("Physical interface for Node: {}, If: {}",
326                      node, phyIfName);
327
328         return phyIfName;
329     }
330
331     /**
332      * Returns the Bridge for a given node and bridgeName
333      */
334     public Bridge getBridge (Node node, String bridgeName) {
335         Preconditions.checkNotNull(ovsdbConfigurationService);
336         try {
337             Map<String, Row> bridgeTable =
338                     ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Bridge.class));
339             if (bridgeTable != null) {
340                 for (String key : bridgeTable.keySet()) {
341                     Bridge bridge = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeTable.get(key));
342                     if (bridge.getName().equals(bridgeName)) {
343                         return bridge;
344                     }
345                 }
346             }
347         } catch (Exception e) {
348             logger.error("Error getting Bridge Identifier for {} / {}", node, bridgeName, e);
349         }
350         return null;
351     }
352
353     /**
354      * Returns true if a patch port exists between the Integration Bridge and Network Bridge
355      */
356     private boolean isNetworkPatchCreated (Node node, Bridge intBridge, Bridge netBridge) {
357         Preconditions.checkNotNull(ovsdbConfigurationService);
358
359         boolean isPatchCreated = false;
360
361         String portName = configurationService.getPatchPortName(new ImmutablePair<>(intBridge, netBridge));
362         if (isPortOnBridge(node, intBridge, portName)) {
363             portName = configurationService.getPatchPortName(new ImmutablePair<>(netBridge, intBridge));
364             if (isPortOnBridge(node, netBridge, portName)) {
365                 isPatchCreated = true;
366             }
367         }
368
369         return isPatchCreated;
370     }
371
372     /**
373      * Creates the Integration Bridge
374      */
375     private void createIntegrationBridge (Node node) throws Exception {
376         Preconditions.checkNotNull(ovsdbConfigurationService);
377
378         String brInt = configurationService.getIntegrationBridgeName();
379
380         Status status = this.addBridge(node, brInt, null, null);
381         if (!status.isSuccess()) {
382             logger.debug("Integration Bridge Creation Status: {}", status);
383         }
384     }
385
386     /**
387      * Create and configure bridges for all network types and OpenFlow versions.
388      *
389        OF 1.0 vlan:
390        Bridge br-int
391             Port patch-net
392                 Interface patch-net
393                     type: patch
394                     options: {peer=patch-int}
395             Port br-int
396                 Interface br-int
397                     type: internal
398        Bridge br-net
399             Port "eth1"
400                 Interface "eth1"
401             Port patch-int
402                 Interface patch-int
403                     type: patch
404                     options: {peer=patch-net}
405             Port br-net
406                 Interface br-net
407                     type: internal
408
409        OF 1.0 tunnel:
410        Bridge br-int
411             Port patch-net
412                 Interface patch-net
413                     type: patch
414                     options: {peer=patch-int}
415             Port br-int
416                 Interface br-int
417                     type: internal
418        Bridge "br-net"
419             Port patch-int
420                 Interface patch-int
421                     type: patch
422                     options: {peer=patch-net}
423             Port br-net
424                 Interface br-net
425                     type: internal
426
427        OF 1.3 vlan:
428        Bridge br-int
429             Port "eth1"
430                 Interface "eth1"
431             Port br-int
432                 Interface br-int
433                     type: internal
434
435        OF 1.3 tunnel:
436        Bridge br-int
437             Port br-int
438                 Interface br-int
439                     type: internal
440      */
441     private boolean createBridges(Node node, NeutronNetwork network) throws Exception {
442         Preconditions.checkNotNull(ovsdbConfigurationService);
443         Preconditions.checkNotNull(networkingProviderManager);
444         Status status;
445
446         logger.debug("createBridges: node: {}, network type: {}", node, network.getProviderNetworkType());
447
448         if (networkingProviderManager == null) {
449             logger.error("Provider Network Manager is not available");
450             return false;
451         }
452         if (networkingProviderManager.getProvider(node).hasPerTenantTunneling()) { /* indicates OF 1.0 */
453             String brInt = configurationService.getIntegrationBridgeName();
454             String brNet = configurationService.getNetworkBridgeName();
455             String patchNet = configurationService.getPatchPortName(new ImmutablePair<>(brInt, brNet));
456             String patchInt = configurationService.getPatchPortName(new ImmutablePair<>(brNet, brInt));
457
458             status = this.addBridge(node, brInt, patchNet, patchInt);
459             if (!status.isSuccess()) {
460                 logger.debug("{} Bridge Creation Status: {}", brInt, status);
461                 return false;
462             }
463             status = this.addBridge(node, brNet, patchInt, patchNet);
464             if (!status.isSuccess()) {
465                 logger.debug("{} Bridge Creation Status: {}", brNet, status);
466                 return false;
467             }
468
469             /* For vlan network types add physical port to br-net. */
470             if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
471                 String phyNetName = this.getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork());
472                 status = addPortToBridge(node, brNet, phyNetName);
473                 if (!status.isSuccess()) {
474                     logger.debug("Add Port {} to Bridge {} Status: {}", phyNetName, brNet, status);
475                     return false;
476                 }
477             }
478         } else {
479             String brInt = configurationService.getIntegrationBridgeName();
480             status = this.addBridge(node, brInt, null, null);
481             if (!status.isSuccess()) {
482                 logger.debug("{} Bridge Creation Status: {}", brInt, status);
483                 return false;
484             }
485
486             /* For vlan network types add physical port to br-int. */
487             if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
488                 String phyNetName = this.getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork());
489                 status = addPortToBridge(node, brInt, phyNetName);
490                 if (!status.isSuccess()) {
491                     logger.debug("Add Port {} to Bridge {} Status: {}", phyNetName, brInt, status);
492                     return false;
493                 }
494             }
495         }
496
497         logger.debug("createNetNetwork: node: {}, status: success", node);
498         return true;
499     }
500
501     /**
502      * Add a Port to a Bridge
503      */
504     private Status addPortToBridge (Node node, String bridgeName, String portName) throws Exception {
505         Preconditions.checkNotNull(ovsdbConfigurationService);
506
507         logger.debug("addPortToBridge: Adding port: {} to Bridge {}, Node {}", portName, bridgeName, node);
508
509         String bridgeUUID = this.getBridgeUuid(node, bridgeName);
510         if (bridgeUUID == null) {
511             logger.error("addPortToBridge: Could not find Bridge {} in Node {}", bridgeName, node);
512             return new Status(StatusCode.NOTFOUND, "Could not find "+bridgeName+" in "+node);
513         }
514
515         /* Check if the port already exists. */
516         Row row = ovsdbConfigurationService
517                 .getRow(node, ovsdbConfigurationService.getTableName(node, Bridge.class), bridgeUUID);
518         Bridge bridge = ovsdbConfigurationService.getTypedRow(node, Bridge.class, row);
519         if (bridge != null) {
520             if (isPortOnBridge(node, bridge, portName)) {
521                 logger.debug("addPortToBridge: Port {} already in Bridge {}, Node {}", portName, bridgeName, node);
522                 return new Status(StatusCode.SUCCESS);
523             }
524         } else {
525             logger.error("addPortToBridge: Could not find Port {} in Bridge {}, Node {}", portName, bridgeName, node);
526             return new Status(StatusCode.NOTFOUND, "Could not find "+portName+" in "+bridgeName);
527         }
528
529         Port port = ovsdbConfigurationService.createTypedRow(node, Port.class);
530         port.setName(portName);
531         StatusWithUuid statusWithUuid =
532                 ovsdbConfigurationService.insertRow(node, port.getSchema().getName(), bridgeUUID, port.getRow());
533         if (!statusWithUuid.isSuccess()) {
534             logger.error("addPortToBridge: Failed to add Port {} in Bridge {}, Node {}", portName, bridgeName, node);
535             return statusWithUuid;
536         }
537
538         String portUUID = statusWithUuid.getUuid().toString();
539         String interfaceUUID = null;
540         int timeout = 6;
541         while ((interfaceUUID == null) && (timeout > 0)) {
542             Row portRow = ovsdbConfigurationService.getRow(node, port.getSchema().getName(), portUUID);
543             port = ovsdbConfigurationService.getTypedRow(node, Port.class, portRow);
544             Set<UUID> interfaces = port.getInterfacesColumn().getData();
545             if (interfaces == null || interfaces.size() == 0) {
546                 // Wait for the OVSDB update to sync up the Local cache.
547                 Thread.sleep(500);
548                 timeout--;
549                 continue;
550             }
551             interfaceUUID = interfaces.toArray()[0].toString();
552             Row intf = ovsdbConfigurationService.getRow(node,
553                                                 ovsdbConfigurationService.getTableName(node, Interface.class), interfaceUUID);
554             if (intf == null) {
555                 interfaceUUID = null;
556             }
557         }
558
559         if (interfaceUUID == null) {
560             logger.error("addPortToBridge: Cannot identify Interface for port {}/{}", portName, portUUID);
561             return new Status(StatusCode.INTERNALERROR);
562         }
563
564         return new Status(StatusCode.SUCCESS);
565     }
566
567     /**
568      * Add a Patch Port to a Bridge
569      */
570     private Status addPatchPort (Node node, String bridgeUUID, String portName, String peerPortName) throws Exception {
571         Preconditions.checkNotNull(ovsdbConfigurationService);
572
573         logger.debug("addPatchPort: node: {}, bridgeUUID: {}, port: {}, peer: {}",
574                      node, bridgeUUID, portName, peerPortName);
575
576         /* Check if the port already exists. */
577         Row bridgeRow = ovsdbConfigurationService.getRow(node,
578                                                   ovsdbConfigurationService.getTableName(node, Bridge.class), bridgeUUID);
579         Bridge bridge = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeRow);
580         if (bridge != null) {
581             if (isPortOnBridge(node, bridge, portName)) {
582                 logger.debug("addPatchPort: Port {} already in Bridge, Node {}", portName, node);
583                 return new Status(StatusCode.SUCCESS);
584             }
585         } else {
586             logger.error("addPatchPort: Could not find Port {} in Bridge, Node {}", portName, node);
587             return new Status(StatusCode.NOTFOUND, "Could not find "+portName+" in Bridge");
588         }
589
590         Port patchPort = ovsdbConfigurationService.createTypedRow(node, Port.class);
591         patchPort.setName(portName);
592         // Create patch port and interface
593         StatusWithUuid statusWithUuid =
594                 ovsdbConfigurationService.insertRow(node, patchPort.getSchema().getName(), bridgeUUID, patchPort.getRow());
595         if (!statusWithUuid.isSuccess()) return statusWithUuid;
596
597         String patchPortUUID = statusWithUuid.getUuid().toString();
598
599         String interfaceUUID = null;
600         int timeout = 6;
601         while ((interfaceUUID == null) && (timeout > 0)) {
602             Row portRow = ovsdbConfigurationService.getRow(node, patchPort.getSchema().getName(), patchPortUUID);
603             patchPort = ovsdbConfigurationService.getTypedRow(node, Port.class, portRow);
604             Set<UUID> interfaces = patchPort.getInterfacesColumn().getData();
605             if (interfaces == null || interfaces.size() == 0) {
606                 // Wait for the OVSDB update to sync up the Local cache.
607                 Thread.sleep(500);
608                 timeout--;
609                 continue;
610             }
611             interfaceUUID = interfaces.toArray()[0].toString();
612         }
613
614         if (interfaceUUID == null) {
615             return new Status(StatusCode.INTERNALERROR);
616         }
617
618         Interface intf = ovsdbConfigurationService.createTypedRow(node, Interface.class);
619         intf.setType("patch");
620         Map<String, String> options = Maps.newHashMap();
621         options.put("peer", peerPortName);
622         intf.setOptions(options);
623         return ovsdbConfigurationService.updateRow(node,
624                                             intf.getSchema().getName(),
625                                             patchPortUUID,
626                                             interfaceUUID,
627                                             intf.getRow());
628     }
629
630     /**
631      * Add Bridge to a Node
632      */
633     private Status addBridge(Node node, String bridgeName,
634                              String localPatchName, String remotePatchName) throws Exception {
635         Preconditions.checkNotNull(ovsdbConfigurationService);
636
637         String bridgeUUID = this.getBridgeUuid(node, bridgeName);
638         Bridge bridge = ovsdbConfigurationService.createTypedRow(node, Bridge.class);
639         Set<String> failMode = new HashSet<>();
640         failMode.add("secure");
641         bridge.setFailMode(failMode);
642
643         Set<String> protocols = new HashSet<>();
644         if (networkingProviderManager == null) {
645             logger.error("Provider Network Manager is not available");
646             return new Status(StatusCode.INTERNALERROR);
647         }
648
649         /* ToDo: Plugin should expose an easy way to get the OVS Version or Schema Version
650          * or, alternatively it should not attempt to add set unsupported fields
651          */
652
653         try {
654             protocols.add(Constants.OPENFLOW13);
655             bridge.setProtocols(protocols);
656         } catch (SchemaVersionMismatchException e) {
657             logger.info(e.toString());
658         }
659
660         if (bridgeUUID == null) {
661             bridge.setName(bridgeName);
662
663             StatusWithUuid statusWithUuid = ovsdbConfigurationService.insertRow(node,
664                                                                          bridge.getSchema().getName(),
665                                                                          null,
666                                                                          bridge.getRow());
667             if (!statusWithUuid.isSuccess()) return statusWithUuid;
668             bridgeUUID = statusWithUuid.getUuid().toString();
669             Port port = ovsdbConfigurationService.createTypedRow(node, Port.class);
670             port.setName(bridgeName);
671             Status status = ovsdbConfigurationService.insertRow(node, port.getSchema().getName(), bridgeUUID, port.getRow());
672             logger.debug("addBridge: Inserting Bridge {} {} with protocols {} and status {}",
673                          bridgeName, bridgeUUID, protocols, status);
674         } else {
675             Status status = ovsdbConfigurationService.updateRow(node,
676                                                          bridge.getSchema().getName(),
677                                                          null,
678                                                          bridgeUUID,
679                                                          bridge.getRow());
680             logger.debug("addBridge: Updating Bridge {} {} with protocols {} and status {}",
681                          bridgeName, bridgeUUID, protocols, status);
682         }
683
684         ovsdbConfigurationService.setOFController(node, bridgeUUID);
685
686         if (localPatchName != null &&
687             remotePatchName != null &&
688             networkingProviderManager.getProvider(node).hasPerTenantTunneling()) {
689             return addPatchPort(node, bridgeUUID, localPatchName, remotePatchName);
690         }
691         return new Status(StatusCode.SUCCESS);
692     }
693
694
695 }