2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.apihandlerinfra;
24 import java.io.StringReader;
25 import java.io.StringWriter;
26 import java.sql.Timestamp;
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
29 import java.util.List;
31 import javax.ws.rs.core.Response;
32 import javax.xml.bind.JAXBContext;
33 import javax.xml.bind.JAXBException;
34 import javax.xml.bind.Marshaller;
35 import javax.xml.bind.Unmarshaller;
36 import javax.xml.transform.OutputKeys;
37 import javax.xml.transform.Transformer;
38 import javax.xml.transform.TransformerFactory;
39 import javax.xml.transform.dom.DOMSource;
40 import javax.xml.transform.sax.SAXSource;
41 import javax.xml.transform.stream.StreamResult;
43 import org.hibernate.Session;
45 import org.w3c.dom.Document;
46 import org.w3c.dom.Element;
47 import org.w3c.dom.Node;
48 import org.w3c.dom.NodeList;
49 import org.xml.sax.InputSource;
51 import org.openecomp.mso.apihandler.common.ErrorNumbers;
52 import org.openecomp.mso.apihandler.common.ValidationException;
53 import org.openecomp.mso.apihandlerinfra.vnfbeans.ActionType;
54 import org.openecomp.mso.apihandlerinfra.vnfbeans.ObjectFactory;
55 import org.openecomp.mso.apihandlerinfra.vnfbeans.RequestInfo;
56 import org.openecomp.mso.apihandlerinfra.vnfbeans.RequestStatusType;
57 import org.openecomp.mso.apihandlerinfra.vnfbeans.VnfInputs;
58 import org.openecomp.mso.apihandlerinfra.vnfbeans.VnfRequest;
59 import org.openecomp.mso.logger.MsoLogger;
60 import org.openecomp.mso.logger.MessageEnum;
61 import org.openecomp.mso.properties.MsoJavaProperties;
62 import org.openecomp.mso.requestsdb.HibernateUtil;
63 import org.openecomp.mso.requestsdb.InfraActiveRequests;
64 import org.openecomp.mso.requestsdb.RequestsDatabase;
66 public class VnfMsoInfraRequest {
68 private String requestId;
69 private String requestXML;
70 private String requestUri;
71 private VnfRequest vnfReq;
72 private RequestInfo rinfo;
73 private VnfInputs vnfInputs;
74 private String vnfParams;
75 private ActionType action;
76 private String errorMessage;
77 private String httpResponse;
78 private String responseBody;
79 private RequestStatusType status;
80 private long startTime;
81 private long progress = Constants.PROGRESS_REQUEST_RECEIVED;
83 private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH);
84 private static final String NOT_PROVIDED = "not provided";
86 VnfMsoInfraRequest (String requestId) {
87 this.requestId = requestId;
88 this.startTime = System.currentTimeMillis();
89 MsoLogger.setLogContext (requestId, null);
94 void parse (String reqXML, String version, MsoJavaProperties props) throws ValidationException {
96 msoLogger.debug ("Validating the request");
98 this.requestXML = reqXML;
100 VnfRequest vnfReq = null;
101 boolean isWrongRootElement = false;
104 JAXBContext jaxbContext = JAXBContext.newInstance (VnfRequest.class);
105 Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller ();
107 InputSource inputSource = new InputSource (new StringReader (reqXML));
108 SAXSource source = new SAXSource (inputSource);
110 if (reqXML.contains ("vnf-request") && !reqXML.contains ("network-request")) {
111 vnfReq = jaxbUnmarshaller.unmarshal (source, VnfRequest.class).getValue ();
113 isWrongRootElement = true;
116 } catch (Exception e) {
117 msoLogger.error (MessageEnum.APIH_VNFREQUEST_VALIDATION_ERROR, "", "", MsoLogger.ErrorCode.DataError, "Exception in format for vnf request ", e);
118 throw new ValidationException ("format for vnf request");
121 if (isWrongRootElement) {
122 msoLogger.error (MessageEnum.APIH_REQUEST_VALIDATION_ERROR_REASON, "root element is not correct", "", "", MsoLogger.ErrorCode.DataError, "root element is not correct");
123 throw new ValidationException ("root element <vnf-request> expected");
126 if (vnfReq == null) {
127 throw new ValidationException ("vnf-request");
129 this.vnfReq = vnfReq;
131 this.rinfo = vnfReq.getRequestInfo ();
133 if (this.rinfo == null) {
134 throw new ValidationException ("request-info");
136 if (this.rinfo.getRequestId () != null) {
137 msoLogger.info (MessageEnum.APIH_GENERATED_REQUEST_ID, requestId, this.rinfo.getRequestId ());
140 action = this.rinfo.getAction ();
141 if (action == null) {
142 throw new ValidationException ("action");
145 this.vnfInputs = vnfReq.getVnfInputs ();
146 if (this.vnfInputs == null) {
147 throw new ValidationException ("vnf-inputs");
150 // Verify that BPMN-specific elements are not in the APIH incoming request
151 if (this.vnfInputs.getPersonaModelId () != null || this.vnfInputs.getPersonaModelVersion () != null ||
152 this.vnfInputs.getIsBaseVfModule () != null || this.vnfInputs.getVnfPersonaModelId () != null ||
153 this.vnfInputs.getVnfPersonaModelVersion () != null) {
154 throw new ValidationException ("format for vnf request");
156 // Verify that the elements correspond to the version
159 if (version.equals(Constants.SCHEMA_VERSION_V1)) {
160 if (this.vnfInputs.getVfModuleName () != null || this.vnfInputs.getVfModuleId () != null ||
161 this.vnfInputs.getVfModuleModelName () != null || this.vnfInputs.getAsdcServiceModelVersion () != null ||
162 this.vnfInputs.getBackoutOnFailure() != null || this.vnfInputs.getAicCloudRegion() != null ||
163 this.vnfInputs.getServiceInstanceId () != null) {
164 throw new ValidationException ("format for v1 version of vnf request");
167 else if (version.equals(Constants.SCHEMA_VERSION_V2)) {
168 if (this.vnfInputs.getServiceType() != null || this.vnfInputs.getAicNodeClli() != null || this.vnfInputs.getServiceInstanceId () != null) {
169 throw new ValidationException ("format for v2 version of vnf request");
172 else if (version.equals(Constants.SCHEMA_VERSION_V3)) {
173 if (this.vnfInputs.getServiceType() != null || this.vnfInputs.getAicNodeClli() != null) {
174 throw new ValidationException ("format for v3 version of vnf request");
179 if (!InfraUtils.isActionAllowed (props, "vnf", version, action.value ())) {
180 throw new ValidationException ("action allowable for version " + version + " of vnf request");
183 if ((ActionType.UPDATE.equals(action) || ActionType.DELETE.equals(action)) && this.vnfInputs.getVnfId () == null) {
184 throw new ValidationException("vnf-id");
187 if ((ActionType.UPDATE_VF_MODULE.equals (action) || ActionType.DELETE_VF_MODULE.equals (action)) && this.vnfInputs.getVfModuleId () == null) {
188 throw new ValidationException ("vf-module-id");
191 if (ActionType.CREATE.equals (action) && this.vnfInputs.getVnfName () == null) {
192 throw new ValidationException ("vnf-name");
195 if (ActionType.CREATE_VF_MODULE.equals (action)) {
196 if (this.vnfInputs.getVfModuleName () == null) {
197 throw new ValidationException ("vf-module-name");
199 if (!InfraUtils.isValidHeatName(this.vnfInputs.getVfModuleName ())) {
200 throw new ValidationException ("vf-module-name: no value meeting heat stack name syntax requirements");
204 if (this.vnfInputs.getVnfType () == null) {
205 throw new ValidationException ("vnf-type");
208 if ((ActionType.CREATE_VF_MODULE.equals (action) || ActionType.UPDATE_VF_MODULE.equals (action) || ActionType.DELETE_VF_MODULE.equals (action)) && this.vnfInputs.getVfModuleModelName () == null) {
209 throw new ValidationException ("vf-module-model-name");
212 if (!version.equals(Constants.SCHEMA_VERSION_V1) && this.vnfInputs.getServiceId () == null) {
213 throw new ValidationException ("service-id ");
216 if (this.vnfInputs.getServiceType () != null && this.vnfInputs.getServiceId () != null) {
217 throw new ValidationException ("service-type or service-id ");
220 if (version.equals(Constants.SCHEMA_VERSION_V1) && this.vnfInputs.getAicNodeClli () == null) {
221 throw new ValidationException ("aic-node-clli");
224 if ((version.equals(Constants.SCHEMA_VERSION_V2) || version.equals(Constants.SCHEMA_VERSION_V3)) && (this.vnfInputs.getAicCloudRegion () == null || this.vnfInputs.getAicCloudRegion ().isEmpty())) {
225 throw new ValidationException ("aic-cloud-region");
228 if (version.equals(Constants.SCHEMA_VERSION_V3) && this.vnfInputs.getServiceInstanceId () == null) {
229 throw new ValidationException ("service-instance-id");
233 if (this.vnfInputs.getTenantId () == null) {
234 throw new ValidationException ("tenant-id");
237 Object vp = vnfReq.getVnfParams ();
240 msoLogger.debug ("This object is not null");
242 Node node = (Node) vp;
243 Document doc = node.getOwnerDocument ();
244 this.vnfParams = domToStr (doc);
247 msoLogger.debug ("VNFParams: " + this.vnfParams);
249 msoLogger.debug ("Request valid");
251 // Rebuild the request string for BPEL to include request-id
252 rinfo.setRequestId (this.requestId);
253 this.vnfReq.setRequestInfo (rinfo);
255 StringWriter stringWriter = new StringWriter ();
257 JAXBContext jaxbContext = JAXBContext.newInstance (VnfRequest.class);
258 Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
260 // output pretty printed
261 jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
263 jaxbMarshaller.marshal (this.vnfReq, stringWriter);
265 } catch (JAXBException e) {
266 msoLogger.debug ("Exception: ", e);
269 this.requestXML = stringWriter.toString ();
270 msoLogger.debug("REQUEST XML to BPEL: " + this.requestXML);
274 public void createRequestRecord (Status status) {
276 Session session = null;
279 session = HibernateUtil.getSessionFactory ().openSession ();
280 session.beginTransaction ();
282 InfraActiveRequests aq = new InfraActiveRequests ();
283 aq.setRequestId (requestId);
284 aq.setClientRequestId(rinfo.getRequestId());
286 Timestamp startTimeStamp = new Timestamp (System.currentTimeMillis());
288 if (rinfo.getAction () != null) {
289 aq.setAction (rinfo.getAction ().value ());
290 aq.setRequestAction (RequestActionMap.getMappedRequestAction (rinfo.getAction ().value ()));
292 aq.setSource (rinfo.getSource ());
294 // Set up mandatory parameters
295 aq.setAction (NOT_PROVIDED);
296 aq.setRequestAction (NOT_PROVIDED);
299 aq.setRequestBody (this.requestXML);
300 aq.setRequestScope ("");
302 if (vnfInputs != null) {
303 if (vnfInputs.getVnfId () != null) {
304 aq.setVnfId (vnfInputs.getVnfId ());
306 if (vnfInputs.getVnfName () != null) {
307 aq.setVnfName (vnfInputs.getVnfName ());
309 if (vnfInputs.getVnfType () != null) {
310 aq.setVnfType (vnfInputs.getVnfType ());
312 if (vnfInputs.getServiceInstanceId () != null) {
313 aq.setServiceInstanceId (vnfInputs.getServiceInstanceId ());
315 if (vnfInputs.getServiceType () != null) {
316 aq.setServiceType (vnfInputs.getServiceType ());
318 if (vnfInputs.getServiceId () != null) {
319 aq.setAaiServiceId (vnfInputs.getServiceId ());
321 if (vnfInputs.getAicNodeClli () != null) {
322 aq.setAicNodeClli (vnfInputs.getAicNodeClli ());
324 if (vnfInputs.getAicCloudRegion () != null) {
325 aq.setAicCloudRegion (vnfInputs.getAicCloudRegion ());
327 if (vnfInputs.getTenantId () != null) {
328 aq.setTenantId (vnfInputs.getTenantId ());
330 if (vnfInputs.getProvStatus () != null) {
331 aq.setProvStatus (vnfInputs.getProvStatus ());
333 if (vnfInputs.getVolumeGroupName () != null) {
334 aq.setVolumeGroupName (vnfInputs.getVolumeGroupName ());
336 if (vnfInputs.getVolumeGroupId () != null) {
337 aq.setVolumeGroupId (vnfInputs.getVolumeGroupId ());
339 if (vnfInputs.getVfModuleId () != null) {
340 aq.setVfModuleId (vnfInputs.getVfModuleId ());
342 if (vnfInputs.getVfModuleName () != null) {
343 aq.setVfModuleName (vnfInputs.getVfModuleName ());
345 if (vnfInputs.getVfModuleModelName () != null) {
346 aq.setVfModuleModelName (vnfInputs.getVfModuleModelName ());
349 if (vnfInputs.getVfModuleName () != null || vnfInputs.getVfModuleId () != null) {
350 aq.setRequestScope (ModelType.vfModule.name ());
353 aq.setRequestScope (ModelType.vnf.name ());
359 aq.setStartTime (startTimeStamp);
360 aq.setRequestStatus (status.toString ());
361 aq.setLastModifiedBy (Constants.MODIFIED_BY_APIHANDLER);
362 aq.setRequestType ("VNF");
364 if (vnfParams != null) {
365 msoLogger.debug ("Storing vnfParams: " + vnfParams);
366 aq.setVnfParams (this.vnfParams);
369 if ((status == Status.FAILED) || (status == Status.COMPLETE)) {
370 aq.setStatusMessage (this.errorMessage);
371 aq.setResponseBody (this.responseBody);
373 Timestamp endTimeStamp = new Timestamp (System.currentTimeMillis());
374 aq.setEndTime (endTimeStamp);
377 aq.setProgress (this.progress);
380 msoLogger.debug ("About to insert a record");
383 session.getTransaction ().commit ();
385 } catch (Exception e) {
386 msoLogger.error (MessageEnum.APIH_DB_INSERT_EXC, "", "", MsoLogger.ErrorCode.AvailabilityError, "Exception in createRequestRecord", e);
387 if (session != null) {
390 if (!status.equals (Status.FAILED)) {
396 public void updateFinalStatus (Status status) {
399 result = RequestsDatabase.updateInfraFinalStatus (requestId,
404 Constants.MODIFIED_BY_APIHANDLER);
405 } catch (Exception e) {
406 msoLogger.error (MessageEnum.APIH_DB_UPDATE_EXC, e.getMessage (), "", "", MsoLogger.ErrorCode.AvailabilityError, "Exception in updateFinalStatus");
407 msoLogger.debug ("Exception: ", e);
411 public Response buildResponse (int httpResponseCode, String errorCode, InfraActiveRequests inProgress) {
412 return buildResponseWithError (httpResponseCode, errorCode, inProgress, null);
415 public Response buildResponseWithError (int httpResponseCode,
417 InfraActiveRequests inProgress,
418 String errorString) {
420 ObjectFactory beansObjectFactory = new ObjectFactory ();
422 VnfRequest vr = beansObjectFactory.createVnfRequest ();
424 RequestInfo ri = beansObjectFactory.createRequestInfo ();
426 ri.setRequestId (requestId);
427 ri.setRequestStatus (this.status);
428 ri.setAction (this.rinfo.getAction ());
429 ri.setSource (this.rinfo.getSource ());
431 String errorMsg = null;
432 if (errorCode != null) {
433 // If error code is actually an XML error response from BPEL, treat it specially:
434 if (!Messages.errors.containsKey (errorCode)) {
435 if (errorCode.length () > 300) {
436 errorMsg = errorCode.substring (0, 299);
438 errorMsg = errorCode;
443 if (inProgress == null) {
444 if (errorCode.equals (ErrorNumbers.RECIPE_DOES_NOT_EXIST)) {
445 errorMsg = String.format (Messages.errors.get (errorCode), "vnf", errorString);
447 errorMsg = String.format (Messages.errors.get (errorCode), errorString);
449 } else if (errorCode.equals (ErrorNumbers.LOCKED_CREATE_ON_THE_SAME_VNF_NAME_IN_PROGRESS)) {
450 errorMsg = String.format (Messages.errors.get (errorCode),
452 inProgress.getVnfName (),
453 inProgress.getRequestStatus (),
455 } else if (errorCode.equals (ErrorNumbers.LOCKED_SAME_ACTION_AND_VNF_ID)) {
456 errorMsg = String.format (Messages.errors.get (errorCode),
458 inProgress.getVnfId (),
459 inProgress.getRequestStatus (),
460 inProgress.getAction (),
465 ri.setStatusMessage (errorMsg);
466 this.errorMessage = errorMsg;
468 ri.setProgress ((int) this.progress);
470 Date startDate = new Date (this.startTime);
471 SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSS");
472 String startTimeString = sdf.format (startDate);
474 ri.setStartTime (startTimeString);
476 if (this.progress == Constants.PROGRESS_REQUEST_COMPLETED) {
477 ri.setEndTime(startTimeString);
480 vr.setRequestInfo (ri);
482 this.vnfInputs.setIsBaseVfModule(null);
483 this.vnfInputs.setPersonaModelId(null);
484 this.vnfInputs.setPersonaModelVersion(null);
485 this.vnfInputs.setVnfPersonaModelId(null);
486 this.vnfInputs.setVnfPersonaModelVersion(null);
488 vr.setVnfInputs (this.vnfInputs);
490 StringWriter stringWriter = new StringWriter ();
492 JAXBContext jaxbContext = JAXBContext.newInstance (VnfRequest.class);
493 Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
494 jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
496 jaxbMarshaller.marshal (vr, stringWriter);
498 } catch (JAXBException e) {
499 msoLogger.debug ("Exception: ", e);
502 String response = stringWriter.toString ();
504 this.httpResponse = Integer.toString (httpResponseCode);
505 this.responseBody = response;
508 // Log the failed request into the MSO Requests database
510 return Response.status (httpResponseCode).entity (response).build ();
514 public Response buildResponseFailedValidation (int httpResponseCode, String exceptionMessage) {
516 ObjectFactory beansObjectFactory = new ObjectFactory ();
517 VnfRequest vr = beansObjectFactory.createVnfRequest ();
519 RequestInfo ri = beansObjectFactory.createRequestInfo ();
520 ri.setRequestId (requestId);
522 if (this.rinfo != null) {
523 if (this.rinfo.getAction () != null) {
524 ri.setAction (this.rinfo.getAction ());
526 ri.setAction (ActionType.NOT_PROVIDED);
528 if (this.rinfo.getSource () != null) {
529 ri.setSource (this.rinfo.getSource ());
532 ri.setAction (ActionType.NOT_PROVIDED);
535 // Nothing more is expected for this request
537 String errorMsg = String.format (Messages.errors.get (ErrorNumbers.REQUEST_FAILED_SCHEMA_VALIDATION
540 ri.setStatusMessage (errorMsg);
541 this.errorMessage = errorMsg;
543 ri.setProgress ((int) this.progress);
544 ri.setRequestStatus (RequestStatusType.FAILED);
545 Date startDate = new Date (this.startTime);
546 SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSS");
547 String startTimeString = sdf.format (startDate);
549 ri.setStartTime (startTimeString);
550 ri.setEndTime(startTimeString);
552 vr.setRequestInfo (ri);
554 vr.setVnfInputs (this.vnfInputs);
556 StringWriter stringWriter = new StringWriter ();
558 JAXBContext jaxbContext = JAXBContext.newInstance (VnfRequest.class);
559 Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
561 // output pretty printed
562 jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
564 jaxbMarshaller.marshal (vr, stringWriter);
566 } catch (JAXBException e) {
567 msoLogger.debug ("Error marshalling", e);
570 String response = stringWriter.toString ();
572 this.httpResponse = Integer.toString (httpResponseCode);
573 this.responseBody = response;
575 return Response.status (httpResponseCode).entity (response).build ();
578 public String getRequestUri () {
582 public void setRequestUri (String requestUri) {
583 this.requestUri = requestUri;
586 public VnfInputs getVnfInputs () {
590 public RequestInfo getRequestInfo () {
594 public String getResponseBody () {
598 public void setResponseBody (String responseBody) {
599 this.responseBody = responseBody;
602 public String getHttpResponse () {
606 public void setHttpResponse (String httpResponse) {
607 this.httpResponse = httpResponse;
610 public String getRequestId () {
614 public String getRequestXML () {
618 public void setRequestXML (String requestXML) {
619 this.requestXML = requestXML;
622 public RequestStatusType getStatus () {
626 public String getServiceType () {
627 if (this.vnfInputs.getServiceType () != null)
628 return this.vnfInputs.getServiceType ();
629 if (this.vnfInputs.getServiceId () != null)
630 return this.vnfInputs.getServiceId ();
634 public void setStatus (RequestStatusType status) {
635 this.status = status;
639 this.progress = Constants.PROGRESS_REQUEST_COMPLETED;
642 this.progress = Constants.PROGRESS_REQUEST_IN_PROGRESS;
647 public static String domToStr (Document doc) {
653 StringWriter sw = new StringWriter ();
654 StreamResult sr = new StreamResult (sw);
655 TransformerFactory tf = TransformerFactory.newInstance ();
656 Transformer t = tf.newTransformer ();
657 t.setOutputProperty (OutputKeys.STANDALONE, "yes");
658 NodeList nl = doc.getDocumentElement ().getChildNodes ();
659 DOMSource source = null;
660 for (int x = 0; x < nl.getLength (); x++) {
661 Node e = nl.item (x);
662 if (e instanceof Element) {
663 source = new DOMSource (e);
667 if (source != null) {
668 t.transform (source, sr);
670 String s = sw.toString ();
676 } catch (Exception e) {
677 msoLogger.error (MessageEnum.APIH_DOM2STR_ERROR, "", "", MsoLogger.ErrorCode.DataError, "Exception in domToStr", e);
682 public void addBPMNSpecificInputs(String personaModelId, String personaModelVersion, Boolean isBaseVfModule,
683 String vnfPersonaModelId, String vnfPersonaModelVersion) {
684 vnfInputs.setPersonaModelId(personaModelId);
685 vnfInputs.setPersonaModelVersion(personaModelVersion);
686 vnfInputs.setIsBaseVfModule(isBaseVfModule);
687 vnfInputs.setVnfPersonaModelId(vnfPersonaModelId);
688 vnfInputs.setVnfPersonaModelVersion(vnfPersonaModelVersion);
690 this.vnfReq.setVnfInputs(vnfInputs);
692 StringWriter stringWriter = new StringWriter ();
694 JAXBContext jaxbContext = JAXBContext.newInstance (VnfRequest.class);
695 Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
697 // output pretty printed
698 jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
700 jaxbMarshaller.marshal (this.vnfReq, stringWriter);
702 } catch (JAXBException e) {
703 msoLogger.debug ("Exception: ", e);
706 this.requestXML = stringWriter.toString ();
707 msoLogger.debug("REQUEST XML to BPEL: " + this.requestXML);