Merge Sonar fixes from stable/lithium..
[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 LOGGER = 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             LOGGER.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             LOGGER.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             LOGGER.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                         LOGGER.trace("isNodeL3Ready: node: {}, {} missing",
135                                 bridgeNode, portNameExt);
136                     }
137                 } else {
138                     LOGGER.trace("isNodeL3Ready: node: {}, {} missing",
139                             bridgeNode, portNameInt);
140                 }
141             } else {
142                 LOGGER.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             LOGGER.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             LOGGER.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             LOGGER.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                     LOGGER.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                     LOGGER.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             LOGGER.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 phyIf = null;
244         String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
245                 configurationService.getProviderMappingsKey());
246         if (providerMaps == null) {
247             providerMaps = configurationService.getDefaultProviderMapping();
248         }
249
250         if (providerMaps != null) {
251             for (String map : providerMaps.split(",")) {
252                 String[] pair = map.split(":");
253                 phyIfName.add(pair[1]);
254             }
255         }
256
257         return phyIfName;
258     }
259
260     /**
261      * Returns true if a patch port exists between the Integration Bridge and Network Bridge
262      */
263     private boolean isNetworkPatchCreated(Node node, Node intBridge, Node netBridge) {
264         Preconditions.checkNotNull(configurationService);
265
266         boolean isPatchCreated = false;
267
268         String portName = configurationService.getPatchPortName(new ImmutablePair<>(intBridge, netBridge));
269         if (isPortOnBridge(intBridge, portName)) {
270             portName = configurationService.getPatchPortName(new ImmutablePair<>(netBridge, intBridge));
271             if (isPortOnBridge(netBridge, portName)) {
272                 isPatchCreated = true;
273             }
274         }
275
276         return isPatchCreated;
277     }
278
279     /**
280      * Creates the Integration Bridge
281      */
282     private boolean createIntegrationBridge(Node ovsdbNode) throws Exception {
283         Preconditions.checkNotNull(configurationService);
284
285         if (!addBridge(ovsdbNode, configurationService.getIntegrationBridgeName())) {
286             LOGGER.debug("Integration Bridge Creation failed");
287             return false;
288         }
289         return true;
290     }
291
292     private boolean createExternalBridge(Node ovsdbNode) throws Exception {
293         Preconditions.checkNotNull(configurationService);
294
295         if (!addBridge(ovsdbNode, configurationService.getExternalBridgeName())) {
296             LOGGER.debug("External Bridge Creation failed");
297             return false;
298         }
299         return true;
300     }
301
302     /**
303      * Create and configure bridges for all network types and OpenFlow versions.
304      *
305        OF 1.0 vlan:
306        Bridge br-int
307             Port patch-net
308                 Interface patch-net
309                     type: patch
310                     options: {peer=patch-int}
311             Port br-int
312                 Interface br-int
313                     type: internal
314        Bridge br-net
315             Port "eth1"
316                 Interface "eth1"
317             Port patch-int
318                 Interface patch-int
319                     type: patch
320                     options: {peer=patch-net}
321             Port br-net
322                 Interface br-net
323                     type: internal
324
325        OF 1.0 tunnel:
326        Bridge br-int
327             Port patch-net
328                 Interface patch-net
329                     type: patch
330                     options: {peer=patch-int}
331             Port br-int
332                 Interface br-int
333                     type: internal
334        Bridge "br-net"
335             Port patch-int
336                 Interface patch-int
337                     type: patch
338                     options: {peer=patch-net}
339             Port br-net
340                 Interface br-net
341                     type: internal
342
343        OF 1.3 vlan:
344        Bridge br-int
345             Port "eth1"
346                 Interface "eth1"
347             Port br-int
348                 Interface br-int
349                     type: internal
350
351        OF 1.3 tunnel:
352        Bridge br-int
353             Port br-int
354                 Interface br-int
355                     type: internal
356      */
357     private boolean createBridges(Node bridgeNode, Node ovsdbNode, NeutronNetwork network) throws Exception {
358         Preconditions.checkNotNull(configurationService);
359         Preconditions.checkNotNull(networkingProviderManager);
360
361         LOGGER.debug("createBridges: node: {}, network type: {}", bridgeNode, network.getProviderNetworkType());
362
363         final String brInt = configurationService.getIntegrationBridgeName();
364         if (! createIntegrationBridge(ovsdbNode)) {
365             LOGGER.debug("{} Bridge creation failed", brInt);
366             return false;
367         }
368
369         if (configurationService.isL3ForwardingEnabled()) {
370             final String brExt = configurationService.getExternalBridgeName();
371             if (! createExternalBridge(ovsdbNode)) {
372                 LOGGER.error("{} Bridge creation failed", brExt);
373                 return false;
374             }
375
376             //get two patch port names
377             final String portNameInt = configurationService.getPatchPortName(new ImmutablePair<>(brInt, brExt));
378             final String portNameExt = configurationService.getPatchPortName(new ImmutablePair<>(brExt, brInt));
379             Preconditions.checkNotNull(portNameInt);
380             Preconditions.checkNotNull(portNameExt);
381
382             if (!addPatchPort(bridgeNode, brInt, portNameInt, portNameExt)) {
383                 LOGGER.error("Add Port {} to Bridge {} failed", portNameInt, brInt);
384                 return false;
385             }
386             Node extBridgeNode = southbound.readBridgeNode(ovsdbNode, brExt);
387             Preconditions.checkNotNull(extBridgeNode);
388             if (!addPatchPort(extBridgeNode, brExt, portNameExt, portNameInt)) {
389                 LOGGER.error("Add Port {} to Bridge {} failed", portNameExt, brExt);
390                 return false;
391             }
392         }
393
394         /* For vlan network types add physical port to br-int. */
395         if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
396             String phyNetName = this.getPhysicalInterfaceName(bridgeNode, network.getProviderPhysicalNetwork());
397             if (!addPortToBridge(bridgeNode, brInt, phyNetName)) {
398                 LOGGER.debug("Add Port {} to Bridge {} failed", phyNetName, brInt);
399                 return false;
400             }
401         }
402
403         LOGGER.debug("createBridges: node: {}, status: success", bridgeNode);
404         return true;
405     }
406
407     /**
408      * Add a Port to a Bridge
409      */
410     private boolean addPortToBridge (Node node, String bridgeName, String portName) throws Exception {
411         boolean rv = true;
412
413         if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
414             rv = southbound.addTerminationPoint(node, bridgeName, portName, null);
415
416             if (rv) {
417                 LOGGER.info("addPortToBridge: node: {}, bridge: {}, portname: {} status: success",
418                         node.getNodeId().getValue(), bridgeName, portName);
419             } else {
420                 LOGGER.error("addPortToBridge: node: {}, bridge: {}, portname: {} status: FAILED",
421                         node.getNodeId().getValue(), bridgeName, portName);
422             }
423         } else {
424             LOGGER.trace("addPortToBridge: node: {}, bridge: {}, portname: {} status: not_needed",
425                     node.getNodeId().getValue(), bridgeName, portName);
426         }
427
428         return rv;
429     }
430
431     /**
432      * Add a Patch Port to a Bridge
433      */
434     private boolean addPatchPort (Node node, String bridgeName, String portName, String peerPortName) throws Exception {
435         boolean rv = true;
436
437         if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
438             rv = southbound.addPatchTerminationPoint(node, bridgeName, portName, peerPortName);
439
440             if (rv) {
441                 LOGGER.info("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: success",
442                         node.getNodeId().getValue(), bridgeName, portName, peerPortName);
443             } else {
444                 LOGGER.error("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: FAILED",
445                         node.getNodeId().getValue(), bridgeName, portName, peerPortName);
446             }
447         } else {
448             LOGGER.trace("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: not_needed",
449                     node.getNodeId().getValue(), bridgeName, portName, peerPortName);
450         }
451
452         return rv;
453     }
454
455     /**
456      * Add Bridge to a Node
457      */
458     private boolean addBridge(Node ovsdbNode, String bridgeName) throws Exception {
459         boolean rv = true;
460         if (!southbound.isBridgeOnOvsdbNode(ovsdbNode, bridgeName)) {
461             rv = southbound.addBridge(ovsdbNode, bridgeName, getControllerTarget(ovsdbNode));
462         }
463         return rv;
464     }
465
466     private String getControllerIPAddress() {
467         InetAddress controllerIP = null;
468
469         String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address");
470         if (addressString != null) {
471             try {
472                 controllerIP = InetAddress.getByName(addressString);
473                 if (controllerIP != null) {
474                     return addressString;
475                 }
476             } catch (UnknownHostException e) {
477                 LOGGER.error("Host {} is invalid", addressString);
478             }
479         }
480
481         addressString = ConfigProperties.getProperty(this.getClass(), "of.address");
482         if (addressString != null) {
483             try {
484                 controllerIP = InetAddress.getByName(addressString);
485                 if (controllerIP != null) {
486                     return addressString;
487                 }
488             } catch (UnknownHostException e) {
489                 LOGGER.error("Host {} is invalid", addressString);
490             }
491         }
492
493         return null;
494     }
495
496     private short getControllerOFPort() {
497         Short defaultOpenFlowPort = Constants.OPENFLOW_PORT;
498         Short openFlowPort = defaultOpenFlowPort;
499         String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort");
500         if (portString != null) {
501             try {
502                 openFlowPort = Short.decode(portString).shortValue();
503             } catch (NumberFormatException e) {
504                 LOGGER.warn("Invalid port:{}, use default({})", portString,
505                         openFlowPort);
506             }
507         }
508         return openFlowPort;
509     }
510
511     private String getControllerTarget(Node node) {
512         String setControllerStr = null;
513         String controllerIpStr = null;
514         short openflowPort = Constants.OPENFLOW_PORT;
515         //Look at user configuration.
516         //TODO: In case we move to config subsystem to expose these user facing parameter,
517         // we will have to modify this code.
518
519         controllerIpStr = getControllerIPAddress();
520
521         if(controllerIpStr == null){
522             // Check if ovsdb node has connection info
523             OvsdbNodeAugmentation ovsdbNodeAugmentation = southbound.extractOvsdbNode(node);
524             if (ovsdbNodeAugmentation != null) {
525                 ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
526                 if(connectionInfo != null && connectionInfo.getLocalIp() != null) {
527                     controllerIpStr = new String(connectionInfo.getLocalIp().getValue());
528                 }else{
529                     LOGGER.warn("Ovsdb Node does not contains connection info : {}",node);
530                 }
531             }
532         }else {
533             openflowPort = getControllerOFPort();
534         }
535
536         if(controllerIpStr == null) {
537             // Neither user provided ip nor ovsdb node has controller ip, Lets use local machine ip address
538             LOGGER.debug("Use local machine ip address as a OpenFlow Controller ip address");
539             controllerIpStr = getLocalControllerHostIpAddress();
540         }
541         if(controllerIpStr != null){
542             LOGGER.debug("Targe OpenFlow Controller found : {}",controllerIpStr);
543             setControllerStr = Constants.OPENFLOW_CONNECTION_PROTOCOL + ":" + controllerIpStr + ":" + openflowPort;
544         }else {
545             LOGGER.warn("Failed to determine OpenFlow controller ip address");
546         }
547         return setControllerStr;
548     }
549
550     private String getLocalControllerHostIpAddress() {
551         String ipaddress = null;
552         try{
553             for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
554                 NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
555
556                 for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
557                     InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
558                     if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
559                         ipaddress = inetAddr.getHostAddress();
560                         break;
561                     }
562                 }
563             }
564         }catch (Exception e){
565             LOGGER.warn("Exception while fetching local host ip address ",e);
566         }
567         return ipaddress;
568     }
569
570     @Override
571     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
572         configurationService =
573                 (ConfigurationService) ServiceHelper.getGlobalInstance(ConfigurationService.class, this);
574         networkingProviderManager =
575                 (NetworkingProviderManager) ServiceHelper.getGlobalInstance(NetworkingProviderManager.class, this);
576         southbound =
577                 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
578     }
579
580     @Override
581     public void setDependencies(Object impl) {
582     }
583 }