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