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.OvsdbNodeAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagerEntry;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import com.google.common.base.Preconditions;
28 import com.google.common.collect.Lists;
30 import java.net.InetAddress;
31 import java.net.NetworkInterface;
32 import java.net.UnknownHostException;
33 import java.util.ArrayList;
34 import java.util.Enumeration;
35 import java.util.List;
37 import org.apache.commons.lang3.tuple.ImmutablePair;
38 import org.osgi.framework.ServiceReference;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * @author Madhu Venugopal
44 * @author Brent Salisbury
45 * @author Sam Hague (shague@redhat.com)
47 public class BridgeConfigurationManagerImpl implements BridgeConfigurationManager, ConfigInterface {
48 private static final Logger LOG = LoggerFactory.getLogger(BridgeConfigurationManagerImpl.class);
50 // The implementation for each of these services is resolved by the OSGi Service Manager
51 private volatile ConfigurationService configurationService;
52 private volatile NetworkingProviderManager networkingProviderManager;
53 private volatile Southbound southbound;
55 public void setConfigurationService(ConfigurationService configurationService) {
56 this.configurationService = configurationService;
59 public void setSouthbound(Southbound southbound) {
60 this.southbound = southbound;
64 public String getBridgeUuid(Node node, String bridgeName) {
65 return southbound.getBridgeUuid(node, bridgeName);
69 public boolean isNodeNeutronReady(Node node) {
70 Preconditions.checkNotNull(configurationService);
71 return southbound.getBridge(node, configurationService.getIntegrationBridgeName()) != null;
75 public boolean isNodeOverlayReady(Node node) {
76 Preconditions.checkNotNull(configurationService);
77 return isNodeNeutronReady(node)
78 && southbound.getBridge(node, configurationService.getNetworkBridgeName()) != null;
82 public boolean isPortOnBridge (Node bridgeNode, String portName) {
83 return southbound.extractTerminationPointAugmentation(bridgeNode, portName) != null;
87 public boolean isNodeTunnelReady(Node bridgeNode, Node ovsdbNode) {
88 Preconditions.checkNotNull(configurationService);
89 if (!southbound.isBridgeOnOvsdbNode(ovsdbNode, configurationService.getIntegrationBridgeName())) {
90 LOG.trace("isNodeTunnelReady: node: {}, {} missing",
91 bridgeNode, configurationService.getIntegrationBridgeName());
95 return isNodeL3Ready(bridgeNode, ovsdbNode);
99 public boolean isNodeVlanReady(Node bridgeNode, Node ovsdbNode, NeutronNetwork network) {
100 Preconditions.checkNotNull(configurationService);
102 final String brInt = configurationService.getIntegrationBridgeName();
103 if (!southbound.isBridgeOnOvsdbNode(ovsdbNode, brInt)) {
104 LOG.trace("isNodeVlanReady: node: {}, {} missing", bridgeNode, brInt);
108 /* Check if physical device is added to br-int. */
109 String phyNetName = getPhysicalInterfaceName(ovsdbNode, network.getProviderPhysicalNetwork());
110 if (!isPortOnBridge(bridgeNode, phyNetName)) {
111 LOG.trace("isNodeVlanReady: node: {}, eth missing", bridgeNode);
115 return isNodeL3Ready(bridgeNode, ovsdbNode);
118 public boolean isNodeL3Ready(Node bridgeNode, Node ovsdbNode) {
119 Preconditions.checkNotNull(configurationService);
120 boolean ready = false;
121 if (configurationService.isL3ForwardingEnabled()) {
122 final String brInt = configurationService.getIntegrationBridgeName();
123 final String brExt = configurationService.getExternalBridgeName();
124 final String portNameInt = configurationService.getPatchPortName(new ImmutablePair<>(brInt, brExt));
125 final String portNameExt = configurationService.getPatchPortName(new ImmutablePair<>(brExt, brInt));
126 Preconditions.checkNotNull(portNameInt);
127 Preconditions.checkNotNull(portNameExt);
129 if (southbound.isBridgeOnOvsdbNode(ovsdbNode, brExt)) {
130 ready = isNetworkPatchCreated(bridgeNode, southbound.readBridgeNode(ovsdbNode, brExt));
132 LOG.trace("isNodeL3Ready: node: {}, {} missing",
142 public void prepareNode(Node ovsdbNode) {
143 Preconditions.checkNotNull(configurationService);
146 createIntegrationBridge(ovsdbNode);
147 } catch (Exception e) {
148 LOG.error("Error creating Integration Bridge on {}", ovsdbNode, e);
153 if (configurationService.isL3ForwardingEnabled()) {
154 createExternalBridge(ovsdbNode);
156 } catch (Exception e) {
157 LOG.error("Error creating External Bridge on {}", ovsdbNode, e);
160 // this node is an ovsdb node so it doesn't have a bridge
161 // so either look up the bridges or just wait for the bridge update to come in
162 // and add the flows there.
163 //networkingProviderManager.getProvider(node).initializeFlowRules(node);
167 * Check if the full network setup is available. If not, create it.
170 public boolean createLocalNetwork (Node bridgeNode, NeutronNetwork network) {
171 boolean isCreated = false;
172 Node ovsdbNode = southbound.readOvsdbNode(bridgeNode);
173 if (ovsdbNode == null) {
174 //this should never happen
175 LOG.error("createLocalNetwork could not find ovsdbNode from bridge node " + bridgeNode);
178 if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
179 if (!isNodeVlanReady(bridgeNode, ovsdbNode, network)) {
181 isCreated = createBridges(bridgeNode, ovsdbNode, network);
182 } catch (Exception e) {
183 LOG.error("Error creating internal vlan net network " + bridgeNode, e);
188 } else if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
189 network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)) {
190 if (!isNodeTunnelReady(bridgeNode, ovsdbNode)) {
192 isCreated = createBridges(bridgeNode, ovsdbNode, network);
193 } catch (Exception e) {
194 LOG.error("Error creating internal vxlan/gre net network " + bridgeNode, e);
206 public String getExternalInterfaceName (Node node, String extNetwork) {
208 String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
209 configurationService.getProviderMappingsKey());
210 if (providerMaps != null) {
211 for (String map : providerMaps.split(",")) {
212 String[] pair = map.split(":");
213 if (pair[0].equals(extNetwork)) {
220 LOG.error("External interface not found for Node: {}, Network {}",
224 LOG.info("External interface found for Node: {}, Network {} is {}",node,extNetwork,phyIf);
232 public String getPhysicalInterfaceName (Node node, String physicalNetwork) {
234 String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
235 configurationService.getProviderMappingsKey());
236 if (providerMaps == null) {
237 providerMaps = configurationService.getDefaultProviderMapping();
240 if (providerMaps != null) {
241 for (String map : providerMaps.split(",")) {
242 String[] pair = map.split(":");
243 if (pair[0].equals(physicalNetwork)) {
251 LOG.error("Physical interface not found for Node: {}, Network {}",
252 node, physicalNetwork);
259 public List<String> getAllPhysicalInterfaceNames(Node node) {
260 List<String> phyIfName = Lists.newArrayList();
261 String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
262 configurationService.getProviderMappingsKey());
263 if (providerMaps == null) {
264 providerMaps = configurationService.getDefaultProviderMapping();
267 if (providerMaps != null) {
268 for (String map : providerMaps.split(",")) {
269 String[] pair = map.split(":");
270 phyIfName.add(pair[1]);
278 * Returns true if a patch port exists between the Integration Bridge and Network Bridge
280 private boolean isNetworkPatchCreated(Node intBridge, Node netBridge) {
281 Preconditions.checkNotNull(configurationService);
283 boolean isPatchCreated = false;
285 String portName = configurationService.getPatchPortName(new ImmutablePair<>(intBridge, netBridge));
286 if (isPortOnBridge(intBridge, portName)) {
287 portName = configurationService.getPatchPortName(new ImmutablePair<>(netBridge, intBridge));
288 if (isPortOnBridge(netBridge, portName)) {
289 isPatchCreated = true;
293 return isPatchCreated;
297 * Creates the Integration Bridge
299 private boolean createIntegrationBridge(Node ovsdbNode) {
300 Preconditions.checkNotNull(configurationService);
302 if (!addBridge(ovsdbNode, configurationService.getIntegrationBridgeName())) {
303 LOG.debug("Integration Bridge Creation failed");
309 private boolean createExternalBridge(Node ovsdbNode) {
310 Preconditions.checkNotNull(configurationService);
312 if (!addBridge(ovsdbNode, configurationService.getExternalBridgeName())) {
313 LOG.debug("External Bridge Creation failed");
320 * Create and configure bridges for all network types and OpenFlow versions.
327 options: {peer=patch-int}
337 options: {peer=patch-net}
347 options: {peer=patch-int}
355 options: {peer=patch-net}
374 private boolean createBridges(Node bridgeNode, Node ovsdbNode, NeutronNetwork network) {
375 Preconditions.checkNotNull(configurationService);
376 Preconditions.checkNotNull(networkingProviderManager);
378 LOG.debug("createBridges: node: {}, network type: {}", bridgeNode, network.getProviderNetworkType());
380 final String brInt = configurationService.getIntegrationBridgeName();
381 if (! createIntegrationBridge(ovsdbNode)) {
382 LOG.debug("{} Bridge creation failed", brInt);
386 if (configurationService.isL3ForwardingEnabled()) {
387 final String brExt = configurationService.getExternalBridgeName();
388 if (! createExternalBridge(ovsdbNode)) {
389 LOG.error("{} Bridge creation failed", brExt);
393 //get two patch port names
394 final String portNameInt = configurationService.getPatchPortName(new ImmutablePair<>(brInt, brExt));
395 final String portNameExt = configurationService.getPatchPortName(new ImmutablePair<>(brExt, brInt));
396 Preconditions.checkNotNull(portNameInt);
397 Preconditions.checkNotNull(portNameExt);
399 if (!addPatchPort(bridgeNode, brInt, portNameInt, portNameExt)) {
400 LOG.error("Add Port {} to Bridge {} failed", portNameInt, brInt);
403 Node extBridgeNode = southbound.readBridgeNode(ovsdbNode, brExt);
404 Preconditions.checkNotNull(extBridgeNode);
405 if (!addPatchPort(extBridgeNode, brExt, portNameExt, portNameInt)) {
406 LOG.error("Add Port {} to Bridge {} failed", portNameExt, brExt);
409 String extNetName = getExternalInterfaceName(extBridgeNode, brExt);
410 if ( extNetName != null) {
411 if (!addPortToBridge(extBridgeNode, brExt, extNetName)) {
412 LOG.error("Add External Port {} to Bridge {} failed", extNetName, brExt);
415 LOG.info("Add External Port {} to Ext Bridge {} success", extNetName, brExt);
418 /* For vlan network types add physical port to br-int. */
419 if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
420 String phyNetName = this.getPhysicalInterfaceName(bridgeNode, network.getProviderPhysicalNetwork());
421 if (!addPortToBridge(bridgeNode, brInt, phyNetName)) {
422 LOG.debug("Add Port {} to Bridge {} failed", phyNetName, brInt);
427 LOG.info("createBridges: node: {}, status: success", bridgeNode);
432 * Add a Port to a Bridge
434 private boolean addPortToBridge (Node node, String bridgeName, String portName) {
437 if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
438 rv = southbound.addTerminationPoint(node, bridgeName, portName, null);
441 LOG.info("addPortToBridge: node: {}, bridge: {}, portname: {} status: success",
442 node.getNodeId().getValue(), bridgeName, portName);
444 LOG.error("addPortToBridge: node: {}, bridge: {}, portname: {} status: FAILED",
445 node.getNodeId().getValue(), bridgeName, portName);
448 LOG.trace("addPortToBridge: node: {}, bridge: {}, portname: {} status: not_needed",
449 node.getNodeId().getValue(), bridgeName, portName);
456 * Add a Patch Port to a Bridge
458 private boolean addPatchPort (Node node, String bridgeName, String portName, String peerPortName) {
461 if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
462 rv = southbound.addPatchTerminationPoint(node, bridgeName, portName, peerPortName);
465 LOG.info("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: success",
466 node.getNodeId().getValue(), bridgeName, portName, peerPortName);
468 LOG.error("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: FAILED",
469 node.getNodeId().getValue(), bridgeName, portName, peerPortName);
472 LOG.trace("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: not_needed",
473 node.getNodeId().getValue(), bridgeName, portName, peerPortName);
480 * Add Bridge to a Node
482 private boolean addBridge(Node ovsdbNode, String bridgeName) {
484 if ((!southbound.isBridgeOnOvsdbNode(ovsdbNode, bridgeName)) ||
485 (southbound.getBridgeFromConfig(ovsdbNode, bridgeName) == null)) {
486 rv = southbound.addBridge(ovsdbNode, bridgeName, getControllersFromOvsdbNode(ovsdbNode));
491 private String getControllerIPAddress() {
492 String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address");
493 if (addressString != null) {
495 if (InetAddress.getByName(addressString) != null) {
496 return addressString;
498 } catch (UnknownHostException e) {
499 LOG.error("Host {} is invalid", addressString);
503 addressString = ConfigProperties.getProperty(this.getClass(), "of.address");
504 if (addressString != null) {
506 if (InetAddress.getByName(addressString) != null) {
507 return addressString;
509 } catch (UnknownHostException e) {
510 LOG.error("Host {} is invalid", addressString);
517 private short getControllerOFPort() {
518 short openFlowPort = Constants.OPENFLOW_PORT;
519 String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort");
520 if (portString != null) {
522 openFlowPort = Short.parseShort(portString);
523 } catch (NumberFormatException e) {
524 LOG.warn("Invalid port:{}, use default({})", portString,
531 private List<String> getControllersFromOvsdbNode(Node node) {
532 List<String> controllersStr = new ArrayList<>();
534 String controllerIpStr = getControllerIPAddress();
535 if (controllerIpStr != null) {
536 // If codepath makes it here, the ip address to be used was explicitly provided.
537 // Being so, also fetch openflowPort provided via ConfigProperties.
538 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
539 + ":" + controllerIpStr + ":" + getControllerOFPort());
541 // Check if ovsdb node has manager entries
542 OvsdbNodeAugmentation ovsdbNodeAugmentation = southbound.extractOvsdbNode(node);
543 if (ovsdbNodeAugmentation != null) {
544 List<ManagerEntry> managerEntries = ovsdbNodeAugmentation.getManagerEntry();
545 if (managerEntries != null && !managerEntries.isEmpty()) {
546 for (ManagerEntry managerEntry : managerEntries) {
547 if (managerEntry == null || managerEntry.getTarget() == null) {
550 String[] tokens = managerEntry.getTarget().getValue().split(":");
551 if (tokens.length == 3 && tokens[0].equalsIgnoreCase("tcp")) {
552 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
553 + ":" + tokens[1] + ":" + getControllerOFPort());
554 } else if (tokens[0].equalsIgnoreCase("ptcp")) {
555 ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
556 if (connectionInfo != null && connectionInfo.getLocalIp() != null) {
557 controllerIpStr = new String(connectionInfo.getLocalIp().getValue());
558 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
559 + ":" + controllerIpStr + ":" + Constants.OPENFLOW_PORT);
561 LOG.warn("Ovsdb Node does not contain connection info: {}", node);
564 LOG.trace("Skipping manager entry {} for node {}",
565 managerEntry.getTarget(), node.getNodeId().getValue());
569 LOG.warn("Ovsdb Node does not contain manager entries : {}", node);
574 if (controllersStr.isEmpty()) {
575 // Neither user provided ip nor ovsdb node has manager entries. Lets use local machine ip address.
576 LOG.debug("Use local machine ip address as a OpenFlow Controller ip address");
577 controllerIpStr = getLocalControllerHostIpAddress();
578 if (controllerIpStr != null) {
579 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
580 + ":" + controllerIpStr + ":" + Constants.OPENFLOW_PORT);
584 if (controllersStr.isEmpty()) {
585 LOG.warn("Failed to determine OpenFlow controller ip address");
586 } else if (LOG.isDebugEnabled()) {
587 controllerIpStr = "";
588 for (String currControllerIpStr : controllersStr) {
589 controllerIpStr += " " + currControllerIpStr;
591 LOG.debug("Found {} OpenFlow Controller(s) :{}", controllersStr.size(), controllerIpStr);
594 return controllersStr;
597 private String getLocalControllerHostIpAddress() {
598 String ipaddress = null;
600 for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
601 NetworkInterface iface = ifaces.nextElement();
603 for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
604 InetAddress inetAddr = inetAddrs.nextElement();
605 if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
606 ipaddress = inetAddr.getHostAddress();
611 }catch (Exception e){
612 LOG.warn("Exception while fetching local host ip address ", e);
618 public void setDependencies(ServiceReference serviceReference) {
619 configurationService =
620 (ConfigurationService) ServiceHelper.getGlobalInstance(ConfigurationService.class, this);
621 networkingProviderManager =
622 (NetworkingProviderManager) ServiceHelper.getGlobalInstance(NetworkingProviderManager.class, this);
624 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
628 public void setDependencies(Object impl) {