51824d6ce1a93e42f372e6863e0f91ee2f696bb2
[controller.git] / opendaylight / networkconfiguration / neutron / implementation / src / main / java / org / opendaylight / controller / networkconfig / neutron / implementation / NeutronFloatingIPInterface.java
1 /*
2  * Copyright IBM Corporation, 2013.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.networkconfig.neutron.implementation;
10
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;
16 import java.util.Set;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19
20 import org.opendaylight.controller.networkconfig.neutron.INeutronFloatingIPCRUD;
21 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
22 import org.opendaylight.controller.networkconfig.neutron.INeutronPortCRUD;
23 import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;
24 import org.opendaylight.controller.networkconfig.neutron.NeutronCRUDInterfaces;
25 import org.opendaylight.controller.networkconfig.neutron.NeutronFloatingIP;
26 import org.opendaylight.controller.networkconfig.neutron.NeutronPort;
27 import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class NeutronFloatingIPInterface implements INeutronFloatingIPCRUD {
32     private static final Logger logger = LoggerFactory.getLogger(NeutronFloatingIPInterface.class);
33
34     private ConcurrentMap<String, NeutronFloatingIP> floatingIPDB  = new ConcurrentHashMap<String, NeutronFloatingIP>();
35
36     // this method uses reflection to update an object from it's delta.
37
38     @SuppressWarnings("unused")
39     private boolean overwrite(Object target, Object delta) {
40         Method[] methods = target.getClass().getMethods();
41
42         for(Method toMethod: methods){
43             if(toMethod.getDeclaringClass().equals(target.getClass())
44                     && toMethod.getName().startsWith("set")){
45
46                 String toName = toMethod.getName();
47                 String fromName = toName.replace("set", "get");
48
49                 try {
50                     Method fromMethod = delta.getClass().getMethod(fromName);
51                     Object value = fromMethod.invoke(delta, (Object[])null);
52                     if(value != null){
53                         toMethod.invoke(target, value);
54                     }
55                 } catch (Exception e) {
56                     e.printStackTrace();
57                     return false;
58                 }
59             }
60         }
61         return true;
62     }
63
64     // IfNBFloatingIPCRUD interface methods
65
66     @Override
67     public boolean floatingIPExists(String uuid) {
68         return floatingIPDB.containsKey(uuid);
69     }
70
71     @Override
72     public NeutronFloatingIP getFloatingIP(String uuid) {
73         if (!floatingIPExists(uuid)) {
74             return null;
75         }
76         return floatingIPDB.get(uuid);
77     }
78
79     @Override
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);
85         }
86         logger.debug("Exiting getAllFloatingIPs, Found {} FloatingIPs", allIPs.size());
87         List<NeutronFloatingIP> ans = new ArrayList<NeutronFloatingIP>();
88         ans.addAll(allIPs);
89         return ans;
90     }
91
92     @Override
93     public boolean addFloatingIP(NeutronFloatingIP input) {
94         INeutronNetworkCRUD networkCRUD = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
95         INeutronSubnetCRUD subnetCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
96         INeutronPortCRUD portCRUD = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
97
98         if (floatingIPExists(input.getID())) {
99             return false;
100         }
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());
105         }
106         subnet.allocateIP(input.getFloatingIPAddress());
107
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);
112         }
113
114         floatingIPDB.putIfAbsent(input.getID(), input);
115         return true;
116     }
117
118     @Override
119     public boolean removeFloatingIP(String uuid) {
120         INeutronNetworkCRUD networkCRUD = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
121         INeutronSubnetCRUD subnetCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
122         INeutronPortCRUD portCRUD = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
123
124         if (!floatingIPExists(uuid)) {
125             return false;
126         }
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());
134         }
135         floatingIPDB.remove(uuid);
136         return true;
137     }
138
139     @Override
140     public boolean updateFloatingIP(String uuid, NeutronFloatingIP delta) {
141         INeutronPortCRUD portCRUD = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
142
143         if (!floatingIPExists(uuid)) {
144             return false;
145         }
146         NeutronFloatingIP target = floatingIPDB.get(uuid);
147         if (target.getPortUUID() != null) {
148             NeutronPort port = portCRUD.getPort(target.getPortUUID());
149             port.removeFloatingIP(target.getFixedIPAddress());
150         }
151
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);
156         }
157
158         target.setPortUUID(delta.getPortUUID());
159         target.setFixedIPAddress(delta.getFixedIPAddress());
160         return true;
161     }
162 }