Merge "Fixed string literal comparision issue"
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / openecomp / mso / apihandlerinfra / VnfInfoHandler.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.vnfbeans.ActionType;
43 import org.openecomp.mso.apihandlerinfra.vnfbeans.ObjectFactory;
44 import org.openecomp.mso.apihandlerinfra.vnfbeans.RequestInfo;
45 import org.openecomp.mso.apihandlerinfra.vnfbeans.RequestStatusType;
46 import org.openecomp.mso.apihandlerinfra.vnfbeans.VnfInputs;
47 import org.openecomp.mso.apihandlerinfra.vnfbeans.VnfOutputs;
48 import org.openecomp.mso.apihandlerinfra.vnfbeans.VnfRequest;
49 import org.openecomp.mso.apihandlerinfra.vnfbeans.VnfRequests;
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}/vnf-request")
61 @Api(value="/{version: v1|v2|v3}/vnf-request",description="API Requests of vnfRequest")
62 public class VnfInfoHandler {
63
64     protected ObjectFactory beansObjectFactory = new ObjectFactory ();
65
66     private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH);
67
68
69     @GET
70     @ApiOperation(value="Finds Volume Requests",response=Response.class)
71     public Response queryFilters (@QueryParam("vnf-type") String vnfType,
72                                   @QueryParam("service-type") String serviceType,
73                                   @QueryParam("aic-node-clli") String aicNodeClli,
74                                   @QueryParam("tenant-id") String tenantId,
75                                   @QueryParam("volume-group-id") String volumeGroupId,
76                                   @QueryParam("volume-group-name") String volumeGroupName,
77                                   @QueryParam("vnf-name") String vnfName,
78                                   @PathParam("version") String version) {
79         long startTime = System.currentTimeMillis ();
80         MsoLogger.setServiceName ("VNFQueryFilters");
81         // Generate a Request Id
82         UUIDChecker.generateUUID(msoLogger);
83         msoLogger.debug ("Incoming request received for queryFilter with vnf-type:" + vnfType
84                                                                 + " service-type:" + serviceType
85                                                                 + " aic-node-clli:" + aicNodeClli
86                                                                 + " tenant-id:" + tenantId
87                                                                 + " volume-group-id:" + volumeGroupId
88                                                                 + " volume-group-name:" + volumeGroupName
89                                                                 + " vnf-name: " + vnfName);
90         Response response;
91         if (vnfType != null) {
92             response = this.getRequestList ("vnfType", vnfType, version);
93         } else {
94             response = queryGenericFilters (serviceType, aicNodeClli, tenantId, volumeGroupId, volumeGroupName, vnfName, version);
95         }
96         msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
97         msoLogger.debug ("End of the transaction, the final response is: " + (String) response.getEntity ());
98         return response;
99     }
100
101     @GET
102     @Path(Constants.REQUEST_ID_PATH)
103     @ApiOperation(value="Add a Vnf Outputs from requestId and version",response=Response.class)
104     public Response getRequest (@PathParam("request-id") String requestId, @PathParam("version") String version) {
105         // Check INFRA_ACTIVE_REQUESTS table to find info
106         // on this request
107         long startTime = System.currentTimeMillis ();
108         MsoLogger.setServiceName ("VNFGetRequest");
109         // Generate a Request Id
110         UUIDChecker.generateUUID(msoLogger);
111         msoLogger.debug ("Incoming request received for getRequest with request-id:" + requestId + ", version = " + version);
112
113         Response response = getRequestGeneric (requestId, version);
114         msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
115         msoLogger.debug ("End of the transaction, the final response is: " + (String) response.getEntity ());
116         return response;
117     }
118
119     protected MsoLogger getMsoLogger () {
120         return msoLogger;
121     }
122
123     protected String getRequestType () {
124         return VnfRequestType.VNF.toString ();
125     }
126
127     protected void fillVnfRequest (VnfRequest qr, InfraRequests ar, String version) {
128         VnfInputs vi = beansObjectFactory.createVnfInputs ();
129
130         if (ar.getVnfId () != null) {
131             vi.setVnfId (ar.getVnfId ());
132         }
133         if (ar.getVnfName () != null) {
134             vi.setVnfName (ar.getVnfName ());
135         }
136         if (ar.getVnfType () != null) {
137             vi.setVnfType (ar.getVnfType ());
138         }        
139         if (ar.getTenantId () != null) {
140             vi.setTenantId (ar.getTenantId ());
141         }
142         if (ar.getProvStatus () != null) {
143             vi.setProvStatus (ar.getProvStatus ());
144         }
145         if (ar.getVolumeGroupName () != null) {
146                 vi.setVolumeGroupName (ar.getVolumeGroupName ());
147         }
148         if (ar.getVolumeGroupId () != null) {
149                 vi.setVolumeGroupId (ar.getVolumeGroupId ());
150         }
151         if (version.equals(Constants.SCHEMA_VERSION_V1)) {
152                 if (ar.getServiceType () != null) {
153                         vi.setServiceType (ar.getServiceType ());
154                 }
155                 if (ar.getAicNodeClli () != null) {
156                         vi.setAicNodeClli (ar.getAicNodeClli ());
157                 }
158         }
159         else if (version.equals(Constants.SCHEMA_VERSION_V2)) {
160                 if (ar.getAaiServiceId () != null) {
161                         vi.setServiceId (ar.getAaiServiceId ());
162                 }
163                 if (ar.getAicCloudRegion () != null) {
164                         vi.setAicCloudRegion (ar.getAicCloudRegion ());
165                 }
166                 if (ar.getVfModuleName () != null) {
167                         vi.setVfModuleName (ar.getVfModuleName ());
168                 }
169                 if (ar.getVfModuleId () != null) {
170                         vi.setVfModuleId (ar.getVfModuleId ());
171                 }
172                 if (ar.getVfModuleModelName () != null) {
173                         vi.setVfModuleModelName (ar.getVfModuleModelName ());
174                 }               
175         }
176         else if (version.equals(Constants.SCHEMA_VERSION_V3)) {
177                 if (ar.getAaiServiceId () != null) {
178                         vi.setServiceId (ar.getAaiServiceId ());
179                 }
180                 if (ar.getAicCloudRegion () != null) {
181                         vi.setAicCloudRegion (ar.getAicCloudRegion ());
182                 }
183                 if (ar.getVfModuleName () != null) {
184                         vi.setVfModuleName (ar.getVfModuleName ());
185                 }
186                 if (ar.getVfModuleId () != null) {
187                         vi.setVfModuleId (ar.getVfModuleId ());
188                 }
189                 if (ar.getVfModuleModelName () != null) {
190                         vi.setVfModuleModelName (ar.getVfModuleModelName ());
191                 }
192                 if (ar.getServiceInstanceId () != null) {
193                         vi.setServiceInstanceId (ar.getServiceInstanceId ());
194                 }
195         }
196         qr.setVnfInputs (vi);
197
198         qr.setVnfParams (ar.getVnfParams ());
199
200         try {
201             String vnfoutputs = ar.getVnfOutputs ();
202             if (vnfoutputs != null && vnfoutputs.length () > 0) {
203                 msoLogger.debug ("Read VNF outputs: " + vnfoutputs);
204                 VnfOutputs vnfOutput = null;
205
206                 // Now unmarshal it into vnf outputs
207                 try {
208                     JAXBContext jaxbContext = JAXBContext.newInstance (VnfOutputs.class);
209                     Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller ();
210
211                     InputSource inputSource = new InputSource (new StringReader (vnfoutputs));
212                     SAXSource source = new SAXSource (inputSource);
213
214                     vnfOutput = jaxbUnmarshaller.unmarshal (source, VnfOutputs.class).getValue ();
215
216                 } catch (Exception e) {
217                     msoLogger.debug ("Validation failed", e);
218                     throw new ValidationException ("format for vnf outputs");
219                 }
220
221                 qr.setVnfOutputs (vnfOutput);
222             }
223         } catch (Exception e) {
224             msoLogger.debug ("exception reading vnfOutputs Clob", e);
225         }
226     }
227
228     protected Response queryGenericFilters (String serviceType, String aicNodeClli, String tenantId, String volumeGroupId, String volumeGroupName, String vnfName, String version) {
229         if (serviceType != null) {
230             return this.getRequestList ("serviceType", serviceType, version);
231         }
232         if (aicNodeClli != null) {
233             return this.getRequestList ("aicNodeClli", aicNodeClli, version);
234         }
235         if (tenantId != null) {
236             return this.getRequestList ("tenantId", tenantId, version);
237         }
238         if (volumeGroupId != null) {
239                 return this.getRequestList ("volumeGroupId", volumeGroupId, version);
240         }
241         if (volumeGroupName != null) {
242                 return this.getRequestList ("volumeGroupName", volumeGroupName, version);
243         }
244         if (vnfName != null) {
245                 return this.getRequestList ("vnfName", vnfName, version);
246         }
247         return Response.status (HttpStatus.SC_BAD_REQUEST).entity ("").build ();
248     }
249
250     protected Response getRequestGeneric (String requestId, String version) {
251         // Check INFRA_ACTIVE_REQUESTS table to find info
252         // on this request
253
254         getMsoLogger ().debug ("getRequest: " + requestId);
255
256         String responseString;
257
258         InfraActiveRequests activeReq = (RequestsDatabase.getInstance()).getRequestFromInfraActive (requestId, getRequestType ());
259         if (activeReq != null) {
260             // build response for active
261             responseString = infraRequestsResponse (activeReq, version);
262             return Response.status (HttpStatus.SC_OK).entity (responseString).build ();
263         } else {
264             // Report that no request has been found
265             return Response.status (HttpStatus.SC_NOT_FOUND).entity ("").build ();
266         }
267     }
268
269     protected Response getRequestList (String queryAttribute, String queryValue, String version) {
270         // Check INFRA_ACTIVE_REQUESTS table to find info
271         // on this request
272
273         getMsoLogger ().debug ("getRequest based on " + queryAttribute + ": " + queryValue);
274
275         List <InfraActiveRequests> activeReqList = (RequestsDatabase.getInstance()).getRequestListFromInfraActive (queryAttribute,
276                                                                                                    queryValue,
277                                                                                                    getRequestType ());
278
279         List <VnfRequest> queryResponseList = new LinkedList <> ();
280
281         if (activeReqList != null) {
282             // build response for active
283             queryResponseList = infraRequestsResponses (activeReqList, version);
284
285         }
286
287         if (queryResponseList != null && !queryResponseList.isEmpty ()) {
288             String result = this.translateVnfRequests (queryResponseList);
289             return Response.status (HttpStatus.SC_OK).entity (result).build ();
290
291         } else {
292             // Report that no request has been found
293             return Response.status (HttpStatus.SC_NOT_FOUND).entity ("").build ();
294         }
295     }
296
297     private VnfRequest fillGeneric (InfraRequests ar) {
298         VnfRequest qr = beansObjectFactory.createVnfRequest ();
299         RequestInfo ri = beansObjectFactory.createRequestInfo ();
300         ri.setRequestId (ar.getRequestId ());
301         ri.setAction (ActionType.fromValue (ar.getAction ()));
302         ri.setRequestStatus (RequestStatusType.fromValue (ar.getRequestStatus ()));
303         if (ar.getProgress () != null) {
304             ri.setProgress (ar.getProgress ().intValue ());
305         }
306         if (ar.getSource () != null) {
307             ri.setSource (ar.getSource ());
308         }
309
310         ri.setStartTime (ar.getStartTime ().toString ());
311         if (ar.getEndTime () != null) {
312             ri.setEndTime (ar.getEndTime ().toString ());
313         }
314
315         if (ar.getStatusMessage () != null) {
316             ri.setStatusMessage (ar.getStatusMessage ());
317         }
318         qr.setRequestInfo (ri);
319         return qr;
320     }
321
322     private List <VnfRequest> infraRequestsResponses (List <? extends InfraRequests> arList, String version) {
323         List <VnfRequest> queryResponseList = new LinkedList <> ();
324
325         for (InfraRequests ar : arList) {
326             VnfRequest qr = fillGeneric (ar);
327             fillVnfRequest (qr, ar, version);
328             queryResponseList.add (qr);
329         }
330         return queryResponseList;
331     }
332
333     private String translateVnfRequests (List <VnfRequest> queryResponseList) {
334         VnfRequests queryResponses = new VnfRequests ();
335         for (int i = 0; i < queryResponseList.size (); i++) {
336             queryResponses.getVnfRequest ().add (queryResponseList.get (i));
337         }
338
339         StringWriter stringWriter = new StringWriter ();
340         try {
341             JAXBContext jaxbContext = JAXBContext.newInstance (VnfRequests.class);
342             Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
343
344             // output pretty printed
345             jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
346
347             jaxbMarshaller.marshal (queryResponses, stringWriter);
348
349         } catch (JAXBException e) {
350             getMsoLogger ().debug ("Marshalling issue", e);
351         }
352
353         return stringWriter.toString ();
354     }
355
356     private String infraRequestsResponse (InfraRequests ar, String version) {
357         VnfRequest qr = fillGeneric (ar);
358         fillVnfRequest (qr, ar, version);
359
360         StringWriter stringWriter = new StringWriter ();
361         try {
362             JAXBContext jaxbContext = JAXBContext.newInstance (VnfRequest.class);
363             Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
364
365             jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
366             jaxbMarshaller.marshal (qr, stringWriter);
367
368         } catch (JAXBException e) {
369             getMsoLogger ().debug ("Marshalling issue", e);
370         }
371
372         String response = stringWriter.toString ();
373         return response;
374     }
375 }