Commit includes ControlLoopPolicy API and bugfixes
[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 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         policyParameters.setConfigBodyType(PolicyType.valueOf(configPolicyAPIRequest.getConfigType()));
79         policyParameters.setConfigBody(configPolicyAPIRequest.getBody());
80         policyParameters.setRiskLevel(configPolicyAPIRequest.getRiskLevel());
81         policyParameters.setRiskType(configPolicyAPIRequest.getRiskType());
82         policyParameters.setGuard(Boolean.parseBoolean(configPolicyAPIRequest.getGuard()));
83         try {
84             policyParameters.setTtlDate(new SimpleDateFormat("dd-MM-yyyy").parse(configPolicyAPIRequest.getTtlDate()));
85         } catch (ParseException e) {
86             LOGGER.warn("Error Parsing date given " + configPolicyAPIRequest.getTtlDate());
87             policyParameters.setTtlDate(null);
88         }
89         CreateUpdatePolicyService createUpdatePolicyService = new CreateUpdatePolicyServiceImpl(policyParameters, requestID, updateFlag);
90         status = createUpdatePolicyService.getResponseCode();
91         response = createUpdatePolicyService.getResult();
92     }
93
94     public String getResult() {
95         return response;
96     }
97
98     public HttpStatus getResponseCode() {
99         return status;
100     }
101
102 }