Bug 1824 : OpenFlow13 Protocol plugin is made the default. Also Karaf edition doesnt...
[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 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.Constants;
22 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
23 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.google.common.collect.Maps;
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     @Override
117     public String getPatchPortName(Pair portTuple){
118         return this.patchPortNames.get(portTuple);
119     }
120
121     @Override
122     public String getDefaultProviderMapping() {
123         return providerMapping;
124     }
125
126     @Override
127     public void setDefaultProviderMapping(String providerMapping) {
128         this.providerMapping = providerMapping;
129     }
130
131     @Override
132     public InetAddress getTunnelEndPoint(Node node) {
133         InetAddress address = null;
134         try {
135             Map<String, Row> ovsTable = ovsdbConfigurationService.getRows(node,
136                     ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
137
138             if (ovsTable == null) {
139                 logger.error("OpenVSwitch table is null for Node {} ", node);
140                 return null;
141             }
142
143             // While there is only one entry in the HashMap, we can't access it by index...
144             for (Row row : ovsTable.values()) {
145                 OpenVSwitch ovsRow = ovsdbConfigurationService.getTypedRow(node, OpenVSwitch.class, row);
146                 Map<String, String> configs = ovsRow.getOtherConfigColumn().getData();
147
148                 if (configs == null) {
149                     logger.debug("OpenVSwitch table is null for Node {} ", node);
150                     continue;
151                 }
152
153                 String tunnelEndpoint = configs.get(tunnelEndpointKey);
154
155                 if (tunnelEndpoint == null) {
156                     continue;
157                 }
158
159                 address = InetAddress.getByName(tunnelEndpoint);
160                 logger.debug("Tunnel Endpoint for Node {} {}", node, address.getHostAddress());
161                 break;
162             }
163         }
164         catch (Exception e) {
165             logger.error("Error populating Tunnel Endpoint for Node {} ", node, e);
166         }
167
168         return address;
169     }
170
171     @Override
172     public String getOpenflowVersion(Node node) {
173
174         String configuredVersion = System.getProperty("ovsdb.of.version", "1.3");
175         if (configuredVersion != null){
176             switch (configuredVersion){
177                 case "1.0":
178                     return Constants.OPENFLOW10;
179                 case "1.3":
180                     //fall through
181                 default:
182                     return Constants.OPENFLOW13;
183
184             }
185         }
186
187         Map<String, Row> ovsRows = ovsdbConfigurationService.getRows(node,
188                 ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
189
190         if (ovsRows == null) {
191             logger.info("The OVS node {} has no Open_vSwitch rows", node.toString());
192             return null;
193         }
194
195         Version ovsVersion = null;
196         // While there is only one entry in the HashMap, we can't access it by index...
197         for (Row row : ovsRows.values()) {
198             OpenVSwitch ovsRow = ovsdbConfigurationService.getTypedRow(node, OpenVSwitch.class, row);
199             Set<String> versionSet = ovsRow.getOvsVersionColumn().getData();
200             if (versionSet != null && versionSet.iterator().hasNext()) {
201                 ovsVersion = Version.fromString(versionSet.iterator().next());
202             }
203         }
204
205         if (ovsVersion == null || ovsVersion.compareTo(Constants.OPENFLOW13_SUPPORTED) < 0) {
206             return Constants.OPENFLOW10;
207         }
208
209         return Constants.OPENFLOW13;
210     }
211
212     @Override
213     public String getDefaultGatewayMacAddress(Node node) {
214         final String l3gatewayForNode =
215             node != null ? System.getProperty("ovsdb.l3gateway.mac." + node.getNodeIDString()) : null;
216         return l3gatewayForNode != null ? l3gatewayForNode : System.getProperty("ovsdb.l3gateway.mac");
217     }
218 }