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