2 * Copyright IBM Corporation, 2013. All rights reserved.
\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
9 package org.opendaylight.controller.networkconfig.neutron.implementation;
\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
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
37 public class NeutronFloatingIPInterface implements INeutronFloatingIPCRUD {
\r
38 private static final Logger logger = LoggerFactory.getLogger(NeutronFloatingIPInterface.class);
\r
39 private String containerName = null;
\r
41 private IClusterContainerServices clusterContainerService = null;
\r
42 private ConcurrentMap<String, NeutronFloatingIP> floatingIPDB;
\r
44 // methods needed for creating caches
\r
46 void setClusterContainerService(IClusterContainerServices s) {
\r
47 logger.debug("Cluster Service set");
\r
48 this.clusterContainerService = s;
\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
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
64 logger.debug("Creating Cache for Neutron FloatingIPs");
\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
74 logger.debug("Cache successfully created for NeutronFloatingIps");
\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
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
90 logger.debug("Cache was successfully retrieved for Neutron FloatingIPs");
\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
99 logger.debug("Destroying Cache for HostTracker");
\r
100 this.clusterContainerService.destroyCache("neutronFloatingIPs");
\r
103 private void startUp() {
\r
109 * Function called by the dependency manager when all the required
\r
110 * dependencies are satisfied
\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
119 // In the Global instance case the containerName is empty
\r
120 this.containerName = "";
\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
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
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
152 // this method uses reflection to update an object from it's delta.
\r
154 private boolean overwrite(Object target, Object delta) {
\r
155 Method[] methods = target.getClass().getMethods();
\r
157 for(Method toMethod: methods){
\r
158 if(toMethod.getDeclaringClass().equals(target.getClass())
\r
159 && toMethod.getName().startsWith("set")){
\r
161 String toName = toMethod.getName();
\r
162 String fromName = toName.replace("set", "get");
\r
165 Method fromMethod = delta.getClass().getMethod(fromName);
\r
166 Object value = fromMethod.invoke(delta, (Object[])null);
\r
168 toMethod.invoke(target, value);
\r
170 } catch (Exception e) {
\r
171 e.printStackTrace();
\r
179 // IfNBFloatingIPCRUD interface methods
\r
181 public boolean floatingIPExists(String uuid) {
\r
182 return floatingIPDB.containsKey(uuid);
\r
185 public NeutronFloatingIP getFloatingIP(String uuid) {
\r
186 if (!floatingIPExists(uuid))
\r
188 return floatingIPDB.get(uuid);
\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
197 logger.debug("Exiting getAllFloatingIPs, Found {} FloatingIPs", allIPs.size());
\r
198 List<NeutronFloatingIP> ans = new ArrayList<NeutronFloatingIP>();
\r
199 ans.addAll(allIPs);
\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
208 if (floatingIPExists(input.getID()))
\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
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
222 floatingIPDB.putIfAbsent(input.getID(), input);
\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
231 if (!floatingIPExists(uuid))
\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
241 floatingIPDB.remove(uuid);
\r
245 public boolean updateFloatingIP(String uuid, NeutronFloatingIP delta) {
\r
246 INeutronPortCRUD portCRUD = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
\r
248 if (!floatingIPExists(uuid))
\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
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
262 target.setPortUUID(delta.getPortUUID());
\r
263 target.setFixedIPAddress(delta.getFixedIPAddress());
\r