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 * Authors : Madhu Venugopal, Brent Salisbury, Sam Hague
10 package org.opendaylight.ovsdb.openstack.netvirt.impl;
12 import java.net.InetAddress;
13 import java.net.NetworkInterface;
14 import java.net.UnknownHostException;
16 import org.opendaylight.neutron.spi.NeutronNetwork;
17 import org.opendaylight.ovsdb.lib.notation.Row;
18 import org.opendaylight.ovsdb.lib.notation.UUID;
19 import org.opendaylight.ovsdb.openstack.netvirt.NetworkHandler;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.*;
21 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
22 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
23 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
24 import org.opendaylight.ovsdb.schema.openvswitch.Port;
25 import org.opendaylight.ovsdb.utils.config.ConfigProperties;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
29 import com.google.common.base.Preconditions;
30 import com.google.common.collect.Lists;
31 import com.google.common.collect.Maps;
33 import org.apache.commons.lang3.tuple.ImmutablePair;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import java.util.Enumeration;
38 import java.util.List;
42 public class BridgeConfigurationManagerImpl implements BridgeConfigurationManager {
43 static final Logger LOGGER = LoggerFactory.getLogger(BridgeConfigurationManagerImpl.class);
45 // The implementation for each of these services is resolved by the OSGi Service Manager
46 private volatile ConfigurationService configurationService;
47 private volatile NetworkingProviderManager networkingProviderManager;
50 LOGGER.info(">>>>>> init {}", this.getClass());
54 public String getBridgeUuid(Node node, String bridgeName) {
55 return MdsalUtils.getBridgeUuid(node, bridgeName).toString();
59 public boolean isNodeNeutronReady(Node node) {
60 Preconditions.checkNotNull(configurationService);
61 return MdsalUtils.getBridge(node, configurationService.getIntegrationBridgeName()) != null;
65 public boolean isNodeOverlayReady(Node node) {
66 Preconditions.checkNotNull(configurationService);
67 return isNodeNeutronReady(node)
68 && MdsalUtils.getBridge(node, configurationService.getNetworkBridgeName()) != null;
72 public boolean isPortOnBridge (Node node, Bridge bridge, String portName) {
73 return MdsalUtils.getPort(node, portName) != null;
77 public boolean isNodeTunnelReady(Node node) {
78 Preconditions.checkNotNull(configurationService);
79 return MdsalUtils.getBridge(node, configurationService.getIntegrationBridgeName()) != null;
83 public boolean isNodeVlanReady(Node node, NeutronNetwork network) {
84 Preconditions.checkNotNull(networkingProviderManager);
86 /* is br-int created */
87 OvsdbBridgeAugmentation intBridge = MdsalUtils.getBridge(node, configurationService.getIntegrationBridgeName());
88 if (intBridge == null) {
89 LOGGER.trace("isNodeVlanReady: node: {}, br-int missing", node);
93 /* Check if physical device is added to br-int. */
94 String phyNetName = this.getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork());
95 if (MdsalUtils.getPort(node, phyNetName) == null) {
96 LOGGER.trace("isNodeVlanReady: node: {}, eth missing", node);
104 public void prepareNode(Node node) {
105 Preconditions.checkNotNull(networkingProviderManager);
108 createIntegrationBridge(node);
109 } catch (Exception e) {
110 LOGGER.error("Error creating Integration Bridge on {}", node, e);
113 // this node is an ovsdb node so it doesn't have a bridge
114 // so either look up the bridges or just wait for the bridge update to come in
115 // and add the flows there.
116 //networkingProviderManager.getProvider(node).initializeFlowRules(node);
120 * Check if the full network setup is available. If not, create it.
123 public boolean createLocalNetwork (Node node, NeutronNetwork network) {
124 boolean isCreated = false;
125 if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
126 if (!isNodeVlanReady(node, network)) {
128 isCreated = createBridges(node, network);
129 } catch (Exception e) {
130 LOGGER.error("Error creating internal net network " + node, e);
135 } else if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
136 network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)) {
137 if (!isNodeTunnelReady(node)) {
139 isCreated = createBridges(node, network);
140 } catch (Exception e) {
141 LOGGER.error("Error creating internal net network " + node, e);
151 public String getPhysicalInterfaceName (Node node, String physicalNetwork) {
153 String providerMaps = MdsalUtils.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
154 configurationService.getProviderMappingsKey());
155 if (providerMaps == null) {
156 providerMaps = configurationService.getDefaultProviderMapping();
159 if (providerMaps != null) {
160 for (String map : providerMaps.split(",")) {
161 String[] pair = map.split(":");
162 if (pair[0].equals(physicalNetwork)) {
170 LOGGER.error("Physical interface not found for Node: {}, Network {}",
171 node, physicalNetwork);
178 public List<String> getAllPhysicalInterfaceNames(Node node) {
179 List<String> phyIfName = Lists.newArrayList();
181 String providerMaps = MdsalUtils.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
182 configurationService.getProviderMappingsKey());
183 if (providerMaps == null) {
184 providerMaps = configurationService.getDefaultProviderMapping();
187 if (providerMaps != null) {
188 for (String map : providerMaps.split(",")) {
189 String[] pair = map.split(":");
190 phyIfName.add(pair[1]);
198 * Returns true if a patch port exists between the Integration Bridge and Network Bridge
200 private boolean isNetworkPatchCreated(Node node, Bridge intBridge, Bridge netBridge) {
201 Preconditions.checkNotNull(configurationService);
203 boolean isPatchCreated = false;
205 String portName = configurationService.getPatchPortName(new ImmutablePair<>(intBridge, netBridge));
206 if (isPortOnBridge(node, intBridge, portName)) {
207 portName = configurationService.getPatchPortName(new ImmutablePair<>(netBridge, intBridge));
208 if (isPortOnBridge(node, netBridge, portName)) {
209 isPatchCreated = true;
213 return isPatchCreated;
217 * Creates the Integration Bridge
219 private void createIntegrationBridge(Node node) throws Exception {
220 Preconditions.checkNotNull(configurationService);
222 String brIntName = configurationService.getIntegrationBridgeName();
224 if (!addBridge(node, brIntName, null, null)) {
225 LOGGER.debug("Integration Bridge Creation failed");
230 * Create and configure bridges for all network types and OpenFlow versions.
237 options: {peer=patch-int}
247 options: {peer=patch-net}
257 options: {peer=patch-int}
265 options: {peer=patch-net}
284 private boolean createBridges(Node node, NeutronNetwork network) throws Exception {
285 Preconditions.checkNotNull(configurationService);
286 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("createNetNetwork: 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.getPort(node, portName) == null) {
317 rv = MdsalUtils.addPort(node, bridgeName, portName);
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.getPort(node, portName) == null) {
330 rv = MdsalUtils.addPatchPort(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 {
341 Preconditions.checkNotNull(networkingProviderManager);
342 //sb will also add port and interface if this is a new bridge
343 return MdsalUtils.addBridge(node, bridgeName, getControllerTarget());
346 private InetAddress getControllerIPAddress(/*Connection connection*/) {
347 InetAddress controllerIP = null;
349 String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address");
350 if (addressString != null) {
352 controllerIP = InetAddress.getByName(addressString);
353 if (controllerIP != null) {
356 } catch (UnknownHostException e) {
357 LOGGER.error("Host {} is invalid", addressString);
361 addressString = ConfigProperties.getProperty(this.getClass(), "of.address");
362 if (addressString != null) {
364 controllerIP = InetAddress.getByName(addressString);
365 if (controllerIP != null) {
368 } catch (UnknownHostException e) {
369 LOGGER.error("Host {} is invalid", addressString);
375 controllerIP = connection.getClient().getConnectionInfo().getLocalAddress();
377 } catch (Exception e) {
378 LOGGER.debug("Invalid connection provided to getControllerIPAddresses", e);
382 if (addressString != null) {
384 controllerIP = InetAddress.getByName(addressString);
385 if (controllerIP != null) {
388 } catch (UnknownHostException e) {
389 LOGGER.error("Host {} is invalid", addressString);
396 private short getControllerOFPort() {
397 Short defaultOpenFlowPort = 6633;
398 Short openFlowPort = defaultOpenFlowPort;
399 String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort");
400 if (portString != null) {
402 openFlowPort = Short.decode(portString).shortValue();
403 } catch (NumberFormatException e) {
404 LOGGER.warn("Invalid port:{}, use default({})", portString,
411 private String getControllerTarget() {
413 * hardcoding value, need to find better way to get local ip
415 //String target = "tcp:" + getControllerIPAddress() + ":" + getControllerOFPort();
416 //TODO: dirty fix, need to remove it once we have proper solution
417 String ipaddress = null;
419 for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
420 NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
422 for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
423 InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
424 if (!inetAddr.isLoopbackAddress()) {
425 if (inetAddr.isSiteLocalAddress()) {
426 ipaddress = inetAddr.getHostAddress();
432 }catch (Exception e){
433 LOGGER.warn("ROYALLY SCREWED : Exception while fetching local host ip address ",e);
435 return "tcp:"+ipaddress+":6633";