2 * Copyright IBM Corporation, 2013. All rights reserved.
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
9 package org.opendaylight.controller.networkconfig.neutron.implementation;
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;
22 import java.util.concurrent.ConcurrentMap;
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;
45 public class NeutronFloatingIPInterface implements INeutronFloatingIPCRUD, IConfigurationContainerAware,
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;
51 private IClusterContainerServices clusterContainerService = null;
52 private IConfigurationContainerService configurationService;
53 private ConcurrentMap<String, NeutronFloatingIP> floatingIPDB;
55 // methods needed for creating caches
57 void setClusterContainerService(IClusterContainerServices s) {
58 logger.debug("Cluster Service set");
59 this.clusterContainerService = s;
62 void unsetClusterContainerService(IClusterContainerServices s) {
63 if (this.clusterContainerService == s) {
64 logger.debug("Cluster Service removed!");
65 this.clusterContainerService = null;
69 public void setConfigurationContainerService(IConfigurationContainerService service) {
70 logger.trace("Configuration service set: {}", service);
71 this.configurationService = service;
74 public void unsetConfigurationContainerService(IConfigurationContainerService service) {
75 logger.trace("Configuration service removed: {}", service);
76 this.configurationService = null;
79 private void allocateCache() {
80 if (this.clusterContainerService == null) {
81 logger.error("un-initialized clusterContainerService, can't create cache");
84 logger.debug("Creating Cache for Neutron FloatingIPs");
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");
94 logger.debug("Cache successfully created for NeutronFloatingIps");
97 @SuppressWarnings({ "unchecked" })
98 private void retrieveCache() {
99 if (this.clusterContainerService == null) {
100 logger.error("un-initialized clusterContainerService, can't retrieve cache");
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");
110 logger.debug("Cache was successfully retrieved for Neutron FloatingIPs");
113 private void destroyCache() {
114 if (this.clusterContainerService == null) {
115 logger.error("un-initialized clusterMger, can't destroy cache");
118 logger.debug("Destroying Cache for HostTracker");
119 this.clusterContainerService.destroyCache("neutronFloatingIPs");
122 private void startUp() {
129 * Function called by the dependency manager when all the required
130 * dependencies are satisfied
133 void init(Component c) {
134 Dictionary<?, ?> props = c.getServiceProperties();
136 this.containerName = (String) props.get("containerName");
137 logger.debug("Running containerName: {}", this.containerName);
139 // In the Global instance case the containerName is empty
140 this.containerName = "";
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.
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
164 * Function called by the dependency manager before the services exported by
165 * the component are unregistered, this will be followed by a "destroy ()"
172 // this method uses reflection to update an object from it's delta.
174 @SuppressWarnings("unused")
175 private boolean overwrite(Object target, Object delta) {
176 Method[] methods = target.getClass().getMethods();
178 for(Method toMethod: methods){
179 if(toMethod.getDeclaringClass().equals(target.getClass())
180 && toMethod.getName().startsWith("set")){
182 String toName = toMethod.getName();
183 String fromName = toName.replace("set", "get");
186 Method fromMethod = delta.getClass().getMethod(fromName);
187 Object value = fromMethod.invoke(delta, (Object[])null);
189 toMethod.invoke(target, value);
191 } catch (Exception e) {
200 // IfNBFloatingIPCRUD interface methods
203 public boolean floatingIPExists(String uuid) {
204 return floatingIPDB.containsKey(uuid);
208 public NeutronFloatingIP getFloatingIP(String uuid) {
209 if (!floatingIPExists(uuid)) {
212 return floatingIPDB.get(uuid);
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);
222 logger.debug("Exiting getAllFloatingIPs, Found {} FloatingIPs", allIPs.size());
223 List<NeutronFloatingIP> ans = new ArrayList<NeutronFloatingIP>();
229 public boolean addFloatingIP(NeutronFloatingIP input) {
230 INeutronNetworkCRUD networkCRUD = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
231 INeutronSubnetCRUD subnetCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
232 INeutronPortCRUD portCRUD = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
234 if (floatingIPExists(input.getID())) {
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());
242 subnet.allocateIP(input.getFloatingIPAddress());
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);
250 floatingIPDB.putIfAbsent(input.getID(), input);
255 public boolean removeFloatingIP(String uuid) {
256 INeutronNetworkCRUD networkCRUD = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
257 INeutronSubnetCRUD subnetCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
258 INeutronPortCRUD portCRUD = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
260 if (!floatingIPExists(uuid)) {
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());
271 floatingIPDB.remove(uuid);
276 public boolean updateFloatingIP(String uuid, NeutronFloatingIP delta) {
277 INeutronPortCRUD portCRUD = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
279 if (!floatingIPExists(uuid)) {
282 NeutronFloatingIP target = floatingIPDB.get(uuid);
283 if (target.getPortUUID() != null) {
284 NeutronPort port = portCRUD.getPort(target.getPortUUID());
285 port.removeFloatingIP(target.getFixedIPAddress());
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);
294 target.setPortUUID(delta.getPortUUID());
295 target.setFixedIPAddress(delta.getFixedIPAddress());
299 private void loadConfiguration() {
300 for (ConfigurationObject conf : configurationService.retrieveConfiguration(this, FILE_NAME)) {
301 NeutronFloatingIP nfIP = (NeutronFloatingIP) conf;
302 floatingIPDB.put(nfIP.getID(), nfIP);
307 public Status saveConfiguration() {
308 return configurationService.persistConfiguration(new ArrayList<ConfigurationObject>(floatingIPDB.values()),
313 public Object readObject(ObjectInputStream ois) throws FileNotFoundException, IOException, ClassNotFoundException {
314 return ois.readObject();