Optimize PAP policy constructor with builder
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / FirewallPolicyService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.policy.pdp.rest.api.services;
22
23 import javax.json.JsonException;
24 import javax.json.JsonObject;
25
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.xacml.api.XACMLErrorConstants;
32 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
33 import org.onap.policy.xacml.std.pap.StdPAPPolicyParams;
34
35 /**
36  * Firewall Policy Implementation.
37  *
38  * @version 0.1
39  */
40 public class FirewallPolicyService {
41     private static final Logger LOGGER = FlexLogger.getLogger(FirewallPolicyService.class.getName());
42
43     private PAPServices papServices = null;
44     private PolicyParameters policyParameters = null;
45     private String message = null;
46     private String policyName = null;
47     private String policyScope = null;
48     private String date = null;
49     private JsonObject firewallJson = null;
50
51     public FirewallPolicyService(String policyName, String policyScope,
52                                  PolicyParameters policyParameters, String date) {
53         this.policyParameters = policyParameters;
54         this.policyName = policyName;
55         this.policyScope = policyScope;
56         this.date = date;
57         papServices = new PAPServices();
58     }
59
60     public Boolean getValidation() {
61         if (policyParameters.getConfigBody() == null) {
62             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Body given.";
63             return false;
64         }
65         try {
66             firewallJson = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody());
67         } catch (JsonException | IllegalStateException e) {
68             message =
69                     XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " +
70                             policyParameters.getConfigBody();
71             LOGGER.error("Error while parsing JSON body for creating Firewall Policy ", e);
72             return false;
73         }
74         if (firewallJson == null || firewallJson.isEmpty()) {
75             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config-Body given.";
76             return false;
77         }
78         boolean levelCheck = false;
79         levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
80         if (!levelCheck) {
81             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
82             return false;
83         }
84         return true;
85     }
86
87     public String getMessage() {
88         return message;
89     }
90
91     public String getResult(boolean updateFlag) throws PolicyException {
92         String response = null;
93         String operation = null;
94         if (updateFlag) {
95             operation = "update";
96         } else {
97             operation = "create";
98         }
99         //set values for basic policy information
100         if (!firewallJson.containsKey("configName")) {
101             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No configName given in firwall JSON.";
102             LOGGER.error(message);
103             return message;
104         }
105         String configName = firewallJson.get("configName").toString();
106         String configDescription = "";
107         String json = firewallJson.toString();
108         // Create Policy.
109         StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
110                 .configPolicyType("Firewall Config")
111                 .policyName(policyName)
112                 .description(configDescription)
113                 .configName(configName)
114                 .editPolicy(updateFlag)
115                 .domain(policyScope)
116                 .jsonBody(json)
117                 .highestVersion(0)
118                 .riskLevel(policyParameters.getRiskLevel())
119                 .riskType(policyParameters.getRiskType())
120                 .guard(String.valueOf(policyParameters.getGuard()))
121                 .ttlDate(date)
122                 .build());
123         // Send Json to PAP.
124         response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
125                 "policyType=Config"}, policyParameters.getRequestID(), "ConfigFirewall");
126         LOGGER.info(response);
127         return response;
128     }
129
130 }