[POLICY-117] Resolve the Policy Critical issues
[policy/engine.git] / ECOMP-PDP-REST / src / main / java / org / openecomp / policy / pdp / rest / api / services / CreateUpdatePolicyServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-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.openecomp.policy.pdp.rest.api.services;
21
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 import java.util.UUID;
25
26 import org.openecomp.policy.api.PolicyException;
27 import org.openecomp.policy.api.PolicyParameters;
28 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
29 import org.openecomp.policy.common.logging.flexlogger.Logger;
30 import org.openecomp.policy.pdp.rest.api.utils.PolicyApiUtils;
31 import org.openecomp.policy.utils.PolicyUtils;
32 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
33 import org.springframework.http.HttpStatus;
34
35 public class CreateUpdatePolicyServiceImpl implements CreateUpdatePolicyService {
36         private static final Logger LOGGER = FlexLogger.getLogger(CreateUpdatePolicyServiceImpl.class.getName());
37     
38     private String policyResult = null;
39     private HttpStatus status = HttpStatus.BAD_REQUEST;
40     private Boolean updateFlag = false;
41     private String message = null;
42     private PolicyParameters policyParameters = new PolicyParameters();
43     private String policyName = null;
44     private String policyScope = null;
45     private String date = null;
46     
47         public CreateUpdatePolicyServiceImpl(PolicyParameters policyParameters,
48                         String requestID, boolean updateFlag) {
49                 this.updateFlag = updateFlag;
50         this.policyParameters = policyParameters;
51         if(policyParameters.getRequestID()==null){
52             UUID requestUUID = null;
53             if (requestID != null && !requestID.isEmpty()) {
54                 try {
55                     requestUUID = UUID.fromString(requestID);
56                 } catch (IllegalArgumentException e) {
57                     requestUUID = UUID.randomUUID();
58                     LOGGER.info("Generated Random UUID: " + requestUUID.toString(), e);
59                 }
60             }else{
61                 requestUUID = UUID.randomUUID();
62                 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
63             }
64             this.policyParameters.setRequestID(requestUUID);
65         }
66         try{
67             run();
68             specialCheck();
69         }catch(PolicyException e){
70             policyResult = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
71             status = HttpStatus.BAD_REQUEST;
72         }
73     }
74
75     public void run() throws PolicyException{
76         // Check Validation. 
77         if(!getValidation()){
78             LOGGER.error(message);
79             throw new PolicyException(message);
80         }
81         // Get Result. 
82         try{
83             status = HttpStatus.OK;
84             policyResult = processResult();
85         }catch (Exception e){
86             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
87             status = HttpStatus.BAD_REQUEST;
88             throw new PolicyException(e);
89         }
90     }
91     
92     @SuppressWarnings("incomplete-switch")
93     public String processResult() throws PolicyException{
94         String response = null;
95         if(policyParameters.getPolicyConfigType()!=null){
96             // This is a Config Type Policy. 
97             switch(policyParameters.getPolicyConfigType()){
98             case BRMS_PARAM:
99                 BRMSParamPolicyService bRMSParamPolicyService = new BRMSParamPolicyService(policyName, policyScope, policyParameters, date);
100                 // Check Validation. 
101                 if(!bRMSParamPolicyService.getValidation()){
102                     LOGGER.error(bRMSParamPolicyService.getMessage());
103                     status = HttpStatus.BAD_REQUEST;
104                     return bRMSParamPolicyService.getMessage();
105                 }
106                 // Get Result. 
107                 response = bRMSParamPolicyService.getResult(updateFlag);
108                 break;
109             case BRMS_RAW:
110                 BRMSRawPolicyService bRMSRawPolicyService = new BRMSRawPolicyService(policyName, policyScope, policyParameters, date);
111                 // Check Validation. 
112                 if(!bRMSRawPolicyService.getValidation()){
113                     LOGGER.error(bRMSRawPolicyService.getMessage());
114                     status = HttpStatus.BAD_REQUEST;
115                     return bRMSRawPolicyService.getMessage();
116                 }
117                 // Get Result. 
118                 response = bRMSRawPolicyService.getResult(updateFlag);
119                 break;
120             case Base:
121                 ConfigPolicyService configPolicyService = new ConfigPolicyService(policyName, policyScope, policyParameters, date);
122                 // Check Validation. 
123                 if(!configPolicyService.getValidation()){
124                     LOGGER.error(configPolicyService.getMessage());
125                     status = HttpStatus.BAD_REQUEST;
126                     return configPolicyService.getMessage();
127                 }
128                 // Get Result. 
129                 response = configPolicyService.getResult(updateFlag);
130                 break;
131             case ClosedLoop_Fault:
132                 ClosedLoopFaultPolicyService closedLoopFaultPolicyService = new ClosedLoopFaultPolicyService(policyName, policyScope, policyParameters, date);
133                 // Check Validation. 
134                 if(!closedLoopFaultPolicyService.getValidation()){
135                     LOGGER.error(closedLoopFaultPolicyService.getMessage());
136                     status = HttpStatus.BAD_REQUEST;
137                     return closedLoopFaultPolicyService.getMessage();
138                 }
139                 // Get Result. 
140                 response = closedLoopFaultPolicyService.getResult(updateFlag);
141                 break;
142             case ClosedLoop_PM:
143                 ClosedLoopPMPolicyService closedLoopPMPolicyService = new ClosedLoopPMPolicyService(policyName, policyScope, policyParameters, date);
144                 // Check Validation. 
145                 if(!closedLoopPMPolicyService.getValidation()){
146                     LOGGER.error(closedLoopPMPolicyService.getMessage());
147                     status = HttpStatus.BAD_REQUEST;
148                     return closedLoopPMPolicyService.getMessage();
149                 }
150                 // Get Result. 
151                 response = closedLoopPMPolicyService.getResult(updateFlag);
152                 break;
153             case Firewall:
154                 FirewallPolicyService firewallPolicyService = new FirewallPolicyService(policyName, policyScope, policyParameters, date);
155                 // Check Validation. 
156                 if(!firewallPolicyService.getValidation()){
157                     LOGGER.error(firewallPolicyService.getMessage());
158                     status = HttpStatus.BAD_REQUEST;
159                     return firewallPolicyService.getMessage();
160                 }
161                 // Get Result. 
162                 response = firewallPolicyService.getResult(updateFlag);
163                 break;
164             case MicroService:
165                 MicroServicesPolicyService microServicesPolicyService = new MicroServicesPolicyService(policyName, policyScope, policyParameters, date);
166                 // Check Validation. 
167                 if(!microServicesPolicyService.getValidation()){
168                     LOGGER.error(microServicesPolicyService.getMessage());
169                     status = HttpStatus.BAD_REQUEST;
170                     return microServicesPolicyService.getMessage();
171                 }
172                 // Get Result. 
173                 response = microServicesPolicyService.getResult(updateFlag);
174                 break;
175             default:
176                 String message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " Invalid Config Type Present";
177                 LOGGER.error(message);
178                 status = HttpStatus.BAD_REQUEST;
179                 return message;
180             }
181         }else if (policyParameters.getPolicyClass()!=null){
182             switch (policyParameters.getPolicyClass()){
183             case Action:
184                 ActionPolicyService actionPolicyService = new ActionPolicyService(policyScope, policyName, policyParameters);
185                 // Check Validation. 
186                 if(!actionPolicyService.getValidation()){
187                     LOGGER.error(actionPolicyService.getMessage());
188                     status = HttpStatus.BAD_REQUEST;
189                     return actionPolicyService.getMessage();
190                 }
191                 // Get Result. 
192                 response = actionPolicyService.getResult(updateFlag);
193                 break;
194             case Decision:
195                 DecisionPolicyService decisionPolicyService = new DecisionPolicyService(policyScope, policyName, policyParameters);
196                 // Check Validation. 
197                 if(!decisionPolicyService.getValidation()){
198                     LOGGER.error(decisionPolicyService.getMessage());
199                     status = HttpStatus.BAD_REQUEST;
200                     return decisionPolicyService.getMessage();
201                 }
202                 // Get Result. 
203                 response = decisionPolicyService.getResult(updateFlag);
204                 break;
205             }
206         }else {
207             String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Class found.";
208             LOGGER.error(message);
209             status = HttpStatus.BAD_REQUEST;
210             response = message;
211         }
212         return response;
213     }
214
215     protected boolean getValidation() {
216         if(policyParameters == null){
217             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy parameters given. ";
218             return false;
219         }
220         if(policyParameters.getPolicyName() == null){
221             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
222             return false;
223         }
224         if (policyParameters.getPolicyName().contains(".")) {
225             policyName = policyParameters.getPolicyName().substring(policyParameters.getPolicyName().lastIndexOf('.') + 1,
226                     policyParameters.getPolicyName().length());
227             policyScope = policyParameters.getPolicyName().substring(0,policyParameters.getPolicyName().lastIndexOf('.'));
228             LOGGER.info("Name is " + policyName + "   scope is " + policyScope);
229         } else {
230             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
231             return false;
232         }
233         if (policyName==null||policyName.trim().isEmpty()){
234             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
235             return false;
236         }
237         message = PolicyUtils.emptyPolicyValidator(policyScope);
238         if(!message.contains("success")){
239             message = XACMLErrorConstants.ERROR_DATA_ISSUE+ message;
240             return false;
241         }
242         message = PolicyUtils.emptyPolicyValidator(policyName);
243         if(!message.contains("success")){
244             message = XACMLErrorConstants.ERROR_DATA_ISSUE+ message;
245             return false;
246         }
247         if(policyParameters.getPolicyDescription()!=null){
248             message = PolicyUtils.descriptionValidator(policyParameters.getPolicyDescription());
249             if(!message.contains("success")){
250                 message = XACMLErrorConstants.ERROR_DATA_ISSUE+ message;
251                 return false;
252             }
253         }
254         if(!PolicyApiUtils.validateNONASCIICharactersAndAllowSpaces(policyParameters.toString())){
255             message = XACMLErrorConstants.ERROR_DATA_ISSUE+ "This requests contains Non ASCII Characters. Please review your input parameter"
256                     + " values and correct the illegal characters.";
257             return false;
258         }
259         // Set some default Values. 
260         if (policyParameters.getTtlDate()!=null){
261             date = convertDate(policyParameters.getTtlDate());
262         }
263         return true;
264     }
265     
266     protected String convertDate(Date date) {
267         String strDate = null;
268         if (date!=null) {
269             SimpleDateFormat dateformatJava = new SimpleDateFormat("dd-MM-yyyy");
270             strDate = dateformatJava.format(date);
271         }
272         return (strDate==null) ? "NA": strDate;
273     }
274
275     protected void specialCheck() {
276         if(policyResult== null || policyResult.contains("BAD REQUEST")||policyResult.contains("PE300")){
277             status = HttpStatus.BAD_REQUEST;
278         } else if (policyResult.contains("Policy Exist Error")) {
279             status = HttpStatus.CONFLICT;
280         } else if (policyResult.contains("PE200")||policyResult.contains("PE900")){
281             status = HttpStatus.INTERNAL_SERVER_ERROR;
282         }
283     }
284
285     public String getResult() {
286         return policyResult;
287     }
288
289     public HttpStatus getResponseCode() {
290         return status;
291     }
292
293 }