d13212a48301222ec2d313c6bbb3b2eb1b30cdb7
[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         switch (version) {
152             case Constants.SCHEMA_VERSION_V1:
153                 if (ar.getServiceType() != null) {
154                     vi.setServiceType(ar.getServiceType());
155                 }
156                 if (ar.getAicNodeClli() != null) {
157                     vi.setAicNodeClli(ar.getAicNodeClli());
158                 }
159                 break;
160             case Constants.SCHEMA_VERSION_V2:
161                 if (ar.getAaiServiceId() != null) {
162                     vi.setServiceId(ar.getAaiServiceId());
163                 }
164                 if (ar.getAicCloudRegion() != null) {
165                     vi.setAicCloudRegion(ar.getAicCloudRegion());
166                 }
167                 if (ar.getVfModuleName() != null) {
168                     vi.setVfModuleName(ar.getVfModuleName());
169                 }
170                 if (ar.getVfModuleId() != null) {
171                     vi.setVfModuleId(ar.getVfModuleId());
172                 }
173                 if (ar.getVfModuleModelName() != null) {
174                     vi.setVfModuleModelName(ar.getVfModuleModelName());
175                 }
176                 break;
177             case Constants.SCHEMA_VERSION_V3:
178                 if (ar.getAaiServiceId() != null) {
179                     vi.setServiceId(ar.getAaiServiceId());
180                 }
181                 if (ar.getAicCloudRegion() != null) {
182                     vi.setAicCloudRegion(ar.getAicCloudRegion());
183                 }
184                 if (ar.getVfModuleName() != null) {
185                     vi.setVfModuleName(ar.getVfModuleName());
186                 }
187                 if (ar.getVfModuleId() != null) {
188                     vi.setVfModuleId(ar.getVfModuleId());
189                 }
190                 if (ar.getVfModuleModelName() != null) {
191                     vi.setVfModuleModelName(ar.getVfModuleModelName());
192                 }
193                 if (ar.getServiceInstanceId() != null) {
194                     vi.setServiceInstanceId(ar.getServiceInstanceId());
195                 }
196                 break;
197         }
198         qr.setVnfInputs (vi);
199
200         qr.setVnfParams (ar.getVnfParams ());
201
202         try {
203             String vnfoutputs = ar.getVnfOutputs ();
204             if (vnfoutputs != null && vnfoutputs.length () > 0) {
205                 msoLogger.debug ("Read VNF outputs: " + vnfoutputs);
206                 VnfOutputs vnfOutput = null;
207
208                 // Now unmarshal it into vnf outputs
209                 try {
210                     JAXBContext jaxbContext = JAXBContext.newInstance (VnfOutputs.class);
211                     Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller ();
212
213                     InputSource inputSource = new InputSource (new StringReader (vnfoutputs));
214                     SAXSource source = new SAXSource (inputSource);
215
216                     vnfOutput = jaxbUnmarshaller.unmarshal (source, VnfOutputs.class).getValue ();
217
218                 } catch (Exception e) {
219                     msoLogger.debug ("Validation failed", e);
220                     throw new ValidationException ("format for vnf outputs");
221                 }
222
223                 qr.setVnfOutputs (vnfOutput);
224             }
225         } catch (Exception e) {
226             msoLogger.debug ("exception reading vnfOutputs Clob", e);
227         }
228     }
229
230     protected Response queryGenericFilters (String serviceType, String aicNodeClli, String tenantId, String volumeGroupId, String volumeGroupName, String vnfName, String version) {
231         if (serviceType != null) {
232             return this.getRequestList ("serviceType", serviceType, version);
233         }
234         if (aicNodeClli != null) {
235             return this.getRequestList ("aicNodeClli", aicNodeClli, version);
236         }
237         if (tenantId != null) {
238             return this.getRequestList ("tenantId", tenantId, version);
239         }
240         if (volumeGroupId != null) {
241                 return this.getRequestList ("volumeGroupId", volumeGroupId, version);
242         }
243         if (volumeGroupName != null) {
244                 return this.getRequestList ("volumeGroupName", volumeGroupName, version);
245         }
246         if (vnfName != null) {
247                 return this.getRequestList ("vnfName", vnfName, version);
248         }
249         return Response.status (HttpStatus.SC_BAD_REQUEST).entity ("").build ();
250     }
251
252     protected Response getRequestGeneric (String requestId, String version) {
253         // Check INFRA_ACTIVE_REQUESTS table to find info
254         // on this request
255
256         getMsoLogger ().debug ("getRequest: " + requestId);
257
258         String responseString;
259
260         InfraActiveRequests activeReq = (RequestsDatabase.getInstance()).getRequestFromInfraActive (requestId, getRequestType ());
261         if (activeReq != null) {
262             // build response for active
263             responseString = infraRequestsResponse (activeReq, version);
264             return Response.status (HttpStatus.SC_OK).entity (responseString).build ();
265         } else {
266             // Report that no request has been found
267             return Response.status (HttpStatus.SC_NOT_FOUND).entity ("").build ();
268         }
269     }
270
271     protected Response getRequestList (String queryAttribute, String queryValue, String version) {
272         // Check INFRA_ACTIVE_REQUESTS table to find info
273         // on this request
274
275         getMsoLogger ().debug ("getRequest based on " + queryAttribute + ": " + queryValue);
276
277         List <InfraActiveRequests> activeReqList = (RequestsDatabase.getInstance()).getRequestListFromInfraActive (queryAttribute,
278                                                                                                    queryValue,
279                                                                                                    getRequestType ());
280
281         List <VnfRequest> queryResponseList = new LinkedList <> ();
282
283         if (activeReqList != null) {
284             // build response for active
285             queryResponseList = infraRequestsResponses (activeReqList, version);
286
287         }
288
289         if (queryResponseList != null && !queryResponseList.isEmpty ()) {
290             String result = this.translateVnfRequests (queryResponseList);
291             return Response.status (HttpStatus.SC_OK).entity (result).build ();
292
293         } else {
294             // Report that no request has been found
295             return Response.status (HttpStatus.SC_NOT_FOUND).entity ("").build ();
296         }
297     }
298
299     private VnfRequest fillGeneric (InfraRequests ar) {
300         VnfRequest qr = beansObjectFactory.createVnfRequest ();
301         RequestInfo ri = beansObjectFactory.createRequestInfo ();
302         ri.setRequestId (ar.getRequestId ());
303         ri.setAction (ActionType.fromValue (ar.getAction ()));
304         ri.setRequestStatus (RequestStatusType.fromValue (ar.getRequestStatus ()));
305         if (ar.getProgress () != null) {
306             ri.setProgress (ar.getProgress ().intValue ());
307         }
308         if (ar.getSource () != null) {
309             ri.setSource (ar.getSource ());
310         }
311
312         ri.setStartTime (ar.getStartTime ().toString ());
313         if (ar.getEndTime () != null) {
314             ri.setEndTime (ar.getEndTime ().toString ());
315         }
316
317         if (ar.getStatusMessage () != null) {
318             ri.setStatusMessage (ar.getStatusMessage ());
319         }
320         qr.setRequestInfo (ri);
321         return qr;
322     }
323
324     private List <VnfRequest> infraRequestsResponses (List <? extends InfraRequests> arList, String version) {
325         List <VnfRequest> queryResponseList = new LinkedList <> ();
326
327         for (InfraRequests ar : arList) {
328             VnfRequest qr = fillGeneric (ar);
329             fillVnfRequest (qr, ar, version);
330             queryResponseList.add (qr);
331         }
332         return queryResponseList;
333     }
334
335     private String translateVnfRequests (List <VnfRequest> queryResponseList) {
336         VnfRequests queryResponses = new VnfRequests ();
337         for (VnfRequest aQueryResponseList : queryResponseList) {
338             queryResponses.getVnfRequest().add(aQueryResponseList);
339         }
340
341         StringWriter stringWriter = new StringWriter ();
342         try {
343             JAXBContext jaxbContext = JAXBContext.newInstance (VnfRequests.class);
344             Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
345
346             // output pretty printed
347             jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
348
349             jaxbMarshaller.marshal (queryResponses, stringWriter);
350
351         } catch (JAXBException e) {
352             getMsoLogger ().debug ("Marshalling issue", e);
353         }
354
355         return stringWriter.toString ();
356     }
357
358     private String infraRequestsResponse (InfraRequests ar, String version) {
359         VnfRequest qr = fillGeneric (ar);
360         fillVnfRequest (qr, ar, version);
361
362         StringWriter stringWriter = new StringWriter ();
363         try {
364             JAXBContext jaxbContext = JAXBContext.newInstance (VnfRequest.class);
365             Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
366
367             jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
368             jaxbMarshaller.marshal (qr, stringWriter);
369
370         } catch (JAXBException e) {
371             getMsoLogger ().debug ("Marshalling issue", e);
372         }
373
374         String response = stringWriter.toString ();
375         return response;
376     }
377 }