2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pdp.rest.api.services;
23 import javax.json.JsonException;
24 import javax.json.JsonObject;
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;
36 * Firewall Policy Implementation.
40 public class FirewallPolicyService {
41 private static final Logger LOGGER = FlexLogger.getLogger(FirewallPolicyService.class.getName());
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;
51 public FirewallPolicyService(String policyName, String policyScope,
52 PolicyParameters policyParameters, String date) {
53 this.policyParameters = policyParameters;
54 this.policyName = policyName;
55 this.policyScope = policyScope;
57 papServices = new PAPServices();
60 public Boolean getValidation() {
61 if (policyParameters.getConfigBody() == null) {
62 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Body given.";
66 firewallJson = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody());
67 } catch (JsonException | IllegalStateException e) {
69 XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " +
70 policyParameters.getConfigBody();
71 LOGGER.error("Error while parsing JSON body for creating Firewall Policy ", e);
74 if (firewallJson == null || firewallJson.isEmpty()) {
75 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config-Body given.";
78 boolean levelCheck = false;
79 levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
81 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
87 public String getMessage() {
91 public String getResult(boolean updateFlag) throws PolicyException {
92 String response = null;
93 String operation = null;
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);
105 String configName = firewallJson.get("configName").toString();
106 String configDescription = "";
107 String json = firewallJson.toString();
109 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
110 .configPolicyType("Firewall Config")
111 .policyName(policyName)
112 .description(configDescription)
113 .configName(configName)
114 .editPolicy(updateFlag)
118 .riskLevel(policyParameters.getRiskLevel())
119 .riskType(policyParameters.getRiskType())
120 .guard(String.valueOf(policyParameters.getGuard()))
124 response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
125 "policyType=Config"}, policyParameters.getRequestID(), "ConfigFirewall");
126 LOGGER.info(response);