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.neutron.spi.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.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 import com.google.common.base.Preconditions;
27 import com.google.common.collect.Lists;
29 import java.net.InetAddress;
30 import java.net.NetworkInterface;
31 import java.net.UnknownHostException;
32 import java.util.Enumeration;
33 import java.util.List;
35 import org.apache.commons.lang3.tuple.ImmutablePair;
36 import org.osgi.framework.BundleContext;
37 import org.osgi.framework.ServiceReference;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * @author Madhu Venugopal
43 * @author Brent Salisbury
44 * @author Sam Hague (shague@redhat.com)
46 public class BridgeConfigurationManagerImpl implements BridgeConfigurationManager, ConfigInterface {
47 private static final Logger LOG = LoggerFactory.getLogger(BridgeConfigurationManagerImpl.class);
49 // The implementation for each of these services is resolved by the OSGi Service Manager
50 private volatile ConfigurationService configurationService;
51 private volatile NetworkingProviderManager networkingProviderManager;
52 private volatile Southbound southbound;
54 public void setConfigurationService(ConfigurationService configurationService) {
55 this.configurationService = configurationService;
58 public void setSouthbound(Southbound southbound) {
59 this.southbound = southbound;
63 public String getBridgeUuid(Node node, String bridgeName) {
64 return southbound.getBridgeUuid(node, bridgeName);
68 public boolean isNodeNeutronReady(Node node) {
69 Preconditions.checkNotNull(configurationService);
70 return southbound.getBridge(node, configurationService.getIntegrationBridgeName()) != null;
74 public boolean isNodeOverlayReady(Node node) {
75 Preconditions.checkNotNull(configurationService);
76 return isNodeNeutronReady(node)
77 && southbound.getBridge(node, configurationService.getNetworkBridgeName()) != null;
81 public boolean isPortOnBridge (Node bridgeNode, String portName) {
82 return southbound.extractTerminationPointAugmentation(bridgeNode, portName) != null;
86 public boolean isNodeTunnelReady(Node bridgeNode, Node ovsdbNode) {
87 Preconditions.checkNotNull(configurationService);
88 if (!southbound.isBridgeOnOvsdbNode(ovsdbNode, configurationService.getIntegrationBridgeName())) {
89 LOG.trace("isNodeTunnelReady: node: {}, {} missing",
90 bridgeNode, configurationService.getIntegrationBridgeName());
94 return isNodeL3Ready(bridgeNode, ovsdbNode);
98 public boolean isNodeVlanReady(Node bridgeNode, Node ovsdbNode, NeutronNetwork network) {
99 Preconditions.checkNotNull(configurationService);
101 final String brInt = configurationService.getIntegrationBridgeName();
102 if (!southbound.isBridgeOnOvsdbNode(ovsdbNode, brInt)) {
103 LOG.trace("isNodeVlanReady: node: {}, {} missing", bridgeNode, brInt);
107 /* Check if physical device is added to br-int. */
108 String phyNetName = getPhysicalInterfaceName(ovsdbNode, network.getProviderPhysicalNetwork());
109 if (!isPortOnBridge(bridgeNode, phyNetName)) {
110 LOG.trace("isNodeVlanReady: node: {}, eth missing", bridgeNode);
114 return isNodeL3Ready(bridgeNode, ovsdbNode);
117 public boolean isNodeL3Ready(Node bridgeNode, Node ovsdbNode) {
118 Preconditions.checkNotNull(configurationService);
119 boolean ready = false;
120 if (configurationService.isL3ForwardingEnabled()) {
121 final String brInt = configurationService.getIntegrationBridgeName();
122 final String brExt = configurationService.getExternalBridgeName();
123 final String portNameInt = configurationService.getPatchPortName(new ImmutablePair<>(brInt, brExt));
124 final String portNameExt = configurationService.getPatchPortName(new ImmutablePair<>(brExt, brInt));
125 Preconditions.checkNotNull(portNameInt);
126 Preconditions.checkNotNull(portNameExt);
128 if (southbound.isBridgeOnOvsdbNode(ovsdbNode, brExt)) {
129 ready = isNetworkPatchCreated(bridgeNode, southbound.readBridgeNode(ovsdbNode, brExt));
131 LOG.trace("isNodeL3Ready: node: {}, {} missing",
141 public void prepareNode(Node ovsdbNode) {
142 Preconditions.checkNotNull(configurationService);
145 createIntegrationBridge(ovsdbNode);
146 } catch (Exception e) {
147 LOG.error("Error creating Integration Bridge on {}", ovsdbNode, e);
152 if (configurationService.isL3ForwardingEnabled()) {
153 createExternalBridge(ovsdbNode);
155 } catch (Exception e) {
156 LOG.error("Error creating External Bridge on {}", ovsdbNode, e);
159 // this node is an ovsdb node so it doesn't have a bridge
160 // so either look up the bridges or just wait for the bridge update to come in
161 // and add the flows there.
162 //networkingProviderManager.getProvider(node).initializeFlowRules(node);
166 * Check if the full network setup is available. If not, create it.
169 public boolean createLocalNetwork (Node bridgeNode, NeutronNetwork network) {
170 boolean isCreated = false;
171 Node ovsdbNode = southbound.readOvsdbNode(bridgeNode);
172 if (ovsdbNode == null) {
173 //this should never happen
174 LOG.error("createLocalNetwork could not find ovsdbNode from bridge node " + bridgeNode);
177 if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
178 if (!isNodeVlanReady(bridgeNode, ovsdbNode, network)) {
180 isCreated = createBridges(bridgeNode, ovsdbNode, network);
181 } catch (Exception e) {
182 LOG.error("Error creating internal vlan net network " + bridgeNode, e);
187 } else if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
188 network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)) {
189 if (!isNodeTunnelReady(bridgeNode, ovsdbNode)) {
191 isCreated = createBridges(bridgeNode, ovsdbNode, network);
192 } catch (Exception e) {
193 LOG.error("Error creating internal vxlan/gre net network " + bridgeNode, e);
203 public String getPhysicalInterfaceName (Node node, String physicalNetwork) {
205 String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
206 configurationService.getProviderMappingsKey());
207 if (providerMaps == null) {
208 providerMaps = configurationService.getDefaultProviderMapping();
211 if (providerMaps != null) {
212 for (String map : providerMaps.split(",")) {
213 String[] pair = map.split(":");
214 if (pair[0].equals(physicalNetwork)) {
222 LOG.error("Physical interface not found for Node: {}, Network {}",
223 node, physicalNetwork);
230 public List<String> getAllPhysicalInterfaceNames(Node node) {
231 List<String> phyIfName = Lists.newArrayList();
232 String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
233 configurationService.getProviderMappingsKey());
234 if (providerMaps == null) {
235 providerMaps = configurationService.getDefaultProviderMapping();
238 if (providerMaps != null) {
239 for (String map : providerMaps.split(",")) {
240 String[] pair = map.split(":");
241 phyIfName.add(pair[1]);
249 * Returns true if a patch port exists between the Integration Bridge and Network Bridge
251 private boolean isNetworkPatchCreated(Node intBridge, Node netBridge) {
252 Preconditions.checkNotNull(configurationService);
254 boolean isPatchCreated = false;
256 String portName = configurationService.getPatchPortName(new ImmutablePair<>(intBridge, netBridge));
257 if (isPortOnBridge(intBridge, portName)) {
258 portName = configurationService.getPatchPortName(new ImmutablePair<>(netBridge, intBridge));
259 if (isPortOnBridge(netBridge, portName)) {
260 isPatchCreated = true;
264 return isPatchCreated;
268 * Creates the Integration Bridge
270 private boolean createIntegrationBridge(Node ovsdbNode) {
271 Preconditions.checkNotNull(configurationService);
273 if (!addBridge(ovsdbNode, configurationService.getIntegrationBridgeName())) {
274 LOG.debug("Integration Bridge Creation failed");
280 private boolean createExternalBridge(Node ovsdbNode) {
281 Preconditions.checkNotNull(configurationService);
283 if (!addBridge(ovsdbNode, configurationService.getExternalBridgeName())) {
284 LOG.debug("External Bridge Creation failed");
291 * Create and configure bridges for all network types and OpenFlow versions.
298 options: {peer=patch-int}
308 options: {peer=patch-net}
318 options: {peer=patch-int}
326 options: {peer=patch-net}
345 private boolean createBridges(Node bridgeNode, Node ovsdbNode, NeutronNetwork network) {
346 Preconditions.checkNotNull(configurationService);
347 Preconditions.checkNotNull(networkingProviderManager);
349 LOG.debug("createBridges: node: {}, network type: {}", bridgeNode, network.getProviderNetworkType());
351 final String brInt = configurationService.getIntegrationBridgeName();
352 if (! createIntegrationBridge(ovsdbNode)) {
353 LOG.debug("{} Bridge creation failed", brInt);
357 if (configurationService.isL3ForwardingEnabled()) {
358 final String brExt = configurationService.getExternalBridgeName();
359 if (! createExternalBridge(ovsdbNode)) {
360 LOG.error("{} Bridge creation failed", brExt);
364 //get two patch port names
365 final String portNameInt = configurationService.getPatchPortName(new ImmutablePair<>(brInt, brExt));
366 final String portNameExt = configurationService.getPatchPortName(new ImmutablePair<>(brExt, brInt));
367 Preconditions.checkNotNull(portNameInt);
368 Preconditions.checkNotNull(portNameExt);
370 if (!addPatchPort(bridgeNode, brInt, portNameInt, portNameExt)) {
371 LOG.error("Add Port {} to Bridge {} failed", portNameInt, brInt);
374 Node extBridgeNode = southbound.readBridgeNode(ovsdbNode, brExt);
375 Preconditions.checkNotNull(extBridgeNode);
376 if (!addPatchPort(extBridgeNode, brExt, portNameExt, portNameInt)) {
377 LOG.error("Add Port {} to Bridge {} failed", portNameExt, brExt);
382 /* For vlan network types add physical port to br-int. */
383 if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
384 String phyNetName = this.getPhysicalInterfaceName(bridgeNode, network.getProviderPhysicalNetwork());
385 if (!addPortToBridge(bridgeNode, brInt, phyNetName)) {
386 LOG.debug("Add Port {} to Bridge {} failed", phyNetName, brInt);
391 LOG.debug("createBridges: node: {}, status: success", bridgeNode);
396 * Add a Port to a Bridge
398 private boolean addPortToBridge (Node node, String bridgeName, String portName) {
401 if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
402 rv = southbound.addTerminationPoint(node, bridgeName, portName, null);
405 LOG.info("addPortToBridge: node: {}, bridge: {}, portname: {} status: success",
406 node.getNodeId().getValue(), bridgeName, portName);
408 LOG.error("addPortToBridge: node: {}, bridge: {}, portname: {} status: FAILED",
409 node.getNodeId().getValue(), bridgeName, portName);
412 LOG.trace("addPortToBridge: node: {}, bridge: {}, portname: {} status: not_needed",
413 node.getNodeId().getValue(), bridgeName, portName);
420 * Add a Patch Port to a Bridge
422 private boolean addPatchPort (Node node, String bridgeName, String portName, String peerPortName) {
425 if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
426 rv = southbound.addPatchTerminationPoint(node, bridgeName, portName, peerPortName);
429 LOG.info("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: success",
430 node.getNodeId().getValue(), bridgeName, portName, peerPortName);
432 LOG.error("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: FAILED",
433 node.getNodeId().getValue(), bridgeName, portName, peerPortName);
436 LOG.trace("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: not_needed",
437 node.getNodeId().getValue(), bridgeName, portName, peerPortName);
444 * Add Bridge to a Node
446 private boolean addBridge(Node ovsdbNode, String bridgeName) {
448 if ((!southbound.isBridgeOnOvsdbNode(ovsdbNode, bridgeName)) ||
449 (southbound.getBridgeFromConfig(ovsdbNode, bridgeName) == null)) {
450 rv = southbound.addBridge(ovsdbNode, bridgeName, getControllerTarget(ovsdbNode));
455 private String getControllerIPAddress() {
456 String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address");
457 if (addressString != null) {
459 if (InetAddress.getByName(addressString) != null) {
460 return addressString;
462 } catch (UnknownHostException e) {
463 LOG.error("Host {} is invalid", addressString);
467 addressString = ConfigProperties.getProperty(this.getClass(), "of.address");
468 if (addressString != null) {
470 if (InetAddress.getByName(addressString) != null) {
471 return addressString;
473 } catch (UnknownHostException e) {
474 LOG.error("Host {} is invalid", addressString);
481 private short getControllerOFPort() {
482 short openFlowPort = Constants.OPENFLOW_PORT;
483 String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort");
484 if (portString != null) {
486 openFlowPort = Short.parseShort(portString);
487 } catch (NumberFormatException e) {
488 LOG.warn("Invalid port:{}, use default({})", portString,
495 private String getControllerTarget(Node node) {
496 String setControllerStr = null;
497 short openflowPort = Constants.OPENFLOW_PORT;
498 //Look at user configuration.
499 //TODO: In case we move to config subsystem to expose these user facing parameter,
500 // we will have to modify this code.
502 String controllerIpStr = getControllerIPAddress();
504 if(controllerIpStr == null){
505 // Check if ovsdb node has connection info
506 OvsdbNodeAugmentation ovsdbNodeAugmentation = southbound.extractOvsdbNode(node);
507 if (ovsdbNodeAugmentation != null) {
508 ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
509 if(connectionInfo != null && connectionInfo.getLocalIp() != null) {
510 controllerIpStr = new String(connectionInfo.getLocalIp().getValue());
512 LOG.warn("Ovsdb Node does not contains connection info : {}", node);
516 openflowPort = getControllerOFPort();
519 if(controllerIpStr == null) {
520 // Neither user provided ip nor ovsdb node has controller ip, Lets use local machine ip address
521 LOG.debug("Use local machine ip address as a OpenFlow Controller ip address");
522 controllerIpStr = getLocalControllerHostIpAddress();
524 if(controllerIpStr != null){
525 LOG.debug("Targe OpenFlow Controller found : {}", controllerIpStr);
526 setControllerStr = Constants.OPENFLOW_CONNECTION_PROTOCOL + ":" + controllerIpStr + ":" + openflowPort;
528 LOG.warn("Failed to determine OpenFlow controller ip address");
530 return setControllerStr;
533 private String getLocalControllerHostIpAddress() {
534 String ipaddress = null;
536 for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
537 NetworkInterface iface = ifaces.nextElement();
539 for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
540 InetAddress inetAddr = inetAddrs.nextElement();
541 if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
542 ipaddress = inetAddr.getHostAddress();
547 }catch (Exception e){
548 LOG.warn("Exception while fetching local host ip address ", e);
554 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
555 configurationService =
556 (ConfigurationService) ServiceHelper.getGlobalInstance(ConfigurationService.class, this);
557 networkingProviderManager =
558 (NetworkingProviderManager) ServiceHelper.getGlobalInstance(NetworkingProviderManager.class, this);
560 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
564 public void setDependencies(Object impl) {