Fixes for sonar critical issues
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / PolicyEngineImportService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20 package org.onap.policy.pdp.rest.api.services;
21
22 import java.io.BufferedInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.UUID;
26
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;
36
37 public class PolicyEngineImportService {
38     private static Logger LOGGER = FlexLogger.getLogger(PolicyEngineImportService.class.getName());
39     
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;
45
46     public PolicyEngineImportService(String importParametersJson,
47             MultipartFile file,
48             String requestID) {
49         try {
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. 
55             return; 
56         }
57         this.file = file;
58         if(importParameters.getRequestID()==null){
59             UUID requestUUID = null;
60             if (requestID != null && !requestID.isEmpty()) {
61                 try {
62                     requestUUID = UUID.fromString(requestID);
63                 } catch (IllegalArgumentException e) {
64                     requestUUID = UUID.randomUUID();
65                     LOGGER.info("Generated Random UUID: " + requestUUID.toString(), e);
66                 }
67             }else{
68                 requestUUID = UUID.randomUUID();
69                 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
70             }
71             this.importParameters.setRequestID(requestUUID);
72         }
73         try{
74             run();
75             specialCheck();
76         }catch(PolicyException e){
77             importResponse = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
78             status = HttpStatus.BAD_REQUEST;
79         }
80     }
81
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;
87         }
88     }
89
90     private void run() throws PolicyException{
91         // Check Validation. 
92         if(!getValidation()){
93             LOGGER.error(message);
94             throw new PolicyException(message);
95         }
96         // Get Result. 
97         try{
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);
104         }
105     }
106
107     private String processResult() throws PolicyException{
108         String response = null;
109         InputStream targetStream = null;
110         String fileName = file.getOriginalFilename();
111         switch (importParameters.getServiceType()){
112                 case MICROSERVICE:
113                         if (fileName.endsWith(".xmi") ||  fileName.endsWith(".zip")){
114                     try {
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);
119                         return response;
120                     }
121                 }else{
122                     response = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect File Type Given. Please use a file of type .xmi or .zip.";
123                     LOGGER.error(response);     
124                     return response;
125                 }
126                         break;
127                 case BRMSPARAM:
128                         if (fileName.endsWith(".drl")){
129                     try {
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);
134                         return response;
135                     }
136                 }else{
137                     response = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect File Type Given. Please use a file of type .drl";
138                     LOGGER.error(response);     
139                     return response;
140                 }
141                         break;
142                 default:
143                         response = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect ServiceType Given. ";
144                         LOGGER.error(response);     
145                         return response;
146         }
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");
151         return response;
152     }
153
154     private boolean getValidation() {
155         if(importParameters==null){
156             message = XACMLErrorConstants.ERROR_DATA_ISSUE + " no Import Parameters given. ";
157             return false;
158         }
159         if(importParameters.getServiceName()==null || importParameters.getServiceName().trim().isEmpty()){
160             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing service name value.";
161             return false;
162         }
163         if(importParameters.getServiceType()==null || importParameters.getServiceType().toString().trim().isEmpty()){
164             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing service Type value.";
165             return false;
166         }
167         if(importParameters.getServiceType().equals(IMPORT_TYPE.MICROSERVICE) && (importParameters.getVersion()==null || importParameters.getVersion().trim().isEmpty())){
168             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing version value.";
169             return false;
170         }
171         if(file==null || file.isEmpty()){
172             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing File.";
173             return false;
174         }
175         return true;
176     }
177
178     public String getResult() {
179         return importResponse;
180     }
181
182     public HttpStatus getResponseCode() {
183         return status;
184     }
185
186 }