1b7454322022107082c9cc5d077188fb01e75244
[plugin2oc.git] / neutron / src / main / java / org / opendaylight / plugin2oc / neutron / FloatingIpHandler.java
1 /*
2  * Copyright (C) 2014 Juniper Networks, Inc.
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.plugin2oc.neutron;
10
11 import java.io.IOException;
12 import java.net.HttpURLConnection;
13 import java.util.UUID;
14
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;
21
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;
26
27 /**
28  * Handle requests for Neutron Floating IP.
29  */
30 public class FloatingIpHandler implements INeutronFloatingIPAware {
31     /**
32      * Logger instance.
33      */
34     static final Logger LOGGER = LoggerFactory.getLogger(PortHandler.class);
35     static ApiConnector apiConnector;
36
37     /**
38      * Invoked when a floating ip creation is requested to check if the
39      * specified floating ip can be created.
40      *
41      * @param floatingip
42      *            An instance of proposed new Neutron Floating ip object.
43      *
44      * @return A HTTP status code to the creation request.
45      */
46     @Override
47     public int canCreateFloatingIP(NeutronFloatingIP fip) {
48         apiConnector = Activator.apiConnector;
49         if (fip == null) {
50             LOGGER.error("Neutron Floating Ip can not be null ");
51             return HttpURLConnection.HTTP_BAD_REQUEST;
52         }
53         if (("").equals(fip.getFloatingIPUUID())) {
54             LOGGER.error("Floating Ip UUID can not be null ");
55             return HttpURLConnection.HTTP_BAD_REQUEST;
56         }
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;
60         }
61         if (fip.getFloatingIPAddress() == null) {
62             LOGGER.error(" Floating Ip address can not be null");
63             return HttpURLConnection.HTTP_BAD_REQUEST;
64         }
65         try {
66             return createfloatingIp(fip);
67         } catch (Exception e) {
68             e.printStackTrace();
69             LOGGER.error("Exception :   " + e);
70             return HttpURLConnection.HTTP_INTERNAL_ERROR;
71         }
72     }
73
74     /**
75      * Invoked when a floating ip creation is requested to create the floating
76      * ip
77      *
78      * @param floatingip
79      *            An instance of proposed new Neutron Floating ip object.
80      *
81      * @return A HTTP status code to the creation request.
82      */
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();
88         try {
89             floatingPoolNetworkId = neutronFloatingIp.getFloatingNetworkUUID();
90             projectUUID = neutronFloatingIp.getTenantUUID().toString();
91             if (!(projectUUID.contains("-"))) {
92                 projectUUID = uuidFormater(projectUUID);
93             }
94             projectUUID = UUID.fromString(projectUUID).toString();
95         } catch (Exception ex) {
96             LOGGER.error("UUID input incorrect", ex);
97             return HttpURLConnection.HTTP_INTERNAL_ERROR;
98         }
99         Project project;
100         try {
101             project = (Project) apiConnector.findById(Project.class, projectUUID);
102             if (project == null) {
103                 try {
104                     Thread.currentThread();
105                     Thread.sleep(3000);
106                 } catch (InterruptedException interruptedException) {
107                     LOGGER.error("InterruptedException :    ", interruptedException);
108                     return HttpURLConnection.HTTP_INTERNAL_ERROR;
109                 }
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;
114                 }
115             }
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;
120             }
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;
126             }
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);
139                 }
140             }
141             boolean floatingIpCreaterd = apiConnector.create(floatingIp);
142             if (!floatingIpCreaterd) {
143                 LOGGER.warn("Floating Ip creation failed..");
144                 return HttpURLConnection.HTTP_INTERNAL_ERROR;
145             }
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;
151         }
152     }
153
154     /**
155      * Invoked to take action after a floating ip has been created.
156      *
157      * @param floatingip
158      *            An instance of proposed new Neutron Floating ip object.
159      *
160      * @return A HTTP status code to the creation request.
161      */
162     @Override
163     public void neutronFloatingIPCreated(NeutronFloatingIP neutronFloatingIp) {
164         FloatingIp floatingIp = null;
165         try {
166             floatingIp = (FloatingIp) apiConnector.findById(FloatingIp.class, neutronFloatingIp.getFloatingIPUUID());
167             if (floatingIp != null) {
168                 LOGGER.info("Floating Ip creation verified....");
169             }
170         } catch (Exception ex) {
171             LOGGER.error("Exception :    " + ex);
172         }
173
174     }
175
176     /**
177      * Invoked when a floating ip update is requested to indicate if the
178      * specified floating ip can be changed using the specified delta.
179      *
180      * @param delta
181      *            Updates to the floating ip object using patch semantics.
182      * @param original
183      *            An instance of the Neutron floating ip object to be updated.
184      * @return A HTTP status code to the update request.
185      */
186     @Override
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;
194         }
195         try {
196             floatingIP = (FloatingIp) apiConnector.findById(FloatingIp.class, originalFloatingIp.getFloatingIPUUID());
197         } catch (IOException e) {
198             LOGGER.error("Exception :     " + e);
199             return HttpURLConnection.HTTP_INTERNAL_ERROR;
200         }
201         if (floatingIP == null) {
202             LOGGER.error("No network exists for the specified UUID...");
203             return HttpURLConnection.HTTP_FORBIDDEN;
204         }
205         try {
206             return updateFloatingIP(originalFloatingIp.getFloatingIPUUID(), deltaFloatingIp);
207         } catch (IOException ex) {
208             LOGGER.error("Exception : " + ex);
209             return HttpURLConnection.HTTP_INTERNAL_ERROR;
210         }
211
212     }
213
214     /**
215      * Invoked to update the floating ip
216      *
217      * @param string
218      *            An instance of floating ip UUID.
219      * @param delta_floatingip
220      *            An instance of delta floating ip.
221      *
222      * @return A boolean to the update request.
223      * @throws IOException
224      */
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);
233             }
234         }
235         if (virtualMachineInterfaceUUID == null) {
236             floatingIP.clearVirtualMachineInterface();
237         }
238         boolean floatingIpUpdate = apiConnector.update(floatingIP);
239         if (!floatingIpUpdate) {
240             LOGGER.warn("Floating Ip Updation failed..");
241             return HttpURLConnection.HTTP_INTERNAL_ERROR;
242         }
243         LOGGER.info("Floating Ip  having UUID : " + floatingIP.getUuid() + "  has been sucessfully updated...");
244         return HttpURLConnection.HTTP_OK;
245     }
246
247     /**
248      * Invoked to take action after a floating ip has been updated.
249      *
250      * @param floatingIp
251      *            An instance of modified Neutron floating ip object.
252      */
253     @Override
254     public void neutronFloatingIPUpdated(NeutronFloatingIP neutronFloatingIp) {
255         try {
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.");
260                 } else {
261                     LOGGER.info("Floating Ip Updation failed..");
262                 }
263             } else if (neutronFloatingIp.getPortUUID() == null && floatingIp.getVirtualMachineInterface() == null) {
264                 LOGGER.info("Floating Ip with floating UUID " + neutronFloatingIp.getFloatingIPUUID() + " is Updated successfully.");
265             } else {
266                 LOGGER.info("Floating Ip Updation failed..");
267             }
268         } catch (Exception e) {
269             LOGGER.error("Exception :" + e);
270         }
271     }
272
273     /**
274      * Invoked when a floating IP deletion is requested to indicate if the
275      * specified floating IP can be deleted.
276      *
277      * @param NeutronFloatingIP
278      *            An instance of the Neutron {@link FloatingIp} object to be
279      *            deleted.
280      * @return A HTTP status code to the deletion request.
281      */
282     @Override
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;
288         }
289         try {
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;
297         }
298     }
299
300     /**
301      * Invoked to delete the specified Neutron floating ip.
302      *
303      * @param String
304      *            An instance of floating ip UUID.
305      *
306      * @return A boolean to the delete request.
307      * @throws IOException
308      */
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;
315         } else {
316             LOGGER.info("No Floating Ip exists with UUID :  " + neutronFloatingIp);
317             return HttpURLConnection.HTTP_NOT_FOUND;
318         }
319     }
320
321     /**
322      * Invoked to take action after a floatingIP has been deleted.
323      *
324      * @param NeutronfloatingIP
325      *            An instance of deleted floatingIP Network object.
326      */
327     @Override
328     public void neutronFloatingIPDeleted(NeutronFloatingIP neutronFloatingIp) {
329         FloatingIp fip = null;
330         try {
331             fip = (FloatingIp) apiConnector.findById(FloatingIp.class, neutronFloatingIp.getFloatingIPUUID());
332             if (fip == null) {
333                 LOGGER.info("Floating ip deletion verified....");
334             }
335         } catch (Exception e) {
336             LOGGER.error("Exception :   " + e);
337         }
338
339     }
340
341     /**
342      * Invoked to format the UUID if UUID is not in correct format.
343      *
344      * @param String
345      *            An instance of UUID string.
346      *
347      * @return Correctly formated UUID string.
348      */
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);
357         return uuidPattern;
358     }
359
360 }