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.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.utils.PolicyUtils;
 
  32 import org.onap.policy.xacml.api.XACMLErrorConstants;
 
  33 import org.springframework.http.HttpStatus;
 
  35 public class CreateUpdatePolicyServiceImpl implements CreateUpdatePolicyService {
 
  36         private static final Logger LOGGER = FlexLogger.getLogger(CreateUpdatePolicyServiceImpl.class.getName());
 
  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;
 
  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()) {
 
  55                     requestUUID = UUID.fromString(requestID);
 
  56                 } catch (IllegalArgumentException e) {
 
  57                     requestUUID = UUID.randomUUID();
 
  58                     LOGGER.info("Generated Random UUID: " + requestUUID.toString(), e);
 
  61                 requestUUID = UUID.randomUUID();
 
  62                 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
 
  64             this.policyParameters.setRequestID(requestUUID);
 
  69         }catch(PolicyException e){
 
  70             policyResult = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
 
  71             status = HttpStatus.BAD_REQUEST;
 
  75     public void run() throws PolicyException{
 
  78             LOGGER.error(message);
 
  79             throw new PolicyException(message);
 
  83             status = HttpStatus.OK;
 
  84             policyResult = processResult();
 
  86             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
 
  87             status = HttpStatus.BAD_REQUEST;
 
  88             throw new PolicyException(e);
 
  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()){
 
  99                 BRMSParamPolicyService bRMSParamPolicyService = new BRMSParamPolicyService(policyName, policyScope, policyParameters, date);
 
 101                 if(!bRMSParamPolicyService.getValidation()){
 
 102                     LOGGER.error(bRMSParamPolicyService.getMessage());
 
 103                     status = HttpStatus.BAD_REQUEST;
 
 104                     return bRMSParamPolicyService.getMessage();
 
 107                 response = bRMSParamPolicyService.getResult(updateFlag);
 
 110                 BRMSRawPolicyService bRMSRawPolicyService = new BRMSRawPolicyService(policyName, policyScope, policyParameters, date);
 
 112                 if(!bRMSRawPolicyService.getValidation()){
 
 113                     LOGGER.error(bRMSRawPolicyService.getMessage());
 
 114                     status = HttpStatus.BAD_REQUEST;
 
 115                     return bRMSRawPolicyService.getMessage();
 
 118                 response = bRMSRawPolicyService.getResult(updateFlag);
 
 121                 ConfigPolicyService configPolicyService = new ConfigPolicyService(policyName, policyScope, policyParameters, date);
 
 123                 if(!configPolicyService.getValidation()){
 
 124                     LOGGER.error(configPolicyService.getMessage());
 
 125                     status = HttpStatus.BAD_REQUEST;
 
 126                     return configPolicyService.getMessage();
 
 129                 response = configPolicyService.getResult(updateFlag);
 
 131             case ClosedLoop_Fault:
 
 132                 ClosedLoopFaultPolicyService closedLoopFaultPolicyService = new ClosedLoopFaultPolicyService(policyName, policyScope, policyParameters, date);
 
 134                 if(!closedLoopFaultPolicyService.getValidation()){
 
 135                     LOGGER.error(closedLoopFaultPolicyService.getMessage());
 
 136                     status = HttpStatus.BAD_REQUEST;
 
 137                     return closedLoopFaultPolicyService.getMessage();
 
 140                 response = closedLoopFaultPolicyService.getResult(updateFlag);
 
 143                 ClosedLoopPMPolicyService closedLoopPMPolicyService = new ClosedLoopPMPolicyService(policyName, policyScope, policyParameters, date);
 
 145                 if(!closedLoopPMPolicyService.getValidation()){
 
 146                     LOGGER.error(closedLoopPMPolicyService.getMessage());
 
 147                     status = HttpStatus.BAD_REQUEST;
 
 148                     return closedLoopPMPolicyService.getMessage();
 
 151                 response = closedLoopPMPolicyService.getResult(updateFlag);
 
 154                 FirewallPolicyService firewallPolicyService = new FirewallPolicyService(policyName, policyScope, policyParameters, date);
 
 156                 if(!firewallPolicyService.getValidation()){
 
 157                     LOGGER.error(firewallPolicyService.getMessage());
 
 158                     status = HttpStatus.BAD_REQUEST;
 
 159                     return firewallPolicyService.getMessage();
 
 162                 response = firewallPolicyService.getResult(updateFlag);
 
 165                 MicroServicesPolicyService microServicesPolicyService = new MicroServicesPolicyService(policyName, policyScope, policyParameters, date);
 
 167                 if(!microServicesPolicyService.getValidation()){
 
 168                     LOGGER.error(microServicesPolicyService.getMessage());
 
 169                     status = HttpStatus.BAD_REQUEST;
 
 170                     return microServicesPolicyService.getMessage();
 
 173                 response = microServicesPolicyService.getResult(updateFlag);
 
 176                 String message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " Invalid Config Type Present";
 
 177                 LOGGER.error(message);
 
 178                 status = HttpStatus.BAD_REQUEST;
 
 181         }else if (policyParameters.getPolicyClass()!=null){
 
 182             switch (policyParameters.getPolicyClass()){
 
 184                 ActionPolicyService actionPolicyService = new ActionPolicyService(policyScope, policyName, policyParameters);
 
 186                 if(!actionPolicyService.getValidation()){
 
 187                     LOGGER.error(actionPolicyService.getMessage());
 
 188                     status = HttpStatus.BAD_REQUEST;
 
 189                     return actionPolicyService.getMessage();
 
 192                 response = actionPolicyService.getResult(updateFlag);
 
 195                 DecisionPolicyService decisionPolicyService = new DecisionPolicyService(policyScope, policyName, policyParameters);
 
 197                 if(!decisionPolicyService.getValidation()){
 
 198                     LOGGER.error(decisionPolicyService.getMessage());
 
 199                     status = HttpStatus.BAD_REQUEST;
 
 200                     return decisionPolicyService.getMessage();
 
 203                 response = decisionPolicyService.getResult(updateFlag);
 
 207             String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Class found.";
 
 208             LOGGER.error(message);
 
 209             status = HttpStatus.BAD_REQUEST;
 
 215     protected boolean getValidation() {
 
 216         if(policyParameters == null){
 
 217             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy parameters given. ";
 
 220         if(policyParameters.getPolicyName() == null){
 
 221             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
 
 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);
 
 230             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
 
 233         if (policyName==null||policyName.trim().isEmpty()){
 
 234             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
 
 237         message = PolicyUtils.policySpecialCharValidator(policyScope);
 
 238         if(!message.contains("success")){
 
 239             message = XACMLErrorConstants.ERROR_DATA_ISSUE+ message;
 
 242         message = PolicyUtils.policySpecialCharValidator(policyName);
 
 243         if(!message.contains("success")){
 
 244             message = XACMLErrorConstants.ERROR_DATA_ISSUE+ message;
 
 247         if(policyParameters.getPolicyDescription()!=null){
 
 248             message = PolicyUtils.descriptionValidator(policyParameters.getPolicyDescription());
 
 249             if(!message.contains("success")){
 
 250                 message = XACMLErrorConstants.ERROR_DATA_ISSUE+ message;
 
 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.";
 
 259         // Set some default Values. 
 
 260         if (policyParameters.getTtlDate()!=null){
 
 261             date = convertDate(policyParameters.getTtlDate());
 
 266     protected String convertDate(Date date) {
 
 267         String strDate = null;
 
 269             SimpleDateFormat dateformatJava = new SimpleDateFormat("dd-MM-yyyy");
 
 270             strDate = dateformatJava.format(date);
 
 272         return (strDate==null) ? "NA": strDate;
 
 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;
 
 285     public String getResult() {
 
 289     public HttpStatus getResponseCode() {