Code ReOrganization and Re-Architecture changes
[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.util.Map;
14
15 import org.opendaylight.controller.sal.core.Node;
16 import org.opendaylight.controller.sal.utils.ServiceHelper;
17 import org.opendaylight.ovsdb.lib.table.Open_vSwitch;
18 import org.opendaylight.ovsdb.lib.table.Table;
19 import org.opendaylight.ovsdb.plugin.OVSDBConfigService;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class AdminConfigManager {
24     static final Logger logger = LoggerFactory.getLogger(AdminConfigManager.class);
25
26     private String integrationBridgeName;
27     private String tunnelBridgeName;
28     private String externalBridgeName;
29     private String tunnelEndpointConfigName;
30     private String patchToIntegration;
31     private String patchToTunnel;
32
33     // Refer to /etc/quantum/plugins/openvswitch/ovs_quantum_plugin.ini
34     private static String DEFAULT_TUNNEL_ENDPOINT_CONFIG_STRING = "local_ip";
35     private static String DEFAULT_INTEGRATION_BRIDGENAME = "br-int";
36     private static String DEFAULT_TUNNEL_BRIDGENAME = "br-tun";
37     private static String DEFAULT_EXTERNAL_BRIDGENAME = "br-ex";
38     private static String DEFAULT_PATCH_TO_INTEGRATION = "patch-int";
39     private static String DEFAULT_PATCH_TO_TUNNEL = "patch-tun";
40     private static String CONFIG_TUNNEL_ENDPOINT_CONFIG = "tunnel_endpoint_config_string";
41     private static String CONFIG_INTEGRATION_BRIDGENAME = "integration_bridge";
42     private static String CONFIG_TUNNEL_BRIDGENAME = "tunnel_bridge";
43     private static String CONFIG_EXTERNAL_BRIDGENAME = "external_bridge";
44     private static String CONFIG_PATCH_TO_INTEGRATION = "patch-int";
45     private static String CONFIG_PATCH_TO_TUNNEL = "patch-tun";
46
47     private static AdminConfigManager adminConfiguration = new AdminConfigManager();
48
49     private AdminConfigManager() {
50         tunnelEndpointConfigName = System.getProperty(CONFIG_TUNNEL_ENDPOINT_CONFIG);
51         integrationBridgeName = System.getProperty(CONFIG_INTEGRATION_BRIDGENAME);
52         tunnelBridgeName = System.getProperty(CONFIG_TUNNEL_BRIDGENAME);
53         externalBridgeName = System.getProperty(CONFIG_EXTERNAL_BRIDGENAME);
54         patchToIntegration = System.getProperty(CONFIG_PATCH_TO_INTEGRATION);
55         patchToTunnel = System.getProperty(CONFIG_PATCH_TO_TUNNEL);
56
57         if (tunnelEndpointConfigName == null) tunnelEndpointConfigName = DEFAULT_TUNNEL_ENDPOINT_CONFIG_STRING;
58         if (integrationBridgeName == null) integrationBridgeName = DEFAULT_INTEGRATION_BRIDGENAME;
59         if (tunnelBridgeName == null) tunnelBridgeName = DEFAULT_TUNNEL_BRIDGENAME;
60         if (externalBridgeName == null) externalBridgeName = DEFAULT_EXTERNAL_BRIDGENAME;
61         if (patchToIntegration == null) patchToIntegration = DEFAULT_PATCH_TO_INTEGRATION;
62         if (patchToTunnel == null) patchToTunnel = DEFAULT_PATCH_TO_TUNNEL;
63     }
64
65     public static AdminConfigManager getManager() {
66         return adminConfiguration;
67     }
68
69     public String getIntegrationBridgeName() {
70         return integrationBridgeName;
71     }
72
73     public void setIntegrationBridgeName(String integrationBridgeName) {
74         this.integrationBridgeName = integrationBridgeName;
75     }
76
77     public String getTunnelBridgeName() {
78         return tunnelBridgeName;
79     }
80
81     public void setTunnelBridgeName(String tunnelBridgeName) {
82         this.tunnelBridgeName = tunnelBridgeName;
83     }
84
85     public String getExternalBridgeName() {
86         return externalBridgeName;
87     }
88
89     public void setExternalBridgeName (String externalBridgeName) {
90         this.externalBridgeName = externalBridgeName;
91     }
92
93     public String getPatchToIntegration() {
94         return patchToIntegration;
95     }
96
97     public void setPatchToIntegration(String patchToIntegration) {
98         this.patchToIntegration = patchToIntegration;
99     }
100
101     public String getPatchToTunnel() {
102         return patchToTunnel;
103     }
104
105     public void setPatchToTunnel(String patchToTunnel) {
106         this.patchToTunnel = patchToTunnel;
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     public boolean isInterested (String tableName) {
149         return tableName.equalsIgnoreCase(Open_vSwitch.NAME.getName());
150     }
151
152 }