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