329cd5647d7042d530157f76dcea3aa7fe029d7e
[ovsdb.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / impl / ConfigurationServiceImpl.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, Dave Tucker
9  */
10 package org.opendaylight.ovsdb.openstack.netvirt.impl;
11
12 import org.opendaylight.controller.sal.core.Node;
13 import org.opendaylight.ovsdb.lib.notation.Row;
14 import org.opendaylight.ovsdb.lib.notation.Version;
15 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
16 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
17 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
18
19 import com.google.common.collect.Maps;
20 import org.apache.commons.lang3.tuple.ImmutablePair;
21 import org.apache.commons.lang3.tuple.Pair;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import java.net.InetAddress;
26 import java.util.Map;
27 import java.util.Set;
28
29 public class ConfigurationServiceImpl implements org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService {
30     static final Logger logger = LoggerFactory.getLogger(ConfigurationServiceImpl.class);
31
32     private volatile OvsdbConfigurationService ovsdbConfigurationService;
33
34     private String integrationBridgeName;
35     private String networkBridgeName;
36     private String externalBridgeName;
37     private String tunnelEndpointKey;
38
39     private Map<Pair<String, String>, String> patchPortNames = Maps.newHashMap();
40     private String providerMappingsKey;
41     private String providerMapping;
42
43     public ConfigurationServiceImpl() {
44         tunnelEndpointKey = Constants.TUNNEL_ENDPOINT_KEY;
45         integrationBridgeName = Constants.INTEGRATION_BRIDGE;
46         networkBridgeName = Constants.NETWORK_BRIDGE;
47         externalBridgeName = Constants.EXTERNAL_BRIDGE;
48         patchPortNames.put(new ImmutablePair<>(integrationBridgeName, networkBridgeName),
49                            Constants.PATCH_PORT_TO_NETWORK_BRIDGE_NAME);
50         patchPortNames.put(new ImmutablePair<>(networkBridgeName, integrationBridgeName),
51                            Constants.PATCH_PORT_TO_INTEGRATION_BRIDGE_NAME);
52         providerMappingsKey = Constants.PROVIDER_MAPPINGS_KEY;
53         providerMapping = Constants.PROVIDER_MAPPING;
54     }
55
56     @Override
57     public String getIntegrationBridgeName() {
58         return integrationBridgeName;
59     }
60
61     @Override
62     public void setIntegrationBridgeName(String integrationBridgeName) {
63         this.integrationBridgeName = integrationBridgeName;
64     }
65
66     @Override
67     public String getNetworkBridgeName() {
68         return networkBridgeName;
69     }
70
71     @Override
72     public void setNetworkBridgeName(String networkBridgeName) {
73         this.networkBridgeName = networkBridgeName;
74     }
75
76     @Override
77     public String getExternalBridgeName() {
78         return externalBridgeName;
79     }
80
81     @Override
82     public void setExternalBridgeName(String externalBridgeName) {
83         this.externalBridgeName = externalBridgeName;
84     }
85
86     @Override
87     public String getTunnelEndpointKey() {
88         return tunnelEndpointKey;
89     }
90
91     @Override
92     public void setTunnelEndpointKey(String tunnelEndpointKey) {
93         this.tunnelEndpointKey = tunnelEndpointKey;
94     }
95
96     @Override
97     public String getProviderMappingsKey() {
98         return providerMappingsKey;
99     }
100
101     @Override
102     public void setProviderMappingsKey(String providerMappingsKey) {
103         this.providerMappingsKey = providerMappingsKey;
104     }
105
106     @Override
107     public Map<Pair<String, String>, String> getPatchPortNames() {
108         return patchPortNames;
109     }
110
111     @Override
112     public void setPatchPortNames(Map<Pair<String, String>, String> patchPortNames) {
113         this.patchPortNames = patchPortNames;
114     }
115
116     public String getPatchPortName(Pair portTuple){
117         return this.patchPortNames.get(portTuple);
118     }
119
120     @Override
121     public String getDefaultProviderMapping() {
122         return providerMapping;
123     }
124
125     @Override
126     public void setDefaultProviderMapping(String providerMapping) {
127         this.providerMapping = providerMapping;
128     }
129
130     @Override
131     public InetAddress getTunnelEndPoint(Node node) {
132         InetAddress address = null;
133         try {
134             Map<String, Row> ovsTable = ovsdbConfigurationService.getRows(node,
135                     ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
136
137             if (ovsTable == null) {
138                 logger.error("OpenVSwitch table is null for Node {} ", node);
139                 return null;
140             }
141
142             // While there is only one entry in the HashMap, we can't access it by index...
143             for (Row row : ovsTable.values()) {
144                 OpenVSwitch ovsRow = ovsdbConfigurationService.getTypedRow(node, OpenVSwitch.class, row);
145                 Map<String, String> configs = ovsRow.getOtherConfigColumn().getData();
146
147                 if (configs == null) {
148                     logger.debug("OpenVSwitch table is null for Node {} ", node);
149                     continue;
150                 }
151
152                 String tunnelEndpoint = configs.get(tunnelEndpointKey);
153
154                 if (tunnelEndpoint == null) {
155                     continue;
156                 }
157
158                 address = InetAddress.getByName(tunnelEndpoint);
159                 logger.debug("Tunnel Endpoint for Node {} {}", node, address.getHostAddress());
160                 break;
161             }
162         }
163         catch (Exception e) {
164             logger.error("Error populating Tunnel Endpoint for Node {} ", node, e);
165         }
166
167         return address;
168     }
169
170     @Override
171     public String getOpenflowVersion(Node node) {
172
173         String configuredVersion = System.getProperty("ovsdb.of.version");
174         if (configuredVersion != null){
175             switch (configuredVersion){
176                 case "1.0":
177                     return Constants.OPENFLOW10;
178                 case "1.3":
179                     //fall through
180                 default:
181                     return Constants.OPENFLOW13;
182
183             }
184         }
185
186         Map<String, Row> ovsRows = ovsdbConfigurationService.getRows(node,
187                 ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
188
189         if (ovsRows == null) {
190             logger.info("The OVS node {} has no Open_vSwitch rows", node.toString());
191             return null;
192         }
193
194         Version ovsVersion = null;
195         // While there is only one entry in the HashMap, we can't access it by index...
196         for (Row row : ovsRows.values()) {
197             OpenVSwitch ovsRow = ovsdbConfigurationService.getTypedRow(node, OpenVSwitch.class, row);
198             Set<String> versionSet = ovsRow.getOvsVersionColumn().getData();
199             if (versionSet != null && versionSet.iterator().hasNext()) {
200                 ovsVersion = Version.fromString(versionSet.iterator().next());
201             }
202         }
203
204         if (ovsVersion == null || ovsVersion.compareTo(Constants.OPENFLOW13_SUPPORTED) < 0) {
205             return Constants.OPENFLOW10;
206         }
207
208         return Constants.OPENFLOW13;
209     }
210
211     @Override
212     public String getDefaultGatewayMacAddress(Node node) {
213         final String l3gatewayForNode =
214             node != null ? System.getProperty("ovsdb.l3gateway.mac." + node.getNodeIDString()) : null;
215         return l3gatewayForNode != null ? l3gatewayForNode : System.getProperty("ovsdb.l3gateway.mac");
216     }
217 }