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);
204 public String getPhysicalInterfaceName (Node node, String physicalNetwork) {
206 String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
207 configurationService.getProviderMappingsKey());
208 if (providerMaps == null) {
209 providerMaps = configurationService.getDefaultProviderMapping();
212 if (providerMaps != null) {
213 for (String map : providerMaps.split(",")) {
214 String[] pair = map.split(":");
215 if (pair[0].equals(physicalNetwork)) {
223 LOG.error("Physical interface not found for Node: {}, Network {}",
224 node, physicalNetwork);
231 public List<String> getAllPhysicalInterfaceNames(Node node) {
232 List<String> phyIfName = Lists.newArrayList();
233 String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
234 configurationService.getProviderMappingsKey());
235 if (providerMaps == null) {
236 providerMaps = configurationService.getDefaultProviderMapping();
239 if (providerMaps != null) {
240 for (String map : providerMaps.split(",")) {
241 String[] pair = map.split(":");
242 phyIfName.add(pair[1]);
250 * Returns true if a patch port exists between the Integration Bridge and Network Bridge
252 private boolean isNetworkPatchCreated(Node intBridge, Node netBridge) {
253 Preconditions.checkNotNull(configurationService);
255 boolean isPatchCreated = false;
257 String portName = configurationService.getPatchPortName(new ImmutablePair<>(intBridge, netBridge));
258 if (isPortOnBridge(intBridge, portName)) {
259 portName = configurationService.getPatchPortName(new ImmutablePair<>(netBridge, intBridge));
260 if (isPortOnBridge(netBridge, portName)) {
261 isPatchCreated = true;
265 return isPatchCreated;
269 * Creates the Integration Bridge
271 private boolean createIntegrationBridge(Node ovsdbNode) {
272 Preconditions.checkNotNull(configurationService);
274 if (!addBridge(ovsdbNode, configurationService.getIntegrationBridgeName())) {
275 LOG.debug("Integration Bridge Creation failed");
281 private boolean createExternalBridge(Node ovsdbNode) {
282 Preconditions.checkNotNull(configurationService);
284 if (!addBridge(ovsdbNode, configurationService.getExternalBridgeName())) {
285 LOG.debug("External Bridge Creation failed");
292 * Create and configure bridges for all network types and OpenFlow versions.
299 options: {peer=patch-int}
309 options: {peer=patch-net}
319 options: {peer=patch-int}
327 options: {peer=patch-net}
346 private boolean createBridges(Node bridgeNode, Node ovsdbNode, NeutronNetwork network) {
347 Preconditions.checkNotNull(configurationService);
348 Preconditions.checkNotNull(networkingProviderManager);
350 LOG.debug("createBridges: node: {}, network type: {}", bridgeNode, network.getProviderNetworkType());
352 final String brInt = configurationService.getIntegrationBridgeName();
353 if (! createIntegrationBridge(ovsdbNode)) {
354 LOG.debug("{} Bridge creation failed", brInt);
358 if (configurationService.isL3ForwardingEnabled()) {
359 final String brExt = configurationService.getExternalBridgeName();
360 if (! createExternalBridge(ovsdbNode)) {
361 LOG.error("{} Bridge creation failed", brExt);
365 //get two patch port names
366 final String portNameInt = configurationService.getPatchPortName(new ImmutablePair<>(brInt, brExt));
367 final String portNameExt = configurationService.getPatchPortName(new ImmutablePair<>(brExt, brInt));
368 Preconditions.checkNotNull(portNameInt);
369 Preconditions.checkNotNull(portNameExt);
371 if (!addPatchPort(bridgeNode, brInt, portNameInt, portNameExt)) {
372 LOG.error("Add Port {} to Bridge {} failed", portNameInt, brInt);
375 Node extBridgeNode = southbound.readBridgeNode(ovsdbNode, brExt);
376 Preconditions.checkNotNull(extBridgeNode);
377 if (!addPatchPort(extBridgeNode, brExt, portNameExt, portNameInt)) {
378 LOG.error("Add Port {} to Bridge {} failed", portNameExt, brExt);
383 /* For vlan network types add physical port to br-int. */
384 if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
385 String phyNetName = this.getPhysicalInterfaceName(bridgeNode, network.getProviderPhysicalNetwork());
386 if (!addPortToBridge(bridgeNode, brInt, phyNetName)) {
387 LOG.debug("Add Port {} to Bridge {} failed", phyNetName, brInt);
392 LOG.debug("createBridges: node: {}, status: success", bridgeNode);
397 * Add a Port to a Bridge
399 private boolean addPortToBridge (Node node, String bridgeName, String portName) {
402 if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
403 rv = southbound.addTerminationPoint(node, bridgeName, portName, null);
406 LOG.info("addPortToBridge: node: {}, bridge: {}, portname: {} status: success",
407 node.getNodeId().getValue(), bridgeName, portName);
409 LOG.error("addPortToBridge: node: {}, bridge: {}, portname: {} status: FAILED",
410 node.getNodeId().getValue(), bridgeName, portName);
413 LOG.trace("addPortToBridge: node: {}, bridge: {}, portname: {} status: not_needed",
414 node.getNodeId().getValue(), bridgeName, portName);
421 * Add a Patch Port to a Bridge
423 private boolean addPatchPort (Node node, String bridgeName, String portName, String peerPortName) {
426 if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
427 rv = southbound.addPatchTerminationPoint(node, bridgeName, portName, peerPortName);
430 LOG.info("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: success",
431 node.getNodeId().getValue(), bridgeName, portName, peerPortName);
433 LOG.error("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: FAILED",
434 node.getNodeId().getValue(), bridgeName, portName, peerPortName);
437 LOG.trace("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: not_needed",
438 node.getNodeId().getValue(), bridgeName, portName, peerPortName);
445 * Add Bridge to a Node
447 private boolean addBridge(Node ovsdbNode, String bridgeName) {
449 if ((!southbound.isBridgeOnOvsdbNode(ovsdbNode, bridgeName)) ||
450 (southbound.getBridgeFromConfig(ovsdbNode, bridgeName) == null)) {
451 rv = southbound.addBridge(ovsdbNode, bridgeName, getControllersFromOvsdbNode(ovsdbNode));
456 private String getControllerIPAddress() {
457 String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address");
458 if (addressString != null) {
460 if (InetAddress.getByName(addressString) != null) {
461 return addressString;
463 } catch (UnknownHostException e) {
464 LOG.error("Host {} is invalid", addressString);
468 addressString = ConfigProperties.getProperty(this.getClass(), "of.address");
469 if (addressString != null) {
471 if (InetAddress.getByName(addressString) != null) {
472 return addressString;
474 } catch (UnknownHostException e) {
475 LOG.error("Host {} is invalid", addressString);
482 private short getControllerOFPort() {
483 short openFlowPort = Constants.OPENFLOW_PORT;
484 String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort");
485 if (portString != null) {
487 openFlowPort = Short.parseShort(portString);
488 } catch (NumberFormatException e) {
489 LOG.warn("Invalid port:{}, use default({})", portString,
496 private List<String> getControllersFromOvsdbNode(Node node) {
497 List<String> controllersStr = new ArrayList<>();
499 String controllerIpStr = getControllerIPAddress();
500 if (controllerIpStr != null) {
501 // If codepath makes it here, the ip address to be used was explicitly provided.
502 // Being so, also fetch openflowPort provided via ConfigProperties.
503 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
504 + ":" + controllerIpStr + ":" + getControllerOFPort());
506 // Check if ovsdb node has manager entries
507 OvsdbNodeAugmentation ovsdbNodeAugmentation = southbound.extractOvsdbNode(node);
508 if (ovsdbNodeAugmentation != null) {
509 List<ManagerEntry> managerEntries = ovsdbNodeAugmentation.getManagerEntry();
510 if (managerEntries != null && !managerEntries.isEmpty()) {
511 for (ManagerEntry managerEntry : managerEntries) {
512 if (managerEntry == null || managerEntry.getTarget() == null) {
515 String[] tokens = managerEntry.getTarget().getValue().split(":");
516 if (tokens.length == 3 && tokens[0].equalsIgnoreCase("tcp")) {
517 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
518 + ":" + tokens[1] + ":" + getControllerOFPort());
519 } else if (tokens[0].equalsIgnoreCase("ptcp")) {
520 ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
521 if (connectionInfo != null && connectionInfo.getLocalIp() != null) {
522 controllerIpStr = new String(connectionInfo.getLocalIp().getValue());
523 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
524 + ":" + controllerIpStr + ":" + Constants.OPENFLOW_PORT);
526 LOG.warn("Ovsdb Node does not contain connection info: {}", node);
529 LOG.trace("Skipping manager entry {} for node {}",
530 managerEntry.getTarget(), node.getNodeId().getValue());
534 LOG.warn("Ovsdb Node does not contain manager entries : {}", node);
539 if (controllersStr.isEmpty()) {
540 // Neither user provided ip nor ovsdb node has manager entries. Lets use local machine ip address.
541 LOG.debug("Use local machine ip address as a OpenFlow Controller ip address");
542 controllerIpStr = getLocalControllerHostIpAddress();
543 if (controllerIpStr != null) {
544 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
545 + ":" + controllerIpStr + ":" + Constants.OPENFLOW_PORT);
549 if (controllersStr.isEmpty()) {
550 LOG.warn("Failed to determine OpenFlow controller ip address");
551 } else if (LOG.isDebugEnabled()) {
552 controllerIpStr = "";
553 for (String currControllerIpStr : controllersStr) {
554 controllerIpStr += " " + currControllerIpStr;
556 LOG.debug("Found {} OpenFlow Controller(s) :{}", controllersStr.size(), controllerIpStr);
559 return controllersStr;
562 private String getLocalControllerHostIpAddress() {
563 String ipaddress = null;
565 for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
566 NetworkInterface iface = ifaces.nextElement();
568 for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
569 InetAddress inetAddr = inetAddrs.nextElement();
570 if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
571 ipaddress = inetAddr.getHostAddress();
576 }catch (Exception e){
577 LOG.warn("Exception while fetching local host ip address ", e);
583 public void setDependencies(ServiceReference serviceReference) {
584 configurationService =
585 (ConfigurationService) ServiceHelper.getGlobalInstance(ConfigurationService.class, this);
586 networkingProviderManager =
587 (NetworkingProviderManager) ServiceHelper.getGlobalInstance(NetworkingProviderManager.class, this);
589 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
593 public void setDependencies(Object impl) {