[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / CreateUpdateFirewallPolicyService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-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.onap.policy.pdp.rest.api.services;
21
22 import java.text.ParseException;
23 import java.text.SimpleDateFormat;
24
25 import org.onap.policy.api.PolicyClass;
26 import org.onap.policy.api.PolicyConfigType;
27 import org.onap.policy.api.PolicyException;
28 import org.onap.policy.api.PolicyParameters;
29 import org.onap.policy.common.logging.flexlogger.FlexLogger;
30 import org.onap.policy.common.logging.flexlogger.Logger;
31 import org.onap.policy.pdp.rest.api.models.ConfigFirewallPolicyAPIRequest;
32 import org.onap.policy.xacml.api.XACMLErrorConstants;
33 import org.springframework.http.HttpStatus;
34
35 public class CreateUpdateFirewallPolicyService {
36     private static final Logger LOGGER = FlexLogger.getLogger(CreateUpdateFirewallPolicyService.class.getName());
37     
38     private String response = null;
39     private HttpStatus status = HttpStatus.BAD_REQUEST;
40
41     public CreateUpdateFirewallPolicyService(
42             ConfigFirewallPolicyAPIRequest configFirewallPolicyAPIRequest,
43             String requestID, boolean updateFlag) {
44         try{
45             run(configFirewallPolicyAPIRequest, requestID, updateFlag);
46         }catch(PolicyException e){
47             response = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
48             status = HttpStatus.BAD_REQUEST;
49         }
50     }
51
52     private void run(
53             ConfigFirewallPolicyAPIRequest configFirewallPolicyAPIRequest,
54             String requestID, boolean updateFlag) throws PolicyException{
55         PolicyParameters policyParameters = new PolicyParameters();
56         policyParameters.setPolicyClass(PolicyClass.Config);
57         policyParameters.setPolicyConfigType(PolicyConfigType.Firewall);
58         if(configFirewallPolicyAPIRequest.getPolicyScope()==null|| configFirewallPolicyAPIRequest.getPolicyScope().trim().isEmpty()){
59             String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
60             LOGGER.error(message);
61             throw new PolicyException(message);
62         }
63         if(configFirewallPolicyAPIRequest.getPolicyName()==null|| configFirewallPolicyAPIRequest.getPolicyName().trim().isEmpty()){
64             String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
65             LOGGER.error(message);
66             throw new PolicyException(message);
67         }
68         policyParameters.setPolicyName(configFirewallPolicyAPIRequest.getPolicyScope()+"."+configFirewallPolicyAPIRequest.getPolicyName());
69         policyParameters.setConfigBody(configFirewallPolicyAPIRequest.getFirewallJson());
70         policyParameters.setRiskLevel(configFirewallPolicyAPIRequest.getRiskLevel());
71         policyParameters.setRiskType(configFirewallPolicyAPIRequest.getRiskType());
72         policyParameters.setGuard(Boolean.parseBoolean(configFirewallPolicyAPIRequest.getGuard()));
73         try {
74             policyParameters.setTtlDate(new SimpleDateFormat("dd-MM-yyyy").parse(configFirewallPolicyAPIRequest.getTtlDate()));
75         } catch (NullPointerException | ParseException e) {
76             LOGGER.warn("Error Parsing date given " + configFirewallPolicyAPIRequest.getTtlDate(), e);
77             policyParameters.setTtlDate(null);
78         }
79         CreateUpdatePolicyService createUpdatePolicyService = new CreateUpdatePolicyServiceImpl(policyParameters, requestID, updateFlag);
80         status = createUpdatePolicyService.getResponseCode();
81         response = createUpdatePolicyService.getResult();
82     }
83
84     public String getResult() {
85         return response;
86     }
87
88     public HttpStatus getResponseCode() {
89         return status;
90     }
91
92 }