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