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;
51 public String getBridgeUuid(Node node, String bridgeName) {
52 return MdsalUtils.getBridgeUuid(node, bridgeName).toString();
56 public boolean isNodeNeutronReady(Node node) {
57 Preconditions.checkNotNull(configurationService);
58 return MdsalUtils.getBridge(node, configurationService.getIntegrationBridgeName()) != null;
62 public boolean isNodeOverlayReady(Node node) {
63 Preconditions.checkNotNull(configurationService);
64 return isNodeNeutronReady(node)
65 && MdsalUtils.getBridge(node, configurationService.getNetworkBridgeName()) != null;
69 public boolean isPortOnBridge (Node node, String portName) {
70 return MdsalUtils.extractTerminationPointAugmentation(node, portName) != null;
74 public boolean isNodeTunnelReady(Node node) {
75 Preconditions.checkNotNull(configurationService);
76 return MdsalUtils.getBridge(node, configurationService.getIntegrationBridgeName()) != null;
80 public boolean isNodeVlanReady(Node node, NeutronNetwork network) {
81 Preconditions.checkNotNull(networkingProviderManager);
83 /* is br-int created */
84 OvsdbBridgeAugmentation intBridge = MdsalUtils.getBridge(node, configurationService.getIntegrationBridgeName());
85 if (intBridge == null) {
86 LOGGER.trace("isNodeVlanReady: node: {}, br-int missing", node);
90 /* Check if physical device is added to br-int. */
91 String phyNetName = this.getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork());
92 if (MdsalUtils.extractTerminationPointAugmentation(node, phyNetName) == null) {
93 LOGGER.trace("isNodeVlanReady: node: {}, eth missing", node);
101 public void prepareNode(Node node) {
102 Preconditions.checkNotNull(networkingProviderManager);
105 createIntegrationBridge(node);
106 } catch (Exception e) {
107 LOGGER.error("Error creating Integration Bridge on {}", node, e);
110 // this node is an ovsdb node so it doesn't have a bridge
111 // so either look up the bridges or just wait for the bridge update to come in
112 // and add the flows there.
113 //networkingProviderManager.getProvider(node).initializeFlowRules(node);
117 * Check if the full network setup is available. If not, create it.
120 public boolean createLocalNetwork (Node node, NeutronNetwork network) {
121 boolean isCreated = false;
122 if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
123 if (!isNodeVlanReady(node, network)) {
125 isCreated = createBridges(node, network);
126 } catch (Exception e) {
127 LOGGER.error("Error creating internal net network " + node, e);
132 } else if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
133 network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)) {
134 if (!isNodeTunnelReady(node)) {
136 isCreated = createBridges(node, network);
137 } catch (Exception e) {
138 LOGGER.error("Error creating internal net network " + node, e);
148 public String getPhysicalInterfaceName (Node node, String physicalNetwork) {
150 String providerMaps = MdsalUtils.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
151 configurationService.getProviderMappingsKey());
152 if (providerMaps == null) {
153 providerMaps = configurationService.getDefaultProviderMapping();
156 if (providerMaps != null) {
157 for (String map : providerMaps.split(",")) {
158 String[] pair = map.split(":");
159 if (pair[0].equals(physicalNetwork)) {
167 LOGGER.error("Physical interface not found for Node: {}, Network {}",
168 node, physicalNetwork);
175 public List<String> getAllPhysicalInterfaceNames(Node node) {
176 List<String> phyIfName = Lists.newArrayList();
178 String providerMaps = MdsalUtils.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
179 configurationService.getProviderMappingsKey());
180 if (providerMaps == null) {
181 providerMaps = configurationService.getDefaultProviderMapping();
184 if (providerMaps != null) {
185 for (String map : providerMaps.split(",")) {
186 String[] pair = map.split(":");
187 phyIfName.add(pair[1]);
195 * Returns true if a patch port exists between the Integration Bridge and Network Bridge
197 private boolean isNetworkPatchCreated(Node node, Node intBridge, Node netBridge) {
198 Preconditions.checkNotNull(configurationService);
200 boolean isPatchCreated = false;
202 String portName = configurationService.getPatchPortName(new ImmutablePair<>(intBridge, netBridge));
203 if (isPortOnBridge(intBridge, portName)) {
204 portName = configurationService.getPatchPortName(new ImmutablePair<>(netBridge, intBridge));
205 if (isPortOnBridge(netBridge, portName)) {
206 isPatchCreated = true;
210 return isPatchCreated;
214 * Creates the Integration Bridge
216 private void createIntegrationBridge(Node node) throws Exception {
217 Preconditions.checkNotNull(configurationService);
219 String brIntName = configurationService.getIntegrationBridgeName();
221 if (!addBridge(node, brIntName, null, null)) {
222 LOGGER.debug("Integration Bridge Creation failed");
227 * Create and configure bridges for all network types and OpenFlow versions.
234 options: {peer=patch-int}
244 options: {peer=patch-net}
254 options: {peer=patch-int}
262 options: {peer=patch-net}
281 private boolean createBridges(Node node, NeutronNetwork network) throws Exception {
282 Preconditions.checkNotNull(configurationService);
283 Preconditions.checkNotNull(networkingProviderManager);
285 LOGGER.debug("createBridges: node: {}, network type: {}", node, network.getProviderNetworkType());
287 String brInt = configurationService.getIntegrationBridgeName();
288 if (!addBridge(node, brInt, null, null)) {
289 LOGGER.debug("{} Bridge creation failed", brInt);
293 /* For vlan network types add physical port to br-int. */
294 if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
295 String phyNetName = this.getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork());
296 if (!addPortToBridge(node, brInt, phyNetName)) {
297 LOGGER.debug("Add Port {} to Bridge {} failed", phyNetName, brInt);
302 LOGGER.debug("createBridges: node: {}, status: success", node);
307 * Add a Port to a Bridge
309 private boolean addPortToBridge (Node node, String bridgeName, String portName) throws Exception {
312 if (MdsalUtils.extractTerminationPointAugmentation(node, portName) == null) {
313 rv = MdsalUtils.addTerminationPoint(node, bridgeName, portName, null);
320 * Add a Patch Port to a Bridge
322 private boolean addPatchPort (Node node, String bridgeName, String portName, String peerPortName) throws Exception {
325 if (MdsalUtils.extractTerminationPointAugmentation(node, portName) == null) {
326 rv = MdsalUtils.addPatchTerminationPoint(node, bridgeName, portName, peerPortName);
333 * Add Bridge to a Node
335 private boolean addBridge(Node node, String bridgeName,
336 String localPatchName, String remotePatchName) throws Exception {
338 if (MdsalUtils.getBridge(node, bridgeName) == null) {
339 rv = MdsalUtils.addBridge(node, bridgeName, getControllerTarget(node));
344 private InetAddress getControllerIPAddress() {
345 InetAddress controllerIP = null;
347 String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address");
348 if (addressString != null) {
350 controllerIP = InetAddress.getByName(addressString);
351 if (controllerIP != null) {
354 } catch (UnknownHostException e) {
355 LOGGER.error("Host {} is invalid", addressString);
359 addressString = ConfigProperties.getProperty(this.getClass(), "of.address");
360 if (addressString != null) {
362 controllerIP = InetAddress.getByName(addressString);
363 if (controllerIP != null) {
366 } catch (UnknownHostException e) {
367 LOGGER.error("Host {} is invalid", addressString);
373 controllerIP = connection.getClient().getConnectionInfo().getLocalAddress();
375 } catch (Exception e) {
376 LOGGER.debug("Invalid connection provided to getControllerIPAddresses", e);
380 if (addressString != null) {
382 controllerIP = InetAddress.getByName(addressString);
383 if (controllerIP != null) {
386 } catch (UnknownHostException e) {
387 LOGGER.error("Host {} is invalid", addressString);
394 private short getControllerOFPort() {
395 Short defaultOpenFlowPort = 6633;
396 Short openFlowPort = defaultOpenFlowPort;
397 String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort");
398 if (portString != null) {
400 openFlowPort = Short.decode(portString).shortValue();
401 } catch (NumberFormatException e) {
402 LOGGER.warn("Invalid port:{}, use default({})", portString,
409 private String getControllerTarget(Node node) {
410 String target = null;
411 OvsdbNodeAugmentation ovsdbNodeAugmentation = MdsalUtils.extractOvsdbNode(node);
412 if (ovsdbNodeAugmentation != null) {
413 ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
414 String addressStr = new String(connectionInfo.getLocalIp().getValue());
415 target = "tcp:" + addressStr + ":6633";
417 target = getControllerTarget();
422 private String getControllerTarget() {
424 * hardcoding value, need to find better way to get local ip
426 //String target = "tcp:" + getControllerIPAddress() + ":" + getControllerOFPort();
427 //TODO: dirty fix, need to remove it once we have proper solution
428 String ipaddress = null;
430 for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
431 NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
433 for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
434 InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
435 if (!inetAddr.isLoopbackAddress()) {
436 if (inetAddr.isSiteLocalAddress()) {
437 ipaddress = inetAddr.getHostAddress();
443 }catch (Exception e){
444 LOGGER.warn("ROYALLY SCREWED : Exception while fetching local host ip address ",e);
446 return "tcp:"+ipaddress+":6633";
450 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
451 configurationService =
452 (ConfigurationService) ServiceHelper.getGlobalInstance(ConfigurationService.class, this);
453 networkingProviderManager =
454 (NetworkingProviderManager) ServiceHelper.getGlobalInstance(NetworkingProviderManager.class, this);
458 public void setDependencies(Object impl) {}