Convert neutron implementation classes to unix line delimiters
[controller.git] / opendaylight / networkconfiguration / neutron / implementation / src / main / java / org / opendaylight / controller / networkconfig / neutron / implementation / NeutronRouterInterface.java
1 /*
2  * Copyright IBM Corporation, 2013.  All rights reserved.
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
9 package org.opendaylight.controller.networkconfig.neutron.implementation;
10
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;
17 import java.util.Set;
18 import java.util.Map.Entry;
19 import java.util.concurrent.ConcurrentMap;
20
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;
30
31 public class NeutronRouterInterface implements INeutronRouterCRUD {
32     private static final Logger logger = LoggerFactory.getLogger(NeutronRouterInterface.class);
33     private String containerName = null;
34
35     private IClusterContainerServices clusterContainerService = null;
36     private ConcurrentMap<String, NeutronRouter> routerDB;
37     // methods needed for creating caches
38
39     void setClusterContainerService(IClusterContainerServices s) {
40         logger.debug("Cluster Service set");
41         this.clusterContainerService = s;
42     }
43
44     void unsetClusterContainerService(IClusterContainerServices s) {
45         if (this.clusterContainerService == s) {
46             logger.debug("Cluster Service removed!");
47             this.clusterContainerService = null;
48         }
49     }
50
51     @SuppressWarnings("deprecation")
52     private void allocateCache() {
53         if (this.clusterContainerService == null) {
54             logger.error("un-initialized clusterContainerService, can't create cache");
55             return;
56         }
57         logger.debug("Creating Cache for Neutron Routers");
58         try {
59             // neutron caches
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");
66         }
67         logger.debug("Cache successfully created for Neutron Routers");
68     }
69
70     @SuppressWarnings({ "unchecked", "deprecation" })
71     private void retrieveCache() {
72         if (this.clusterContainerService == null) {
73             logger.error("un-initialized clusterContainerService, can't retrieve cache");
74             return;
75         }
76
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");
82         }
83         logger.debug("Cache was successfully retrieved for Neutron Routers");
84     }
85
86     @SuppressWarnings("deprecation")
87     private void destroyCache() {
88         if (this.clusterContainerService == null) {
89             logger.error("un-initialized clusterMger, can't destroy cache");
90             return;
91         }
92         logger.debug("Destroying Cache for HostTracker");
93         this.clusterContainerService.destroyCache("neutronRouters");
94     }
95
96     private void startUp() {
97         allocateCache();
98         retrieveCache();
99     }
100
101     /**
102      * Function called by the dependency manager when all the required
103      * dependencies are satisfied
104      *
105      */
106     void init(Component c) {
107         Dictionary<?, ?> props = c.getServiceProperties();
108         if (props != null) {
109             this.containerName = (String) props.get("containerName");
110             logger.debug("Running containerName: {}", this.containerName);
111         } else {
112             // In the Global instance case the containerName is empty
113             this.containerName = "";
114         }
115         startUp();
116     }
117
118     /**
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.
122      *
123      */
124     void destroy() {
125         destroyCache();
126     }
127
128     /**
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
131      *
132      */
133     void start() {
134     }
135
136     /**
137      * Function called by the dependency manager before the services exported by
138      * the component are unregistered, this will be followed by a "destroy ()"
139      * calls
140      *
141      */
142     void stop() {
143     }
144
145     // this method uses reflection to update an object from it's delta.
146
147     private boolean overwrite(Object target, Object delta) {
148         Method[] methods = target.getClass().getMethods();
149
150         for(Method toMethod: methods){
151             if(toMethod.getDeclaringClass().equals(target.getClass())
152                     && toMethod.getName().startsWith("set")){
153
154                 String toName = toMethod.getName();
155                 String fromName = toName.replace("set", "get");
156
157                 try {
158                     Method fromMethod = delta.getClass().getMethod(fromName);
159                     Object value = fromMethod.invoke(delta, (Object[])null);
160                     if(value != null){
161                         toMethod.invoke(target, value);
162                     }
163                 } catch (Exception e) {
164                     e.printStackTrace();
165                     return false;
166                 }
167             }
168         }
169         return true;
170     }
171
172
173     // IfNBRouterCRUD Interface methods
174
175     public boolean routerExists(String uuid) {
176         return routerDB.containsKey(uuid);
177     }
178
179     public NeutronRouter getRouter(String uuid) {
180         if (!routerExists(uuid))
181             return null;
182         return routerDB.get(uuid);
183     }
184
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);
190         }
191         logger.debug("Exiting getAllRouters, Found {} Routers", allRouters.size());
192         List<NeutronRouter> ans = new ArrayList<NeutronRouter>();
193         ans.addAll(allRouters);
194         return ans;
195     }
196
197     public boolean addRouter(NeutronRouter input) {
198         if (routerExists(input.getID()))
199             return false;
200         routerDB.putIfAbsent(input.getID(), input);
201         return true;
202     }
203
204     public boolean removeRouter(String uuid) {
205         if (!routerExists(uuid))
206             return false;
207         routerDB.remove(uuid);
208         return true;
209     }
210
211     public boolean updateRouter(String uuid, NeutronRouter delta) {
212         if (!routerExists(uuid))
213             return false;
214         NeutronRouter target = routerDB.get(uuid);
215         return overwrite(target, delta);
216     }
217
218     public boolean routerInUse(String routerUUID) {
219         if (!routerExists(routerUUID))
220             return true;
221         NeutronRouter target = routerDB.get(routerUUID);
222         return (target.getInterfaces().size() > 0);
223     }
224 }