2 * Copyright (C) 2013 Red Hat, Inc.
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
8 package org.opendaylight.ovsdb.openstack.netvirt.impl;
10 import org.opendaylight.neutron.spi.NeutronNetwork;
11 import org.opendaylight.ovsdb.openstack.netvirt.ConfigInterface;
12 import org.opendaylight.ovsdb.openstack.netvirt.MdsalUtils;
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.NetworkingProviderManager;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbTables;
18 import org.opendaylight.ovsdb.utils.config.ConfigProperties;
19 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
25 import com.google.common.base.Preconditions;
26 import com.google.common.collect.Lists;
27 import java.net.InetAddress;
28 import java.net.NetworkInterface;
29 import java.net.UnknownHostException;
30 import java.util.Enumeration;
31 import java.util.List;
32 import org.apache.commons.lang3.tuple.ImmutablePair;
33 import org.osgi.framework.BundleContext;
34 import org.osgi.framework.ServiceReference;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * @author Madhu Venugopal
40 * @author Brent Salisbury
41 * @author Sam Hague (shague@redhat.com)
43 public class BridgeConfigurationManagerImpl implements BridgeConfigurationManager, ConfigInterface {
44 static final Logger LOGGER = LoggerFactory.getLogger(BridgeConfigurationManagerImpl.class);
46 // The implementation for each of these services is resolved by the OSGi Service Manager
47 private volatile ConfigurationService configurationService;
48 private volatile NetworkingProviderManager networkingProviderManager;
50 public void setConfigurationService(ConfigurationService configurationService) {
51 this.configurationService = configurationService;
55 public String getBridgeUuid(Node node, String bridgeName) {
56 return MdsalUtils.getBridgeUuid(node, bridgeName);
60 public boolean isNodeNeutronReady(Node node) {
61 Preconditions.checkNotNull(configurationService);
62 return MdsalUtils.getBridge(node, configurationService.getIntegrationBridgeName()) != null;
66 public boolean isNodeOverlayReady(Node node) {
67 Preconditions.checkNotNull(configurationService);
68 return isNodeNeutronReady(node)
69 && MdsalUtils.getBridge(node, configurationService.getNetworkBridgeName()) != null;
73 public boolean isPortOnBridge (Node node, String portName) {
74 return MdsalUtils.extractTerminationPointAugmentation(node, portName) != null;
78 public boolean isNodeTunnelReady(Node node) {
79 Preconditions.checkNotNull(configurationService);
80 return MdsalUtils.getBridge(node, configurationService.getIntegrationBridgeName()) != null;
84 public boolean isNodeVlanReady(Node node, NeutronNetwork network) {
85 Preconditions.checkNotNull(configurationService);
87 /* is br-int created */
88 OvsdbBridgeAugmentation intBridge = MdsalUtils.getBridge(node, configurationService.getIntegrationBridgeName());
89 if (intBridge == null) {
90 LOGGER.trace("isNodeVlanReady: node: {}, br-int missing", node);
94 /* Check if physical device is added to br-int. */
95 String phyNetName = getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork());
96 if (MdsalUtils.extractTerminationPointAugmentation(node, phyNetName) == null) {
97 LOGGER.trace("isNodeVlanReady: node: {}, eth missing", node);
105 public void prepareNode(Node node) {
106 //Preconditions.checkNotNull(networkingProviderManager);
109 createIntegrationBridge(node);
110 } catch (Exception e) {
111 LOGGER.error("Error creating Integration Bridge on {}", node, e);
114 // this node is an ovsdb node so it doesn't have a bridge
115 // so either look up the bridges or just wait for the bridge update to come in
116 // and add the flows there.
117 //networkingProviderManager.getProvider(node).initializeFlowRules(node);
121 * Check if the full network setup is available. If not, create it.
124 public boolean createLocalNetwork (Node node, NeutronNetwork network) {
125 boolean isCreated = false;
126 if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
127 if (!isNodeVlanReady(node, network)) {
129 isCreated = createBridges(node, network);
130 } catch (Exception e) {
131 LOGGER.error("Error creating internal net network " + node, e);
136 } else if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
137 network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)) {
138 if (!isNodeTunnelReady(node)) {
140 isCreated = createBridges(node, network);
141 } catch (Exception e) {
142 LOGGER.error("Error creating internal net network " + node, e);
152 public String getPhysicalInterfaceName (Node node, String physicalNetwork) {
154 String providerMaps = MdsalUtils.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
155 configurationService.getProviderMappingsKey());
156 if (providerMaps == null) {
157 providerMaps = configurationService.getDefaultProviderMapping();
160 if (providerMaps != null) {
161 for (String map : providerMaps.split(",")) {
162 String[] pair = map.split(":");
163 if (pair[0].equals(physicalNetwork)) {
171 LOGGER.error("Physical interface not found for Node: {}, Network {}",
172 node, physicalNetwork);
179 public List<String> getAllPhysicalInterfaceNames(Node node) {
180 List<String> phyIfName = Lists.newArrayList();
182 String providerMaps = MdsalUtils.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
183 configurationService.getProviderMappingsKey());
184 if (providerMaps == null) {
185 providerMaps = configurationService.getDefaultProviderMapping();
188 if (providerMaps != null) {
189 for (String map : providerMaps.split(",")) {
190 String[] pair = map.split(":");
191 phyIfName.add(pair[1]);
199 * Returns true if a patch port exists between the Integration Bridge and Network Bridge
201 private boolean isNetworkPatchCreated(Node node, Node intBridge, Node netBridge) {
202 Preconditions.checkNotNull(configurationService);
204 boolean isPatchCreated = false;
206 String portName = configurationService.getPatchPortName(new ImmutablePair<>(intBridge, netBridge));
207 if (isPortOnBridge(intBridge, portName)) {
208 portName = configurationService.getPatchPortName(new ImmutablePair<>(netBridge, intBridge));
209 if (isPortOnBridge(netBridge, portName)) {
210 isPatchCreated = true;
214 return isPatchCreated;
218 * Creates the Integration Bridge
220 private void createIntegrationBridge(Node node) throws Exception {
221 Preconditions.checkNotNull(configurationService);
223 String brIntName = configurationService.getIntegrationBridgeName();
225 if (!addBridge(node, brIntName, null, null)) {
226 LOGGER.debug("Integration Bridge Creation failed");
231 * Create and configure bridges for all network types and OpenFlow versions.
238 options: {peer=patch-int}
248 options: {peer=patch-net}
258 options: {peer=patch-int}
266 options: {peer=patch-net}
285 private boolean createBridges(Node node, NeutronNetwork network) throws Exception {
286 Preconditions.checkNotNull(configurationService);
287 Preconditions.checkNotNull(networkingProviderManager);
289 LOGGER.debug("createBridges: node: {}, network type: {}", node, network.getProviderNetworkType());
291 String brInt = configurationService.getIntegrationBridgeName();
292 if (!addBridge(node, brInt, null, null)) {
293 LOGGER.debug("{} Bridge creation failed", brInt);
297 /* For vlan network types add physical port to br-int. */
298 if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
299 String phyNetName = this.getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork());
300 if (!addPortToBridge(node, brInt, phyNetName)) {
301 LOGGER.debug("Add Port {} to Bridge {} failed", phyNetName, brInt);
306 LOGGER.debug("createBridges: node: {}, status: success", node);
311 * Add a Port to a Bridge
313 private boolean addPortToBridge (Node node, String bridgeName, String portName) throws Exception {
316 if (MdsalUtils.extractTerminationPointAugmentation(node, portName) == null) {
317 rv = MdsalUtils.addTerminationPoint(node, bridgeName, portName, null);
324 * Add a Patch Port to a Bridge
326 private boolean addPatchPort (Node node, String bridgeName, String portName, String peerPortName) throws Exception {
329 if (MdsalUtils.extractTerminationPointAugmentation(node, portName) == null) {
330 rv = MdsalUtils.addPatchTerminationPoint(node, bridgeName, portName, peerPortName);
337 * Add Bridge to a Node
339 private boolean addBridge(Node node, String bridgeName,
340 String localPatchName, String remotePatchName) throws Exception {
342 if (MdsalUtils.getBridge(node, bridgeName) == null) {
343 rv = MdsalUtils.addBridge(node, bridgeName, getControllerTarget(node));
348 private InetAddress getControllerIPAddress() {
349 InetAddress controllerIP = null;
351 String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address");
352 if (addressString != null) {
354 controllerIP = InetAddress.getByName(addressString);
355 if (controllerIP != null) {
358 } catch (UnknownHostException e) {
359 LOGGER.error("Host {} is invalid", addressString);
363 addressString = ConfigProperties.getProperty(this.getClass(), "of.address");
364 if (addressString != null) {
366 controllerIP = InetAddress.getByName(addressString);
367 if (controllerIP != null) {
370 } catch (UnknownHostException e) {
371 LOGGER.error("Host {} is invalid", addressString);
377 controllerIP = connection.getClient().getConnectionInfo().getLocalAddress();
379 } catch (Exception e) {
380 LOGGER.debug("Invalid connection provided to getControllerIPAddresses", e);
384 if (addressString != null) {
386 controllerIP = InetAddress.getByName(addressString);
387 if (controllerIP != null) {
390 } catch (UnknownHostException e) {
391 LOGGER.error("Host {} is invalid", addressString);
398 private short getControllerOFPort() {
399 Short defaultOpenFlowPort = 6633;
400 Short openFlowPort = defaultOpenFlowPort;
401 String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort");
402 if (portString != null) {
404 openFlowPort = Short.decode(portString).shortValue();
405 } catch (NumberFormatException e) {
406 LOGGER.warn("Invalid port:{}, use default({})", portString,
413 private String getControllerTarget(Node node) {
414 String target = null;
415 OvsdbNodeAugmentation ovsdbNodeAugmentation = MdsalUtils.extractOvsdbNode(node);
416 if (ovsdbNodeAugmentation != null) {
417 ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
418 String addressStr = new String(connectionInfo.getLocalIp().getValue());
419 target = "tcp:" + addressStr + ":6633";
421 target = getControllerTarget();
426 private String getControllerTarget() {
428 * hardcoding value, need to find better way to get local ip
430 //String target = "tcp:" + getControllerIPAddress() + ":" + getControllerOFPort();
431 //TODO: dirty fix, need to remove it once we have proper solution
432 String ipaddress = null;
434 for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
435 NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
437 for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
438 InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
439 if (!inetAddr.isLoopbackAddress()) {
440 if (inetAddr.isSiteLocalAddress()) {
441 ipaddress = inetAddr.getHostAddress();
447 }catch (Exception e){
448 LOGGER.warn("ROYALLY SCREWED : Exception while fetching local host ip address ",e);
450 return "tcp:"+ipaddress+":6633";
454 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
455 configurationService =
456 (ConfigurationService) ServiceHelper.getGlobalInstance(ConfigurationService.class, this);
457 networkingProviderManager =
458 (NetworkingProviderManager) ServiceHelper.getGlobalInstance(NetworkingProviderManager.class, this);
462 public void setDependencies(Object impl) {}