2 * Copyright IBM Corporation, 2013. All rights reserved.
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
9 package org.opendaylight.controller.networkconfig.neutron.implementation;
11 import java.lang.reflect.Method;
12 import java.util.ArrayList;
13 import java.util.Dictionary;
14 import java.util.EnumSet;
15 import java.util.HashSet;
16 import java.util.List;
18 import java.util.Map.Entry;
19 import java.util.concurrent.ConcurrentMap;
21 import org.apache.felix.dm.Component;
22 import org.opendaylight.controller.clustering.services.CacheConfigException;
23 import org.opendaylight.controller.clustering.services.CacheExistException;
24 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
25 import org.opendaylight.controller.clustering.services.IClusterServices;
26 import org.opendaylight.controller.networkconfig.neutron.INeutronRouterCRUD;
27 import org.opendaylight.controller.networkconfig.neutron.NeutronRouter;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class NeutronRouterInterface implements INeutronRouterCRUD {
32 private static final Logger logger = LoggerFactory.getLogger(NeutronRouterInterface.class);
33 private String containerName = null;
35 private IClusterContainerServices clusterContainerService = null;
36 private ConcurrentMap<String, NeutronRouter> routerDB;
37 // methods needed for creating caches
39 void setClusterContainerService(IClusterContainerServices s) {
40 logger.debug("Cluster Service set");
41 this.clusterContainerService = s;
44 void unsetClusterContainerService(IClusterContainerServices s) {
45 if (this.clusterContainerService == s) {
46 logger.debug("Cluster Service removed!");
47 this.clusterContainerService = null;
51 @SuppressWarnings("deprecation")
52 private void allocateCache() {
53 if (this.clusterContainerService == null) {
54 logger.error("un-initialized clusterContainerService, can't create cache");
57 logger.debug("Creating Cache for Neutron Routers");
60 this.clusterContainerService.createCache("neutronRouters",
61 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
62 } catch (CacheConfigException cce) {
63 logger.error("Cache couldn't be created for Neutron Routers - check cache mode");
64 } catch (CacheExistException cce) {
65 logger.error("Cache for Neutron Routers already exists, destroy and recreate");
67 logger.debug("Cache successfully created for Neutron Routers");
70 @SuppressWarnings({ "unchecked", "deprecation" })
71 private void retrieveCache() {
72 if (this.clusterContainerService == null) {
73 logger.error("un-initialized clusterContainerService, can't retrieve cache");
77 logger.debug("Retrieving cache for Neutron Routers");
78 routerDB = (ConcurrentMap<String, NeutronRouter>) this.clusterContainerService
79 .getCache("neutronRouters");
80 if (routerDB == null) {
81 logger.error("Cache couldn't be retrieved for Neutron Routers");
83 logger.debug("Cache was successfully retrieved for Neutron Routers");
86 @SuppressWarnings("deprecation")
87 private void destroyCache() {
88 if (this.clusterContainerService == null) {
89 logger.error("un-initialized clusterMger, can't destroy cache");
92 logger.debug("Destroying Cache for HostTracker");
93 this.clusterContainerService.destroyCache("neutronRouters");
96 private void startUp() {
102 * Function called by the dependency manager when all the required
103 * dependencies are satisfied
106 void init(Component c) {
107 Dictionary<?, ?> props = c.getServiceProperties();
109 this.containerName = (String) props.get("containerName");
110 logger.debug("Running containerName: {}", this.containerName);
112 // In the Global instance case the containerName is empty
113 this.containerName = "";
119 * Function called by the dependency manager when at least one dependency
120 * become unsatisfied or when the component is shutting down because for
121 * example bundle is being stopped.
129 * Function called by dependency manager after "init ()" is called and after
130 * the services provided by the class are registered in the service registry
137 * Function called by the dependency manager before the services exported by
138 * the component are unregistered, this will be followed by a "destroy ()"
145 // this method uses reflection to update an object from it's delta.
147 private boolean overwrite(Object target, Object delta) {
148 Method[] methods = target.getClass().getMethods();
150 for(Method toMethod: methods){
151 if(toMethod.getDeclaringClass().equals(target.getClass())
152 && toMethod.getName().startsWith("set")){
154 String toName = toMethod.getName();
155 String fromName = toName.replace("set", "get");
158 Method fromMethod = delta.getClass().getMethod(fromName);
159 Object value = fromMethod.invoke(delta, (Object[])null);
161 toMethod.invoke(target, value);
163 } catch (Exception e) {
173 // IfNBRouterCRUD Interface methods
175 public boolean routerExists(String uuid) {
176 return routerDB.containsKey(uuid);
179 public NeutronRouter getRouter(String uuid) {
180 if (!routerExists(uuid))
182 return routerDB.get(uuid);
185 public List<NeutronRouter> getAllRouters() {
186 Set<NeutronRouter> allRouters = new HashSet<NeutronRouter>();
187 for (Entry<String, NeutronRouter> entry : routerDB.entrySet()) {
188 NeutronRouter router = entry.getValue();
189 allRouters.add(router);
191 logger.debug("Exiting getAllRouters, Found {} Routers", allRouters.size());
192 List<NeutronRouter> ans = new ArrayList<NeutronRouter>();
193 ans.addAll(allRouters);
197 public boolean addRouter(NeutronRouter input) {
198 if (routerExists(input.getID()))
200 routerDB.putIfAbsent(input.getID(), input);
204 public boolean removeRouter(String uuid) {
205 if (!routerExists(uuid))
207 routerDB.remove(uuid);
211 public boolean updateRouter(String uuid, NeutronRouter delta) {
212 if (!routerExists(uuid))
214 NeutronRouter target = routerDB.get(uuid);
215 return overwrite(target, delta);
218 public boolean routerInUse(String routerUUID) {
219 if (!routerExists(routerUUID))
221 NeutronRouter target = routerDB.get(routerUUID);
222 return (target.getInterfaces().size() > 0);