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.NetworkHandler;
13 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
14 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
15 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProviderManager;
16 import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbTables;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
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;
49 private volatile Southbound southbound;
51 public void setConfigurationService(ConfigurationService configurationService) {
52 this.configurationService = configurationService;
55 public void setSouthbound(Southbound southbound) {
56 this.southbound = southbound;
60 public String getBridgeUuid(Node node, String bridgeName) {
61 return southbound.getBridgeUuid(node, bridgeName);
65 public boolean isNodeNeutronReady(Node node) {
66 Preconditions.checkNotNull(configurationService);
67 return southbound.getBridge(node, configurationService.getIntegrationBridgeName()) != null;
71 public boolean isNodeOverlayReady(Node node) {
72 Preconditions.checkNotNull(configurationService);
73 return isNodeNeutronReady(node)
74 && southbound.getBridge(node, configurationService.getNetworkBridgeName()) != null;
78 public boolean isPortOnBridge (Node node, String portName) {
79 return southbound.extractTerminationPointAugmentation(node, portName) != null;
83 public boolean isNodeTunnelReady(Node node) {
84 Preconditions.checkNotNull(configurationService);
85 return southbound.getBridge(node, configurationService.getIntegrationBridgeName()) != null;
89 public boolean isNodeVlanReady(Node node, NeutronNetwork network) {
90 Preconditions.checkNotNull(configurationService);
92 /* is br-int created */
93 OvsdbBridgeAugmentation intBridge = southbound.getBridge(node, configurationService.getIntegrationBridgeName());
94 if (intBridge == null) {
95 LOGGER.trace("isNodeVlanReady: node: {}, br-int missing", node);
99 /* Check if physical device is added to br-int. */
100 String phyNetName = getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork());
101 if (southbound.extractTerminationPointAugmentation(node, phyNetName) == null) {
102 LOGGER.trace("isNodeVlanReady: node: {}, eth missing", node);
110 public void prepareNode(Node node) {
111 //Preconditions.checkNotNull(networkingProviderManager);
114 createIntegrationBridge(node);
115 } catch (Exception e) {
116 LOGGER.error("Error creating Integration Bridge on {}", node, e);
119 // this node is an ovsdb node so it doesn't have a bridge
120 // so either look up the bridges or just wait for the bridge update to come in
121 // and add the flows there.
122 //networkingProviderManager.getProvider(node).initializeFlowRules(node);
126 * Check if the full network setup is available. If not, create it.
129 public boolean createLocalNetwork (Node node, NeutronNetwork network) {
130 boolean isCreated = false;
131 if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
132 if (!isNodeVlanReady(node, network)) {
134 isCreated = createBridges(node, network);
135 } catch (Exception e) {
136 LOGGER.error("Error creating internal net network " + node, e);
141 } else if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
142 network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)) {
143 if (!isNodeTunnelReady(node)) {
145 isCreated = createBridges(node, network);
146 } catch (Exception e) {
147 LOGGER.error("Error creating internal net network " + node, e);
157 public String getPhysicalInterfaceName (Node node, String physicalNetwork) {
159 String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
160 configurationService.getProviderMappingsKey());
161 if (providerMaps == null) {
162 providerMaps = configurationService.getDefaultProviderMapping();
165 if (providerMaps != null) {
166 for (String map : providerMaps.split(",")) {
167 String[] pair = map.split(":");
168 if (pair[0].equals(physicalNetwork)) {
176 LOGGER.error("Physical interface not found for Node: {}, Network {}",
177 node, physicalNetwork);
184 public List<String> getAllPhysicalInterfaceNames(Node node) {
185 List<String> phyIfName = Lists.newArrayList();
187 String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
188 configurationService.getProviderMappingsKey());
189 if (providerMaps == null) {
190 providerMaps = configurationService.getDefaultProviderMapping();
193 if (providerMaps != null) {
194 for (String map : providerMaps.split(",")) {
195 String[] pair = map.split(":");
196 phyIfName.add(pair[1]);
204 * Returns true if a patch port exists between the Integration Bridge and Network Bridge
206 private boolean isNetworkPatchCreated(Node node, Node intBridge, Node netBridge) {
207 Preconditions.checkNotNull(configurationService);
209 boolean isPatchCreated = false;
211 String portName = configurationService.getPatchPortName(new ImmutablePair<>(intBridge, netBridge));
212 if (isPortOnBridge(intBridge, portName)) {
213 portName = configurationService.getPatchPortName(new ImmutablePair<>(netBridge, intBridge));
214 if (isPortOnBridge(netBridge, portName)) {
215 isPatchCreated = true;
219 return isPatchCreated;
223 * Creates the Integration Bridge
225 private void createIntegrationBridge(Node node) throws Exception {
226 Preconditions.checkNotNull(configurationService);
228 String brIntName = configurationService.getIntegrationBridgeName();
230 if (!addBridge(node, brIntName, null, null)) {
231 LOGGER.debug("Integration Bridge Creation failed");
236 * Create and configure bridges for all network types and OpenFlow versions.
243 options: {peer=patch-int}
253 options: {peer=patch-net}
263 options: {peer=patch-int}
271 options: {peer=patch-net}
290 private boolean createBridges(Node node, NeutronNetwork network) throws Exception {
291 Preconditions.checkNotNull(configurationService);
292 Preconditions.checkNotNull(networkingProviderManager);
294 LOGGER.debug("createBridges: node: {}, network type: {}", node, network.getProviderNetworkType());
296 String brInt = configurationService.getIntegrationBridgeName();
297 if (!addBridge(node, brInt, null, null)) {
298 LOGGER.debug("{} Bridge creation failed", brInt);
302 /* For vlan network types add physical port to br-int. */
303 if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
304 String phyNetName = this.getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork());
305 if (!addPortToBridge(node, brInt, phyNetName)) {
306 LOGGER.debug("Add Port {} to Bridge {} failed", phyNetName, brInt);
311 LOGGER.debug("createBridges: node: {}, status: success", node);
316 * Add a Port to a Bridge
318 private boolean addPortToBridge (Node node, String bridgeName, String portName) throws Exception {
321 if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
322 rv = southbound.addTerminationPoint(node, bridgeName, portName, null);
329 * Add a Patch Port to a Bridge
331 private boolean addPatchPort (Node node, String bridgeName, String portName, String peerPortName) throws Exception {
334 if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
335 rv = southbound.addPatchTerminationPoint(node, bridgeName, portName, peerPortName);
342 * Add Bridge to a Node
344 private boolean addBridge(Node node, String bridgeName,
345 String localPatchName, String remotePatchName) throws Exception {
347 if (southbound.getBridge(node, bridgeName) == null) {
348 rv = southbound.addBridge(node, bridgeName, getControllerTarget(node));
353 private InetAddress getControllerIPAddress() {
354 InetAddress controllerIP = null;
356 String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address");
357 if (addressString != null) {
359 controllerIP = InetAddress.getByName(addressString);
360 if (controllerIP != null) {
363 } catch (UnknownHostException e) {
364 LOGGER.error("Host {} is invalid", addressString);
368 addressString = ConfigProperties.getProperty(this.getClass(), "of.address");
369 if (addressString != null) {
371 controllerIP = InetAddress.getByName(addressString);
372 if (controllerIP != null) {
375 } catch (UnknownHostException e) {
376 LOGGER.error("Host {} is invalid", addressString);
382 controllerIP = connection.getClient().getConnectionInfo().getLocalAddress();
384 } catch (Exception e) {
385 LOGGER.debug("Invalid connection provided to getControllerIPAddresses", e);
389 if (addressString != null) {
391 controllerIP = InetAddress.getByName(addressString);
392 if (controllerIP != null) {
395 } catch (UnknownHostException e) {
396 LOGGER.error("Host {} is invalid", addressString);
403 private short getControllerOFPort() {
404 Short defaultOpenFlowPort = 6633;
405 Short openFlowPort = defaultOpenFlowPort;
406 String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort");
407 if (portString != null) {
409 openFlowPort = Short.decode(portString).shortValue();
410 } catch (NumberFormatException e) {
411 LOGGER.warn("Invalid port:{}, use default({})", portString,
418 private String getControllerTarget(Node node) {
419 String target = null;
420 OvsdbNodeAugmentation ovsdbNodeAugmentation = southbound.extractOvsdbNode(node);
421 if (ovsdbNodeAugmentation != null) {
422 ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
423 String addressStr = new String(connectionInfo.getLocalIp().getValue());
424 target = "tcp:" + addressStr + ":6633";
426 target = getControllerTarget();
431 private String getControllerTarget() {
433 * hardcoding value, need to find better way to get local ip
435 //String target = "tcp:" + getControllerIPAddress() + ":" + getControllerOFPort();
436 //TODO: dirty fix, need to remove it once we have proper solution
437 String ipaddress = null;
439 for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
440 NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
442 for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
443 InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
444 if (!inetAddr.isLoopbackAddress()) {
445 if (inetAddr.isSiteLocalAddress()) {
446 ipaddress = inetAddr.getHostAddress();
452 }catch (Exception e){
453 LOGGER.warn("ROYALLY SCREWED : Exception while fetching local host ip address ",e);
455 return "tcp:"+ipaddress+":6633";
459 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
460 configurationService =
461 (ConfigurationService) ServiceHelper.getGlobalInstance(ConfigurationService.class, this);
462 networkingProviderManager =
463 (NetworkingProviderManager) ServiceHelper.getGlobalInstance(NetworkingProviderManager.class, this);
465 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
469 public void setDependencies(Object impl) {}