2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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.text.SimpleDateFormat;
23 import java.util.Date;
24 import java.util.UUID;
26 import org.onap.policy.api.PolicyException;
27 import org.onap.policy.api.PolicyParameters;
28 import org.onap.policy.common.logging.flexlogger.FlexLogger;
29 import org.onap.policy.common.logging.flexlogger.Logger;
30 import org.onap.policy.pdp.rest.api.utils.PolicyApiUtils;
31 import org.onap.policy.rest.adapter.PolicyRestAdapter;
32 import org.onap.policy.rest.util.PolicyValidation;
33 import org.onap.policy.rest.util.PolicyValidationRequestWrapper;
34 import org.onap.policy.xacml.api.XACMLErrorConstants;
35 import org.springframework.http.HttpStatus;
37 import com.google.common.base.Strings;
39 public class CreateUpdatePolicyServiceImpl implements CreateUpdatePolicyService {
40 private static final Logger LOGGER = FlexLogger.getLogger(CreateUpdatePolicyServiceImpl.class.getName());
42 private String policyResult = null;
43 private HttpStatus status = HttpStatus.BAD_REQUEST;
44 private Boolean updateFlag = false;
45 private String message = null;
46 private PolicyParameters policyParameters = new PolicyParameters();
47 private String policyName = null;
48 private String policyScope = null;
49 private String date = null;
51 public CreateUpdatePolicyServiceImpl(PolicyParameters policyParameters,
52 String requestID, boolean updateFlag) {
53 this.updateFlag = updateFlag;
54 this.policyParameters = policyParameters;
55 if(policyParameters.getRequestID()==null){
56 UUID requestUUID = null;
57 if (requestID != null && !requestID.isEmpty()) {
59 requestUUID = UUID.fromString(requestID);
60 } catch (IllegalArgumentException e) {
61 requestUUID = UUID.randomUUID();
62 LOGGER.info("Generated Random UUID: " + requestUUID.toString(), e);
65 requestUUID = UUID.randomUUID();
66 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
68 this.policyParameters.setRequestID(requestUUID);
73 }catch(PolicyException e){
74 policyResult = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
75 status = HttpStatus.BAD_REQUEST;
79 public void run() throws PolicyException{
82 LOGGER.error(message);
83 throw new PolicyException(message);
87 status = HttpStatus.OK;
88 policyResult = processResult();
90 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
91 status = HttpStatus.BAD_REQUEST;
92 throw new PolicyException(e);
96 @SuppressWarnings("incomplete-switch")
97 public String processResult() throws PolicyException{
98 String response = null;
99 if(policyParameters.getPolicyConfigType()!=null){
100 // This is a Config Type Policy.
101 switch(policyParameters.getPolicyConfigType()){
103 BRMSParamPolicyService bRMSParamPolicyService = new BRMSParamPolicyService(policyName, policyScope, policyParameters, date);
105 if(!bRMSParamPolicyService.getValidation()){
106 LOGGER.error(bRMSParamPolicyService.getMessage());
107 status = HttpStatus.BAD_REQUEST;
108 return bRMSParamPolicyService.getMessage();
111 response = bRMSParamPolicyService.getResult(updateFlag);
114 BRMSRawPolicyService bRMSRawPolicyService = new BRMSRawPolicyService(policyName, policyScope, policyParameters, date);
116 if(!bRMSRawPolicyService.getValidation()){
117 LOGGER.error(bRMSRawPolicyService.getMessage());
118 status = HttpStatus.BAD_REQUEST;
119 return bRMSRawPolicyService.getMessage();
122 response = bRMSRawPolicyService.getResult(updateFlag);
125 ConfigPolicyService configPolicyService = new ConfigPolicyService(policyName, policyScope, policyParameters, date);
127 if(!configPolicyService.getValidation()){
128 LOGGER.error(configPolicyService.getMessage());
129 status = HttpStatus.BAD_REQUEST;
130 return configPolicyService.getMessage();
133 response = configPolicyService.getResult(updateFlag);
135 case ClosedLoop_Fault:
136 ClosedLoopFaultPolicyService closedLoopFaultPolicyService = new ClosedLoopFaultPolicyService(policyName, policyScope, policyParameters, date);
138 if(!closedLoopFaultPolicyService.getValidation()){
139 LOGGER.error(closedLoopFaultPolicyService.getMessage());
140 status = HttpStatus.BAD_REQUEST;
141 return closedLoopFaultPolicyService.getMessage();
144 response = closedLoopFaultPolicyService.getResult(updateFlag);
147 ClosedLoopPMPolicyService closedLoopPMPolicyService = new ClosedLoopPMPolicyService(policyName, policyScope, policyParameters, date);
149 if(!closedLoopPMPolicyService.getValidation()){
150 LOGGER.error(closedLoopPMPolicyService.getMessage());
151 status = HttpStatus.BAD_REQUEST;
152 return closedLoopPMPolicyService.getMessage();
155 response = closedLoopPMPolicyService.getResult(updateFlag);
158 FirewallPolicyService firewallPolicyService = new FirewallPolicyService(policyName, policyScope, policyParameters, date);
160 if(!firewallPolicyService.getValidation()){
161 LOGGER.error(firewallPolicyService.getMessage());
162 status = HttpStatus.BAD_REQUEST;
163 return firewallPolicyService.getMessage();
166 response = firewallPolicyService.getResult(updateFlag);
169 MicroServicesPolicyService microServicesPolicyService = new MicroServicesPolicyService(policyName, policyScope, policyParameters, date);
171 if(!microServicesPolicyService.getValidation()){
172 LOGGER.error(microServicesPolicyService.getMessage());
173 status = HttpStatus.BAD_REQUEST;
174 return microServicesPolicyService.getMessage();
177 response = microServicesPolicyService.getResult(updateFlag);
180 OptimizationPolicyService optimizationPolicyService = new OptimizationPolicyService(policyName, policyScope, policyParameters, date);
183 response = optimizationPolicyService.getResult(updateFlag);
186 message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " Invalid Config Type Present";
187 LOGGER.error(message);
188 status = HttpStatus.BAD_REQUEST;
191 }else if (policyParameters.getPolicyClass()!=null){
192 switch (policyParameters.getPolicyClass()){
194 ActionPolicyService actionPolicyService = new ActionPolicyService(policyScope, policyName, policyParameters);
196 if(!actionPolicyService.getValidation()){
197 LOGGER.error(actionPolicyService.getMessage());
198 status = HttpStatus.BAD_REQUEST;
199 return actionPolicyService.getMessage();
202 response = actionPolicyService.getResult(updateFlag);
205 DecisionPolicyService decisionPolicyService = new DecisionPolicyService(policyScope, policyName, policyParameters);
207 if(!decisionPolicyService.getValidation()){
208 LOGGER.error(decisionPolicyService.getMessage());
209 status = HttpStatus.BAD_REQUEST;
210 return decisionPolicyService.getMessage();
213 response = decisionPolicyService.getResult(updateFlag);
217 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Class found.";
218 LOGGER.error(message);
219 status = HttpStatus.BAD_REQUEST;
225 protected boolean getValidation() {
227 PolicyValidation validation = new PolicyValidation();
229 StringBuilder responseString;
231 if (policyParameters != null) {
233 if (!Strings.isNullOrEmpty(policyParameters.getPolicyName())){
234 if (policyParameters.getPolicyName().contains(".")) {
235 policyName = policyParameters.getPolicyName().substring(policyParameters.getPolicyName().lastIndexOf('.') + 1,
236 policyParameters.getPolicyName().length());
237 policyScope = policyParameters.getPolicyName().substring(0,policyParameters.getPolicyName().lastIndexOf('.'));
238 policyParameters.setPolicyName(policyName);
239 LOGGER.info("Name is " + policyName + " scope is " + policyScope);
241 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Policy Scope: No Policy Scope given";
242 LOGGER.error("Common validation did not return success: " + message);
246 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "PolicyName: PolicyName Should not be empty";
247 LOGGER.error("Common validation did not return success: " + message);
251 if(policyParameters.getPolicyClass() != null && "Config".equals(policyParameters.getPolicyClass().toString())){
252 String policyConfigType = policyParameters.getPolicyConfigType().toString();
253 if(!"BRMS_Param".equalsIgnoreCase(policyConfigType) && Strings.isNullOrEmpty(policyParameters.getConfigBody())){
254 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "ConfigBody: No Config Body given";
255 LOGGER.error("Common validation did not return success: " + message);
261 PolicyValidationRequestWrapper wrapper = new PolicyValidationRequestWrapper();
262 PolicyRestAdapter policyData = wrapper.populateRequestParameters(policyParameters);
263 if(policyData!=null) {
264 responseString = validation.validatePolicy(policyData);
266 message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper JSON object : " + policyParameters.getConfigBody();
269 } catch (Exception e) {
270 LOGGER.error("Exception Occured during Policy Validation" +e);
271 if(e.getMessage()!=null){
272 if("Action".equals(policyParameters.getPolicyClass().toString()) && e.getMessage().contains("Index:")){
273 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Rule Algorithms: One or more Fields in Rule Algorithms is Empty.";
275 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Exception Occured During Policy Validation: " + e;
281 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy parameters given. ";
285 // Set some default Values.
286 if (policyParameters.getTtlDate()!=null){
287 date = convertDate(policyParameters.getTtlDate());
290 if (responseString!=null){
291 String response = responseString.toString().substring(0, 7);
292 if("success".equals(response)) {
295 message = XACMLErrorConstants.ERROR_DATA_ISSUE + PolicyApiUtils.formatResponse(responseString);
296 LOGGER.error("Common validation did not return success: " + message);
300 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Unknown Error Occured During Policy Validation";
301 LOGGER.error(message);
307 protected String convertDate(Date date) {
308 String strDate = null;
310 SimpleDateFormat dateformatJava = new SimpleDateFormat("dd-MM-yyyy");
311 strDate = dateformatJava.format(date);
313 return (strDate==null) ? "NA": strDate;
316 protected void specialCheck() {
317 if(policyResult== null || policyResult.contains("BAD REQUEST")||policyResult.contains("PE300")){
318 status = HttpStatus.BAD_REQUEST;
319 } else if (policyResult.contains("Policy Exist Error")) {
320 status = HttpStatus.CONFLICT;
321 } else if (policyResult.contains("PE200")||policyResult.contains("PE900")){
322 status = HttpStatus.INTERNAL_SERVER_ERROR;
326 public String getResult() {
330 public HttpStatus getResponseCode() {