1710 Rebase - Second Attempt
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / openecomp / mso / apihandlerinfra / VolumeInfoHandler.java
1 package org.openecomp.mso.apihandlerinfra;
2
3 /*-
4  * #%L
5  * MSO
6  * %%
7  * Copyright (C) 2016 OPENECOMP - MSO
8  * %%
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * #L%
21  */
22
23
24 import java.io.StringReader;
25 import java.io.StringWriter;
26 import java.util.LinkedList;
27 import java.util.List;
28
29 import javax.ws.rs.GET;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.PathParam;
32 import javax.ws.rs.QueryParam;
33 import javax.ws.rs.core.Response;
34 import javax.xml.bind.JAXBContext;
35 import javax.xml.bind.JAXBException;
36 import javax.xml.bind.Marshaller;
37 import javax.xml.bind.Unmarshaller;
38 import javax.xml.transform.sax.SAXSource;
39
40 import org.apache.http.HttpStatus;
41 import org.openecomp.mso.apihandler.common.ValidationException;
42 import org.openecomp.mso.apihandlerinfra.volumebeans.ActionType;
43 import org.openecomp.mso.apihandlerinfra.volumebeans.ObjectFactory;
44 import org.openecomp.mso.apihandlerinfra.volumebeans.RequestInfo;
45 import org.openecomp.mso.apihandlerinfra.volumebeans.RequestStatusType;
46 import org.openecomp.mso.apihandlerinfra.volumebeans.VolumeInputs;
47 import org.openecomp.mso.apihandlerinfra.volumebeans.VolumeOutputs;
48 import org.openecomp.mso.apihandlerinfra.volumebeans.VolumeRequest;
49 import org.openecomp.mso.apihandlerinfra.volumebeans.VolumeRequests;
50 import org.openecomp.mso.logger.MsoLogger;
51 import org.openecomp.mso.requestsdb.InfraActiveRequests;
52 import org.openecomp.mso.requestsdb.InfraRequests;
53 import org.openecomp.mso.requestsdb.RequestsDatabase;
54 import org.openecomp.mso.utils.UUIDChecker;
55 import org.xml.sax.InputSource;
56
57 import com.wordnik.swagger.annotations.Api;
58 import com.wordnik.swagger.annotations.ApiOperation;
59
60 @Path("/{version: v1|v2|v3}/volume-request")
61 @Api(value="/{version: v1|v2|v3}/volume-request",description="API Requests for volumeRequest")
62 public class VolumeInfoHandler {
63         
64         protected ObjectFactory beansObjectFactory = new ObjectFactory ();
65
66         private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH);
67
68     @GET
69     @ApiOperation(value="Finds Volume Requests",response=Response.class)
70     public Response queryFilters (@QueryParam("vnf-type") String vnfType,
71                                   @QueryParam("service-type") String serviceType,
72                                   @QueryParam("aic-node-clli") String aicNodeClli,
73                                   @QueryParam("tenantId") String tenantId,
74                                   @QueryParam("volume-group-id") String volumeGroupId,
75                                   @QueryParam("volume-group-name") String volumeGroupName,
76                                   @PathParam("version") String version) {
77         long startTime = System.currentTimeMillis ();
78         MsoLogger.setServiceName ("VolumeQueryFilters");
79         // Generate a Request Id
80         UUIDChecker.generateUUID(msoLogger);
81         msoLogger.debug ("Incoming request received for queryFilter with vnf-type:" + vnfType
82                                                                 + " service-type:" + serviceType
83                                                                 + " aic-node-clli:" + aicNodeClli
84                                                                 + " tenant-id:" + tenantId
85                                                                 + " volume-group-id:" + volumeGroupId
86                                                                 + " volume-group-name:" + volumeGroupName);
87         Response response = null;
88         if (vnfType != null) {
89             response = this.getRequestList ("vnfType", vnfType, version);
90         } else {
91                 response = queryGenericFilters (serviceType, aicNodeClli, tenantId, volumeGroupId, volumeGroupName, version);
92         }
93         msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
94         msoLogger.debug ("End of the transaction, the final response is: " + (String) response.getEntity ());
95         return response;
96     }
97
98     @GET
99     @Path(Constants.REQUEST_ID_PATH)
100     @ApiOperation(value="Find Volume Outputs by requestId and verison",response=Response.class)
101     public Response getRequest (@PathParam("request-id") String requestId, @PathParam("version") String version) {
102         
103         // Check INFRA_ACTIVE_REQUESTS table to find info
104         // on this request
105         
106         long startTime = System.currentTimeMillis ();
107         MsoLogger.setServiceName ("VolumeGetRequest");
108         // Generate a Request Id
109         UUIDChecker.generateUUID(msoLogger);
110         msoLogger.debug ("Incoming request received for getRequest with request-id:" + requestId);
111
112         Response response = getRequestGeneric (requestId, version);
113         msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
114         msoLogger.debug ("End of the transaction, the final response is: " + (String) response.getEntity ());
115         return response;
116
117     }
118
119    
120     protected MsoLogger getMsoLogger () {
121         return msoLogger;
122     }
123
124    
125      protected void fillVolumeRequest (VolumeRequest qr, InfraRequests ar, String version) {
126         VolumeInputs vi = beansObjectFactory.createVolumeInputs ();
127
128         if (ar.getVolumeGroupId () != null) {
129             vi.setVolumeGroupId (ar.getVolumeGroupId ());
130         }
131         if (ar.getVolumeGroupName () != null) {
132             vi.setVolumeGroupName (ar.getVolumeGroupName ());
133         }
134         if (ar.getVnfType () != null) {
135             vi.setVnfType (ar.getVnfType ());
136         }
137         
138         if (version.equals(Constants.SCHEMA_VERSION_V1)) {
139                 if (ar.getServiceType () != null) {
140                         vi.setServiceType (ar.getServiceType ());
141                 }
142                 if (ar.getAicNodeClli () != null) {
143                         vi.setAicNodeClli (ar.getAicNodeClli ());
144                 }
145         }
146         else if (version.equals(Constants.SCHEMA_VERSION_V2)) {
147                 if (ar.getAaiServiceId () != null) {
148                         vi.setServiceId (ar.getAaiServiceId ());
149                 }
150                 if (ar.getAicCloudRegion () != null) {
151                         vi.setAicCloudRegion (ar.getAicCloudRegion ());
152                 }               
153                 if (ar.getVfModuleModelName () != null) {
154                         vi.setVfModuleModelName (ar.getVfModuleModelName ());
155                 }               
156         }
157         else if (version.equals(Constants.SCHEMA_VERSION_V3)) {
158                 if (ar.getAaiServiceId () != null) {
159                         vi.setServiceId (ar.getAaiServiceId ());
160                 }
161                 if (ar.getAicCloudRegion () != null) {
162                         vi.setAicCloudRegion (ar.getAicCloudRegion ());
163                 }               
164                 if (ar.getVfModuleModelName () != null) {
165                         vi.setVfModuleModelName (ar.getVfModuleModelName ());
166                 }
167                 if (ar.getServiceInstanceId () != null) {
168                         vi.setServiceInstanceId (ar.getServiceInstanceId ());
169                 }
170                 if (ar.getVnfId () != null) {
171                 vi.setVnfId (ar.getVnfId ());
172             }
173         }
174         if (ar.getTenantId () != null) {
175             vi.setTenantId (ar.getTenantId ());
176         }
177         
178         qr.setVolumeInputs (vi);
179         
180         qr.setVolumeParams(ar.getVnfParams ());
181
182         try {
183             String volumeoutputs = ar.getVnfOutputs ();
184             if (volumeoutputs != null && volumeoutputs.length () > 0) {
185                 msoLogger.debug ("Read VOLUME outputs: " + volumeoutputs);
186                 VolumeOutputs volumeOutput = null;
187
188                 // Now unmarshal it into network outputs
189                 try {
190                     JAXBContext jaxbContext = JAXBContext.newInstance (VolumeOutputs.class);
191                     Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller ();
192
193                     InputSource inputSource = new InputSource (new StringReader (volumeoutputs));
194                     SAXSource source = new SAXSource (inputSource);
195
196                     volumeOutput = (VolumeOutputs) jaxbUnmarshaller.unmarshal (source, VolumeOutputs.class).getValue ();
197
198                 } catch (Exception e) {
199                     msoLogger.debug ("Validation failed", e);
200                     throw new ValidationException ("format for volume outputs");
201                 }
202
203                 qr.setVolumeOutputs (volumeOutput);
204             }
205         } catch (Exception e) {
206             msoLogger.debug ("exception reading networkOutputs Clob", e);
207         }
208     }
209     
210     protected Response queryGenericFilters (String serviceType, String aicNodeClli, String tenantId, String volumeGroupId, String volumeGroupName, String version) {
211         if (serviceType != null) {
212             return this.getRequestList ("serviceType", serviceType, version);
213         }
214         if (aicNodeClli != null) {
215             return this.getRequestList ("aicNodeClli", aicNodeClli, version);
216         }
217         if (tenantId != null) {
218             return this.getRequestList ("tenantId", tenantId, version);
219         }
220         if (volumeGroupName != null) {
221                 return this.getRequestList ("volumeGroupName", volumeGroupName, version);
222         }
223         if (volumeGroupId != null) {
224                 return this.getRequestList ("volumeGroupId", volumeGroupId, version);
225         }
226         return Response.status (HttpStatus.SC_BAD_REQUEST).entity ("").build ();
227     }
228
229
230     protected Response getRequestGeneric (String requestId, String version) {
231         // Check INFRA_ACTIVE_REQUESTS table  to find info
232         // on this request
233
234         getMsoLogger ().debug ("getRequest: " + requestId);
235
236         String responseString = null;
237
238         InfraActiveRequests activeReq = (RequestsDatabase.getInstance()).getRequestFromInfraActive (requestId,
239                                                                                     "VOLUME");
240         if (activeReq != null) {
241             // build response for active
242             responseString = infraRequestsResponse (activeReq, version);
243             return Response.status (HttpStatus.SC_OK).entity (responseString).build ();
244         } else {
245             // Report that no request has been found
246             return Response.status (HttpStatus.SC_NOT_FOUND).entity ("").build ();
247          }
248     }
249
250     protected Response getRequestList (String queryAttribute, String queryValue, String version) {
251         // Check INFRA_ACTIVE_REQUESTS table to find info
252         // on this request
253
254         getMsoLogger ().debug ("getRequest based on " + queryAttribute + ": " + queryValue);
255
256         List <InfraActiveRequests> activeReqList = (RequestsDatabase.getInstance()).getRequestListFromInfraActive (queryAttribute,
257                                                                                                    queryValue,
258                                                                                                    "VOLUME");
259   
260         List <VolumeRequest> queryResponseList = new LinkedList <VolumeRequest> ();
261
262         if (activeReqList != null) {
263             // build response for active
264             queryResponseList = infraRequestsResponses (activeReqList, version);
265
266         }
267
268  
269         if (queryResponseList != null && !queryResponseList.isEmpty ()) {
270             String result = this.translateVolumeRequests (queryResponseList);
271             return Response.status (HttpStatus.SC_OK).entity (result).build ();
272
273         } else {
274             // Report that no request has been found
275             return Response.status (HttpStatus.SC_NOT_FOUND).entity ("").build ();
276         }
277     }
278
279     private VolumeRequest fillGeneric (InfraRequests ar) {
280         VolumeRequest qr = beansObjectFactory.createVolumeRequest ();
281         RequestInfo ri = beansObjectFactory.createRequestInfo ();
282         ri.setRequestId (ar.getRequestId ());
283         ri.setAction (ActionType.fromValue (ar.getAction ()));
284         ri.setRequestStatus (RequestStatusType.fromValue (ar.getRequestStatus ()));
285         if (ar.getProgress () != null) {
286             ri.setProgress (ar.getProgress ().intValue ());
287         }
288         if (ar.getSource () != null) {
289             ri.setSource (ar.getSource ());
290         }
291
292         ri.setStartTime (ar.getStartTime ().toString ());
293         if (ar.getEndTime () != null) {
294             ri.setEndTime (ar.getEndTime ().toString ());
295         }
296         
297         if (ar.getStatusMessage () != null) {
298                 ri.setStatusMessage (ar.getStatusMessage ());
299         }
300         qr.setRequestInfo (ri);
301         return qr;
302     }
303
304     private List <VolumeRequest> infraRequestsResponses (List <? extends InfraRequests> arList, String version) {
305         List <VolumeRequest> queryResponseList = new LinkedList <VolumeRequest> ();
306
307         for (InfraRequests ar : arList) {
308             VolumeRequest qr = fillGeneric (ar);
309             fillVolumeRequest (qr, ar, version);
310             queryResponseList.add (qr);
311         }
312         return queryResponseList;
313     }
314
315     private String translateVolumeRequests (List <VolumeRequest> queryResponseList) {
316         VolumeRequests queryResponses = new VolumeRequests ();
317         for (int i = 0; i < queryResponseList.size (); i++) {
318             queryResponses.getVolumeRequest ().add (queryResponseList.get (i));
319         }
320
321         StringWriter stringWriter = new StringWriter ();
322         try {
323             JAXBContext jaxbContext = JAXBContext.newInstance (VolumeRequests.class);
324             Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
325
326             // output pretty printed
327             jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
328
329             jaxbMarshaller.marshal (queryResponses, stringWriter);
330
331         } catch (JAXBException e) {
332             getMsoLogger ().debug ("Marshalling issue", e);
333         }
334
335         return stringWriter.toString ();
336     }
337
338     private String infraRequestsResponse (InfraRequests ar, String version) {
339         VolumeRequest qr = fillGeneric (ar);
340         fillVolumeRequest (qr, ar, version);
341
342         StringWriter stringWriter = new StringWriter ();
343         try {
344             JAXBContext jaxbContext = JAXBContext.newInstance (VolumeRequest.class);
345             Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
346
347             jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
348             jaxbMarshaller.marshal (qr, stringWriter);
349
350         } catch (JAXBException e) {
351             getMsoLogger ().debug ("Marshalling issue", e);
352         }
353
354         String response = stringWriter.toString ();
355         return response;
356     }
357
358     private String getAuditLogReturnMsg (Response response) {
359         String returnMsg = "";
360         if (response.getStatus() == HttpStatus.SC_OK) {
361                 returnMsg = "Successful. StatusCode=" + HttpStatus.SC_OK;
362         } else if (response.getStatus() == HttpStatus.SC_NOT_FOUND) {
363                 returnMsg = "Record not found . StatusCode=" + HttpStatus.SC_NOT_FOUND;
364         } else if (response.getStatus() == HttpStatus.SC_BAD_REQUEST) {
365                 returnMsg = "Bad request: one of the following attribute serviceType, aicNodeClli, tenantId, volumeGroupId, volumeGroupName should be defined. StatusCode=" + HttpStatus.SC_BAD_REQUEST;
366         }
367         return returnMsg;
368     }
369 }