New Optimization Policy
[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                 case OPTIMIZATION:
114                         if (fileName.endsWith(".yml") || fileName.endsWith(".xmi") ||  fileName.endsWith(".zip")){
115                     try {
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);
120                         return response;
121                     }
122                 }else{
123                     response = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect File Type Given. Please use a file of type .xmi or .zip.";
124                     LOGGER.error(response);     
125                     return response;
126                 }
127                         break;
128                 case BRMSPARAM:
129                         if (fileName.endsWith(".drl")){
130                     try {
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);
135                         return response;
136                     }
137                 }else{
138                     response = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect File Type Given. Please use a file of type .drl";
139                     LOGGER.error(response);     
140                     return response;
141                 }
142                         break;
143                 default:
144                         response = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect ServiceType Given. ";
145                         LOGGER.error(response);     
146                         return response;
147         }
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");
152         return response;
153     }
154
155     private boolean getValidation() {
156         if(importParameters==null){
157             message = XACMLErrorConstants.ERROR_DATA_ISSUE + " no Import Parameters given. ";
158             return false;
159         }
160         if(importParameters.getServiceName()==null || importParameters.getServiceName().trim().isEmpty()){
161             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing service name value.";
162             return false;
163         }
164         if(importParameters.getServiceType()==null || importParameters.getServiceType().toString().trim().isEmpty()){
165             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing service Type value.";
166             return false;
167         }
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.";
171             return false;
172         }
173         if(file==null || file.isEmpty()){
174             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing File.";
175             return false;
176         }
177         return true;
178     }
179
180     public String getResult() {
181         return importResponse;
182     }
183
184     public HttpStatus getResponseCode() {
185         return status;
186     }
187
188 }