[POLICY-122] Policy GUI Fixes
[policy/engine.git] / ECOMP-PDP-REST / src / main / java / org / openecomp / policy / pdp / rest / api / services / CreateUpdateConfigPolicyService.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.ParseException;
23 import java.text.SimpleDateFormat;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.openecomp.policy.api.AttributeType;
28 import org.openecomp.policy.api.PolicyClass;
29 import org.openecomp.policy.api.PolicyConfigType;
30 import org.openecomp.policy.api.PolicyException;
31 import org.openecomp.policy.api.PolicyParameters;
32 import org.openecomp.policy.api.PolicyType;
33 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
34 import org.openecomp.policy.common.logging.flexlogger.Logger;
35 import org.openecomp.policy.pdp.rest.api.models.ConfigPolicyAPIRequest;
36 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
37 import org.springframework.http.HttpStatus;
38
39 public class CreateUpdateConfigPolicyService {
40     private static final Logger LOGGER = FlexLogger.getLogger(CreateUpdateConfigPolicyService.class.getName());
41     
42     private String response = null;
43     private HttpStatus status = HttpStatus.BAD_REQUEST;
44
45     public CreateUpdateConfigPolicyService(
46             ConfigPolicyAPIRequest configPolicyAPIRequest, String requestID,
47             boolean updateFlag) {
48         try{
49             run(configPolicyAPIRequest, requestID, updateFlag);
50         }catch(PolicyException e){
51             response = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
52             status = HttpStatus.BAD_REQUEST;
53         }
54     }
55
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);
65         }
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);
70         }
71         policyParameters.setPolicyName(configPolicyAPIRequest.getPolicyScope()+"."+configPolicyAPIRequest.getPolicyName());
72         policyParameters.setPolicyDescription(configPolicyAPIRequest.getPolicyDescription());
73         policyParameters.setEcompName(configPolicyAPIRequest.getEcompName());
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);
82         }
83         try{
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);
89         }
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);
97         }else{
98                 try {
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);
103             }
104         }
105         CreateUpdatePolicyService createUpdatePolicyService = new CreateUpdatePolicyServiceImpl(policyParameters, requestID, updateFlag);
106         status = createUpdatePolicyService.getResponseCode();
107         response = createUpdatePolicyService.getResult();
108     }
109
110     public String getResult() {
111         return response;
112     }
113
114     public HttpStatus getResponseCode() {
115         return status;
116     }
117
118 }