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.MdsalHelper;
14 import org.opendaylight.ovsdb.openstack.netvirt.NetworkHandler;
15 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
16 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProviderManager;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbTables;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
21 import org.opendaylight.ovsdb.utils.config.ConfigProperties;
22 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
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.Enumeration;
34 import java.util.List;
36 import org.apache.commons.lang3.tuple.ImmutablePair;
37 import org.osgi.framework.BundleContext;
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 //this would look better if used a method like isNetworkPatchCreated()
131 if (isPortOnBridge(bridgeNode, portNameInt)) {
132 Node extBridgeNode = southbound.readBridgeNode(ovsdbNode, brExt);
133 if (isPortOnBridge(extBridgeNode, portNameExt)) {
136 LOG.trace("isNodeL3Ready: node: {}, {} missing",
137 bridgeNode, portNameExt);
140 LOG.trace("isNodeL3Ready: node: {}, {} missing",
141 bridgeNode, portNameInt);
144 LOG.trace("isNodeL3Ready: node: {}, {} missing",
154 public void prepareNode(Node ovsdbNode) {
155 Preconditions.checkNotNull(configurationService);
158 createIntegrationBridge(ovsdbNode);
159 } catch (Exception e) {
160 LOG.error("Error creating Integration Bridge on {}", ovsdbNode, e);
165 if (configurationService.isL3ForwardingEnabled()) {
166 createExternalBridge(ovsdbNode);
168 } catch (Exception e) {
169 LOG.error("Error creating External Bridge on {}", ovsdbNode, e);
172 // this node is an ovsdb node so it doesn't have a bridge
173 // so either look up the bridges or just wait for the bridge update to come in
174 // and add the flows there.
175 //networkingProviderManager.getProvider(node).initializeFlowRules(node);
179 * Check if the full network setup is available. If not, create it.
182 public boolean createLocalNetwork (Node bridgeNode, NeutronNetwork network) {
183 boolean isCreated = false;
184 Node ovsdbNode = southbound.readOvsdbNode(bridgeNode);
185 if (ovsdbNode == null) {
186 //this should never happen
187 LOG.error("createLocalNetwork could not find ovsdbNode from bridge node " + bridgeNode);
190 if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
191 if (!isNodeVlanReady(bridgeNode, ovsdbNode, network)) {
193 isCreated = createBridges(bridgeNode, ovsdbNode, network);
194 } catch (Exception e) {
195 LOG.error("Error creating internal vlan net network " + bridgeNode, e);
200 } else if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
201 network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)) {
202 if (!isNodeTunnelReady(bridgeNode, ovsdbNode)) {
204 isCreated = createBridges(bridgeNode, ovsdbNode, network);
205 } catch (Exception e) {
206 LOG.error("Error creating internal vxlan/gre net network " + bridgeNode, e);
216 public String getPhysicalInterfaceName (Node node, String physicalNetwork) {
218 String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
219 configurationService.getProviderMappingsKey());
220 if (providerMaps == null) {
221 providerMaps = configurationService.getDefaultProviderMapping();
224 if (providerMaps != null) {
225 for (String map : providerMaps.split(",")) {
226 String[] pair = map.split(":");
227 if (pair[0].equals(physicalNetwork)) {
235 LOG.error("Physical interface not found for Node: {}, Network {}",
236 node, physicalNetwork);
243 public List<String> getAllPhysicalInterfaceNames(Node node) {
244 List<String> phyIfName = Lists.newArrayList();
245 String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
246 configurationService.getProviderMappingsKey());
247 if (providerMaps == null) {
248 providerMaps = configurationService.getDefaultProviderMapping();
251 if (providerMaps != null) {
252 for (String map : providerMaps.split(",")) {
253 String[] pair = map.split(":");
254 phyIfName.add(pair[1]);
262 * Returns true if a patch port exists between the Integration Bridge and Network Bridge
264 private boolean isNetworkPatchCreated(Node node, Node intBridge, Node netBridge) {
265 Preconditions.checkNotNull(configurationService);
267 boolean isPatchCreated = false;
269 String portName = configurationService.getPatchPortName(new ImmutablePair<>(intBridge, netBridge));
270 if (isPortOnBridge(intBridge, portName)) {
271 portName = configurationService.getPatchPortName(new ImmutablePair<>(netBridge, intBridge));
272 if (isPortOnBridge(netBridge, portName)) {
273 isPatchCreated = true;
277 return isPatchCreated;
281 * Creates the Integration Bridge
283 private boolean createIntegrationBridge(Node ovsdbNode) throws Exception {
284 Preconditions.checkNotNull(configurationService);
286 if (!addBridge(ovsdbNode, configurationService.getIntegrationBridgeName())) {
287 LOG.debug("Integration Bridge Creation failed");
293 private boolean createExternalBridge(Node ovsdbNode) throws Exception {
294 Preconditions.checkNotNull(configurationService);
296 if (!addBridge(ovsdbNode, configurationService.getExternalBridgeName())) {
297 LOG.debug("External Bridge Creation failed");
304 * Create and configure bridges for all network types and OpenFlow versions.
311 options: {peer=patch-int}
321 options: {peer=patch-net}
331 options: {peer=patch-int}
339 options: {peer=patch-net}
358 private boolean createBridges(Node bridgeNode, Node ovsdbNode, NeutronNetwork network) throws Exception {
359 Preconditions.checkNotNull(configurationService);
360 Preconditions.checkNotNull(networkingProviderManager);
362 LOG.debug("createBridges: node: {}, network type: {}", bridgeNode, network.getProviderNetworkType());
364 final String brInt = configurationService.getIntegrationBridgeName();
365 if (! createIntegrationBridge(ovsdbNode)) {
366 LOG.debug("{} Bridge creation failed", brInt);
370 if (configurationService.isL3ForwardingEnabled()) {
371 final String brExt = configurationService.getExternalBridgeName();
372 if (! createExternalBridge(ovsdbNode)) {
373 LOG.error("{} Bridge creation failed", brExt);
377 //get two patch port names
378 final String portNameInt = configurationService.getPatchPortName(new ImmutablePair<>(brInt, brExt));
379 final String portNameExt = configurationService.getPatchPortName(new ImmutablePair<>(brExt, brInt));
380 Preconditions.checkNotNull(portNameInt);
381 Preconditions.checkNotNull(portNameExt);
383 if (!addPatchPort(bridgeNode, brInt, portNameInt, portNameExt)) {
384 LOG.error("Add Port {} to Bridge {} failed", portNameInt, brInt);
387 Node extBridgeNode = southbound.readBridgeNode(ovsdbNode, brExt);
388 Preconditions.checkNotNull(extBridgeNode);
389 if (!addPatchPort(extBridgeNode, brExt, portNameExt, portNameInt)) {
390 LOG.error("Add Port {} to Bridge {} failed", portNameExt, brExt);
395 /* For vlan network types add physical port to br-int. */
396 if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
397 String phyNetName = this.getPhysicalInterfaceName(bridgeNode, network.getProviderPhysicalNetwork());
398 if (!addPortToBridge(bridgeNode, brInt, phyNetName)) {
399 LOG.debug("Add Port {} to Bridge {} failed", phyNetName, brInt);
404 LOG.debug("createBridges: node: {}, status: success", bridgeNode);
409 * Add a Port to a Bridge
411 private boolean addPortToBridge (Node node, String bridgeName, String portName) throws Exception {
414 if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
415 rv = southbound.addTerminationPoint(node, bridgeName, portName, null);
418 LOG.info("addPortToBridge: node: {}, bridge: {}, portname: {} status: success",
419 node.getNodeId().getValue(), bridgeName, portName);
421 LOG.error("addPortToBridge: node: {}, bridge: {}, portname: {} status: FAILED",
422 node.getNodeId().getValue(), bridgeName, portName);
425 LOG.trace("addPortToBridge: node: {}, bridge: {}, portname: {} status: not_needed",
426 node.getNodeId().getValue(), bridgeName, portName);
433 * Add a Patch Port to a Bridge
435 private boolean addPatchPort (Node node, String bridgeName, String portName, String peerPortName) throws Exception {
438 if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
439 rv = southbound.addPatchTerminationPoint(node, bridgeName, portName, peerPortName);
442 LOG.info("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: success",
443 node.getNodeId().getValue(), bridgeName, portName, peerPortName);
445 LOG.error("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: FAILED",
446 node.getNodeId().getValue(), bridgeName, portName, peerPortName);
449 LOG.trace("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: not_needed",
450 node.getNodeId().getValue(), bridgeName, portName, peerPortName);
457 * Add Bridge to a Node
459 private boolean addBridge(Node ovsdbNode, String bridgeName) throws Exception {
461 if ((!southbound.isBridgeOnOvsdbNode(ovsdbNode, bridgeName)) ||
462 (southbound.getBridgeFromConfig(ovsdbNode, bridgeName) == null)) {
463 rv = southbound.addBridge(ovsdbNode, bridgeName, getControllerTarget(ovsdbNode));
468 private String getControllerIPAddress() {
469 InetAddress controllerIP = null;
471 String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address");
472 if (addressString != null) {
474 controllerIP = InetAddress.getByName(addressString);
475 if (controllerIP != null) {
476 return addressString;
478 } catch (UnknownHostException e) {
479 LOG.error("Host {} is invalid", addressString);
483 addressString = ConfigProperties.getProperty(this.getClass(), "of.address");
484 if (addressString != null) {
486 controllerIP = InetAddress.getByName(addressString);
487 if (controllerIP != null) {
488 return addressString;
490 } catch (UnknownHostException e) {
491 LOG.error("Host {} is invalid", addressString);
498 private short getControllerOFPort() {
499 short openFlowPort = Constants.OPENFLOW_PORT;
500 String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort");
501 if (portString != null) {
503 openFlowPort = Short.parseShort(portString);
504 } catch (NumberFormatException e) {
505 LOG.warn("Invalid port:{}, use default({})", portString,
512 private String getControllerTarget(Node node) {
513 String setControllerStr = null;
514 short openflowPort = Constants.OPENFLOW_PORT;
515 //Look at user configuration.
516 //TODO: In case we move to config subsystem to expose these user facing parameter,
517 // we will have to modify this code.
519 String controllerIpStr = getControllerIPAddress();
521 if(controllerIpStr == null){
522 // Check if ovsdb node has connection info
523 OvsdbNodeAugmentation ovsdbNodeAugmentation = southbound.extractOvsdbNode(node);
524 if (ovsdbNodeAugmentation != null) {
525 ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
526 if(connectionInfo != null && connectionInfo.getLocalIp() != null) {
527 controllerIpStr = new String(connectionInfo.getLocalIp().getValue());
529 LOG.warn("Ovsdb Node does not contains connection info : {}", node);
533 openflowPort = getControllerOFPort();
536 if(controllerIpStr == null) {
537 // Neither user provided ip nor ovsdb node has controller ip, Lets use local machine ip address
538 LOG.debug("Use local machine ip address as a OpenFlow Controller ip address");
539 controllerIpStr = getLocalControllerHostIpAddress();
541 if(controllerIpStr != null){
542 LOG.debug("Targe OpenFlow Controller found : {}", controllerIpStr);
543 setControllerStr = Constants.OPENFLOW_CONNECTION_PROTOCOL + ":" + controllerIpStr + ":" + openflowPort;
545 LOG.warn("Failed to determine OpenFlow controller ip address");
547 return setControllerStr;
550 private String getLocalControllerHostIpAddress() {
551 String ipaddress = null;
553 for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
554 NetworkInterface iface = ifaces.nextElement();
556 for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
557 InetAddress inetAddr = inetAddrs.nextElement();
558 if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
559 ipaddress = inetAddr.getHostAddress();
564 }catch (Exception e){
565 LOG.warn("Exception while fetching local host ip address ", e);
571 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
572 configurationService =
573 (ConfigurationService) ServiceHelper.getGlobalInstance(ConfigurationService.class, this);
574 networkingProviderManager =
575 (NetworkingProviderManager) ServiceHelper.getGlobalInstance(NetworkingProviderManager.class, this);
577 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
581 public void setDependencies(Object impl) {