Merge "Added the missing Copyright headers to most of the java files."
[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
9  */
10 package org.opendaylight.ovsdb.neutron;
11
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.HashMap;
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.internal.Table;
21 import org.opendaylight.ovsdb.plugin.OVSDBConfigService;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class AdminConfigManager {
26     static final Logger logger = LoggerFactory.getLogger(AdminConfigManager.class);
27
28     private String integrationBridgeName;
29     private String tunnelBridgeName;
30     private String externalBridgeName;
31     private String tunnelEndpointConfigName;
32     private String patchToIntegration;
33     private String patchToTunnel;
34     private Map<Node, InetAddress> tunnelEndpoints;
35
36     // Refer to /etc/quantum/plugins/openvswitch/ovs_quantum_plugin.ini
37     private static String DEFAULT_TUNNEL_ENDPOINT_CONFIG_STRING = "local_ip";
38     private static String DEFAULT_INTEGRATION_BRIDGENAME = "br-int";
39     private static String DEFAULT_TUNNEL_BRIDGENAME = "br-tun";
40     private static String DEFAULT_EXTERNAL_BRIDGENAME = "br-ex";
41     private static String DEFAULT_PATCH_TO_INTEGRATION = "patch-int";
42     private static String DEFAULT_PATCH_TO_TUNNEL = "patch-tun";
43     private static String CONFIG_TUNNEL_ENDPOINT_CONFIG = "tunnel_endpoint_config_string";
44     private static String CONFIG_INTEGRATION_BRIDGENAME = "integration_bridge";
45     private static String CONFIG_TUNNEL_BRIDGENAME = "tunnel_bridge";
46     private static String CONFIG_EXTERNAL_BRIDGENAME = "external_bridge";
47     private static String CONFIG_PATCH_TO_INTEGRATION = "patch-int";
48     private static String CONFIG_PATCH_TO_TUNNEL = "patch-tun";
49
50     private static AdminConfigManager adminConfiguration = new AdminConfigManager();
51
52     private AdminConfigManager() {
53         tunnelEndpoints = new HashMap<Node, InetAddress>();
54         tunnelEndpointConfigName = System.getProperty(CONFIG_TUNNEL_ENDPOINT_CONFIG);
55         integrationBridgeName = System.getProperty(CONFIG_INTEGRATION_BRIDGENAME);
56         tunnelBridgeName = System.getProperty(CONFIG_TUNNEL_BRIDGENAME);
57         externalBridgeName = System.getProperty(CONFIG_EXTERNAL_BRIDGENAME);
58         patchToIntegration = System.getProperty(CONFIG_PATCH_TO_INTEGRATION);
59         patchToTunnel = System.getProperty(CONFIG_PATCH_TO_TUNNEL);
60
61         if (tunnelEndpointConfigName == null) tunnelEndpointConfigName = DEFAULT_TUNNEL_ENDPOINT_CONFIG_STRING;
62         if (integrationBridgeName == null) integrationBridgeName = DEFAULT_INTEGRATION_BRIDGENAME;
63         if (tunnelBridgeName == null) tunnelBridgeName = DEFAULT_TUNNEL_BRIDGENAME;
64         if (externalBridgeName == null) externalBridgeName = DEFAULT_EXTERNAL_BRIDGENAME;
65         if (patchToIntegration == null) patchToIntegration = DEFAULT_PATCH_TO_INTEGRATION;
66         if (patchToTunnel == null) patchToTunnel = DEFAULT_PATCH_TO_TUNNEL;
67     }
68
69     public static AdminConfigManager getManager() {
70         return adminConfiguration;
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 getTunnelBridgeName() {
82         return tunnelBridgeName;
83     }
84
85     public void setTunnelBridgeName(String tunnelBridgeName) {
86         this.tunnelBridgeName = tunnelBridgeName;
87     }
88
89     public String getExternalBridgeName() {
90         return externalBridgeName;
91     }
92
93     public void setExternalBridgeName (String externalBridgeName) {
94         this.externalBridgeName = externalBridgeName;
95     }
96
97     public String getPatchToIntegration() {
98         return patchToIntegration;
99     }
100
101     public void setPatchToIntegration(String patchToIntegration) {
102         this.patchToIntegration = patchToIntegration;
103     }
104
105     public String getPatchToTunnel() {
106         return patchToTunnel;
107     }
108
109     public void setPatchToTunnel(String patchToTunnel) {
110         this.patchToTunnel = patchToTunnel;
111     }
112
113     public InetAddress getTunnelEndPoint(Node node) {
114         return tunnelEndpoints.get(node);
115     }
116
117     public void addTunnelEndpoint (Node node, InetAddress address) {
118         tunnelEndpoints.put(node, address);
119     }
120
121     public boolean isInterested (String tableName) {
122         return tableName.equalsIgnoreCase(Open_vSwitch.NAME.getName());
123     }
124
125     private void populateTunnelEndpoint (Node node, Open_vSwitch row) {
126         Map<String, String> configs = row.getOther_config();
127         if (configs != null) {
128             String tunnelEndpoint = configs.get(tunnelEndpointConfigName);
129             if (tunnelEndpoint != null) {
130                 try {
131                     InetAddress address = InetAddress.getByName(tunnelEndpoint);
132                     addTunnelEndpoint(node, address);
133                     logger.debug("Tunnel Endpoint for Node {} {}", node, address.getHostAddress());
134                 } catch (UnknownHostException e) {
135                     logger.error("Unable to add tunnel endpoint for node " + node, e);
136                 }
137             }
138         }
139     }
140
141     public void populateTunnelEndpoint (Node node) {
142         OVSDBConfigService ovsdbTable = (OVSDBConfigService)ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
143         try {
144             Map<String, Table<?>> openvswitchTable = ovsdbTable.getRows(node, Open_vSwitch.NAME.getName());
145             if (openvswitchTable == null) {
146                 logger.debug("Open_vSwitch table is null for Node {} ", node);
147                 return;
148             }
149
150             for (Table<?> row : openvswitchTable.values()) {
151                 populateTunnelEndpoint(node, (Open_vSwitch)row);
152             }
153         } catch (Exception e) {
154             logger.error("Error populating Tunnel Endpoint for Node {} ", node, e);
155         }
156     }
157
158     // Use this later if there is a need to update the tunnel-endpoint dynamically
159     public void populateTunnelEndpoint (Node node, String tableName, Table<?> row) {
160         OVSDBConfigService ovsdbTable = (OVSDBConfigService)ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
161         try {
162             if (isInterested(tableName)) {
163                 populateTunnelEndpoint(node, (Open_vSwitch)row);
164             }
165         } catch (Exception e) {
166             logger.error("Error populating Tunnel Endpoint for Node {} ", node, e);
167         }
168     }
169 }