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