Merge remote-tracking branch 'origin/master' into merge-branch
[netvirt.git] / neutron / src / main / java / org / opendaylight / ovsdb / neutron / AdminConfigManager.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.neutron;
11
12 import java.net.InetAddress;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.Map;
16
17 import org.opendaylight.controller.sal.core.Node;
18 import org.opendaylight.controller.sal.utils.ServiceHelper;
19 import org.opendaylight.ovsdb.lib.table.Open_vSwitch;
20 import org.opendaylight.ovsdb.lib.table.Table;
21 import org.opendaylight.ovsdb.plugin.OVSDBConfigService;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class AdminConfigManager implements IAdminConfigManager{
26     static final Logger logger = LoggerFactory.getLogger(AdminConfigManager.class);
27
28     private String integrationBridgeName;
29     private String networkBridgeName;
30     private String externalBridgeName;
31     private String tunnelEndpointConfigName;
32     private String patchToIntegration;
33     private String patchToNetwork;
34     private String providerMappingsConfigName;
35     private String providerMappings;
36
37     // Refer to /etc/quantum/plugins/openvswitch/ovs_quantum_plugin.ini
38     private static String DEFAULT_TUNNEL_ENDPOINT_CONFIG_STRING = "local_ip";
39     private static String DEFAULT_INTEGRATION_BRIDGENAME = "br-int";
40     private static String DEFAULT_NETWORK_BRIDGENAME = "br-net";
41     private static String DEFAULT_EXTERNAL_BRIDGENAME = "br-ex";
42     private static String DEFAULT_PATCH_TO_INTEGRATION = "patch-int";
43     private static String DEFAULT_PATCH_TO_NETWORK = "patch-net";
44     private static String CONFIG_TUNNEL_ENDPOINT_CONFIG = "tunnel_endpoint_config_string";
45     private static String CONFIG_INTEGRATION_BRIDGENAME = "integration_bridge";
46     private static String CONFIG_NETWORK_BRIDGENAME = "network_bridge";
47     private static String CONFIG_EXTERNAL_BRIDGENAME = "external_bridge";
48     private static String CONFIG_PATCH_TO_INTEGRATION = "patch-int";
49     private static String CONFIG_PATCH_TO_NETWORK = "patch-net";
50     private static String DEFAULT_PROVIDER_MAPPINGS_CONFIG_STRING = "provider_mappings";
51     private static String CONFIG_PROVIDER_MAPPINGS_CONFIG = "provider_mappings_config_string";
52     private static String CONFIG_PROVIDER_MAPPINGS = "provider_mappings";
53
54     public AdminConfigManager() {
55         tunnelEndpointConfigName = System.getProperty(CONFIG_TUNNEL_ENDPOINT_CONFIG);
56         integrationBridgeName = System.getProperty(CONFIG_INTEGRATION_BRIDGENAME);
57         networkBridgeName = System.getProperty(CONFIG_NETWORK_BRIDGENAME);
58         externalBridgeName = System.getProperty(CONFIG_EXTERNAL_BRIDGENAME);
59         patchToIntegration = System.getProperty(CONFIG_PATCH_TO_INTEGRATION);
60         patchToNetwork = System.getProperty(CONFIG_PATCH_TO_NETWORK);
61         providerMappingsConfigName = System.getProperty(CONFIG_PROVIDER_MAPPINGS_CONFIG);
62         providerMappings = System.getProperty(CONFIG_PROVIDER_MAPPINGS);
63
64         if (tunnelEndpointConfigName == null) tunnelEndpointConfigName = DEFAULT_TUNNEL_ENDPOINT_CONFIG_STRING;
65         if (integrationBridgeName == null) integrationBridgeName = DEFAULT_INTEGRATION_BRIDGENAME;
66         if (networkBridgeName == null) networkBridgeName = DEFAULT_NETWORK_BRIDGENAME;
67         if (externalBridgeName == null) externalBridgeName = DEFAULT_EXTERNAL_BRIDGENAME;
68         if (patchToIntegration == null) patchToIntegration = DEFAULT_PATCH_TO_INTEGRATION;
69         if (patchToNetwork == null) patchToNetwork  = DEFAULT_PATCH_TO_NETWORK;
70         if (providerMappingsConfigName == null) providerMappingsConfigName = DEFAULT_PROVIDER_MAPPINGS_CONFIG_STRING;
71     }
72
73     public String getIntegrationBridgeName() {
74         return integrationBridgeName;
75     }
76
77     public void setIntegrationBridgeName(String integrationBridgeName) {
78         this.integrationBridgeName = integrationBridgeName;
79     }
80
81     public String getNetworkBridgeName() { return networkBridgeName; }
82
83     public void setNetworkBridgeName(String networkBridgeName) {
84         this.networkBridgeName = networkBridgeName;
85     }
86
87     public String getExternalBridgeName() {
88         return externalBridgeName;
89     }
90
91     public void setExternalBridgeName (String externalBridgeName) {
92         this.externalBridgeName = externalBridgeName;
93     }
94
95     public String getPatchToIntegration() {
96         return patchToIntegration;
97     }
98
99     public void setPatchToIntegration(String patchToIntegration) {
100         this.patchToIntegration = patchToIntegration;
101     }
102
103     public String getPatchToNetwork() { return patchToNetwork; }
104
105     public void setPatchToNetwork(String patchToNetwork) {
106         this.patchToNetwork = patchToNetwork;
107     }
108
109     public InetAddress getTunnelEndPoint(Node node) {
110         InetAddress address = null;
111         OVSDBConfigService ovsdbConfig = (OVSDBConfigService)ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
112         try {
113             Map<String, Table<?>> ovsTable = ovsdbConfig.getRows(node, Open_vSwitch.NAME.getName());
114
115             if (ovsTable == null) {
116                 logger.error("Open_vSwitch table is null for Node {} ", node);
117                 return null;
118             }
119
120             // While there is only one entry in the HashMap, we can't access it by index...
121             for (Table<?> row : ovsTable.values()) {
122                 Open_vSwitch ovsRow = (Open_vSwitch)row;
123                 Map<String, String> configs = ovsRow.getOther_config();
124
125                 if (configs == null) {
126                     logger.debug("Open_vSwitch table is null for Node {} ", node);
127                     continue;
128                 }
129
130                 String tunnelEndpoint = configs.get(tunnelEndpointConfigName);
131
132                 if (tunnelEndpoint == null) {
133                     continue;
134                 }
135
136                 address = InetAddress.getByName(tunnelEndpoint);
137                 logger.debug("Tunnel Endpoint for Node {} {}", node, address.getHostAddress());
138                 break;
139             }
140         }
141         catch (Exception e) {
142             logger.error("Error populating Tunnel Endpoint for Node {} ", node, e);
143         }
144
145         return address;
146     }
147
148
149     /*
150      * Return the physical interface mapped to the given neutron physical network.
151      * Provider mappings will be of the following format:
152      * provider_mappings=physnet1:eth1[,physnet2:eth2]
153      */
154       public String getPhysicalInterfaceName (Node node, String physicalNetwork) {
155           String phyIf = null;
156
157           OVSDBConfigService ovsdbConfig = (OVSDBConfigService) ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
158           try {
159             Map<String, Table<?>> ovsTable = ovsdbConfig.getRows(node, Open_vSwitch.NAME.getName());
160
161             if (ovsTable == null) {
162                 logger.error("Open_vSwitch table is null for Node {} ", node);
163                 return null;
164             }
165
166             // Loop through all the Open_vSwitch rows looking for the first occurrence of other_config.
167             // The specification does not restrict the number of rows so we choose the first we find.
168             for (Table<?> row : ovsTable.values()) {
169                 String providerMaps;
170                 Open_vSwitch ovsRow = (Open_vSwitch) row;
171                 Map<String, String> configs = ovsRow.getOther_config();
172
173                 if (configs == null) {
174                     logger.debug("Open_vSwitch table is null for Node {} ", node);
175                     continue;
176                 }
177
178                 providerMaps = configs.get(providerMappingsConfigName);
179                 if (providerMaps == null) {
180                     providerMaps = providerMappings;
181                 }
182
183                 if (providerMaps != null) {
184                     for (String map : providerMaps.split(",")) {
185                         String[] pair = map.split(":");
186                         if (pair[0].equals(physicalNetwork)) {
187                             phyIf = pair[1];
188                             break;
189                         }
190                     }
191                 }
192
193                 if (phyIf != null) {
194                     break;
195                 }
196             }
197         } catch (Exception e) {
198             logger.error("Unable to find physical interface for Node: {}, Network {}",
199                     node, physicalNetwork, e);
200         }
201
202         if (phyIf == null) {
203             logger.error("Physical interface not found for Node: {}, Network {}",
204                     node, physicalNetwork);
205         }
206
207         return phyIf;
208     }
209
210     /* Return all physical interfaces configure in bridge mapping
211      * Bridge mappings will be of the following format:
212      * bridge_mappings=physnet1:eth1,physnet2:eth2
213      * Method will return list = {eth1, eth2}
214      */
215     public List<String> getAllPhysicalInterfaceNames(Node node) {
216         List<String> phyIfName = new ArrayList<String>();
217
218         try {
219             OVSDBConfigService ovsdbConfig = (OVSDBConfigService) ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
220             Map<String, Table<?>> ovsTable = ovsdbConfig.getRows(node, Open_vSwitch.NAME.getName());
221
222             if (ovsTable == null) {
223                 logger.error("Open_vSwitch table is null for Node {} ", node);
224                 return null;
225             }
226
227             // While there is only one entry in the HashMap, we can't access it by index...
228             for (Table<?> row : ovsTable.values()) {
229                 String bridgeMaps;
230                 Open_vSwitch ovsRow = (Open_vSwitch) row;
231                 Map<String, String> configs = ovsRow.getOther_config();
232
233                 if (configs == null) {
234                     logger.debug("Open_vSwitch table is null for Node {} ", node);
235                     continue;
236                 }
237
238                 bridgeMaps = configs.get(providerMappingsConfigName);
239                 if (bridgeMaps == null) {
240                     bridgeMaps = providerMappings;
241                 }
242
243                 if (bridgeMaps != null) {
244                     for (String map : bridgeMaps.split(",")) {
245                         String[] pair = map.split(":");
246                         phyIfName.add(pair[1]);
247                     }
248                 }
249             }
250         } catch (Exception e) {
251             logger.error("Unable to find physical interface for Node: {}",
252                     node, e);
253         }
254
255         logger.debug("Physical interface for Node: {}, If: {}",
256                 node, phyIfName);
257
258         return phyIfName;
259     }
260
261     public boolean isInterested (String tableName) {
262         return tableName.equalsIgnoreCase(Open_vSwitch.NAME.getName());
263     }
264
265 }