2 * Copyright IBM Corporation, 2013. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
9 package org.opendaylight.controller.networkconfig.neutron.implementation;
\r
11 import java.lang.reflect.Method;
\r
12 import java.util.ArrayList;
\r
13 import java.util.Dictionary;
\r
14 import java.util.EnumSet;
\r
15 import java.util.HashSet;
\r
16 import java.util.Iterator;
\r
17 import java.util.List;
\r
18 import java.util.Set;
\r
19 import java.util.Map.Entry;
\r
20 import java.util.concurrent.ConcurrentMap;
\r
22 import org.apache.felix.dm.Component;
\r
23 import org.opendaylight.controller.clustering.services.CacheConfigException;
\r
24 import org.opendaylight.controller.clustering.services.CacheExistException;
\r
25 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
\r
26 import org.opendaylight.controller.clustering.services.IClusterServices;
\r
27 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
\r
28 import org.opendaylight.controller.networkconfig.neutron.INeutronPortCRUD;
\r
29 import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;
\r
30 import org.opendaylight.controller.networkconfig.neutron.NeutronCRUDInterfaces;
\r
31 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
\r
32 import org.opendaylight.controller.networkconfig.neutron.NeutronPort;
\r
33 import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;
\r
34 import org.opendaylight.controller.networkconfig.neutron.Neutron_IPs;
\r
35 import org.slf4j.Logger;
\r
36 import org.slf4j.LoggerFactory;
\r
38 public class NeutronPortInterface implements INeutronPortCRUD {
\r
39 private static final Logger logger = LoggerFactory.getLogger(NeutronPortInterface.class);
\r
40 private String containerName = null;
\r
42 private IClusterContainerServices clusterContainerService = null;
\r
43 private ConcurrentMap<String, NeutronPort> portDB;
\r
45 // methods needed for creating caches
\r
47 void setClusterContainerService(IClusterContainerServices s) {
\r
48 logger.debug("Cluster Service set");
\r
49 clusterContainerService = s;
\r
52 void unsetClusterContainerService(IClusterContainerServices s) {
\r
53 if (clusterContainerService == s) {
\r
54 logger.debug("Cluster Service removed!");
\r
55 clusterContainerService = null;
\r
59 @SuppressWarnings("deprecation")
\r
60 private void allocateCache() {
\r
61 if (clusterContainerService == null) {
\r
62 logger.error("un-initialized clusterContainerService, can't create cache");
\r
65 logger.debug("Creating Cache for OpenDOVE");
\r
68 clusterContainerService.createCache("neutronPorts",
\r
69 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
\r
70 } catch (CacheConfigException cce) {
\r
71 logger.error("Cache couldn't be created for OpenDOVE - check cache mode");
\r
72 } catch (CacheExistException cce) {
\r
73 logger.error("Cache for OpenDOVE already exists, destroy and recreate");
\r
75 logger.debug("Cache successfully created for OpenDOVE");
\r
78 @SuppressWarnings({ "unchecked", "deprecation" })
\r
79 private void retrieveCache() {
\r
80 if (clusterContainerService == null) {
\r
81 logger.error("un-initialized clusterContainerService, can't retrieve cache");
\r
85 logger.debug("Retrieving cache for Neutron Ports");
\r
86 portDB = (ConcurrentMap<String, NeutronPort>) clusterContainerService
\r
87 .getCache("neutronPorts");
\r
88 if (portDB == null) {
\r
89 logger.error("Cache couldn't be retrieved for Neutron Ports");
\r
91 logger.debug("Cache was successfully retrieved for Neutron Ports");
\r
94 @SuppressWarnings("deprecation")
\r
95 private void destroyCache() {
\r
96 if (clusterContainerService == null) {
\r
97 logger.error("un-initialized clusterMger, can't destroy cache");
\r
100 logger.debug("Destroying Cache for HostTracker");
\r
101 clusterContainerService.destroyCache("neutronPorts");
\r
104 private void startUp() {
\r
110 * Function called by the dependency manager when all the required
\r
111 * dependencies are satisfied
\r
114 void init(Component c) {
\r
115 Dictionary<?, ?> props = c.getServiceProperties();
\r
116 if (props != null) {
\r
117 containerName = (String) props.get("containerName");
\r
118 logger.debug("Running containerName: {}", containerName);
\r
120 // In the Global instance case the containerName is empty
\r
121 containerName = "";
\r
127 * Function called by the dependency manager when at least one dependency
\r
128 * become unsatisfied or when the component is shutting down because for
\r
129 * example bundle is being stopped.
\r
137 * Function called by dependency manager after "init ()" is called and after
\r
138 * the services provided by the class are registered in the service registry
\r
145 * Function called by the dependency manager before the services exported by
\r
146 * the component are unregistered, this will be followed by a "destroy ()"
\r
153 // this method uses reflection to update an object from it's delta.
\r
155 private boolean overwrite(Object target, Object delta) {
\r
156 Method[] methods = target.getClass().getMethods();
\r
158 for(Method toMethod: methods){
\r
159 if(toMethod.getDeclaringClass().equals(target.getClass())
\r
160 && toMethod.getName().startsWith("set")){
\r
162 String toName = toMethod.getName();
\r
163 String fromName = toName.replace("set", "get");
\r
166 Method fromMethod = delta.getClass().getMethod(fromName);
\r
167 Object value = fromMethod.invoke(delta, (Object[])null);
\r
169 toMethod.invoke(target, value);
\r
171 } catch (Exception e) {
\r
172 e.printStackTrace();
\r
180 // IfNBPortCRUD methods
\r
183 public boolean portExists(String uuid) {
\r
184 return portDB.containsKey(uuid);
\r
188 public NeutronPort getPort(String uuid) {
\r
189 if (!portExists(uuid)) {
\r
192 return portDB.get(uuid);
\r
196 public List<NeutronPort> getAllPorts() {
\r
197 Set<NeutronPort> allPorts = new HashSet<NeutronPort>();
\r
198 for (Entry<String, NeutronPort> entry : portDB.entrySet()) {
\r
199 NeutronPort port = entry.getValue();
\r
200 allPorts.add(port);
\r
202 logger.debug("Exiting getAllPorts, Found {} OpenStackPorts", allPorts.size());
\r
203 List<NeutronPort> ans = new ArrayList<NeutronPort>();
\r
204 ans.addAll(allPorts);
\r
209 public boolean addPort(NeutronPort input) {
\r
210 if (portExists(input.getID())) {
\r
213 portDB.putIfAbsent(input.getID(), input);
\r
214 // if there are no fixed IPs, allocate one for each subnet in the network
\r
215 INeutronSubnetCRUD systemCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
\r
216 if (input.getFixedIPs().size() == 0) {
\r
217 List<Neutron_IPs> list = input.getFixedIPs();
\r
218 Iterator<NeutronSubnet> subnetIterator = systemCRUD.getAllSubnets().iterator();
\r
219 while (subnetIterator.hasNext()) {
\r
220 NeutronSubnet subnet = subnetIterator.next();
\r
221 if (subnet.getNetworkUUID().equals(input.getNetworkUUID())) {
\r
222 list.add(new Neutron_IPs(subnet.getID()));
\r
226 Iterator<Neutron_IPs> fixedIPIterator = input.getFixedIPs().iterator();
\r
227 while (fixedIPIterator.hasNext()) {
\r
228 Neutron_IPs ip = fixedIPIterator.next();
\r
229 NeutronSubnet subnet = systemCRUD.getSubnet(ip.getSubnetUUID());
\r
230 if (ip.getIpAddress() == null) {
\r
231 ip.setIpAddress(subnet.getLowAddr());
\r
233 if (!ip.getIpAddress().equals(subnet.getGatewayIP())) {
\r
234 subnet.allocateIP(ip.getIpAddress());
\r
237 subnet.setGatewayIPAllocated();
\r
239 subnet.addPort(input);
\r
241 INeutronNetworkCRUD networkIf = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
\r
243 NeutronNetwork network = networkIf.getNetwork(input.getNetworkUUID());
\r
244 network.addPort(input);
\r
249 public boolean removePort(String uuid) {
\r
250 if (!portExists(uuid)) {
\r
253 NeutronPort port = getPort(uuid);
\r
254 portDB.remove(uuid);
\r
255 INeutronNetworkCRUD networkCRUD = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
\r
256 INeutronSubnetCRUD systemCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
\r
258 NeutronNetwork network = networkCRUD.getNetwork(port.getNetworkUUID());
\r
259 network.removePort(port);
\r
260 Iterator<Neutron_IPs> fixedIPIterator = port.getFixedIPs().iterator();
\r
261 while (fixedIPIterator.hasNext()) {
\r
262 Neutron_IPs ip = fixedIPIterator.next();
\r
263 NeutronSubnet subnet = systemCRUD.getSubnet(ip.getSubnetUUID());
\r
264 if (!ip.getIpAddress().equals(subnet.getGatewayIP())) {
\r
265 subnet.releaseIP(ip.getIpAddress());
\r
268 subnet.resetGatewayIPAllocated();
\r
270 subnet.removePort(port);
\r
276 public boolean updatePort(String uuid, NeutronPort delta) {
\r
277 if (!portExists(uuid)) {
\r
280 NeutronPort target = portDB.get(uuid);
\r
281 // remove old Fixed_IPs
\r
282 if (delta.getFixedIPs() != null) {
\r
283 NeutronPort port = getPort(uuid);
\r
284 INeutronSubnetCRUD systemCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
\r
285 for (Neutron_IPs ip: port.getFixedIPs()) {
\r
286 NeutronSubnet subnet = systemCRUD.getSubnet(ip.getSubnetUUID());
\r
287 subnet.releaseIP(ip.getIpAddress());
\r
290 // allocate new Fixed_IPs
\r
291 for (Neutron_IPs ip: delta.getFixedIPs()) {
\r
292 NeutronSubnet subnet = systemCRUD.getSubnet(ip.getSubnetUUID());
\r
293 if (ip.getIpAddress() == null) {
\r
294 ip.setIpAddress(subnet.getLowAddr());
\r
296 subnet.allocateIP(ip.getIpAddress());
\r
299 return overwrite(target, delta);
\r
303 public boolean macInUse(String macAddress) {
\r
304 List<NeutronPort> ports = getAllPorts();
\r
305 Iterator<NeutronPort> portIterator = ports.iterator();
\r
306 while (portIterator.hasNext()) {
\r
307 NeutronPort port = portIterator.next();
\r
308 if (macAddress.equalsIgnoreCase(port.getMacAddress())) {
\r
316 public NeutronPort getGatewayPort(String subnetUUID) {
\r
317 INeutronSubnetCRUD systemCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
\r
318 NeutronSubnet subnet = systemCRUD.getSubnet(subnetUUID);
\r
319 Iterator<NeutronPort> portIterator = getAllPorts().iterator();
\r
320 while (portIterator.hasNext()) {
\r
321 NeutronPort port = portIterator.next();
\r
322 List<Neutron_IPs> fixedIPs = port.getFixedIPs();
\r
323 if (fixedIPs.size() == 1) {
\r
324 if (subnet.getGatewayIP().equals(fixedIPs.get(0).getIpAddress())) {
\r