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.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;
18 import java.util.Map.Entry;
19 import java.util.concurrent.ConcurrentMap;
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;
37 public class NeutronFloatingIPInterface implements INeutronFloatingIPCRUD {
38 private static final Logger logger = LoggerFactory.getLogger(NeutronFloatingIPInterface.class);
39 private String containerName = null;
41 private IClusterContainerServices clusterContainerService = null;
42 private ConcurrentMap<String, NeutronFloatingIP> floatingIPDB;
44 // methods needed for creating caches
46 void setClusterContainerService(IClusterContainerServices s) {
47 logger.debug("Cluster Service set");
48 this.clusterContainerService = s;
51 void unsetClusterContainerService(IClusterContainerServices s) {
52 if (this.clusterContainerService == s) {
53 logger.debug("Cluster Service removed!");
54 this.clusterContainerService = null;
58 @SuppressWarnings("deprecation")
59 private void allocateCache() {
60 if (this.clusterContainerService == null) {
61 logger.error("un-initialized clusterContainerService, can't create cache");
64 logger.debug("Creating Cache for Neutron FloatingIPs");
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");
74 logger.debug("Cache successfully created for NeutronFloatingIps");
77 @SuppressWarnings({ "unchecked", "deprecation" })
78 private void retrieveCache() {
79 if (this.clusterContainerService == null) {
80 logger.error("un-initialized clusterContainerService, can't retrieve cache");
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");
90 logger.debug("Cache was successfully retrieved for Neutron FloatingIPs");
93 @SuppressWarnings("deprecation")
94 private void destroyCache() {
95 if (this.clusterContainerService == null) {
96 logger.error("un-initialized clusterMger, can't destroy cache");
99 logger.debug("Destroying Cache for HostTracker");
100 this.clusterContainerService.destroyCache("neutronFloatingIPs");
103 private void startUp() {
109 * Function called by the dependency manager when all the required
110 * dependencies are satisfied
113 void init(Component c) {
114 Dictionary<?, ?> props = c.getServiceProperties();
116 this.containerName = (String) props.get("containerName");
117 logger.debug("Running containerName: {}", this.containerName);
119 // In the Global instance case the containerName is empty
120 this.containerName = "";
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.
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
144 * Function called by the dependency manager before the services exported by
145 * the component are unregistered, this will be followed by a "destroy ()"
152 // this method uses reflection to update an object from it's delta.
154 private boolean overwrite(Object target, Object delta) {
155 Method[] methods = target.getClass().getMethods();
157 for(Method toMethod: methods){
158 if(toMethod.getDeclaringClass().equals(target.getClass())
159 && toMethod.getName().startsWith("set")){
161 String toName = toMethod.getName();
162 String fromName = toName.replace("set", "get");
165 Method fromMethod = delta.getClass().getMethod(fromName);
166 Object value = fromMethod.invoke(delta, (Object[])null);
168 toMethod.invoke(target, value);
170 } catch (Exception e) {
179 // IfNBFloatingIPCRUD interface methods
181 public boolean floatingIPExists(String uuid) {
182 return floatingIPDB.containsKey(uuid);
185 public NeutronFloatingIP getFloatingIP(String uuid) {
186 if (!floatingIPExists(uuid))
188 return floatingIPDB.get(uuid);
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);
197 logger.debug("Exiting getAllFloatingIPs, Found {} FloatingIPs", allIPs.size());
198 List<NeutronFloatingIP> ans = new ArrayList<NeutronFloatingIP>();
203 public boolean addFloatingIP(NeutronFloatingIP input) {
204 INeutronNetworkCRUD networkCRUD = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
205 INeutronSubnetCRUD subnetCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
206 INeutronPortCRUD portCRUD = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
208 if (floatingIPExists(input.getID()))
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());
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);
222 floatingIPDB.putIfAbsent(input.getID(), input);
226 public boolean removeFloatingIP(String uuid) {
227 INeutronNetworkCRUD networkCRUD = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
228 INeutronSubnetCRUD subnetCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
229 INeutronPortCRUD portCRUD = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
231 if (!floatingIPExists(uuid))
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());
241 floatingIPDB.remove(uuid);
245 public boolean updateFloatingIP(String uuid, NeutronFloatingIP delta) {
246 INeutronPortCRUD portCRUD = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
248 if (!floatingIPExists(uuid))
250 NeutronFloatingIP target = floatingIPDB.get(uuid);
251 if (target.getPortUUID() != null) {
252 NeutronPort port = portCRUD.getPort(target.getPortUUID());
253 port.removeFloatingIP(target.getFixedIPAddress());
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);
262 target.setPortUUID(delta.getPortUUID());
263 target.setFixedIPAddress(delta.getFixedIPAddress());