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.ParseException;
23 import java.text.SimpleDateFormat;
24 import java.util.HashMap;
27 import org.onap.policy.api.AttributeType;
28 import org.onap.policy.api.PolicyClass;
29 import org.onap.policy.api.PolicyConfigType;
30 import org.onap.policy.api.PolicyException;
31 import org.onap.policy.api.PolicyParameters;
32 import org.onap.policy.api.PolicyType;
33 import org.onap.policy.common.logging.flexlogger.FlexLogger;
34 import org.onap.policy.common.logging.flexlogger.Logger;
35 import org.onap.policy.pdp.rest.api.models.ConfigPolicyAPIRequest;
36 import org.onap.policy.xacml.api.XACMLErrorConstants;
37 import org.springframework.http.HttpStatus;
39 public class CreateUpdateConfigPolicyService {
40 private static final Logger LOGGER = FlexLogger.getLogger(CreateUpdateConfigPolicyService.class.getName());
42 private String response = null;
43 private HttpStatus status = HttpStatus.BAD_REQUEST;
45 public CreateUpdateConfigPolicyService(
46 ConfigPolicyAPIRequest configPolicyAPIRequest, String requestID,
49 run(configPolicyAPIRequest, requestID, updateFlag);
50 }catch(PolicyException e){
51 response = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
52 status = HttpStatus.BAD_REQUEST;
56 private void run(ConfigPolicyAPIRequest configPolicyAPIRequest,
57 String requestID, boolean updateFlag) throws PolicyException{
58 PolicyParameters policyParameters = new PolicyParameters();
59 policyParameters.setPolicyClass(PolicyClass.Config);
60 policyParameters.setPolicyConfigType(PolicyConfigType.Base);
61 if(configPolicyAPIRequest.getPolicyScope()==null|| configPolicyAPIRequest.getPolicyScope().trim().isEmpty()){
62 String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
63 LOGGER.error(message);
64 throw new PolicyException(message);
66 if(configPolicyAPIRequest.getPolicyName()==null|| configPolicyAPIRequest.getPolicyName().trim().isEmpty()){
67 String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
68 LOGGER.error(message);
69 throw new PolicyException(message);
71 policyParameters.setPolicyName(configPolicyAPIRequest.getPolicyScope()+"."+configPolicyAPIRequest.getPolicyName());
72 policyParameters.setPolicyDescription(configPolicyAPIRequest.getPolicyDescription());
73 policyParameters.setOnapName(configPolicyAPIRequest.getOnapName());
74 policyParameters.setConfigName(configPolicyAPIRequest.getConfigName());
75 Map<AttributeType, Map<String, String>> attributes = new HashMap<>();
76 attributes.put(AttributeType.MATCHING, configPolicyAPIRequest.getConfigAttributes());
77 policyParameters.setAttributes(attributes);
78 if(configPolicyAPIRequest.getConfigType()==null){
79 String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy ConfigType given.";
80 LOGGER.error(message);
81 throw new PolicyException(message);
84 policyParameters.setConfigBodyType(PolicyType.valueOf(configPolicyAPIRequest.getConfigType()));
85 }catch(IllegalArgumentException e){
86 String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Improper ConfigType given.";
87 LOGGER.error(message, e);
88 throw new PolicyException(message);
90 policyParameters.setConfigBody(configPolicyAPIRequest.getBody());
91 policyParameters.setRiskLevel(configPolicyAPIRequest.getRiskLevel());
92 policyParameters.setRiskType(configPolicyAPIRequest.getRiskType());
93 policyParameters.setGuard(Boolean.parseBoolean(configPolicyAPIRequest.getGuard()));
94 if(configPolicyAPIRequest.getTtlDate()==null){
95 LOGGER.warn("No TTL date given ");
96 policyParameters.setTtlDate(null);
99 policyParameters.setTtlDate(new SimpleDateFormat("dd-MM-yyyy").parse(configPolicyAPIRequest.getTtlDate()));
100 } catch (ParseException e) {
101 LOGGER.warn("Error Parsing date given " + configPolicyAPIRequest.getTtlDate(), e);
102 policyParameters.setTtlDate(null);
105 CreateUpdatePolicyService createUpdatePolicyService = new CreateUpdatePolicyServiceImpl(policyParameters, requestID, updateFlag);
106 status = createUpdatePolicyService.getResponseCode();
107 response = createUpdatePolicyService.getResult();
110 public String getResult() {
114 public HttpStatus getResponseCode() {