Add UT for SouthboundMapper and SouthboundProvider
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / impl / BridgeConfigurationManagerImpl.java
1 /*
2  * Copyright (c) 2013, 2015 Red Hat, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.ovsdb.openstack.netvirt.impl;
10
11 import org.opendaylight.neutron.spi.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.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
25
26 import com.google.common.base.Preconditions;
27 import com.google.common.collect.Lists;
28
29 import java.net.InetAddress;
30 import java.net.NetworkInterface;
31 import java.net.UnknownHostException;
32 import java.util.Enumeration;
33 import java.util.List;
34
35 import org.apache.commons.lang3.tuple.ImmutablePair;
36 import org.osgi.framework.BundleContext;
37 import org.osgi.framework.ServiceReference;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * @author Madhu Venugopal
43  * @author Brent Salisbury
44  * @author Sam Hague (shague@redhat.com)
45  */
46 public class BridgeConfigurationManagerImpl implements BridgeConfigurationManager, ConfigInterface {
47     private static final Logger LOG = LoggerFactory.getLogger(BridgeConfigurationManagerImpl.class);
48
49     // The implementation for each of these services is resolved by the OSGi Service Manager
50     private volatile ConfigurationService configurationService;
51     private volatile NetworkingProviderManager networkingProviderManager;
52     private volatile Southbound southbound;
53
54     public void setConfigurationService(ConfigurationService configurationService) {
55         this.configurationService = configurationService;
56     }
57
58     public void setSouthbound(Southbound southbound) {
59         this.southbound = southbound;
60     }
61
62     @Override
63     public String getBridgeUuid(Node node, String bridgeName) {
64         return southbound.getBridgeUuid(node, bridgeName);
65     }
66
67     @Override
68     public boolean isNodeNeutronReady(Node node) {
69         Preconditions.checkNotNull(configurationService);
70         return southbound.getBridge(node, configurationService.getIntegrationBridgeName()) != null;
71     }
72
73     @Override
74     public boolean isNodeOverlayReady(Node node) {
75         Preconditions.checkNotNull(configurationService);
76         return isNodeNeutronReady(node)
77                 && southbound.getBridge(node, configurationService.getNetworkBridgeName()) != null;
78     }
79
80     @Override
81     public boolean isPortOnBridge (Node bridgeNode, String portName) {
82         return southbound.extractTerminationPointAugmentation(bridgeNode, portName) != null;
83     }
84
85     @Override
86     public boolean isNodeTunnelReady(Node bridgeNode, Node ovsdbNode) {
87         Preconditions.checkNotNull(configurationService);
88         if (!southbound.isBridgeOnOvsdbNode(ovsdbNode, configurationService.getIntegrationBridgeName())) {
89             LOG.trace("isNodeTunnelReady: node: {}, {} missing",
90                     bridgeNode, configurationService.getIntegrationBridgeName());
91             return false;
92         }
93
94         return isNodeL3Ready(bridgeNode, ovsdbNode);
95     }
96
97     @Override
98     public boolean isNodeVlanReady(Node bridgeNode, Node ovsdbNode, NeutronNetwork network) {
99         Preconditions.checkNotNull(configurationService);
100
101         final String brInt = configurationService.getIntegrationBridgeName();
102         if (!southbound.isBridgeOnOvsdbNode(ovsdbNode, brInt)) {
103             LOG.trace("isNodeVlanReady: node: {}, {} missing", bridgeNode, brInt);
104             return false;
105         }
106
107         /* Check if physical device is added to br-int. */
108         String phyNetName = getPhysicalInterfaceName(ovsdbNode, network.getProviderPhysicalNetwork());
109         if (!isPortOnBridge(bridgeNode, phyNetName)) {
110             LOG.trace("isNodeVlanReady: node: {}, eth missing", bridgeNode);
111             return false;
112         }
113
114         return isNodeL3Ready(bridgeNode, ovsdbNode);
115     }
116
117     public boolean isNodeL3Ready(Node bridgeNode, Node ovsdbNode) {
118         Preconditions.checkNotNull(configurationService);
119         boolean ready = false;
120         if (configurationService.isL3ForwardingEnabled()) {
121             final String brInt = configurationService.getIntegrationBridgeName();
122             final String brExt = configurationService.getExternalBridgeName();
123             final String portNameInt = configurationService.getPatchPortName(new ImmutablePair<>(brInt, brExt));
124             final String portNameExt = configurationService.getPatchPortName(new ImmutablePair<>(brExt, brInt));
125             Preconditions.checkNotNull(portNameInt);
126             Preconditions.checkNotNull(portNameExt);
127
128             if (southbound.isBridgeOnOvsdbNode(ovsdbNode, brExt)) {
129                 ready = isNetworkPatchCreated(bridgeNode, southbound.readBridgeNode(ovsdbNode, brExt));
130             } else {
131                 LOG.trace("isNodeL3Ready: node: {}, {} missing",
132                         bridgeNode, brExt);
133             }
134         } else {
135             ready = true;
136         }
137         return ready;
138     }
139
140     @Override
141     public void prepareNode(Node ovsdbNode) {
142         Preconditions.checkNotNull(configurationService);
143
144         try {
145             createIntegrationBridge(ovsdbNode);
146         } catch (Exception e) {
147             LOG.error("Error creating Integration Bridge on {}", ovsdbNode, e);
148             return;
149         }
150
151         try {
152             if (configurationService.isL3ForwardingEnabled()) {
153                 createExternalBridge(ovsdbNode);
154             }
155         } catch (Exception e) {
156             LOG.error("Error creating External Bridge on {}", ovsdbNode, e);
157             return;
158         }
159         // this node is an ovsdb node so it doesn't have a bridge
160         // so either look up the bridges or just wait for the bridge update to come in
161         // and add the flows there.
162         //networkingProviderManager.getProvider(node).initializeFlowRules(node);
163     }
164
165     /**
166      * Check if the full network setup is available. If not, create it.
167      */
168     @Override
169     public boolean createLocalNetwork (Node bridgeNode, NeutronNetwork network) {
170         boolean isCreated = false;
171         Node ovsdbNode = southbound.readOvsdbNode(bridgeNode);
172         if (ovsdbNode == null) {
173             //this should never happen
174             LOG.error("createLocalNetwork could not find ovsdbNode from bridge node " + bridgeNode);
175             return false;
176         }
177         if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
178             if (!isNodeVlanReady(bridgeNode, ovsdbNode, network)) {
179                 try {
180                     isCreated = createBridges(bridgeNode, ovsdbNode, network);
181                 } catch (Exception e) {
182                     LOG.error("Error creating internal vlan net network " + bridgeNode, e);
183                 }
184             } else {
185                 isCreated = true;
186             }
187         } else if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
188                    network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)) {
189             if (!isNodeTunnelReady(bridgeNode, ovsdbNode)) {
190                 try {
191                     isCreated = createBridges(bridgeNode, ovsdbNode, network);
192                 } catch (Exception e) {
193                     LOG.error("Error creating internal vxlan/gre net network " + bridgeNode, e);
194                 }
195             } else {
196                 isCreated = true;
197             }
198         }
199         return isCreated;
200     }
201
202     @Override
203     public String getPhysicalInterfaceName (Node node, String physicalNetwork) {
204         String phyIf = null;
205         String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
206                 configurationService.getProviderMappingsKey());
207         if (providerMaps == null) {
208             providerMaps = configurationService.getDefaultProviderMapping();
209         }
210
211         if (providerMaps != null) {
212             for (String map : providerMaps.split(",")) {
213                 String[] pair = map.split(":");
214                 if (pair[0].equals(physicalNetwork)) {
215                     phyIf = pair[1];
216                     break;
217                 }
218             }
219         }
220
221         if (phyIf == null) {
222             LOG.error("Physical interface not found for Node: {}, Network {}",
223                     node, physicalNetwork);
224         }
225
226         return phyIf;
227     }
228
229     @Override
230     public List<String> getAllPhysicalInterfaceNames(Node node) {
231         List<String> phyIfName = Lists.newArrayList();
232         String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
233                 configurationService.getProviderMappingsKey());
234         if (providerMaps == null) {
235             providerMaps = configurationService.getDefaultProviderMapping();
236         }
237
238         if (providerMaps != null) {
239             for (String map : providerMaps.split(",")) {
240                 String[] pair = map.split(":");
241                 phyIfName.add(pair[1]);
242             }
243         }
244
245         return phyIfName;
246     }
247
248     /**
249      * Returns true if a patch port exists between the Integration Bridge and Network Bridge
250      */
251     private boolean isNetworkPatchCreated(Node intBridge, Node netBridge) {
252         Preconditions.checkNotNull(configurationService);
253
254         boolean isPatchCreated = false;
255
256         String portName = configurationService.getPatchPortName(new ImmutablePair<>(intBridge, netBridge));
257         if (isPortOnBridge(intBridge, portName)) {
258             portName = configurationService.getPatchPortName(new ImmutablePair<>(netBridge, intBridge));
259             if (isPortOnBridge(netBridge, portName)) {
260                 isPatchCreated = true;
261             }
262         }
263
264         return isPatchCreated;
265     }
266
267     /**
268      * Creates the Integration Bridge
269      */
270     private boolean createIntegrationBridge(Node ovsdbNode) {
271         Preconditions.checkNotNull(configurationService);
272
273         if (!addBridge(ovsdbNode, configurationService.getIntegrationBridgeName())) {
274             LOG.debug("Integration Bridge Creation failed");
275             return false;
276         }
277         return true;
278     }
279
280     private boolean createExternalBridge(Node ovsdbNode) {
281         Preconditions.checkNotNull(configurationService);
282
283         if (!addBridge(ovsdbNode, configurationService.getExternalBridgeName())) {
284             LOG.debug("External Bridge Creation failed");
285             return false;
286         }
287         return true;
288     }
289
290     /**
291      * Create and configure bridges for all network types and OpenFlow versions.
292      *
293        OF 1.0 vlan:
294        Bridge br-int
295             Port patch-net
296                 Interface patch-net
297                     type: patch
298                     options: {peer=patch-int}
299             Port br-int
300                 Interface br-int
301                     type: internal
302        Bridge br-net
303             Port "eth1"
304                 Interface "eth1"
305             Port patch-int
306                 Interface patch-int
307                     type: patch
308                     options: {peer=patch-net}
309             Port br-net
310                 Interface br-net
311                     type: internal
312
313        OF 1.0 tunnel:
314        Bridge br-int
315             Port patch-net
316                 Interface patch-net
317                     type: patch
318                     options: {peer=patch-int}
319             Port br-int
320                 Interface br-int
321                     type: internal
322        Bridge "br-net"
323             Port patch-int
324                 Interface patch-int
325                     type: patch
326                     options: {peer=patch-net}
327             Port br-net
328                 Interface br-net
329                     type: internal
330
331        OF 1.3 vlan:
332        Bridge br-int
333             Port "eth1"
334                 Interface "eth1"
335             Port br-int
336                 Interface br-int
337                     type: internal
338
339        OF 1.3 tunnel:
340        Bridge br-int
341             Port br-int
342                 Interface br-int
343                     type: internal
344      */
345     private boolean createBridges(Node bridgeNode, Node ovsdbNode, NeutronNetwork network) {
346         Preconditions.checkNotNull(configurationService);
347         Preconditions.checkNotNull(networkingProviderManager);
348
349         LOG.debug("createBridges: node: {}, network type: {}", bridgeNode, network.getProviderNetworkType());
350
351         final String brInt = configurationService.getIntegrationBridgeName();
352         if (! createIntegrationBridge(ovsdbNode)) {
353             LOG.debug("{} Bridge creation failed", brInt);
354             return false;
355         }
356
357         if (configurationService.isL3ForwardingEnabled()) {
358             final String brExt = configurationService.getExternalBridgeName();
359             if (! createExternalBridge(ovsdbNode)) {
360                 LOG.error("{} Bridge creation failed", brExt);
361                 return false;
362             }
363
364             //get two patch port names
365             final String portNameInt = configurationService.getPatchPortName(new ImmutablePair<>(brInt, brExt));
366             final String portNameExt = configurationService.getPatchPortName(new ImmutablePair<>(brExt, brInt));
367             Preconditions.checkNotNull(portNameInt);
368             Preconditions.checkNotNull(portNameExt);
369
370             if (!addPatchPort(bridgeNode, brInt, portNameInt, portNameExt)) {
371                 LOG.error("Add Port {} to Bridge {} failed", portNameInt, brInt);
372                 return false;
373             }
374             Node extBridgeNode = southbound.readBridgeNode(ovsdbNode, brExt);
375             Preconditions.checkNotNull(extBridgeNode);
376             if (!addPatchPort(extBridgeNode, brExt, portNameExt, portNameInt)) {
377                 LOG.error("Add Port {} to Bridge {} failed", portNameExt, brExt);
378                 return false;
379             }
380         }
381
382         /* For vlan network types add physical port to br-int. */
383         if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
384             String phyNetName = this.getPhysicalInterfaceName(bridgeNode, network.getProviderPhysicalNetwork());
385             if (!addPortToBridge(bridgeNode, brInt, phyNetName)) {
386                 LOG.debug("Add Port {} to Bridge {} failed", phyNetName, brInt);
387                 return false;
388             }
389         }
390
391         LOG.debug("createBridges: node: {}, status: success", bridgeNode);
392         return true;
393     }
394
395     /**
396      * Add a Port to a Bridge
397      */
398     private boolean addPortToBridge (Node node, String bridgeName, String portName) {
399         boolean rv = true;
400
401         if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
402             rv = southbound.addTerminationPoint(node, bridgeName, portName, null);
403
404             if (rv) {
405                 LOG.info("addPortToBridge: node: {}, bridge: {}, portname: {} status: success",
406                         node.getNodeId().getValue(), bridgeName, portName);
407             } else {
408                 LOG.error("addPortToBridge: node: {}, bridge: {}, portname: {} status: FAILED",
409                         node.getNodeId().getValue(), bridgeName, portName);
410             }
411         } else {
412             LOG.trace("addPortToBridge: node: {}, bridge: {}, portname: {} status: not_needed",
413                     node.getNodeId().getValue(), bridgeName, portName);
414         }
415
416         return rv;
417     }
418
419     /**
420      * Add a Patch Port to a Bridge
421      */
422     private boolean addPatchPort (Node node, String bridgeName, String portName, String peerPortName) {
423         boolean rv = true;
424
425         if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
426             rv = southbound.addPatchTerminationPoint(node, bridgeName, portName, peerPortName);
427
428             if (rv) {
429                 LOG.info("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: success",
430                         node.getNodeId().getValue(), bridgeName, portName, peerPortName);
431             } else {
432                 LOG.error("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: FAILED",
433                         node.getNodeId().getValue(), bridgeName, portName, peerPortName);
434             }
435         } else {
436             LOG.trace("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: not_needed",
437                     node.getNodeId().getValue(), bridgeName, portName, peerPortName);
438         }
439
440         return rv;
441     }
442
443     /**
444      * Add Bridge to a Node
445      */
446     private boolean addBridge(Node ovsdbNode, String bridgeName) {
447         boolean rv = true;
448         if ((!southbound.isBridgeOnOvsdbNode(ovsdbNode, bridgeName)) ||
449                 (southbound.getBridgeFromConfig(ovsdbNode, bridgeName) == null)) {
450             rv = southbound.addBridge(ovsdbNode, bridgeName, getControllerTarget(ovsdbNode));
451         }
452         return rv;
453     }
454
455     private String getControllerIPAddress() {
456         String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address");
457         if (addressString != null) {
458             try {
459                 if (InetAddress.getByName(addressString) != null) {
460                     return addressString;
461                 }
462             } catch (UnknownHostException e) {
463                 LOG.error("Host {} is invalid", addressString);
464             }
465         }
466
467         addressString = ConfigProperties.getProperty(this.getClass(), "of.address");
468         if (addressString != null) {
469             try {
470                 if (InetAddress.getByName(addressString) != null) {
471                     return addressString;
472                 }
473             } catch (UnknownHostException e) {
474                 LOG.error("Host {} is invalid", addressString);
475             }
476         }
477
478         return null;
479     }
480
481     private short getControllerOFPort() {
482         short openFlowPort = Constants.OPENFLOW_PORT;
483         String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort");
484         if (portString != null) {
485             try {
486                 openFlowPort = Short.parseShort(portString);
487             } catch (NumberFormatException e) {
488                 LOG.warn("Invalid port:{}, use default({})", portString,
489                         openFlowPort);
490             }
491         }
492         return openFlowPort;
493     }
494
495     private String getControllerTarget(Node node) {
496         String setControllerStr = null;
497         short openflowPort = Constants.OPENFLOW_PORT;
498         //Look at user configuration.
499         //TODO: In case we move to config subsystem to expose these user facing parameter,
500         // we will have to modify this code.
501
502         String controllerIpStr = getControllerIPAddress();
503
504         if(controllerIpStr == null){
505             // Check if ovsdb node has connection info
506             OvsdbNodeAugmentation ovsdbNodeAugmentation = southbound.extractOvsdbNode(node);
507             if (ovsdbNodeAugmentation != null) {
508                 ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
509                 if(connectionInfo != null && connectionInfo.getLocalIp() != null) {
510                     controllerIpStr = new String(connectionInfo.getLocalIp().getValue());
511                 }else{
512                     LOG.warn("Ovsdb Node does not contains connection info : {}", node);
513                 }
514             }
515         }else {
516             openflowPort = getControllerOFPort();
517         }
518
519         if(controllerIpStr == null) {
520             // Neither user provided ip nor ovsdb node has controller ip, Lets use local machine ip address
521             LOG.debug("Use local machine ip address as a OpenFlow Controller ip address");
522             controllerIpStr = getLocalControllerHostIpAddress();
523         }
524         if(controllerIpStr != null){
525             LOG.debug("Targe OpenFlow Controller found : {}", controllerIpStr);
526             setControllerStr = Constants.OPENFLOW_CONNECTION_PROTOCOL + ":" + controllerIpStr + ":" + openflowPort;
527         }else {
528             LOG.warn("Failed to determine OpenFlow controller ip address");
529         }
530         return setControllerStr;
531     }
532
533     private String getLocalControllerHostIpAddress() {
534         String ipaddress = null;
535         try{
536             for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
537                 NetworkInterface iface = ifaces.nextElement();
538
539                 for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
540                     InetAddress inetAddr = inetAddrs.nextElement();
541                     if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
542                         ipaddress = inetAddr.getHostAddress();
543                         break;
544                     }
545                 }
546             }
547         }catch (Exception e){
548             LOG.warn("Exception while fetching local host ip address ", e);
549         }
550         return ipaddress;
551     }
552
553     @Override
554     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
555         configurationService =
556                 (ConfigurationService) ServiceHelper.getGlobalInstance(ConfigurationService.class, this);
557         networkingProviderManager =
558                 (NetworkingProviderManager) ServiceHelper.getGlobalInstance(NetworkingProviderManager.class, this);
559         southbound =
560                 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
561     }
562
563     @Override
564     public void setDependencies(Object impl) {
565     }
566 }