2 * Copyright (C) 2014 Juniper Networks, Inc.
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.plugin2oc.neutron;
11 import java.io.IOException;
12 import java.net.HttpURLConnection;
13 import java.util.UUID;
15 import net.juniper.contrail.api.ApiConnector;
16 import net.juniper.contrail.api.types.FloatingIp;
17 import net.juniper.contrail.api.types.FloatingIpPool;
18 import net.juniper.contrail.api.types.Project;
19 import net.juniper.contrail.api.types.VirtualMachineInterface;
20 import net.juniper.contrail.api.types.VirtualNetwork;
22 import org.opendaylight.controller.networkconfig.neutron.INeutronFloatingIPAware;
23 import org.opendaylight.controller.networkconfig.neutron.NeutronFloatingIP;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Handle requests for Neutron Floating IP.
30 public class FloatingIpHandler implements INeutronFloatingIPAware {
34 static final Logger LOGGER = LoggerFactory.getLogger(PortHandler.class);
35 static ApiConnector apiConnector;
38 * Invoked when a floating ip creation is requested to check if the
39 * specified floating ip can be created.
42 * An instance of proposed new Neutron Floating ip object.
44 * @return A HTTP status code to the creation request.
47 public int canCreateFloatingIP(NeutronFloatingIP fip) {
48 apiConnector = Activator.apiConnector;
50 LOGGER.error("Neutron Floating Ip can not be null ");
51 return HttpURLConnection.HTTP_BAD_REQUEST;
53 if (("").equals(fip.getFloatingIPUUID())) {
54 LOGGER.error("Floating Ip UUID can not be null ");
55 return HttpURLConnection.HTTP_BAD_REQUEST;
57 if (fip.getTenantUUID() == null || ("").equals(fip.getTenantUUID())) {
58 LOGGER.error("Floating Ip tenant Id can not be null");
59 return HttpURLConnection.HTTP_BAD_REQUEST;
61 if (fip.getFloatingIPAddress() == null) {
62 LOGGER.error(" Floating Ip address can not be null");
63 return HttpURLConnection.HTTP_BAD_REQUEST;
66 return createfloatingIp(fip);
67 } catch (Exception e) {
69 LOGGER.error("Exception : " + e);
70 return HttpURLConnection.HTTP_INTERNAL_ERROR;
75 * Invoked when a floating ip creation is requested to create the floating
79 * An instance of proposed new Neutron Floating ip object.
81 * @return A HTTP status code to the creation request.
83 private int createfloatingIp(NeutronFloatingIP neutronFloatingIp) throws IOException {
84 String projectUUID = null;
85 String floatingPoolNetworkId = null;
86 String fipId = neutronFloatingIp.getID();
87 String floatingIpaddress = neutronFloatingIp.getFloatingIPAddress();
89 floatingPoolNetworkId = neutronFloatingIp.getFloatingNetworkUUID();
90 projectUUID = neutronFloatingIp.getTenantUUID().toString();
91 if (!(projectUUID.contains("-"))) {
92 projectUUID = uuidFormater(projectUUID);
94 projectUUID = UUID.fromString(projectUUID).toString();
95 } catch (Exception ex) {
96 LOGGER.error("UUID input incorrect", ex);
97 return HttpURLConnection.HTTP_INTERNAL_ERROR;
101 project = (Project) apiConnector.findById(Project.class, projectUUID);
102 if (project == null) {
104 Thread.currentThread();
106 } catch (InterruptedException interruptedException) {
107 LOGGER.error("InterruptedException : ", interruptedException);
108 return HttpURLConnection.HTTP_INTERNAL_ERROR;
110 project = (Project) apiConnector.findById(Project.class, projectUUID);
111 if (project == null) {
112 LOGGER.error("Could not find projectUUID...");
113 return HttpURLConnection.HTTP_NOT_FOUND;
116 VirtualNetwork virtualNetwork = (VirtualNetwork) apiConnector.findById(VirtualNetwork.class, floatingPoolNetworkId);
117 if (virtualNetwork == null) {
118 LOGGER.error("Could not find Virtual network...");
119 return HttpURLConnection.HTTP_NOT_FOUND;
121 String floatingPoolId = virtualNetwork.getFloatingIpPools().get(0).getUuid();
122 FloatingIpPool floatingIpPool = (FloatingIpPool) apiConnector.findById(FloatingIpPool.class, floatingPoolId);
123 if (floatingIpPool == null) {
124 LOGGER.error("Could not find Floating ip pool...");
125 return HttpURLConnection.HTTP_NOT_FOUND;
127 FloatingIp floatingIp = new FloatingIp();
128 floatingIp.setUuid(fipId);
129 floatingIp.setName(fipId);
130 floatingIp.setDisplayName(fipId);
131 floatingIp.setAddress(floatingIpaddress);
132 floatingIp.setParent(floatingIpPool);
133 floatingIp.setProject(project);
134 if (neutronFloatingIp.getPortUUID() != null) {
135 VirtualMachineInterface virtualMachineInterface = (VirtualMachineInterface) apiConnector.findById(VirtualMachineInterface.class,
136 neutronFloatingIp.getPortUUID());
137 if (virtualMachineInterface != null) {
138 floatingIp.addVirtualMachineInterface(virtualMachineInterface);
141 boolean floatingIpCreaterd = apiConnector.create(floatingIp);
142 if (!floatingIpCreaterd) {
143 LOGGER.warn("Floating Ip creation failed..");
144 return HttpURLConnection.HTTP_INTERNAL_ERROR;
146 LOGGER.info("Floating Ip : " + floatingIp.getName() + " having UUID : " + floatingIp.getUuid() + " sucessfully created...");
147 return HttpURLConnection.HTTP_OK;
148 } catch (IOException ioEx) {
149 LOGGER.error("Exception : " + ioEx);
150 return HttpURLConnection.HTTP_INTERNAL_ERROR;
155 * Invoked to take action after a floating ip has been created.
158 * An instance of proposed new Neutron Floating ip object.
160 * @return A HTTP status code to the creation request.
163 public void neutronFloatingIPCreated(NeutronFloatingIP neutronFloatingIp) {
164 FloatingIp floatingIp = null;
166 floatingIp = (FloatingIp) apiConnector.findById(FloatingIp.class, neutronFloatingIp.getFloatingIPUUID());
167 if (floatingIp != null) {
168 LOGGER.info("Floating Ip creation verified....");
170 } catch (Exception ex) {
171 LOGGER.error("Exception : " + ex);
177 * Invoked when a floating ip update is requested to indicate if the
178 * specified floating ip can be changed using the specified delta.
181 * Updates to the floating ip object using patch semantics.
183 * An instance of the Neutron floating ip object to be updated.
184 * @return A HTTP status code to the update request.
187 public int canUpdateFloatingIP(NeutronFloatingIP deltaFloatingIp, NeutronFloatingIP originalFloatingIp) {
188 apiConnector = Activator.apiConnector;
189 FloatingIp floatingIP = null;
190 apiConnector = Activator.apiConnector;
191 if (deltaFloatingIp == null || originalFloatingIp == null) {
192 LOGGER.error("Neutron Floating Ip can't be null..");
193 return HttpURLConnection.HTTP_BAD_REQUEST;
196 floatingIP = (FloatingIp) apiConnector.findById(FloatingIp.class, originalFloatingIp.getFloatingIPUUID());
197 } catch (IOException e) {
198 LOGGER.error("Exception : " + e);
199 return HttpURLConnection.HTTP_INTERNAL_ERROR;
201 if (floatingIP == null) {
202 LOGGER.error("No network exists for the specified UUID...");
203 return HttpURLConnection.HTTP_FORBIDDEN;
206 return updateFloatingIP(originalFloatingIp.getFloatingIPUUID(), deltaFloatingIp);
207 } catch (IOException ex) {
208 LOGGER.error("Exception : " + ex);
209 return HttpURLConnection.HTTP_INTERNAL_ERROR;
215 * Invoked to update the floating ip
218 * An instance of floating ip UUID.
219 * @param delta_floatingip
220 * An instance of delta floating ip.
222 * @return A boolean to the update request.
223 * @throws IOException
225 private int updateFloatingIP(String floatingIpUUID, NeutronFloatingIP deltaFloatingIp) throws IOException {
226 FloatingIp floatingIP = (FloatingIp) apiConnector.findById(FloatingIp.class, floatingIpUUID);
227 String virtualMachineInterfaceUUID = deltaFloatingIp.getPortUUID();
228 if (deltaFloatingIp.getPortUUID() != null) {
229 VirtualMachineInterface virtualMachineInterface = (VirtualMachineInterface) apiConnector.findById(VirtualMachineInterface.class,
230 virtualMachineInterfaceUUID);
231 if (virtualMachineInterface != null) {
232 floatingIP.setVirtualMachineInterface(virtualMachineInterface);
235 if (virtualMachineInterfaceUUID == null) {
236 floatingIP.clearVirtualMachineInterface();
238 boolean floatingIpUpdate = apiConnector.update(floatingIP);
239 if (!floatingIpUpdate) {
240 LOGGER.warn("Floating Ip Updation failed..");
241 return HttpURLConnection.HTTP_INTERNAL_ERROR;
243 LOGGER.info("Floating Ip having UUID : " + floatingIP.getUuid() + " has been sucessfully updated...");
244 return HttpURLConnection.HTTP_OK;
248 * Invoked to take action after a floating ip has been updated.
251 * An instance of modified Neutron floating ip object.
254 public void neutronFloatingIPUpdated(NeutronFloatingIP neutronFloatingIp) {
256 FloatingIp floatingIp = (FloatingIp) apiConnector.findById(FloatingIp.class, neutronFloatingIp.getFloatingIPUUID());
257 if (neutronFloatingIp.getPortUUID() != null) {
258 if (floatingIp.getVirtualMachineInterface().get(0).getUuid().matches(neutronFloatingIp.getPortUUID())) {
259 LOGGER.info("Floating Ip with floating UUID " + neutronFloatingIp.getFloatingIPUUID() + " is Updated successfully.");
261 LOGGER.info("Floating Ip Updation failed..");
263 } else if (neutronFloatingIp.getPortUUID() == null && floatingIp.getVirtualMachineInterface() == null) {
264 LOGGER.info("Floating Ip with floating UUID " + neutronFloatingIp.getFloatingIPUUID() + " is Updated successfully.");
266 LOGGER.info("Floating Ip Updation failed..");
268 } catch (Exception e) {
269 LOGGER.error("Exception :" + e);
274 * Invoked when a floating IP deletion is requested to indicate if the
275 * specified floating IP can be deleted.
277 * @param NeutronFloatingIP
278 * An instance of the Neutron {@link FloatingIp} object to be
280 * @return A HTTP status code to the deletion request.
283 public int canDeleteFloatingIP(NeutronFloatingIP neutronFloatingIp) {
284 apiConnector = Activator.apiConnector;
285 if (neutronFloatingIp == null) {
286 LOGGER.error("Neutron Floating Ip can not be null.. ");
287 return HttpURLConnection.HTTP_BAD_REQUEST;
290 return deleteFloatingIP(neutronFloatingIp.getFloatingIPUUID());
291 } catch (IOException ioEx) {
292 LOGGER.error("Exception : " + ioEx);
293 return HttpURLConnection.HTTP_INTERNAL_ERROR;
294 } catch (Exception ex) {
295 LOGGER.error("Exception : " + ex);
296 return HttpURLConnection.HTTP_INTERNAL_ERROR;
301 * Invoked to delete the specified Neutron floating ip.
304 * An instance of floating ip UUID.
306 * @return A boolean to the delete request.
307 * @throws IOException
309 private int deleteFloatingIP(String neutronFloatingIp) throws IOException {
310 FloatingIp floatingIp = (FloatingIp) apiConnector.findById(FloatingIp.class, neutronFloatingIp);
311 if (floatingIp != null) {
312 apiConnector.delete(floatingIp);
313 LOGGER.info("Floating Ip with UUID : " + floatingIp.getUuid() + " has been deleted successfully....");
314 return HttpURLConnection.HTTP_OK;
316 LOGGER.info("No Floating Ip exists with UUID : " + neutronFloatingIp);
317 return HttpURLConnection.HTTP_NOT_FOUND;
322 * Invoked to take action after a floatingIP has been deleted.
324 * @param NeutronfloatingIP
325 * An instance of deleted floatingIP Network object.
328 public void neutronFloatingIPDeleted(NeutronFloatingIP neutronFloatingIp) {
329 FloatingIp fip = null;
331 fip = (FloatingIp) apiConnector.findById(FloatingIp.class, neutronFloatingIp.getFloatingIPUUID());
333 LOGGER.info("Floating ip deletion verified....");
335 } catch (Exception e) {
336 LOGGER.error("Exception : " + e);
342 * Invoked to format the UUID if UUID is not in correct format.
345 * An instance of UUID string.
347 * @return Correctly formated UUID string.
349 private String uuidFormater(String uuid) {
350 String uuidPattern = null;
351 String id1 = uuid.substring(0, 8);
352 String id2 = uuid.substring(8, 12);
353 String id3 = uuid.substring(12, 16);
354 String id4 = uuid.substring(16, 20);
355 String id5 = uuid.substring(20, 32);
356 uuidPattern = (id1 + "-" + id2 + "-" + id3 + "-" + id4 + "-" + id5);