2 * Copyright (C) 2013 Red Hat, Inc.
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
8 * Authors : Madhu Venugopal, Brent Salisbury, Sam Hague
10 package org.opendaylight.ovsdb.openstack.netvirt.impl;
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;
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;
38 import java.util.HashSet;
39 import java.util.List;
43 public class BridgeConfigurationManagerImpl implements BridgeConfigurationManager {
44 static final Logger logger = LoggerFactory.getLogger(BridgeConfigurationManagerImpl.class);
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;
51 public BridgeConfigurationManagerImpl() {
55 public String getBridgeUuid(Node node, String bridgeName) {
56 Preconditions.checkNotNull(ovsdbConfigurationService);
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;
65 } catch (Exception e) {
66 logger.error("Error getting Bridge Identifier for {} / {}", node, bridgeName, e);
72 public boolean isNodeNeutronReady(Node node) {
73 Preconditions.checkNotNull(configurationService);
74 return this.getBridgeUuid(node, configurationService.getIntegrationBridgeName()) != null;
78 public boolean isNodeOverlayReady(Node node) {
79 Preconditions.checkNotNull(ovsdbConfigurationService);
80 return this.isNodeNeutronReady(node)
81 && this.getBridgeUuid(node, configurationService.getNetworkBridgeName()) != null;
85 public boolean isPortOnBridge (Node node, Bridge bridge, String portName) {
86 Preconditions.checkNotNull(ovsdbConfigurationService);
87 for (UUID portsUUID : bridge.getPortsColumn().getData()) {
89 Row portRow = ovsdbConfigurationService.getRow(node,
90 ovsdbConfigurationService.getTableName(node, Port.class),
91 portsUUID.toString());
93 Port port = ovsdbConfigurationService.getTypedRow(node, Port.class, portRow);
94 if ((port != null) && port.getName().equalsIgnoreCase(portName)) {
97 } catch (Exception e) {
98 logger.error("Error getting port {} for bridge domain {}/{}", portsUUID, node, bridge.getName(), e);
106 public boolean isNodeTunnelReady(Node node) {
107 Preconditions.checkNotNull(configurationService);
108 Preconditions.checkNotNull(networkingProviderManager);
110 /* Is br-int created? */
111 Bridge intBridge = this.getBridge(node, configurationService.getIntegrationBridgeName());
112 if (intBridge == null) {
116 if (networkingProviderManager == null) {
117 logger.error("Provider Network Manager is not available");
120 if (networkingProviderManager.getProvider(node).hasPerTenantTunneling()) {
121 /* Is br-net created? */
122 Bridge netBridge = this.getBridge(node, configurationService.getNetworkBridgeName());
123 if (netBridge == null) {
127 if (!isNetworkPatchCreated(node, intBridge, netBridge)) {
135 public boolean isNodeVlanReady(Node node, NeutronNetwork network) {
136 Preconditions.checkNotNull(ovsdbConfigurationService);
137 Preconditions.checkNotNull(networkingProviderManager);
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);
146 if (networkingProviderManager == null) {
147 logger.error("Provider Network Manager is not available");
150 if (networkingProviderManager.getProvider(node).hasPerTenantTunneling()) {
151 /* is br-net created? */
152 Bridge netBridge = this.getBridge(node, configurationService.getNetworkBridgeName());
154 if (netBridge == null) {
155 logger.trace("isNodeVlanReady: node: {}, br-net missing", node);
159 if (!isNetworkPatchCreated(node, intBridge, netBridge)) {
160 logger.trace("isNodeVlanReady: node: {}, patch missing", node);
164 /* Check if physical device is added to br-net. */
165 String phyNetName = this.getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork());
166 if (isPortOnBridge(node, netBridge, phyNetName)) {
170 /* Check if physical device is added to br-int. */
171 String phyNetName = this.getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork());
172 if (isPortOnBridge(node, intBridge, phyNetName)) {
177 logger.trace("isNodeVlanReady: node: {}, eth missing", node);
182 public void prepareNode(Node node) {
183 Preconditions.checkNotNull(networkingProviderManager);
186 this.createIntegrationBridge(node);
187 } catch (Exception e) {
188 logger.error("Error creating Integration Bridge on " + node.toString(), e);
191 if (networkingProviderManager == null) {
192 logger.error("Error creating internal network. Provider Network Manager unavailable");
195 networkingProviderManager.getProvider(node).initializeFlowRules(node);
199 * Check if the full network setup is available. If not, create it.
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)) {
207 isCreated = this.createBridges(node, network);
208 } catch (Exception e) {
209 logger.error("Error creating internal net network " + node, e);
214 } else if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
215 network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)) {
216 if (!this.isNodeTunnelReady(node)) {
218 isCreated = this.createBridges(node, network);
219 } catch (Exception e) {
220 logger.error("Error creating internal net network " + node, e);
230 public String getPhysicalInterfaceName (Node node, String physicalNetwork) {
233 Map<String, Row> ovsTable =
234 ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
236 if (ovsTable == null) {
237 logger.error("OpenVSwitch table is null for Node {} ", node);
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()) {
245 OpenVSwitch ovsRow = ovsdbConfigurationService.getTypedRow(node, OpenVSwitch.class, row);
246 Map<String, String> configs = ovsRow.getOtherConfigColumn().getData();
248 if (configs == null) {
249 logger.debug("OpenVSwitch table is null for Node {} ", node);
253 providerMaps = configs.get(configurationService.getProviderMappingsKey());
254 if (providerMaps == null) {
255 providerMaps = configurationService.getDefaultProviderMapping();
258 if (providerMaps != null) {
259 for (String map : providerMaps.split(",")) {
260 String[] pair = map.split(":");
261 if (pair[0].equals(physicalNetwork)) {
272 } catch (Exception e) {
273 logger.error("Unable to find physical interface for Node: {}, Network {}",
274 node, physicalNetwork, e);
278 logger.error("Physical interface not found for Node: {}, Network {}",
279 node, physicalNetwork);
286 public List<String> getAllPhysicalInterfaceNames(Node node) {
287 List<String> phyIfName = Lists.newArrayList();
290 Map<String, Row> ovsTable =
291 ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
293 if (ovsTable == null) {
294 logger.error("OpenVSwitch table is null for Node {} ", node);
298 // While there is only one entry in the HashMap, we can't access it by index...
299 for (Row row : ovsTable.values()) {
301 OpenVSwitch ovsRow = ovsdbConfigurationService.getTypedRow(node, OpenVSwitch.class, row);
302 Map<String, String> configs = ovsRow.getOtherConfigColumn().getData();
304 if (configs == null) {
305 logger.debug("OpenVSwitch table is null for Node {} ", node);
309 bridgeMaps = configs.get(configurationService.getProviderMappingsKey());
310 if (bridgeMaps == null) {
311 bridgeMaps = configurationService.getDefaultProviderMapping();
314 if (bridgeMaps != null) {
315 for (String map : bridgeMaps.split(",")) {
316 String[] pair = map.split(":");
317 phyIfName.add(pair[1]);
321 } catch (Exception e) {
322 logger.error("Unable to find physical interface for Node: " + node, e);
325 logger.debug("Physical interface for Node: {}, If: {}",
332 * Returns the Bridge for a given node and bridgeName
334 public Bridge getBridge (Node node, String bridgeName) {
335 Preconditions.checkNotNull(ovsdbConfigurationService);
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)) {
347 } catch (Exception e) {
348 logger.error("Error getting Bridge Identifier for {} / {}", node, bridgeName, e);
354 * Returns true if a patch port exists between the Integration Bridge and Network Bridge
356 private boolean isNetworkPatchCreated (Node node, Bridge intBridge, Bridge netBridge) {
357 Preconditions.checkNotNull(ovsdbConfigurationService);
359 boolean isPatchCreated = false;
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;
369 return isPatchCreated;
373 * Creates the Integration Bridge
375 private void createIntegrationBridge (Node node) throws Exception {
376 Preconditions.checkNotNull(ovsdbConfigurationService);
378 String brInt = configurationService.getIntegrationBridgeName();
380 Status status = this.addBridge(node, brInt, null, null);
381 if (!status.isSuccess()) {
382 logger.debug("Integration Bridge Creation Status: {}", status);
387 * Create and configure bridges for all network types and OpenFlow versions.
394 options: {peer=patch-int}
404 options: {peer=patch-net}
414 options: {peer=patch-int}
422 options: {peer=patch-net}
441 private boolean createBridges(Node node, NeutronNetwork network) throws Exception {
442 Preconditions.checkNotNull(ovsdbConfigurationService);
443 Preconditions.checkNotNull(networkingProviderManager);
446 logger.debug("createBridges: node: {}, network type: {}", node, network.getProviderNetworkType());
448 if (networkingProviderManager == null) {
449 logger.error("Provider Network Manager is not available");
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));
458 status = this.addBridge(node, brInt, patchNet, patchInt);
459 if (!status.isSuccess()) {
460 logger.debug("{} Bridge Creation Status: {}", brInt, status);
463 status = this.addBridge(node, brNet, patchInt, patchNet);
464 if (!status.isSuccess()) {
465 logger.debug("{} Bridge Creation Status: {}", brNet, status);
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);
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);
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);
497 logger.debug("createNetNetwork: node: {}, status: success", node);
502 * Add a Port to a Bridge
504 private Status addPortToBridge (Node node, String bridgeName, String portName) throws Exception {
505 Preconditions.checkNotNull(ovsdbConfigurationService);
507 logger.debug("addPortToBridge: Adding port: {} to Bridge {}, Node {}", portName, bridgeName, node);
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);
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);
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);
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;
538 String portUUID = statusWithUuid.getUuid().toString();
539 String interfaceUUID = null;
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.
551 interfaceUUID = interfaces.toArray()[0].toString();
552 Row intf = ovsdbConfigurationService.getRow(node,
553 ovsdbConfigurationService.getTableName(node, Interface.class), interfaceUUID);
555 interfaceUUID = null;
559 if (interfaceUUID == null) {
560 logger.error("addPortToBridge: Cannot identify Interface for port {}/{}", portName, portUUID);
561 return new Status(StatusCode.INTERNALERROR);
564 return new Status(StatusCode.SUCCESS);
568 * Add a Patch Port to a Bridge
570 private Status addPatchPort (Node node, String bridgeUUID, String portName, String peerPortName) throws Exception {
571 Preconditions.checkNotNull(ovsdbConfigurationService);
573 logger.debug("addPatchPort: node: {}, bridgeUUID: {}, port: {}, peer: {}",
574 node, bridgeUUID, portName, peerPortName);
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);
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");
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;
597 String patchPortUUID = statusWithUuid.getUuid().toString();
599 String interfaceUUID = null;
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.
611 interfaceUUID = interfaces.toArray()[0].toString();
614 if (interfaceUUID == null) {
615 return new Status(StatusCode.INTERNALERROR);
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(),
631 * Add Bridge to a Node
633 private Status addBridge(Node node, String bridgeName,
634 String localPatchName, String remotePatchName) throws Exception {
635 Preconditions.checkNotNull(ovsdbConfigurationService);
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);
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);
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
654 protocols.add(Constants.OPENFLOW13);
655 bridge.setProtocols(protocols);
656 } catch (SchemaVersionMismatchException e) {
657 logger.info(e.toString());
660 if (bridgeUUID == null) {
661 bridge.setName(bridgeName);
663 StatusWithUuid statusWithUuid = ovsdbConfigurationService.insertRow(node,
664 bridge.getSchema().getName(),
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);
675 Status status = ovsdbConfigurationService.updateRow(node,
676 bridge.getSchema().getName(),
680 logger.debug("addBridge: Updating Bridge {} {} with protocols {} and status {}",
681 bridgeName, bridgeUUID, protocols, status);
684 ovsdbConfigurationService.setOFController(node, bridgeUUID);
686 if (localPatchName != null &&
687 remotePatchName != null &&
688 networkingProviderManager.getProvider(node).hasPerTenantTunneling()) {
689 return addPatchPort(node, bridgeUUID, localPatchName, remotePatchName);
691 return new Status(StatusCode.SUCCESS);