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