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