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