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.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
21 import org.opendaylight.neutron.spi.INeutronFloatingIPCRUD;
22 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
23 import org.opendaylight.neutron.spi.INeutronPortCRUD;
24 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
25 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
26 import org.opendaylight.neutron.spi.NeutronFloatingIP;
27 import org.opendaylight.neutron.spi.NeutronPort;
28 import org.opendaylight.neutron.spi.NeutronSubnet;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class NeutronFloatingIPInterface extends AbstractNeutronInterface implements INeutronFloatingIPCRUD {
35 private static final Logger logger = LoggerFactory.getLogger(NeutronFloatingIPInterface.class);
37 private ConcurrentMap<String, NeutronFloatingIP> floatingIPDB = new ConcurrentHashMap<String, NeutronFloatingIP>();
40 NeutronFloatingIPInterface(ProviderContext providerContext) {
41 super(providerContext);
44 // this method uses reflection to update an object from it's delta.
46 @SuppressWarnings("unused")
47 private boolean overwrite(Object target, Object delta) {
48 Method[] methods = target.getClass().getMethods();
50 for(Method toMethod: methods){
51 if(toMethod.getDeclaringClass().equals(target.getClass())
52 && toMethod.getName().startsWith("set")){
54 String toName = toMethod.getName();
55 String fromName = toName.replace("set", "get");
58 Method fromMethod = delta.getClass().getMethod(fromName);
59 Object value = fromMethod.invoke(delta, (Object[])null);
61 toMethod.invoke(target, value);
63 } catch (Exception e) {
72 // IfNBFloatingIPCRUD interface methods
75 public boolean floatingIPExists(String uuid) {
76 return floatingIPDB.containsKey(uuid);
80 public NeutronFloatingIP getFloatingIP(String uuid) {
81 if (!floatingIPExists(uuid)) {
84 return floatingIPDB.get(uuid);
88 public List<NeutronFloatingIP> getAllFloatingIPs() {
89 Set<NeutronFloatingIP> allIPs = new HashSet<NeutronFloatingIP>();
90 for (Entry<String, NeutronFloatingIP> entry : floatingIPDB.entrySet()) {
91 NeutronFloatingIP floatingip = entry.getValue();
92 allIPs.add(floatingip);
94 logger.debug("Exiting getAllFloatingIPs, Found {} FloatingIPs", allIPs.size());
95 List<NeutronFloatingIP> ans = new ArrayList<NeutronFloatingIP>();
101 public boolean addFloatingIP(NeutronFloatingIP input) {
102 INeutronNetworkCRUD networkCRUD = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
103 INeutronSubnetCRUD subnetCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
104 INeutronPortCRUD portCRUD = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
106 if (floatingIPExists(input.getID())) {
109 //if floating_ip_address isn't there, allocate from the subnet pool
110 NeutronSubnet subnet = subnetCRUD.getSubnet(networkCRUD.getNetwork(input.getFloatingNetworkUUID()).getSubnets().get(0));
111 if (input.getFloatingIPAddress() == null) {
112 input.setFloatingIPAddress(subnet.getLowAddr());
114 subnet.allocateIP(input.getFloatingIPAddress());
116 //if port_id is there, bind port to this floating ip
117 if (input.getPortUUID() != null) {
118 NeutronPort port = portCRUD.getPort(input.getPortUUID());
119 port.addFloatingIP(input.getFixedIPAddress(), input);
122 floatingIPDB.putIfAbsent(input.getID(), input);
127 public boolean removeFloatingIP(String uuid) {
128 INeutronNetworkCRUD networkCRUD = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
129 INeutronSubnetCRUD subnetCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
130 INeutronPortCRUD portCRUD = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
132 if (!floatingIPExists(uuid)) {
135 NeutronFloatingIP floatIP = getFloatingIP(uuid);
136 //if floating_ip_address isn't there, allocate from the subnet pool
137 NeutronSubnet subnet = subnetCRUD.getSubnet(networkCRUD.getNetwork(floatIP.getFloatingNetworkUUID()).getSubnets().get(0));
138 subnet.releaseIP(floatIP.getFloatingIPAddress());
139 if (floatIP.getPortUUID() != null) {
140 NeutronPort port = portCRUD.getPort(floatIP.getPortUUID());
141 port.removeFloatingIP(floatIP.getFixedIPAddress());
143 floatingIPDB.remove(uuid);
148 public boolean updateFloatingIP(String uuid, NeutronFloatingIP delta) {
149 INeutronPortCRUD portCRUD = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
151 if (!floatingIPExists(uuid)) {
154 NeutronFloatingIP target = floatingIPDB.get(uuid);
155 if (target.getPortUUID() != null) {
156 NeutronPort port = portCRUD.getPort(target.getPortUUID());
157 port.removeFloatingIP(target.getFixedIPAddress());
160 //if port_id is there, bind port to this floating ip
161 if (delta.getPortUUID() != null) {
162 NeutronPort port = portCRUD.getPort(delta.getPortUUID());
163 port.addFloatingIP(delta.getFixedIPAddress(), delta);
166 target.setPortUUID(delta.getPortUUID());
167 target.setFixedIPAddress(delta.getFixedIPAddress());
172 protected InstanceIdentifier createInstanceIdentifier(DataObject item) {
173 // TODO Auto-generated method stub
178 protected DataObject toMd(Object neutronObject) {
179 // TODO Auto-generated method stub
184 protected DataObject toMd(String uuid) {
185 // TODO Auto-generated method stub