2 * Copyright (c) 2013, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.openstack.netvirt.impl;
11 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronNetwork;
12 import org.opendaylight.ovsdb.openstack.netvirt.ConfigInterface;
13 import org.opendaylight.ovsdb.openstack.netvirt.NetworkHandler;
14 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
15 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
16 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProviderManager;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbTables;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
20 import org.opendaylight.ovsdb.utils.config.ConfigProperties;
21 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeBase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeNetdev;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeSystem;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagerEntry;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import com.google.common.base.Preconditions;
31 import com.google.common.collect.Lists;
33 import java.net.InetAddress;
34 import java.net.NetworkInterface;
35 import java.net.UnknownHostException;
36 import java.util.ArrayList;
37 import java.util.Enumeration;
38 import java.util.List;
40 import org.apache.commons.lang3.tuple.ImmutablePair;
41 import org.osgi.framework.ServiceReference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * @author Madhu Venugopal
47 * @author Brent Salisbury
48 * @author Sam Hague (shague@redhat.com)
50 public class BridgeConfigurationManagerImpl implements BridgeConfigurationManager, ConfigInterface {
51 private static final Logger LOG = LoggerFactory.getLogger(BridgeConfigurationManagerImpl.class);
53 // The implementation for each of these services is resolved by the OSGi Service Manager
54 private volatile ConfigurationService configurationService;
55 private volatile NetworkingProviderManager networkingProviderManager;
56 private volatile Southbound southbound;
58 public void setConfigurationService(ConfigurationService configurationService) {
59 this.configurationService = configurationService;
62 public void setSouthbound(Southbound southbound) {
63 this.southbound = southbound;
67 public String getBridgeUuid(Node node, String bridgeName) {
68 return southbound.getBridgeUuid(node, bridgeName);
72 public boolean isNodeNeutronReady(Node node) {
73 Preconditions.checkNotNull(configurationService);
74 return southbound.getBridge(node, configurationService.getIntegrationBridgeName()) != null;
78 public boolean isNodeOverlayReady(Node node) {
79 Preconditions.checkNotNull(configurationService);
80 return isNodeNeutronReady(node)
81 && southbound.getBridge(node, configurationService.getNetworkBridgeName()) != null;
85 public boolean isPortOnBridge (Node bridgeNode, String portName) {
86 return southbound.extractTerminationPointAugmentation(bridgeNode, portName) != null;
90 public boolean isNodeTunnelReady(Node bridgeNode, Node ovsdbNode) {
91 Preconditions.checkNotNull(configurationService);
92 if (!southbound.isBridgeOnOvsdbNode(ovsdbNode, configurationService.getIntegrationBridgeName())) {
93 LOG.trace("isNodeTunnelReady: node: {}, {} missing",
94 bridgeNode, configurationService.getIntegrationBridgeName());
98 return isNodeL3Ready(bridgeNode, ovsdbNode);
102 public boolean isNodeVlanReady(Node bridgeNode, Node ovsdbNode, NeutronNetwork network) {
103 Preconditions.checkNotNull(configurationService);
105 final String brInt = configurationService.getIntegrationBridgeName();
106 if (!southbound.isBridgeOnOvsdbNode(ovsdbNode, brInt)) {
107 LOG.trace("isNodeVlanReady: node: {}, {} missing", bridgeNode, brInt);
111 /* Check if physical device is added to br-int. */
112 String phyNetName = getPhysicalInterfaceName(ovsdbNode, network.getProviderPhysicalNetwork());
113 if (!isPortOnBridge(bridgeNode, phyNetName)) {
114 LOG.trace("isNodeVlanReady: node: {}, eth missing", bridgeNode);
118 return isNodeL3Ready(bridgeNode, ovsdbNode);
121 public boolean isNodeL3Ready(Node bridgeNode, Node ovsdbNode) {
122 Preconditions.checkNotNull(configurationService);
123 boolean ready = false;
124 if (configurationService.isL3ForwardingEnabled()) {
125 final String brInt = configurationService.getIntegrationBridgeName();
126 final String brExt = configurationService.getExternalBridgeName();
127 final String portNameInt = configurationService.getPatchPortName(new ImmutablePair<>(brInt, brExt));
128 final String portNameExt = configurationService.getPatchPortName(new ImmutablePair<>(brExt, brInt));
129 Preconditions.checkNotNull(portNameInt);
130 Preconditions.checkNotNull(portNameExt);
132 if (southbound.isBridgeOnOvsdbNode(ovsdbNode, brExt)) {
133 ready = isNetworkPatchCreated(bridgeNode, southbound.readBridgeNode(ovsdbNode, brExt));
135 LOG.trace("isNodeL3Ready: node: {}, {} missing",
145 public void prepareNode(Node ovsdbNode) {
146 Preconditions.checkNotNull(configurationService);
149 createIntegrationBridge(ovsdbNode);
150 } catch (Exception e) {
151 LOG.error("Error creating Integration Bridge on {}", ovsdbNode, e);
156 if (configurationService.isL3ForwardingEnabled()) {
157 createExternalBridge(ovsdbNode);
159 } catch (Exception e) {
160 LOG.error("Error creating External Bridge on {}", ovsdbNode, e);
163 // this node is an ovsdb node so it doesn't have a bridge
164 // so either look up the bridges or just wait for the bridge update to come in
165 // and add the flows there.
166 //networkingProviderManager.getProvider(node).initializeFlowRules(node);
170 * Check if the full network setup is available. If not, create it.
173 public boolean createLocalNetwork (Node bridgeNode, NeutronNetwork network) {
174 boolean isCreated = false;
175 Node ovsdbNode = southbound.readOvsdbNode(bridgeNode);
176 if (ovsdbNode == null) {
177 //this should never happen
178 LOG.error("createLocalNetwork could not find ovsdbNode from bridge node " + bridgeNode);
181 if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
182 if (!isNodeVlanReady(bridgeNode, ovsdbNode, network)) {
184 isCreated = createBridges(bridgeNode, ovsdbNode, network);
185 } catch (Exception e) {
186 LOG.error("Error creating internal vlan net network " + bridgeNode, e);
191 } else if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
192 network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)) {
193 if (!isNodeTunnelReady(bridgeNode, ovsdbNode)) {
195 isCreated = createBridges(bridgeNode, ovsdbNode, network);
196 } catch (Exception e) {
197 LOG.error("Error creating internal vxlan/gre net network " + bridgeNode, e);
209 public String getExternalInterfaceName (Node node, String extNetwork) {
211 String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
212 configurationService.getProviderMappingsKey());
213 if (providerMaps != null) {
214 for (String map : providerMaps.split(",")) {
215 String[] pair = map.split(":");
216 if (pair[0].equals(extNetwork)) {
223 LOG.error("External interface not found for Node: {}, Network {}",
227 LOG.info("External interface found for Node: {}, Network {} is {}",node,extNetwork,phyIf);
235 public String getPhysicalInterfaceName (Node node, String physicalNetwork) {
237 String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
238 configurationService.getProviderMappingsKey());
239 if (providerMaps == null) {
240 providerMaps = configurationService.getDefaultProviderMapping();
243 if (providerMaps != null) {
244 for (String map : providerMaps.split(",")) {
245 String[] pair = map.split(":");
246 if (pair[0].equals(physicalNetwork)) {
254 LOG.error("Physical interface not found for Node: {}, Network {}",
255 node, physicalNetwork);
262 public List<String> getAllPhysicalInterfaceNames(Node node) {
263 List<String> phyIfName = Lists.newArrayList();
264 String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
265 configurationService.getProviderMappingsKey());
266 if (providerMaps == null) {
267 providerMaps = configurationService.getDefaultProviderMapping();
270 if (providerMaps != null) {
271 for (String map : providerMaps.split(",")) {
272 String[] pair = map.split(":");
273 phyIfName.add(pair[1]);
281 * Returns true if a patch port exists between the Integration Bridge and Network Bridge
283 private boolean isNetworkPatchCreated(Node intBridge, Node netBridge) {
284 Preconditions.checkNotNull(configurationService);
286 boolean isPatchCreated = false;
288 String portName = configurationService.getPatchPortName(new ImmutablePair<>(intBridge, netBridge));
289 if (isPortOnBridge(intBridge, portName)) {
290 portName = configurationService.getPatchPortName(new ImmutablePair<>(netBridge, intBridge));
291 if (isPortOnBridge(netBridge, portName)) {
292 isPatchCreated = true;
296 return isPatchCreated;
300 * Creates the Integration Bridge
302 private boolean createIntegrationBridge(Node ovsdbNode) {
303 Preconditions.checkNotNull(configurationService);
305 if (!addBridge(ovsdbNode, configurationService.getIntegrationBridgeName())) {
306 LOG.debug("Integration Bridge Creation failed");
312 private boolean createExternalBridge(Node ovsdbNode) {
313 Preconditions.checkNotNull(configurationService);
315 if (!addBridge(ovsdbNode, configurationService.getExternalBridgeName())) {
316 LOG.debug("External Bridge Creation failed");
323 * Create and configure bridges for all network types and OpenFlow versions.
330 options: {peer=patch-int}
340 options: {peer=patch-net}
350 options: {peer=patch-int}
358 options: {peer=patch-net}
377 private boolean createBridges(Node bridgeNode, Node ovsdbNode, NeutronNetwork network) {
378 Preconditions.checkNotNull(configurationService);
379 Preconditions.checkNotNull(networkingProviderManager);
381 LOG.debug("createBridges: node: {}, network type: {}", bridgeNode, network.getProviderNetworkType());
383 final String brInt = configurationService.getIntegrationBridgeName();
384 if (! createIntegrationBridge(ovsdbNode)) {
385 LOG.debug("{} Bridge creation failed", brInt);
389 if (configurationService.isL3ForwardingEnabled()) {
390 final String brExt = configurationService.getExternalBridgeName();
391 if (! createExternalBridge(ovsdbNode)) {
392 LOG.error("{} Bridge creation failed", brExt);
396 //get two patch port names
397 final String portNameInt = configurationService.getPatchPortName(new ImmutablePair<>(brInt, brExt));
398 final String portNameExt = configurationService.getPatchPortName(new ImmutablePair<>(brExt, brInt));
399 Preconditions.checkNotNull(portNameInt);
400 Preconditions.checkNotNull(portNameExt);
402 if (!addPatchPort(bridgeNode, brInt, portNameInt, portNameExt)) {
403 LOG.error("Add Port {} to Bridge {} failed", portNameInt, brInt);
406 Node extBridgeNode = southbound.readBridgeNode(ovsdbNode, brExt);
407 Preconditions.checkNotNull(extBridgeNode);
408 if (!addPatchPort(extBridgeNode, brExt, portNameExt, portNameInt)) {
409 LOG.error("Add Port {} to Bridge {} failed", portNameExt, brExt);
412 String extNetName = getExternalInterfaceName(extBridgeNode, brExt);
413 if ( extNetName != null) {
414 if (!addPortToBridge(extBridgeNode, brExt, extNetName)) {
415 LOG.error("Add External Port {} to Bridge {} failed", extNetName, brExt);
418 LOG.info("Add External Port {} to Ext Bridge {} success", extNetName, brExt);
421 /* For vlan network types add physical port to br-int. */
422 if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
423 String phyNetName = this.getPhysicalInterfaceName(bridgeNode, network.getProviderPhysicalNetwork());
424 if (!addPortToBridge(bridgeNode, brInt, phyNetName)) {
425 LOG.debug("Add Port {} to Bridge {} failed", phyNetName, brInt);
430 LOG.info("createBridges: node: {}, status: success", bridgeNode);
435 * Add a Port to a Bridge
437 private boolean addPortToBridge (Node node, String bridgeName, String portName) {
440 if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
441 rv = southbound.addTerminationPoint(node, bridgeName, portName, null);
444 LOG.info("addPortToBridge: node: {}, bridge: {}, portname: {} status: success",
445 node.getNodeId().getValue(), bridgeName, portName);
447 LOG.error("addPortToBridge: node: {}, bridge: {}, portname: {} status: FAILED",
448 node.getNodeId().getValue(), bridgeName, portName);
451 LOG.trace("addPortToBridge: node: {}, bridge: {}, portname: {} status: not_needed",
452 node.getNodeId().getValue(), bridgeName, portName);
459 * Add a Patch Port to a Bridge
461 private boolean addPatchPort (Node node, String bridgeName, String portName, String peerPortName) {
464 if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
465 rv = southbound.addPatchTerminationPoint(node, bridgeName, portName, peerPortName);
468 LOG.info("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: success",
469 node.getNodeId().getValue(), bridgeName, portName, peerPortName);
471 LOG.error("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: FAILED",
472 node.getNodeId().getValue(), bridgeName, portName, peerPortName);
475 LOG.trace("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: not_needed",
476 node.getNodeId().getValue(), bridgeName, portName, peerPortName);
483 * Add Bridge to a Node
485 private boolean addBridge(Node ovsdbNode, String bridgeName) {
487 if ((!southbound.isBridgeOnOvsdbNode(ovsdbNode, bridgeName)) ||
488 (southbound.getBridgeFromConfig(ovsdbNode, bridgeName) == null)) {
489 Class<? extends DatapathTypeBase> dpType = null;
490 if (configurationService.isUserSpaceEnabled()) {
491 dpType = DatapathTypeNetdev.class;
493 rv = southbound.addBridge(ovsdbNode, bridgeName, getControllersFromOvsdbNode(ovsdbNode), dpType);
498 private String getControllerIPAddress() {
499 String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address");
500 if (addressString != null) {
502 if (InetAddress.getByName(addressString) != null) {
503 return addressString;
505 } catch (UnknownHostException e) {
506 LOG.error("Host {} is invalid", addressString);
510 addressString = ConfigProperties.getProperty(this.getClass(), "of.address");
511 if (addressString != null) {
513 if (InetAddress.getByName(addressString) != null) {
514 return addressString;
516 } catch (UnknownHostException e) {
517 LOG.error("Host {} is invalid", addressString);
524 private short getControllerOFPort() {
525 short openFlowPort = Constants.OPENFLOW_PORT;
526 String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort");
527 if (portString != null) {
529 openFlowPort = Short.parseShort(portString);
530 } catch (NumberFormatException e) {
531 LOG.warn("Invalid port:{}, use default({})", portString,
538 private List<String> getControllersFromOvsdbNode(Node node) {
539 List<String> controllersStr = new ArrayList<>();
541 String controllerIpStr = getControllerIPAddress();
542 if (controllerIpStr != null) {
543 // If codepath makes it here, the ip address to be used was explicitly provided.
544 // Being so, also fetch openflowPort provided via ConfigProperties.
545 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
546 + ":" + controllerIpStr + ":" + getControllerOFPort());
548 // Check if ovsdb node has manager entries
549 OvsdbNodeAugmentation ovsdbNodeAugmentation = southbound.extractOvsdbNode(node);
550 if (ovsdbNodeAugmentation != null) {
551 List<ManagerEntry> managerEntries = ovsdbNodeAugmentation.getManagerEntry();
552 if (managerEntries != null && !managerEntries.isEmpty()) {
553 for (ManagerEntry managerEntry : managerEntries) {
554 if (managerEntry == null || managerEntry.getTarget() == null) {
557 String[] tokens = managerEntry.getTarget().getValue().split(":");
558 if (tokens.length == 3 && tokens[0].equalsIgnoreCase("tcp")) {
559 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
560 + ":" + tokens[1] + ":" + getControllerOFPort());
561 } else if (tokens[0].equalsIgnoreCase("ptcp")) {
562 ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
563 if (connectionInfo != null && connectionInfo.getLocalIp() != null) {
564 controllerIpStr = String.valueOf(connectionInfo.getLocalIp().getValue());
565 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
566 + ":" + controllerIpStr + ":" + Constants.OPENFLOW_PORT);
568 LOG.warn("Ovsdb Node does not contain connection info: {}", node);
571 LOG.trace("Skipping manager entry {} for node {}",
572 managerEntry.getTarget(), node.getNodeId().getValue());
576 LOG.warn("Ovsdb Node does not contain manager entries : {}", node);
581 if (controllersStr.isEmpty()) {
582 // Neither user provided ip nor ovsdb node has manager entries. Lets use local machine ip address.
583 LOG.debug("Use local machine ip address as a OpenFlow Controller ip address");
584 controllerIpStr = getLocalControllerHostIpAddress();
585 if (controllerIpStr != null) {
586 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
587 + ":" + controllerIpStr + ":" + Constants.OPENFLOW_PORT);
591 if (controllersStr.isEmpty()) {
592 LOG.warn("Failed to determine OpenFlow controller ip address");
593 } else if (LOG.isDebugEnabled()) {
594 controllerIpStr = "";
595 for (String currControllerIpStr : controllersStr) {
596 controllerIpStr += " " + currControllerIpStr;
598 LOG.debug("Found {} OpenFlow Controller(s) :{}", controllersStr.size(), controllerIpStr);
601 return controllersStr;
604 private String getLocalControllerHostIpAddress() {
605 String ipaddress = null;
607 for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
608 NetworkInterface iface = ifaces.nextElement();
610 for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
611 InetAddress inetAddr = inetAddrs.nextElement();
612 if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
613 ipaddress = inetAddr.getHostAddress();
618 }catch (Exception e){
619 LOG.warn("Exception while fetching local host ip address ", e);
625 public void setDependencies(ServiceReference serviceReference) {
626 configurationService =
627 (ConfigurationService) ServiceHelper.getGlobalInstance(ConfigurationService.class, this);
628 networkingProviderManager =
629 (NetworkingProviderManager) ServiceHelper.getGlobalInstance(NetworkingProviderManager.class, this);
631 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
635 public void setDependencies(Object impl) {