Merge "Reenable BridgeConfigurationManagerImplTest"
[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.MdsalUtils;
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.NetworkingProviderManager;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbTables;
18 import org.opendaylight.ovsdb.utils.config.ConfigProperties;
19 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
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 import java.net.InetAddress;
28 import java.net.NetworkInterface;
29 import java.net.UnknownHostException;
30 import java.util.Enumeration;
31 import java.util.List;
32 import org.apache.commons.lang3.tuple.ImmutablePair;
33 import org.osgi.framework.BundleContext;
34 import org.osgi.framework.ServiceReference;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * @author Madhu Venugopal
40  * @author Brent Salisbury
41  * @author Sam Hague (shague@redhat.com)
42  */
43 public class BridgeConfigurationManagerImpl implements BridgeConfigurationManager, ConfigInterface {
44     static final Logger LOGGER = LoggerFactory.getLogger(BridgeConfigurationManagerImpl.class);
45
46     // The implementation for each of these services is resolved by the OSGi Service Manager
47     private volatile ConfigurationService configurationService;
48     private volatile NetworkingProviderManager networkingProviderManager;
49
50     public void setConfigurationService(ConfigurationService configurationService) {
51         this.configurationService = configurationService;
52     }
53
54     @Override
55     public String getBridgeUuid(Node node, String bridgeName) {
56         return MdsalUtils.getBridgeUuid(node, bridgeName);
57     }
58
59     @Override
60     public boolean isNodeNeutronReady(Node node) {
61         Preconditions.checkNotNull(configurationService);
62         return MdsalUtils.getBridge(node, configurationService.getIntegrationBridgeName()) != null;
63     }
64
65     @Override
66     public boolean isNodeOverlayReady(Node node) {
67         Preconditions.checkNotNull(configurationService);
68         return isNodeNeutronReady(node)
69                 && MdsalUtils.getBridge(node, configurationService.getNetworkBridgeName()) != null;
70     }
71
72     @Override
73     public boolean isPortOnBridge (Node node, String portName) {
74         return MdsalUtils.extractTerminationPointAugmentation(node, portName) != null;
75     }
76
77     @Override
78     public boolean isNodeTunnelReady(Node node) {
79         Preconditions.checkNotNull(configurationService);
80         return MdsalUtils.getBridge(node, configurationService.getIntegrationBridgeName()) != null;
81     }
82
83     @Override
84     public boolean isNodeVlanReady(Node node, NeutronNetwork network) {
85         Preconditions.checkNotNull(configurationService);
86
87         /* is br-int created */
88         OvsdbBridgeAugmentation intBridge = MdsalUtils.getBridge(node, configurationService.getIntegrationBridgeName());
89         if (intBridge == null) {
90             LOGGER.trace("isNodeVlanReady: node: {}, br-int missing", node);
91             return false;
92         }
93
94         /* Check if physical device is added to br-int. */
95         String phyNetName = getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork());
96         if (MdsalUtils.extractTerminationPointAugmentation(node, phyNetName) == null) {
97             LOGGER.trace("isNodeVlanReady: node: {}, eth missing", node);
98             return false;
99         }
100
101         return true;
102     }
103
104     @Override
105     public void prepareNode(Node node) {
106         //Preconditions.checkNotNull(networkingProviderManager);
107
108         try {
109             createIntegrationBridge(node);
110         } catch (Exception e) {
111             LOGGER.error("Error creating Integration Bridge on {}", node, e);
112             return;
113         }
114         // this node is an ovsdb node so it doesn't have a bridge
115         // so either look up the bridges or just wait for the bridge update to come in
116         // and add the flows there.
117         //networkingProviderManager.getProvider(node).initializeFlowRules(node);
118     }
119
120     /**
121      * Check if the full network setup is available. If not, create it.
122      */
123     @Override
124     public boolean createLocalNetwork (Node node, NeutronNetwork network) {
125         boolean isCreated = false;
126         if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
127             if (!isNodeVlanReady(node, network)) {
128                 try {
129                     isCreated = createBridges(node, network);
130                 } catch (Exception e) {
131                     LOGGER.error("Error creating internal net network " + node, e);
132                 }
133             } else {
134                 isCreated = true;
135             }
136         } else if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
137                    network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)) {
138             if (!isNodeTunnelReady(node)) {
139                 try {
140                     isCreated = createBridges(node, network);
141                 } catch (Exception e) {
142                     LOGGER.error("Error creating internal net network " + node, e);
143                 }
144             } else {
145                 isCreated = true;
146             }
147         }
148         return isCreated;
149     }
150
151     @Override
152     public String getPhysicalInterfaceName (Node node, String physicalNetwork) {
153         String phyIf = null;
154         String providerMaps = MdsalUtils.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
155                 configurationService.getProviderMappingsKey());
156         if (providerMaps == null) {
157             providerMaps = configurationService.getDefaultProviderMapping();
158         }
159
160         if (providerMaps != null) {
161             for (String map : providerMaps.split(",")) {
162                 String[] pair = map.split(":");
163                 if (pair[0].equals(physicalNetwork)) {
164                     phyIf = pair[1];
165                     break;
166                 }
167             }
168         }
169
170         if (phyIf == null) {
171             LOGGER.error("Physical interface not found for Node: {}, Network {}",
172                          node, physicalNetwork);
173         }
174
175         return phyIf;
176     }
177
178     @Override
179     public List<String> getAllPhysicalInterfaceNames(Node node) {
180         List<String> phyIfName = Lists.newArrayList();
181         String phyIf = null;
182         String providerMaps = MdsalUtils.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
183                 configurationService.getProviderMappingsKey());
184         if (providerMaps == null) {
185             providerMaps = configurationService.getDefaultProviderMapping();
186         }
187
188         if (providerMaps != null) {
189             for (String map : providerMaps.split(",")) {
190                 String[] pair = map.split(":");
191                 phyIfName.add(pair[1]);
192             }
193         }
194
195         return phyIfName;
196     }
197
198     /**
199      * Returns true if a patch port exists between the Integration Bridge and Network Bridge
200      */
201     private boolean isNetworkPatchCreated(Node node, Node intBridge, Node netBridge) {
202         Preconditions.checkNotNull(configurationService);
203
204         boolean isPatchCreated = false;
205
206         String portName = configurationService.getPatchPortName(new ImmutablePair<>(intBridge, netBridge));
207         if (isPortOnBridge(intBridge, portName)) {
208             portName = configurationService.getPatchPortName(new ImmutablePair<>(netBridge, intBridge));
209             if (isPortOnBridge(netBridge, portName)) {
210                 isPatchCreated = true;
211             }
212         }
213
214         return isPatchCreated;
215     }
216
217     /**
218      * Creates the Integration Bridge
219      */
220     private void createIntegrationBridge(Node node) throws Exception {
221         Preconditions.checkNotNull(configurationService);
222
223         String brIntName = configurationService.getIntegrationBridgeName();
224
225         if (!addBridge(node, brIntName, null, null)) {
226             LOGGER.debug("Integration Bridge Creation failed");
227         }
228     }
229
230     /**
231      * Create and configure bridges for all network types and OpenFlow versions.
232      *
233        OF 1.0 vlan:
234        Bridge br-int
235             Port patch-net
236                 Interface patch-net
237                     type: patch
238                     options: {peer=patch-int}
239             Port br-int
240                 Interface br-int
241                     type: internal
242        Bridge br-net
243             Port "eth1"
244                 Interface "eth1"
245             Port patch-int
246                 Interface patch-int
247                     type: patch
248                     options: {peer=patch-net}
249             Port br-net
250                 Interface br-net
251                     type: internal
252
253        OF 1.0 tunnel:
254        Bridge br-int
255             Port patch-net
256                 Interface patch-net
257                     type: patch
258                     options: {peer=patch-int}
259             Port br-int
260                 Interface br-int
261                     type: internal
262        Bridge "br-net"
263             Port patch-int
264                 Interface patch-int
265                     type: patch
266                     options: {peer=patch-net}
267             Port br-net
268                 Interface br-net
269                     type: internal
270
271        OF 1.3 vlan:
272        Bridge br-int
273             Port "eth1"
274                 Interface "eth1"
275             Port br-int
276                 Interface br-int
277                     type: internal
278
279        OF 1.3 tunnel:
280        Bridge br-int
281             Port br-int
282                 Interface br-int
283                     type: internal
284      */
285     private boolean createBridges(Node node, NeutronNetwork network) throws Exception {
286         Preconditions.checkNotNull(configurationService);
287         Preconditions.checkNotNull(networkingProviderManager);
288
289         LOGGER.debug("createBridges: node: {}, network type: {}", node, network.getProviderNetworkType());
290
291         String brInt = configurationService.getIntegrationBridgeName();
292         if (!addBridge(node, brInt, null, null)) {
293             LOGGER.debug("{} Bridge creation failed", brInt);
294             return false;
295         }
296
297         /* For vlan network types add physical port to br-int. */
298         if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
299             String phyNetName = this.getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork());
300             if (!addPortToBridge(node, brInt, phyNetName)) {
301                 LOGGER.debug("Add Port {} to Bridge {} failed", phyNetName, brInt);
302                 return false;
303             }
304         }
305
306         LOGGER.debug("createBridges: node: {}, status: success", node);
307         return true;
308     }
309
310     /**
311      * Add a Port to a Bridge
312      */
313     private boolean addPortToBridge (Node node, String bridgeName, String portName) throws Exception {
314         boolean rv = true;
315
316         if (MdsalUtils.extractTerminationPointAugmentation(node, portName) == null) {
317             rv = MdsalUtils.addTerminationPoint(node, bridgeName, portName, null);
318         }
319
320         return rv;
321     }
322
323     /**
324      * Add a Patch Port to a Bridge
325      */
326     private boolean addPatchPort (Node node, String bridgeName, String portName, String peerPortName) throws Exception {
327         boolean rv = true;
328
329         if (MdsalUtils.extractTerminationPointAugmentation(node, portName) == null) {
330             rv = MdsalUtils.addPatchTerminationPoint(node, bridgeName, portName, peerPortName);
331         }
332
333         return rv;
334     }
335
336     /**
337      * Add Bridge to a Node
338      */
339     private boolean addBridge(Node node, String bridgeName,
340                               String localPatchName, String remotePatchName) throws Exception {
341         boolean rv = true;
342         if (MdsalUtils.getBridge(node, bridgeName) == null) {
343             rv = MdsalUtils.addBridge(node, bridgeName, getControllerTarget(node));
344         }
345         return rv;
346     }
347
348     private InetAddress getControllerIPAddress() {
349         InetAddress controllerIP = null;
350
351         String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address");
352         if (addressString != null) {
353             try {
354                 controllerIP = InetAddress.getByName(addressString);
355                 if (controllerIP != null) {
356                     return controllerIP;
357                 }
358             } catch (UnknownHostException e) {
359                 LOGGER.error("Host {} is invalid", addressString);
360             }
361         }
362
363         addressString = ConfigProperties.getProperty(this.getClass(), "of.address");
364         if (addressString != null) {
365             try {
366                 controllerIP = InetAddress.getByName(addressString);
367                 if (controllerIP != null) {
368                     return controllerIP;
369                 }
370             } catch (UnknownHostException e) {
371                 LOGGER.error("Host {} is invalid", addressString);
372             }
373         }
374
375         /*
376         try {
377             controllerIP = connection.getClient().getConnectionInfo().getLocalAddress();
378             return controllerIP;
379         } catch (Exception e) {
380             LOGGER.debug("Invalid connection provided to getControllerIPAddresses", e);
381         }
382         */
383
384         if (addressString != null) {
385             try {
386                 controllerIP = InetAddress.getByName(addressString);
387                 if (controllerIP != null) {
388                     return controllerIP;
389                 }
390             } catch (UnknownHostException e) {
391                 LOGGER.error("Host {} is invalid", addressString);
392             }
393         }
394
395         return controllerIP;
396     }
397
398     private short getControllerOFPort() {
399         Short defaultOpenFlowPort = 6633;
400         Short openFlowPort = defaultOpenFlowPort;
401         String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort");
402         if (portString != null) {
403             try {
404                 openFlowPort = Short.decode(portString).shortValue();
405             } catch (NumberFormatException e) {
406                 LOGGER.warn("Invalid port:{}, use default({})", portString,
407                         openFlowPort);
408             }
409         }
410         return openFlowPort;
411     }
412
413     private String getControllerTarget(Node node) {
414         String target = null;
415         OvsdbNodeAugmentation ovsdbNodeAugmentation = MdsalUtils.extractOvsdbNode(node);
416         if (ovsdbNodeAugmentation != null) {
417             ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
418             String addressStr = new String(connectionInfo.getLocalIp().getValue());
419             target = "tcp:" + addressStr + ":6633";
420         } else{
421             target = getControllerTarget();
422         }
423         return target;
424     }
425
426     private String getControllerTarget() {
427         /* TODO SB_MIGRATION
428          * hardcoding value, need to find better way to get local ip
429          */
430         //String target = "tcp:" + getControllerIPAddress() + ":" + getControllerOFPort();
431         //TODO: dirty fix, need to remove it once we have proper solution
432         String ipaddress = null;
433         try{
434             for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
435                 NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
436
437                 for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
438                     InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
439                     if (!inetAddr.isLoopbackAddress()) {
440                         if (inetAddr.isSiteLocalAddress()) {
441                             ipaddress = inetAddr.getHostAddress();
442                             break;
443                         }
444                     }
445                 }
446             }
447         }catch (Exception e){
448             LOGGER.warn("ROYALLY SCREWED : Exception while fetching local host ip address ",e);
449         }
450         return "tcp:"+ipaddress+":6633";
451     }
452
453     @Override
454     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
455         configurationService =
456                 (ConfigurationService) ServiceHelper.getGlobalInstance(ConfigurationService.class, this);
457         networkingProviderManager =
458                 (NetworkingProviderManager) ServiceHelper.getGlobalInstance(NetworkingProviderManager.class, this);
459     }
460
461     @Override
462     public void setDependencies(Object impl) {}
463 }