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.onap.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.onap.policy.api.ImportParameters;
28 import org.onap.policy.api.PolicyException;
29 import org.onap.policy.api.ImportParameters.IMPORT_TYPE;
30 import org.onap.policy.common.logging.flexlogger.FlexLogger;
31 import org.onap.policy.common.logging.flexlogger.Logger;
32 import org.onap.policy.utils.PolicyUtils;
33 import org.onap.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(), e);
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==null || importResponse.contains("PE200")){
84 status = HttpStatus.INTERNAL_SERVER_ERROR;
85 }else if(importResponse.contains("BAD REQUEST") || importResponse.contains("PE300")){
86 status = HttpStatus.BAD_REQUEST;
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()){
114 if (fileName.endsWith(".yml") || fileName.endsWith(".xmi") || fileName.endsWith(".zip")){
116 targetStream = new BufferedInputStream(file.getInputStream());
117 } catch (IOException e) {
118 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "Error in reading in the file provided";
119 LOGGER.error(response + e);
123 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect File Type Given. Please use a file of type .xmi or .zip.";
124 LOGGER.error(response);
129 if (fileName.endsWith(".drl")){
131 targetStream = new BufferedInputStream(file.getInputStream());
132 } catch (IOException e) {
133 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "Error in reading in the file provided";
134 LOGGER.error(response + e);
138 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect File Type Given. Please use a file of type .drl";
139 LOGGER.error(response);
144 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect ServiceType Given. ";
145 LOGGER.error(response);
148 String[] parameters = new String[] {"importService=" + importParameters.getServiceType(), "serviceName="
149 + importParameters.getServiceName(), "fileName=" + fileName, "version=" + importParameters.getVersion(), "description=" + importParameters.getDescription()};
150 PAPServices papServices = new PAPServices();
151 response = (String) papServices.callPAP(targetStream, parameters, importParameters.getRequestID(), "importMS");
155 private boolean getValidation() {
156 if(importParameters==null){
157 message = XACMLErrorConstants.ERROR_DATA_ISSUE + " no Import Parameters given. ";
160 if(importParameters.getServiceName()==null || importParameters.getServiceName().trim().isEmpty()){
161 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing service name value.";
164 if(importParameters.getServiceType()==null || importParameters.getServiceType().toString().trim().isEmpty()){
165 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing service Type value.";
168 if((IMPORT_TYPE.MICROSERVICE.equals(importParameters.getServiceType()) || IMPORT_TYPE.OPTIMIZATION.equals(importParameters.getServiceType()))
169 && (importParameters.getVersion()==null || importParameters.getVersion().trim().isEmpty())){
170 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing version value.";
173 if(file==null || file.isEmpty()){
174 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing File.";
180 public String getResult() {
181 return importResponse;
184 public HttpStatus getResponseCode() {