Merge "Remove unused spring dependency and wrong version in web pom"
[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.Iterator;\r
17 import java.util.List;\r
18 import java.util.Set;\r
19 import java.util.Map.Entry;\r
20 import java.util.concurrent.ConcurrentMap;\r
21 \r
22 import org.apache.felix.dm.Component;\r
23 import org.opendaylight.controller.clustering.services.CacheConfigException;\r
24 import org.opendaylight.controller.clustering.services.CacheExistException;\r
25 import org.opendaylight.controller.clustering.services.IClusterContainerServices;\r
26 import org.opendaylight.controller.clustering.services.IClusterServices;\r
27 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;\r
28 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;\r
29 import org.slf4j.Logger;\r
30 import org.slf4j.LoggerFactory;\r
31 \r
32 public class NeutronNetworkInterface implements INeutronNetworkCRUD {\r
33     private static final Logger logger = LoggerFactory.getLogger(NeutronNetworkInterface.class);\r
34     private String containerName = null;\r
35 \r
36     private ConcurrentMap<String, NeutronNetwork> networkDB;\r
37     private IClusterContainerServices clusterContainerService = null;\r
38 \r
39     // methods needed for creating caches\r
40 \r
41     void setClusterContainerService(IClusterContainerServices s) {\r
42         logger.debug("Cluster Service set");\r
43         this.clusterContainerService = s;\r
44     }\r
45 \r
46     void unsetClusterContainerService(IClusterContainerServices s) {\r
47         if (this.clusterContainerService == s) {\r
48             logger.debug("Cluster Service removed!");\r
49             this.clusterContainerService = null;\r
50         }\r
51     }\r
52 \r
53     @SuppressWarnings("deprecation")\r
54     private void allocateCache() {\r
55         if (this.clusterContainerService == null) {\r
56             logger.error("un-initialized clusterContainerService, can't create cache");\r
57             return;\r
58         }\r
59         logger.debug("Creating Cache for Neutron Networks");\r
60         try {\r
61             // neutron caches\r
62             this.clusterContainerService.createCache("neutronNetworks",\r
63                     EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));\r
64         } catch (CacheConfigException cce) {\r
65             logger.error("Cache couldn't be created for Neutron Networks -  check cache mode");\r
66         } catch (CacheExistException cce) {\r
67             logger.error("Cache for Neutron Networks already exists, destroy and recreate");\r
68         }\r
69         logger.debug("Cache successfully created for Neutron Networks");\r
70     }\r
71 \r
72     @SuppressWarnings({ "unchecked", "deprecation" })\r
73     private void retrieveCache() {\r
74         if (this.clusterContainerService == null) {\r
75             logger.error("un-initialized clusterContainerService, can't retrieve cache");\r
76             return;\r
77         }\r
78         logger.debug("Retrieving cache for Neutron Networks");\r
79         networkDB = (ConcurrentMap<String, NeutronNetwork>) this.clusterContainerService.getCache("neutronNetworks");\r
80         if (networkDB == null) {\r
81             logger.error("Cache couldn't be retrieved for Neutron Networks");\r
82         }\r
83         logger.debug("Cache was successfully retrieved for Neutron Networks");\r
84     }\r
85 \r
86     private void startUp() {\r
87         allocateCache();\r
88         retrieveCache();\r
89     }\r
90 \r
91     /**\r
92      * Function called by the dependency manager when all the required\r
93      * dependencies are satisfied\r
94      *\r
95      */\r
96     void init(Component c) {\r
97         Dictionary<?, ?> props = c.getServiceProperties();\r
98         if (props != null) {\r
99             this.containerName = (String) props.get("containerName");\r
100             logger.debug("Running containerName: {}", this.containerName);\r
101         } else {\r
102             // In the Global instance case the containerName is empty\r
103             this.containerName = "";\r
104         }\r
105         startUp();\r
106     }\r
107 \r
108     @SuppressWarnings("deprecation")\r
109     private void destroyCache() {\r
110         if (this.clusterContainerService == null) {\r
111             logger.error("un-initialized clusterMger, can't destroy cache");\r
112             return;\r
113         }\r
114         logger.debug("Destroying Cache for Neutron Networks");\r
115         this.clusterContainerService.destroyCache("Neutron Networks");\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     // IfNBNetworkCRUD methods\r
173 \r
174     public boolean networkExists(String uuid) {\r
175         return networkDB.containsKey(uuid);\r
176     }\r
177 \r
178     public NeutronNetwork getNetwork(String uuid) {\r
179         if (!networkExists(uuid))\r
180             return null;\r
181         return networkDB.get(uuid);\r
182     }\r
183 \r
184     public List<NeutronNetwork> getAllNetworks() {\r
185         Set<NeutronNetwork> allNetworks = new HashSet<NeutronNetwork>();\r
186         for (Entry<String, NeutronNetwork> entry : networkDB.entrySet()) {\r
187             NeutronNetwork network = entry.getValue();\r
188             allNetworks.add(network);\r
189         }\r
190         logger.debug("Exiting getAllNetworks, Found {} OpenStackNetworks", allNetworks.size());\r
191         List<NeutronNetwork> ans = new ArrayList<NeutronNetwork>();\r
192         ans.addAll(allNetworks);\r
193         return ans;\r
194     }\r
195 \r
196     public boolean addNetwork(NeutronNetwork input) {\r
197         if (networkExists(input.getID()))\r
198             return false;\r
199         networkDB.putIfAbsent(input.getID(), input);\r
200       //TODO: add code to find INeutronNetworkAware services and call newtorkCreated on them\r
201         return true;\r
202     }\r
203 \r
204     public boolean removeNetwork(String uuid) {\r
205         if (!networkExists(uuid))\r
206             return false;\r
207         networkDB.remove(uuid);\r
208       //TODO: add code to find INeutronNetworkAware services and call newtorkDeleted on them\r
209         return true;\r
210     }\r
211 \r
212     public boolean updateNetwork(String uuid, NeutronNetwork delta) {\r
213         if (!networkExists(uuid))\r
214             return false;\r
215         NeutronNetwork target = networkDB.get(uuid);\r
216         return overwrite(target, delta);\r
217     }\r
218 \r
219     public boolean networkInUse(String netUUID) {\r
220         if (!networkExists(netUUID))\r
221             return true;\r
222         NeutronNetwork target = networkDB.get(netUUID);\r
223         if (target.getPortsOnNetwork().size() > 0)\r
224             return true;\r
225         return false;\r
226     }\r
227 }\r