Add missing test methods to IRouterAware and clean up unused imports.
[controller.git] / opendaylight / networkconfiguration / neutron / implementation / src / main / java / org / opendaylight / controller / networkconfig / neutron / implementation / NeutronNetworkInterface.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.INeutronNetworkCRUD;\r
27 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;\r
28 import org.slf4j.Logger;\r
29 import org.slf4j.LoggerFactory;\r
30 \r
31 public class NeutronNetworkInterface implements INeutronNetworkCRUD {\r
32     private static final Logger logger = LoggerFactory.getLogger(NeutronNetworkInterface.class);\r
33     private String containerName = null;\r
34 \r
35     private ConcurrentMap<String, NeutronNetwork> networkDB;\r
36     private IClusterContainerServices clusterContainerService = null;\r
37 \r
38     // methods needed for creating caches\r
39 \r
40     void setClusterContainerService(IClusterContainerServices s) {\r
41         logger.debug("Cluster Service set");\r
42         this.clusterContainerService = s;\r
43     }\r
44 \r
45     void unsetClusterContainerService(IClusterContainerServices s) {\r
46         if (this.clusterContainerService == s) {\r
47             logger.debug("Cluster Service removed!");\r
48             this.clusterContainerService = null;\r
49         }\r
50     }\r
51 \r
52     @SuppressWarnings("deprecation")\r
53     private void allocateCache() {\r
54         if (this.clusterContainerService == null) {\r
55             logger.error("un-initialized clusterContainerService, can't create cache");\r
56             return;\r
57         }\r
58         logger.debug("Creating Cache for Neutron Networks");\r
59         try {\r
60             // neutron caches\r
61             this.clusterContainerService.createCache("neutronNetworks",\r
62                     EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));\r
63         } catch (CacheConfigException cce) {\r
64             logger.error("Cache couldn't be created for Neutron Networks -  check cache mode");\r
65         } catch (CacheExistException cce) {\r
66             logger.error("Cache for Neutron Networks already exists, destroy and recreate");\r
67         }\r
68         logger.debug("Cache successfully created for Neutron Networks");\r
69     }\r
70 \r
71     @SuppressWarnings({ "unchecked", "deprecation" })\r
72     private void retrieveCache() {\r
73         if (this.clusterContainerService == null) {\r
74             logger.error("un-initialized clusterContainerService, can't retrieve cache");\r
75             return;\r
76         }\r
77         logger.debug("Retrieving cache for Neutron Networks");\r
78         networkDB = (ConcurrentMap<String, NeutronNetwork>) this.clusterContainerService.getCache("neutronNetworks");\r
79         if (networkDB == null) {\r
80             logger.error("Cache couldn't be retrieved for Neutron Networks");\r
81         }\r
82         logger.debug("Cache was successfully retrieved for Neutron Networks");\r
83     }\r
84 \r
85     private void startUp() {\r
86         allocateCache();\r
87         retrieveCache();\r
88     }\r
89 \r
90     /**\r
91      * Function called by the dependency manager when all the required\r
92      * dependencies are satisfied\r
93      *\r
94      */\r
95     void init(Component c) {\r
96         Dictionary<?, ?> props = c.getServiceProperties();\r
97         if (props != null) {\r
98             this.containerName = (String) props.get("containerName");\r
99             logger.debug("Running containerName: {}", this.containerName);\r
100         } else {\r
101             // In the Global instance case the containerName is empty\r
102             this.containerName = "";\r
103         }\r
104         startUp();\r
105     }\r
106 \r
107     @SuppressWarnings("deprecation")\r
108     private void destroyCache() {\r
109         if (this.clusterContainerService == null) {\r
110             logger.error("un-initialized clusterMger, can't destroy cache");\r
111             return;\r
112         }\r
113         logger.debug("Destroying Cache for Neutron Networks");\r
114         this.clusterContainerService.destroyCache("Neutron Networks");\r
115     }\r
116 \r
117     /**\r
118      * Function called by the dependency manager when at least one dependency\r
119      * become unsatisfied or when the component is shutting down because for\r
120      * example bundle is being stopped.\r
121      *\r
122      */\r
123     void destroy() {\r
124         destroyCache();\r
125     }\r
126 \r
127     /**\r
128      * Function called by dependency manager after "init ()" is called and after\r
129      * the services provided by the class are registered in the service registry\r
130      *\r
131      */\r
132     void start() {\r
133     }\r
134 \r
135     /**\r
136      * Function called by the dependency manager before the services exported by\r
137      * the component are unregistered, this will be followed by a "destroy ()"\r
138      * calls\r
139      *\r
140      */\r
141     void stop() {\r
142     }\r
143 \r
144     // this method uses reflection to update an object from it's delta.\r
145 \r
146     private boolean overwrite(Object target, Object delta) {\r
147         Method[] methods = target.getClass().getMethods();\r
148 \r
149         for(Method toMethod: methods){\r
150             if(toMethod.getDeclaringClass().equals(target.getClass())\r
151                     && toMethod.getName().startsWith("set")){\r
152 \r
153                 String toName = toMethod.getName();\r
154                 String fromName = toName.replace("set", "get");\r
155 \r
156                 try {\r
157                     Method fromMethod = delta.getClass().getMethod(fromName);\r
158                     Object value = fromMethod.invoke(delta, (Object[])null);\r
159                     if(value != null){\r
160                         toMethod.invoke(target, value);\r
161                     }\r
162                 } catch (Exception e) {\r
163                     e.printStackTrace();\r
164                     return false;\r
165                 }\r
166             }\r
167         }\r
168         return true;\r
169     }\r
170 \r
171     // IfNBNetworkCRUD methods\r
172 \r
173     public boolean networkExists(String uuid) {\r
174         return networkDB.containsKey(uuid);\r
175     }\r
176 \r
177     public NeutronNetwork getNetwork(String uuid) {\r
178         if (!networkExists(uuid))\r
179             return null;\r
180         return networkDB.get(uuid);\r
181     }\r
182 \r
183     public List<NeutronNetwork> getAllNetworks() {\r
184         Set<NeutronNetwork> allNetworks = new HashSet<NeutronNetwork>();\r
185         for (Entry<String, NeutronNetwork> entry : networkDB.entrySet()) {\r
186             NeutronNetwork network = entry.getValue();\r
187             allNetworks.add(network);\r
188         }\r
189         logger.debug("Exiting getAllNetworks, Found {} OpenStackNetworks", allNetworks.size());\r
190         List<NeutronNetwork> ans = new ArrayList<NeutronNetwork>();\r
191         ans.addAll(allNetworks);\r
192         return ans;\r
193     }\r
194 \r
195     public boolean addNetwork(NeutronNetwork input) {\r
196         if (networkExists(input.getID()))\r
197             return false;\r
198         networkDB.putIfAbsent(input.getID(), input);\r
199       //TODO: add code to find INeutronNetworkAware services and call newtorkCreated on them\r
200         return true;\r
201     }\r
202 \r
203     public boolean removeNetwork(String uuid) {\r
204         if (!networkExists(uuid))\r
205             return false;\r
206         networkDB.remove(uuid);\r
207       //TODO: add code to find INeutronNetworkAware services and call newtorkDeleted on them\r
208         return true;\r
209     }\r
210 \r
211     public boolean updateNetwork(String uuid, NeutronNetwork delta) {\r
212         if (!networkExists(uuid))\r
213             return false;\r
214         NeutronNetwork target = networkDB.get(uuid);\r
215         return overwrite(target, delta);\r
216     }\r
217 \r
218     public boolean networkInUse(String netUUID) {\r
219         if (!networkExists(netUUID))\r
220             return true;\r
221         NeutronNetwork target = networkDB.get(netUUID);\r
222         if (target.getPortsOnNetwork().size() > 0)\r
223             return true;\r
224         return false;\r
225     }\r
226 }\r