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