Initial push of Neutron interface
[controller.git] / opendaylight / networkconfiguration / neutron / implementation / src / main / java / org / opendaylight / controller / networkconfig / neutron / implementation / NeutronRouterInterface.java
1 /*\r
2  * Copyright IBM Corporation, 2013.  All rights reserved.\r
3  *\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
7  */\r
8 \r
9 package org.opendaylight.controller.networkconfig.neutron.implementation;\r
10 \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
20 \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
30 \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
34 \r
35     private IClusterContainerServices clusterContainerService = null;\r
36     private ConcurrentMap<String, NeutronRouter> routerDB;\r
37     // methods needed for creating caches\r
38 \r
39     void setClusterContainerService(IClusterContainerServices s) {\r
40         logger.debug("Cluster Service set");\r
41         this.clusterContainerService = s;\r
42     }\r
43 \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
48         }\r
49     }\r
50 \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
55             return;\r
56         }\r
57         logger.debug("Creating Cache for Neutron Routers");\r
58         try {\r
59             // neutron caches\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
66         }\r
67         logger.debug("Cache successfully created for Neutron Routers");\r
68     }\r
69 \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
74             return;\r
75         }\r
76 \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
82         }\r
83         logger.debug("Cache was successfully retrieved for Neutron Routers");\r
84     }\r
85 \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
90             return;\r
91         }\r
92         logger.debug("Destroying Cache for HostTracker");\r
93         this.clusterContainerService.destroyCache("neutronRouters");\r
94     }\r
95 \r
96     private void startUp() {\r
97         allocateCache();\r
98         retrieveCache();\r
99     }\r
100 \r
101     /**\r
102      * Function called by the dependency manager when all the required\r
103      * dependencies are satisfied\r
104      *\r
105      */\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
111         } else {\r
112             // In the Global instance case the containerName is empty\r
113             this.containerName = "";\r
114         }\r
115         startUp();\r
116     }\r
117 \r
118     /**\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
122      *\r
123      */\r
124     void destroy() {\r
125         destroyCache();\r
126     }\r
127 \r
128     /**\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
131      *\r
132      */\r
133     void start() {\r
134     }\r
135 \r
136     /**\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
139      * calls\r
140      *\r
141      */\r
142     void stop() {\r
143     }\r
144 \r
145     // this method uses reflection to update an object from it's delta.\r
146 \r
147     private boolean overwrite(Object target, Object delta) {\r
148         Method[] methods = target.getClass().getMethods();\r
149 \r
150         for(Method toMethod: methods){\r
151             if(toMethod.getDeclaringClass().equals(target.getClass())\r
152                     && toMethod.getName().startsWith("set")){\r
153 \r
154                 String toName = toMethod.getName();\r
155                 String fromName = toName.replace("set", "get");\r
156 \r
157                 try {\r
158                     Method fromMethod = delta.getClass().getMethod(fromName);\r
159                     Object value = fromMethod.invoke(delta, (Object[])null);\r
160                     if(value != null){\r
161                         toMethod.invoke(target, value);\r
162                     }\r
163                 } catch (Exception e) {\r
164                     e.printStackTrace();\r
165                     return false;\r
166                 }\r
167             }\r
168         }\r
169         return true;\r
170     }\r
171 \r
172 \r
173     // IfNBRouterCRUD Interface methods\r
174 \r
175     public boolean routerExists(String uuid) {\r
176         return routerDB.containsKey(uuid);\r
177     }\r
178 \r
179     public NeutronRouter getRouter(String uuid) {\r
180         if (!routerExists(uuid))\r
181             return null;\r
182         return routerDB.get(uuid);\r
183     }\r
184 \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
190         }\r
191         logger.debug("Exiting getAllRouters, Found {} Routers", allRouters.size());\r
192         List<NeutronRouter> ans = new ArrayList<NeutronRouter>();\r
193         ans.addAll(allRouters);\r
194         return ans;\r
195     }\r
196 \r
197     public boolean addRouter(NeutronRouter input) {\r
198         if (routerExists(input.getID()))\r
199             return false;\r
200         routerDB.putIfAbsent(input.getID(), input);\r
201         return true;\r
202     }\r
203 \r
204     public boolean removeRouter(String uuid) {\r
205         if (!routerExists(uuid))\r
206             return false;\r
207         routerDB.remove(uuid);\r
208         return true;\r
209     }\r
210 \r
211     public boolean updateRouter(String uuid, NeutronRouter delta) {\r
212         if (!routerExists(uuid))\r
213             return false;\r
214         NeutronRouter target = routerDB.get(uuid);\r
215         return overwrite(target, delta);\r
216     }\r
217 \r
218     public boolean routerInUse(String routerUUID) {\r
219         if (!routerExists(routerUUID))\r
220             return true;\r
221         NeutronRouter target = routerDB.get(routerUUID);\r
222         return (target.getInterfaces().size() > 0);\r
223     }\r
224 }\r