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