Neutron bundle migration to the new redesigned schema independent Library and Plugin.
[ovsdb.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.notation.Row;
20 import org.opendaylight.ovsdb.plugin.OVSDBConfigService;
21 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
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     @Override
74     public String getIntegrationBridgeName() {
75         return integrationBridgeName;
76     }
77
78     @Override
79     public void setIntegrationBridgeName(String integrationBridgeName) {
80         this.integrationBridgeName = integrationBridgeName;
81     }
82
83     @Override
84     public String getNetworkBridgeName() { return networkBridgeName; }
85
86     @Override
87     public void setNetworkBridgeName(String networkBridgeName) {
88         this.networkBridgeName = networkBridgeName;
89     }
90
91     @Override
92     public String getExternalBridgeName() {
93         return externalBridgeName;
94     }
95
96     @Override
97     public void setExternalBridgeName (String externalBridgeName) {
98         this.externalBridgeName = externalBridgeName;
99     }
100
101     @Override
102     public String getPatchToIntegration() {
103         return patchToIntegration;
104     }
105
106     @Override
107     public void setPatchToIntegration(String patchToIntegration) {
108         this.patchToIntegration = patchToIntegration;
109     }
110
111     @Override
112     public String getPatchToNetwork() { return patchToNetwork; }
113
114     @Override
115     public void setPatchToNetwork(String patchToNetwork) {
116         this.patchToNetwork = patchToNetwork;
117     }
118
119     @Override
120     public InetAddress getTunnelEndPoint(Node node) {
121         InetAddress address = null;
122         OVSDBConfigService ovsdbConfig = (OVSDBConfigService)ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
123         try {
124             Map<String, Row> ovsTable = ovsdbConfig.getRows(node, ovsdbConfig.getTableName(node, OpenVSwitch.class));
125
126             if (ovsTable == null) {
127                 logger.error("OpenVSwitch table is null for Node {} ", node);
128                 return null;
129             }
130
131             // While there is only one entry in the HashMap, we can't access it by index...
132             for (Row row : ovsTable.values()) {
133                 OpenVSwitch ovsRow = ovsdbConfig.getTypedRow(node, OpenVSwitch.class, row);
134                 Map<String, String> configs = ovsRow.getOtherConfigColumn().getData();
135
136                 if (configs == null) {
137                     logger.debug("OpenVSwitch table is null for Node {} ", node);
138                     continue;
139                 }
140
141                 String tunnelEndpoint = configs.get(tunnelEndpointConfigName);
142
143                 if (tunnelEndpoint == null) {
144                     continue;
145                 }
146
147                 address = InetAddress.getByName(tunnelEndpoint);
148                 logger.debug("Tunnel Endpoint for Node {} {}", node, address.getHostAddress());
149                 break;
150             }
151         }
152         catch (Exception e) {
153             logger.error("Error populating Tunnel Endpoint for Node {} ", node, e);
154         }
155
156         return address;
157     }
158
159
160     /*
161      * Return the physical interface mapped to the given neutron physical network.
162      * Provider mappings will be of the following format:
163      * provider_mappings=physnet1:eth1[,physnet2:eth2]
164      */
165       @Override
166     public String getPhysicalInterfaceName (Node node, String physicalNetwork) {
167           String phyIf = null;
168
169           OVSDBConfigService ovsdbConfig = (OVSDBConfigService) ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
170           try {
171             Map<String, Row> ovsTable = ovsdbConfig.getRows(node, ovsdbConfig.getTableName(node, OpenVSwitch.class));
172
173             if (ovsTable == null) {
174                 logger.error("OpenVSwitch table is null for Node {} ", node);
175                 return null;
176             }
177
178             // Loop through all the OpenVSwitch rows looking for the first occurrence of other_config.
179             // The specification does not restrict the number of rows so we choose the first we find.
180             for (Row row : ovsTable.values()) {
181                 String providerMaps;
182                 OpenVSwitch ovsRow = ovsdbConfig.getTypedRow(node, OpenVSwitch.class, row);
183                 Map<String, String> configs = ovsRow.getOtherConfigColumn().getData();
184
185                 if (configs == null) {
186                     logger.debug("OpenVSwitch table is null for Node {} ", node);
187                     continue;
188                 }
189
190                 providerMaps = configs.get(providerMappingsConfigName);
191                 if (providerMaps == null) {
192                     providerMaps = providerMappings;
193                 }
194
195                 if (providerMaps != null) {
196                     for (String map : providerMaps.split(",")) {
197                         String[] pair = map.split(":");
198                         if (pair[0].equals(physicalNetwork)) {
199                             phyIf = pair[1];
200                             break;
201                         }
202                     }
203                 }
204
205                 if (phyIf != null) {
206                     break;
207                 }
208             }
209         } catch (Exception e) {
210             logger.error("Unable to find physical interface for Node: {}, Network {}",
211                     node, physicalNetwork, e);
212         }
213
214         if (phyIf == null) {
215             logger.error("Physical interface not found for Node: {}, Network {}",
216                     node, physicalNetwork);
217         }
218
219         return phyIf;
220     }
221
222     /* Return all physical interfaces configure in bridge mapping
223      * Bridge mappings will be of the following format:
224      * bridge_mappings=physnet1:eth1,physnet2:eth2
225      * Method will return list = {eth1, eth2}
226      */
227     @Override
228     public List<String> getAllPhysicalInterfaceNames(Node node) {
229         List<String> phyIfName = new ArrayList<String>();
230
231         try {
232             OVSDBConfigService ovsdbConfig = (OVSDBConfigService) ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
233             Map<String, Row> ovsTable = ovsdbConfig.getRows(node, ovsdbConfig.getTableName(node, OpenVSwitch.class));
234
235             if (ovsTable == null) {
236                 logger.error("OpenVSwitch table is null for Node {} ", node);
237                 return null;
238             }
239
240             // While there is only one entry in the HashMap, we can't access it by index...
241             for (Row row : ovsTable.values()) {
242                 String bridgeMaps;
243                 OpenVSwitch ovsRow = ovsdbConfig.getTypedRow(node, OpenVSwitch.class, row);
244                 Map<String, String> configs = ovsRow.getOtherConfigColumn().getData();
245
246                 if (configs == null) {
247                     logger.debug("OpenVSwitch table is null for Node {} ", node);
248                     continue;
249                 }
250
251                 bridgeMaps = configs.get(providerMappingsConfigName);
252                 if (bridgeMaps == null) {
253                     bridgeMaps = providerMappings;
254                 }
255
256                 if (bridgeMaps != null) {
257                     for (String map : bridgeMaps.split(",")) {
258                         String[] pair = map.split(":");
259                         phyIfName.add(pair[1]);
260                     }
261                 }
262             }
263         } catch (Exception e) {
264             logger.error("Unable to find physical interface for Node: {}",
265                     node, e);
266         }
267
268         logger.debug("Physical interface for Node: {}, If: {}",
269                 node, phyIfName);
270
271         return phyIfName;
272     }
273 }