Add md-transcribing for HealthMonitors, and add *key "id"* to list healthmonitors...
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / 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.neutron.transcriber;
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.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;
33
34 public class NeutronFloatingIPInterface extends AbstractNeutronInterface implements INeutronFloatingIPCRUD {
35     private static final Logger logger = LoggerFactory.getLogger(NeutronFloatingIPInterface.class);
36
37     private ConcurrentMap<String, NeutronFloatingIP> floatingIPDB  = new ConcurrentHashMap<String, NeutronFloatingIP>();
38
39
40     NeutronFloatingIPInterface(ProviderContext providerContext) {
41         super(providerContext);
42     }
43
44     // this method uses reflection to update an object from it's delta.
45
46     @SuppressWarnings("unused")
47     private boolean overwrite(Object target, Object delta) {
48         Method[] methods = target.getClass().getMethods();
49
50         for(Method toMethod: methods){
51             if(toMethod.getDeclaringClass().equals(target.getClass())
52                     && toMethod.getName().startsWith("set")){
53
54                 String toName = toMethod.getName();
55                 String fromName = toName.replace("set", "get");
56
57                 try {
58                     Method fromMethod = delta.getClass().getMethod(fromName);
59                     Object value = fromMethod.invoke(delta, (Object[])null);
60                     if(value != null){
61                         toMethod.invoke(target, value);
62                     }
63                 } catch (Exception e) {
64                     e.printStackTrace();
65                     return false;
66                 }
67             }
68         }
69         return true;
70     }
71
72     // IfNBFloatingIPCRUD interface methods
73
74     @Override
75     public boolean floatingIPExists(String uuid) {
76         return floatingIPDB.containsKey(uuid);
77     }
78
79     @Override
80     public NeutronFloatingIP getFloatingIP(String uuid) {
81         if (!floatingIPExists(uuid)) {
82             return null;
83         }
84         return floatingIPDB.get(uuid);
85     }
86
87     @Override
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);
93         }
94         logger.debug("Exiting getAllFloatingIPs, Found {} FloatingIPs", allIPs.size());
95         List<NeutronFloatingIP> ans = new ArrayList<NeutronFloatingIP>();
96         ans.addAll(allIPs);
97         return ans;
98     }
99
100     @Override
101     public boolean addFloatingIP(NeutronFloatingIP input) {
102         INeutronNetworkCRUD networkCRUD = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
103         INeutronSubnetCRUD subnetCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
104         INeutronPortCRUD portCRUD = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
105
106         if (floatingIPExists(input.getID())) {
107             return false;
108         }
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());
113         }
114         subnet.allocateIP(input.getFloatingIPAddress());
115
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);
120         }
121
122         floatingIPDB.putIfAbsent(input.getID(), input);
123         return true;
124     }
125
126     @Override
127     public boolean removeFloatingIP(String uuid) {
128         INeutronNetworkCRUD networkCRUD = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
129         INeutronSubnetCRUD subnetCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
130         INeutronPortCRUD portCRUD = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
131
132         if (!floatingIPExists(uuid)) {
133             return false;
134         }
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());
142         }
143         floatingIPDB.remove(uuid);
144         return true;
145     }
146
147     @Override
148     public boolean updateFloatingIP(String uuid, NeutronFloatingIP delta) {
149         INeutronPortCRUD portCRUD = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
150
151         if (!floatingIPExists(uuid)) {
152             return false;
153         }
154         NeutronFloatingIP target = floatingIPDB.get(uuid);
155         if (target.getPortUUID() != null) {
156             NeutronPort port = portCRUD.getPort(target.getPortUUID());
157             port.removeFloatingIP(target.getFixedIPAddress());
158         }
159
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);
164         }
165
166         target.setPortUUID(delta.getPortUUID());
167         target.setFixedIPAddress(delta.getFixedIPAddress());
168         return true;
169     }
170
171     @Override
172     protected InstanceIdentifier createInstanceIdentifier(DataObject item) {
173         // TODO Auto-generated method stub
174         return null;
175     }
176
177     @Override
178     protected DataObject toMd(Object neutronObject) {
179         // TODO Auto-generated method stub
180         return null;
181     }
182
183     @Override
184     protected DataObject toMd(String uuid) {
185         // TODO Auto-generated method stub
186         return null;
187     }
188 }