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.neutron.transcriber;
11 import java.lang.reflect.Method;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map.Entry;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
20 import org.opendaylight.neutron.spi.INeutronFloatingIPCRUD;
21 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
22 import org.opendaylight.neutron.spi.INeutronPortCRUD;
23 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
24 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
25 import org.opendaylight.neutron.spi.NeutronFloatingIP;
26 import org.opendaylight.neutron.spi.NeutronPort;
27 import org.opendaylight.neutron.spi.NeutronSubnet;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class NeutronFloatingIPInterface implements INeutronFloatingIPCRUD {
32 private static final Logger logger = LoggerFactory.getLogger(NeutronFloatingIPInterface.class);
34 private ConcurrentMap<String, NeutronFloatingIP> floatingIPDB = new ConcurrentHashMap<String, NeutronFloatingIP>();
36 // this method uses reflection to update an object from it's delta.
38 @SuppressWarnings("unused")
39 private boolean overwrite(Object target, Object delta) {
40 Method[] methods = target.getClass().getMethods();
42 for(Method toMethod: methods){
43 if(toMethod.getDeclaringClass().equals(target.getClass())
44 && toMethod.getName().startsWith("set")){
46 String toName = toMethod.getName();
47 String fromName = toName.replace("set", "get");
50 Method fromMethod = delta.getClass().getMethod(fromName);
51 Object value = fromMethod.invoke(delta, (Object[])null);
53 toMethod.invoke(target, value);
55 } catch (Exception e) {
64 // IfNBFloatingIPCRUD interface methods
67 public boolean floatingIPExists(String uuid) {
68 return floatingIPDB.containsKey(uuid);
72 public NeutronFloatingIP getFloatingIP(String uuid) {
73 if (!floatingIPExists(uuid)) {
76 return floatingIPDB.get(uuid);
80 public List<NeutronFloatingIP> getAllFloatingIPs() {
81 Set<NeutronFloatingIP> allIPs = new HashSet<NeutronFloatingIP>();
82 for (Entry<String, NeutronFloatingIP> entry : floatingIPDB.entrySet()) {
83 NeutronFloatingIP floatingip = entry.getValue();
84 allIPs.add(floatingip);
86 logger.debug("Exiting getAllFloatingIPs, Found {} FloatingIPs", allIPs.size());
87 List<NeutronFloatingIP> ans = new ArrayList<NeutronFloatingIP>();
93 public boolean addFloatingIP(NeutronFloatingIP input) {
94 INeutronNetworkCRUD networkCRUD = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
95 INeutronSubnetCRUD subnetCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
96 INeutronPortCRUD portCRUD = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
98 if (floatingIPExists(input.getID())) {
101 //if floating_ip_address isn't there, allocate from the subnet pool
102 NeutronSubnet subnet = subnetCRUD.getSubnet(networkCRUD.getNetwork(input.getFloatingNetworkUUID()).getSubnets().get(0));
103 if (input.getFloatingIPAddress() == null) {
104 input.setFloatingIPAddress(subnet.getLowAddr());
106 subnet.allocateIP(input.getFloatingIPAddress());
108 //if port_id is there, bind port to this floating ip
109 if (input.getPortUUID() != null) {
110 NeutronPort port = portCRUD.getPort(input.getPortUUID());
111 port.addFloatingIP(input.getFixedIPAddress(), input);
114 floatingIPDB.putIfAbsent(input.getID(), input);
119 public boolean removeFloatingIP(String uuid) {
120 INeutronNetworkCRUD networkCRUD = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
121 INeutronSubnetCRUD subnetCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
122 INeutronPortCRUD portCRUD = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
124 if (!floatingIPExists(uuid)) {
127 NeutronFloatingIP floatIP = getFloatingIP(uuid);
128 //if floating_ip_address isn't there, allocate from the subnet pool
129 NeutronSubnet subnet = subnetCRUD.getSubnet(networkCRUD.getNetwork(floatIP.getFloatingNetworkUUID()).getSubnets().get(0));
130 subnet.releaseIP(floatIP.getFloatingIPAddress());
131 if (floatIP.getPortUUID() != null) {
132 NeutronPort port = portCRUD.getPort(floatIP.getPortUUID());
133 port.removeFloatingIP(floatIP.getFixedIPAddress());
135 floatingIPDB.remove(uuid);
140 public boolean updateFloatingIP(String uuid, NeutronFloatingIP delta) {
141 INeutronPortCRUD portCRUD = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
143 if (!floatingIPExists(uuid)) {
146 NeutronFloatingIP target = floatingIPDB.get(uuid);
147 if (target.getPortUUID() != null) {
148 NeutronPort port = portCRUD.getPort(target.getPortUUID());
149 port.removeFloatingIP(target.getFixedIPAddress());
152 //if port_id is there, bind port to this floating ip
153 if (delta.getPortUUID() != null) {
154 NeutronPort port = portCRUD.getPort(delta.getPortUUID());
155 port.addFloatingIP(delta.getFixedIPAddress(), delta);
158 target.setPortUUID(delta.getPortUUID());
159 target.setFixedIPAddress(delta.getFixedIPAddress());