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.List;
\r
17 import java.util.Set;
\r
18 import java.util.Map.Entry;
\r
19 import java.util.concurrent.ConcurrentMap;
\r
21 import org.apache.felix.dm.Component;
\r
22 import org.opendaylight.controller.clustering.services.CacheConfigException;
\r
23 import org.opendaylight.controller.clustering.services.CacheExistException;
\r
24 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
\r
25 import org.opendaylight.controller.clustering.services.IClusterServices;
\r
26 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
\r
27 import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;
\r
28 import org.opendaylight.controller.networkconfig.neutron.NeutronCRUDInterfaces;
\r
29 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
\r
30 import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;
\r
31 import org.slf4j.Logger;
\r
32 import org.slf4j.LoggerFactory;
\r
34 public class NeutronSubnetInterface implements INeutronSubnetCRUD {
\r
35 private static final Logger logger = LoggerFactory.getLogger(NeutronSubnetInterface.class);
\r
36 private String containerName = null;
\r
38 private IClusterContainerServices clusterContainerService = null;
\r
39 private ConcurrentMap<String, NeutronSubnet> subnetDB;
\r
41 // methods needed for creating caches
\r
43 void setClusterContainerService(IClusterContainerServices s) {
\r
44 logger.debug("Cluster Service set");
\r
45 this.clusterContainerService = s;
\r
48 void unsetClusterContainerService(IClusterContainerServices s) {
\r
49 if (this.clusterContainerService == s) {
\r
50 logger.debug("Cluster Service removed!");
\r
51 this.clusterContainerService = null;
\r
55 @SuppressWarnings("deprecation")
\r
56 private void allocateCache() {
\r
57 if (this.clusterContainerService == null) {
\r
58 logger.error("un-initialized clusterContainerService, can't create cache");
\r
61 logger.debug("Creating Cache for Neutron Subnets");
\r
64 this.clusterContainerService.createCache("neutronSubnets",
\r
65 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
\r
66 } catch (CacheConfigException cce) {
\r
67 logger.error("Cache couldn't be created for Neutron Subnets - check cache mode");
\r
68 } catch (CacheExistException cce) {
\r
69 logger.error("Cache for Neutron Subnets already exists, destroy and recreate");
\r
71 logger.debug("Cache successfully created for Neutron Subnets");
\r
74 @SuppressWarnings({ "unchecked", "deprecation" })
\r
75 private void retrieveCache() {
\r
76 if (this.clusterContainerService == null) {
\r
77 logger.error("un-initialized clusterContainerService, can't retrieve cache");
\r
81 logger.debug("Retrieving cache for Neutron Subnets");
\r
82 subnetDB = (ConcurrentMap<String, NeutronSubnet>) this.clusterContainerService
\r
83 .getCache("neutronSubnets");
\r
84 if (subnetDB == null) {
\r
85 logger.error("Cache couldn't be retrieved for Neutron Subnets");
\r
87 logger.debug("Cache was successfully retrieved for Neutron Subnets");
\r
90 @SuppressWarnings("deprecation")
\r
91 private void destroyCache() {
\r
92 if (this.clusterContainerService == null) {
\r
93 logger.error("un-initialized clusterMger, can't destroy cache");
\r
96 logger.debug("Destroying Cache for HostTracker");
\r
97 this.clusterContainerService.destroyCache("neutronSubnets");
\r
100 private void startUp() {
\r
106 * Function called by the dependency manager when all the required
\r
107 * dependencies are satisfied
\r
110 void init(Component c) {
\r
111 Dictionary<?, ?> props = c.getServiceProperties();
\r
112 if (props != null) {
\r
113 this.containerName = (String) props.get("containerName");
\r
114 logger.debug("Running containerName: {}", this.containerName);
\r
116 // In the Global instance case the containerName is empty
\r
117 this.containerName = "";
\r
123 * Function called by the dependency manager when at least one dependency
\r
124 * become unsatisfied or when the component is shutting down because for
\r
125 * example bundle is being stopped.
\r
133 * Function called by dependency manager after "init ()" is called and after
\r
134 * the services provided by the class are registered in the service registry
\r
141 * Function called by the dependency manager before the services exported by
\r
142 * the component are unregistered, this will be followed by a "destroy ()"
\r
149 // this method uses reflection to update an object from it's delta.
\r
151 private boolean overwrite(Object target, Object delta) {
\r
152 Method[] methods = target.getClass().getMethods();
\r
154 for(Method toMethod: methods){
\r
155 if(toMethod.getDeclaringClass().equals(target.getClass())
\r
156 && toMethod.getName().startsWith("set")){
\r
158 String toName = toMethod.getName();
\r
159 String fromName = toName.replace("set", "get");
\r
162 Method fromMethod = delta.getClass().getMethod(fromName);
\r
163 Object value = fromMethod.invoke(delta, (Object[])null);
\r
165 toMethod.invoke(target, value);
\r
167 } catch (Exception e) {
\r
168 e.printStackTrace();
\r
177 // IfNBSubnetCRUD methods
\r
179 public boolean subnetExists(String uuid) {
\r
180 return subnetDB.containsKey(uuid);
\r
183 public NeutronSubnet getSubnet(String uuid) {
\r
184 if (!subnetExists(uuid))
\r
186 return subnetDB.get(uuid);
\r
189 public List<NeutronSubnet> getAllSubnets() {
\r
190 Set<NeutronSubnet> allSubnets = new HashSet<NeutronSubnet>();
\r
191 for (Entry<String, NeutronSubnet> entry : subnetDB.entrySet()) {
\r
192 NeutronSubnet subnet = entry.getValue();
\r
193 allSubnets.add(subnet);
\r
195 logger.debug("Exiting getAllSubnets, Found {} OpenStackSubnets", allSubnets.size());
\r
196 List<NeutronSubnet> ans = new ArrayList<NeutronSubnet>();
\r
197 ans.addAll(allSubnets);
\r
201 public boolean addSubnet(NeutronSubnet input) {
\r
202 String id = input.getID();
\r
203 if (subnetExists(id))
\r
205 subnetDB.putIfAbsent(id, input);
\r
206 INeutronNetworkCRUD networkIf = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
\r
208 NeutronNetwork targetNet = networkIf.getNetwork(input.getNetworkUUID());
\r
209 targetNet.addSubnet(id);
\r
213 public boolean removeSubnet(String uuid) {
\r
214 if (!subnetExists(uuid))
\r
216 NeutronSubnet target = subnetDB.get(uuid);
\r
217 INeutronNetworkCRUD networkIf = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
\r
219 NeutronNetwork targetNet = networkIf.getNetwork(target.getNetworkUUID());
\r
220 targetNet.removeSubnet(uuid);
\r
221 subnetDB.remove(uuid);
\r
225 public boolean updateSubnet(String uuid, NeutronSubnet delta) {
\r
226 if (!subnetExists(uuid))
\r
228 NeutronSubnet target = subnetDB.get(uuid);
\r
229 return overwrite(target, delta);
\r
232 public boolean subnetInUse(String subnetUUID) {
\r
233 if (!subnetExists(subnetUUID))
\r
235 NeutronSubnet target = subnetDB.get(subnetUUID);
\r
236 return (target.getPortsInSubnet().size() > 0);
\r