1 package org.openecomp.mso.apihandlerinfra;
7 * Copyright (C) 2016 OPENECOMP - MSO
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 import java.io.StringReader;
25 import java.io.StringWriter;
26 import java.util.LinkedList;
27 import java.util.List;
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;
39 import org.apache.http.HttpStatus;
41 import javax.xml.bind.Unmarshaller;
42 import javax.xml.transform.sax.SAXSource;
44 import org.xml.sax.InputSource;
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;
61 @Path("/{version: v1|v2|v3}/vnf-request")
62 public class VnfInfoHandler {
64 protected ObjectFactory beansObjectFactory = new ObjectFactory ();
66 private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH);
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);
93 response = queryGenericFilters (serviceType, aicNodeClli, tenantId, volumeGroupId, volumeGroupName, vnfName, version);
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 ());
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
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);
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 ());
117 protected MsoLogger getMsoLogger () {
121 protected String getRequestType () {
122 return VnfRequestType.VNF.toString ();
125 protected void fillVnfRequest (VnfRequest qr, InfraRequests ar, String version) {
126 VnfInputs vi = beansObjectFactory.createVnfInputs ();
128 if (ar.getVnfId () != null) {
129 vi.setVnfId (ar.getVnfId ());
131 if (ar.getVnfName () != null) {
132 vi.setVnfName (ar.getVnfName ());
134 if (ar.getVnfType () != null) {
135 vi.setVnfType (ar.getVnfType ());
137 if (ar.getTenantId () != null) {
138 vi.setTenantId (ar.getTenantId ());
140 if (ar.getProvStatus () != null) {
141 vi.setProvStatus (ar.getProvStatus ());
143 if (ar.getVolumeGroupName () != null) {
144 vi.setVolumeGroupName (ar.getVolumeGroupName ());
146 if (ar.getVolumeGroupId () != null) {
147 vi.setVolumeGroupId (ar.getVolumeGroupId ());
149 if (version.equals(Constants.SCHEMA_VERSION_V1)) {
150 if (ar.getServiceType () != null) {
151 vi.setServiceType (ar.getServiceType ());
153 if (ar.getAicNodeClli () != null) {
154 vi.setAicNodeClli (ar.getAicNodeClli ());
157 else if (version.equals(Constants.SCHEMA_VERSION_V2)) {
158 if (ar.getAaiServiceId () != null) {
159 vi.setServiceId (ar.getAaiServiceId ());
161 if (ar.getAicCloudRegion () != null) {
162 vi.setAicCloudRegion (ar.getAicCloudRegion ());
164 if (ar.getVfModuleName () != null) {
165 vi.setVfModuleName (ar.getVfModuleName ());
167 if (ar.getVfModuleId () != null) {
168 vi.setVfModuleId (ar.getVfModuleId ());
170 if (ar.getVfModuleModelName () != null) {
171 vi.setVfModuleModelName (ar.getVfModuleModelName ());
174 else if (version.equals(Constants.SCHEMA_VERSION_V3)) {
175 if (ar.getAaiServiceId () != null) {
176 vi.setServiceId (ar.getAaiServiceId ());
178 if (ar.getAicCloudRegion () != null) {
179 vi.setAicCloudRegion (ar.getAicCloudRegion ());
181 if (ar.getVfModuleName () != null) {
182 vi.setVfModuleName (ar.getVfModuleName ());
184 if (ar.getVfModuleId () != null) {
185 vi.setVfModuleId (ar.getVfModuleId ());
187 if (ar.getVfModuleModelName () != null) {
188 vi.setVfModuleModelName (ar.getVfModuleModelName ());
190 if (ar.getServiceInstanceId () != null) {
191 vi.setServiceInstanceId (ar.getServiceInstanceId ());
194 qr.setVnfInputs (vi);
196 qr.setVnfParams (ar.getVnfParams ());
199 String vnfoutputs = ar.getVnfOutputs ();
200 if (vnfoutputs != null && vnfoutputs.length () > 0) {
201 msoLogger.debug ("Read VNF outputs: " + vnfoutputs);
202 VnfOutputs vnfOutput = null;
204 // Now unmarshal it into vnf outputs
206 JAXBContext jaxbContext = JAXBContext.newInstance (VnfOutputs.class);
207 Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller ();
209 InputSource inputSource = new InputSource (new StringReader (vnfoutputs));
210 SAXSource source = new SAXSource (inputSource);
212 vnfOutput = jaxbUnmarshaller.unmarshal (source, VnfOutputs.class).getValue ();
214 } catch (Exception e) {
215 msoLogger.debug ("Validation failed", e);
216 throw new ValidationException ("format for vnf outputs");
219 qr.setVnfOutputs (vnfOutput);
221 } catch (Exception e) {
222 msoLogger.debug ("exception reading vnfOutputs Clob", e);
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);
230 if (aicNodeClli != null) {
231 return this.getRequestList ("aicNodeClli", aicNodeClli, version);
233 if (tenantId != null) {
234 return this.getRequestList ("tenantId", tenantId, version);
236 if (volumeGroupId != null) {
237 return this.getRequestList ("volumeGroupId", volumeGroupId, version);
239 if (volumeGroupName != null) {
240 return this.getRequestList ("volumeGroupName", volumeGroupName, version);
242 if (vnfName != null) {
243 return this.getRequestList ("vnfName", vnfName, version);
245 return Response.status (HttpStatus.SC_BAD_REQUEST).entity ("").build ();
248 protected Response getRequestGeneric (String requestId, String version) {
249 // Check INFRA_ACTIVE_REQUESTS table to find info
252 getMsoLogger ().debug ("getRequest: " + requestId);
254 String responseString = null;
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 ();
262 // Report that no request has been found
263 return Response.status (HttpStatus.SC_NOT_FOUND).entity ("").build ();
267 protected Response getRequestList (String queryAttribute, String queryValue, String version) {
268 // Check INFRA_ACTIVE_REQUESTS table to find info
271 getMsoLogger ().debug ("getRequest based on " + queryAttribute + ": " + queryValue);
273 List <InfraActiveRequests> activeReqList = RequestsDatabase.getRequestListFromInfraActive (queryAttribute,
277 List <VnfRequest> queryResponseList = new LinkedList <VnfRequest> ();
279 if (activeReqList != null) {
280 // build response for active
281 queryResponseList = infraRequestsResponses (activeReqList, version);
285 if (queryResponseList != null && !queryResponseList.isEmpty ()) {
286 String result = this.translateVnfRequests (queryResponseList);
287 return Response.status (HttpStatus.SC_OK).entity (result).build ();
290 // Report that no request has been found
291 return Response.status (HttpStatus.SC_NOT_FOUND).entity ("").build ();
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 ());
304 if (ar.getSource () != null) {
305 ri.setSource (ar.getSource ());
308 ri.setStartTime (ar.getStartTime ().toString ());
309 if (ar.getEndTime () != null) {
310 ri.setEndTime (ar.getEndTime ().toString ());
313 if (ar.getStatusMessage () != null) {
314 ri.setStatusMessage (ar.getStatusMessage ());
316 qr.setRequestInfo (ri);
320 private List <VnfRequest> infraRequestsResponses (List <? extends InfraRequests> arList, String version) {
321 List <VnfRequest> queryResponseList = new LinkedList <VnfRequest> ();
323 for (InfraRequests ar : arList) {
324 VnfRequest qr = fillGeneric (ar);
325 fillVnfRequest (qr, ar, version);
326 queryResponseList.add (qr);
328 return queryResponseList;
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));
337 StringWriter stringWriter = new StringWriter ();
339 JAXBContext jaxbContext = JAXBContext.newInstance (VnfRequests.class);
340 Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
342 // output pretty printed
343 jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
345 jaxbMarshaller.marshal (queryResponses, stringWriter);
347 } catch (JAXBException e) {
348 getMsoLogger ().debug ("Marshalling issue", e);
351 return stringWriter.toString ();
354 private String infraRequestsResponse (InfraRequests ar, String version) {
355 VnfRequest qr = fillGeneric (ar);
356 fillVnfRequest (qr, ar, version);
358 StringWriter stringWriter = new StringWriter ();
360 JAXBContext jaxbContext = JAXBContext.newInstance (VnfRequest.class);
361 Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
363 jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
364 jaxbMarshaller.marshal (qr, stringWriter);
366 } catch (JAXBException e) {
367 getMsoLogger ().debug ("Marshalling issue", e);
370 String response = stringWriter.toString ();