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=========================================================
20 package org.openecomp.policy.pdp.rest.api.services;
22 import java.io.BufferedInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.UUID;
27 import org.openecomp.policy.api.ImportParameters;
28 import org.openecomp.policy.api.PolicyException;
29 import org.openecomp.policy.api.ImportParameters.IMPORT_TYPE;
30 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
31 import org.openecomp.policy.common.logging.flexlogger.Logger;
32 import org.openecomp.policy.utils.PolicyUtils;
33 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.web.multipart.MultipartFile;
37 public class PolicyEngineImportService {
38 private static Logger LOGGER = FlexLogger.getLogger(PolicyEngineImportService.class.getName());
40 private String importResponse = null;
41 private HttpStatus status = HttpStatus.BAD_REQUEST;
42 private String message = null;
43 private ImportParameters importParameters = null;
44 private MultipartFile file = null;
46 public PolicyEngineImportService(String importParametersJson,
50 this.importParameters = PolicyUtils.jsonStringToObject(importParametersJson, ImportParameters.class);
51 } catch (Exception e) {
52 importResponse = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
53 status = HttpStatus.BAD_REQUEST;
54 // This needs to stop here in case if there a issue here. Avoiding Null pointer exceptions.
58 if(importParameters.getRequestID()==null){
59 UUID requestUUID = null;
60 if (requestID != null && !requestID.isEmpty()) {
62 requestUUID = UUID.fromString(requestID);
63 } catch (IllegalArgumentException e) {
64 requestUUID = UUID.randomUUID();
65 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
68 requestUUID = UUID.randomUUID();
69 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
71 this.importParameters.setRequestID(requestUUID);
76 }catch(PolicyException e){
77 importResponse = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
78 status = HttpStatus.BAD_REQUEST;
82 private void specialCheck() {
83 if(importResponse.contains("BAD REQUEST") || importResponse.contains("PE300")){
84 status = HttpStatus.BAD_REQUEST;
85 }else if(importResponse.contains("PE200")){
86 status = HttpStatus.INTERNAL_SERVER_ERROR;
90 private void run() throws PolicyException{
93 LOGGER.error(message);
94 throw new PolicyException(message);
98 status = HttpStatus.OK;
99 importResponse = processResult();
100 }catch (Exception e){
101 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
102 status = HttpStatus.BAD_REQUEST;
103 throw new PolicyException(e);
107 private String processResult() throws PolicyException{
108 String response = null;
109 InputStream targetStream = null;
110 String fileName = file.getOriginalFilename();
111 switch (importParameters.getServiceType()){
113 if (fileName.endsWith(".xmi") || fileName.endsWith(".zip")){
115 targetStream = new BufferedInputStream(file.getInputStream());
116 } catch (IOException e) {
117 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "Error in reading in the file provided";
118 LOGGER.error(response + e);
122 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect File Type Given. Please use a file of type .xmi or .zip.";
123 LOGGER.error(response);
128 if (fileName.endsWith(".drl")){
130 targetStream = new BufferedInputStream(file.getInputStream());
131 } catch (IOException e) {
132 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "Error in reading in the file provided";
133 LOGGER.error(response + e);
137 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect File Type Given. Please use a file of type .drl";
138 LOGGER.error(response);
143 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect ServiceType Given. ";
144 LOGGER.error(response);
147 String[] parameters = new String[] {"importService=" + importParameters.getServiceType(), "serviceName="
148 + importParameters.getServiceName(), "fileName=" + fileName, "version=" + importParameters.getVersion(), "description=" + importParameters.getDescription()};
149 PAPServices papServices = new PAPServices();
150 response = (String) papServices.callPAP(targetStream, parameters, importParameters.getRequestID(), "importMS");
154 private boolean getValidation() {
155 if(importParameters==null){
156 message = XACMLErrorConstants.ERROR_DATA_ISSUE + " no Import Parameters given. ";
159 if(importParameters.getServiceName()==null || importParameters.getServiceName().trim().isEmpty()){
160 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing service name value.";
163 if(importParameters.getServiceType()==null || importParameters.getServiceType().toString().trim().isEmpty()){
164 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing service Type value.";
167 if(importParameters.getServiceType().equals(IMPORT_TYPE.MICROSERVICE) && (importParameters.getVersion()==null || importParameters.getVersion().trim().isEmpty())){
168 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing version value.";
171 if(file==null || file.isEmpty()){
172 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing File.";
178 public String getResult() {
179 return importResponse;
182 public HttpStatus getResponseCode() {