2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Modifications 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;
24 import java.util.UUID;
26 import org.onap.policy.api.DeletePolicyParameters;
27 import org.onap.policy.api.PolicyException;
28 import org.onap.policy.common.logging.flexlogger.FlexLogger;
29 import org.onap.policy.common.logging.flexlogger.Logger;
30 import org.onap.policy.xacml.api.XACMLErrorConstants;
31 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
32 import org.onap.policy.xacml.std.pap.StdPAPPolicyParams;
33 import org.springframework.http.HttpStatus;
35 public class DeletePolicyService {
36 private static final Logger LOGGER = FlexLogger.getLogger(DeletePolicyService.class.getName());
38 private String deleteResult = null;
39 private HttpStatus status = HttpStatus.BAD_REQUEST;
40 private DeletePolicyParameters deletePolicyParameters = null;
41 private String message = null;
42 private String filePrefix = null;
43 private String clientScope = null;
44 private String policyType = null;
45 private String policyName = null;
46 private String policyScope = null;
48 public DeletePolicyService(final DeletePolicyParameters deletePolicyParameters, final String requestID) {
49 this.deletePolicyParameters = deletePolicyParameters;
50 if (deletePolicyParameters.getRequestID() == null) {
51 UUID requestUUID = null;
52 if (requestID != null && !requestID.isEmpty()) {
54 requestUUID = UUID.fromString(requestID);
55 } catch (final IllegalArgumentException e) {
56 requestUUID = UUID.randomUUID();
57 LOGGER.info("Generated Random UUID: " + requestUUID.toString(), e);
60 requestUUID = UUID.randomUUID();
61 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
63 this.deletePolicyParameters.setRequestID(requestUUID);
68 } catch (final PolicyException e) {
69 LOGGER.error("Unable to process delete policy request for : " + this.deletePolicyParameters);
70 deleteResult = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
71 status = HttpStatus.BAD_REQUEST;
75 private void specialCheck() {
76 if (deleteResult == null) {
79 if (deleteResult.contains("BAD REQUEST") || deleteResult.contains("PE300") || deleteResult.contains("PE200")
80 || deleteResult.contains("not exist") || deleteResult.contains("Invalid policyName")) {
81 if (deleteResult.contains("groupId")) {
82 status = HttpStatus.NOT_FOUND;
84 status = HttpStatus.BAD_REQUEST;
86 } else if (deleteResult.contains("locked down")) {
87 status = HttpStatus.ACCEPTED;
88 } else if (deleteResult.contains("not Authorized")) {
89 status = HttpStatus.FORBIDDEN;
90 } else if (deleteResult.contains("JPAUtils") || deleteResult.contains("database")
91 || deleteResult.contains("policy file") || deleteResult.contains("unknown")
92 || deleteResult.contains("configuration")) {
93 status = HttpStatus.INTERNAL_SERVER_ERROR;
97 private void run() throws PolicyException {
99 if (!getValidation()) {
100 LOGGER.error(message);
101 throw new PolicyException(message);
105 LOGGER.debug("Processing delete request: " + deletePolicyParameters.toString());
106 status = HttpStatus.OK;
107 deleteResult = processResult();
108 } catch (final Exception e) {
109 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
110 status = HttpStatus.BAD_REQUEST;
111 throw new PolicyException(e);
115 private String processResult() throws PolicyException {
116 String response = null;
117 String fullPolicyName = null;
118 String pdpGroup = deletePolicyParameters.getPdpGroup();
120 if (pdpGroup == null) {
123 final PAPServices papServices = new PAPServices();
124 if (!deletePolicyParameters.getPolicyName().contains("xml")) {
126 final String activeVersion = papServices.getActiveVersion(policyScope, filePrefix, policyName, clientScope,
127 deletePolicyParameters.getRequestID());
128 LOGGER.debug("The active version of " + policyScope + File.separator + filePrefix + policyName + " is "
131 if ("pe100".equalsIgnoreCase(activeVersion)) {
132 response = XACMLErrorConstants.ERROR_PERMISSIONS
133 + "response code of the URL is 403. PEP is not Authorized for making this Request!! "
134 + "Contact Administrator for this Scope. ";
135 LOGGER.error(response);
137 } else if ("pe300".equalsIgnoreCase(activeVersion)) {
138 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "response code of the URL is 404. "
139 + "This indicates a problem with getting the version from the PAP or the policy does not exist.";
140 LOGGER.error(response);
143 if (!"0".equalsIgnoreCase(activeVersion)) {
144 id = policyScope + "." + filePrefix + policyName + "." + activeVersion + ".xml";
145 LOGGER.debug("The policyId is " + id);
147 response = XACMLErrorConstants.ERROR_DATA_ISSUE
148 + "could not retrieve the activeVersion for this policy. could not retrieve the activeVersion for this policy. "
149 + "This indicates the policy does not exist, please verify the policy exists.";
150 LOGGER.error(response);
154 fullPolicyName = policyScope + "." + filePrefix + policyName + "." + activeVersion + ".xml";
157 fullPolicyName = policyName;
160 if ("PAP".equalsIgnoreCase(deletePolicyParameters.getPolicyComponent())) {
161 if (deletePolicyParameters.getDeleteCondition() == null
162 || deletePolicyParameters.getDeleteCondition().toString().trim().isEmpty()) {
163 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Delete Condition given.";
164 LOGGER.error(message);
167 // for deleting policies from the API
168 final StdPAPPolicy deletePapPolicy =
169 new StdPAPPolicy(StdPAPPolicyParams.builder()
170 .policyName(fullPolicyName)
171 .deleteCondition(deletePolicyParameters.getDeleteCondition().toString())
173 // send JSON object to PAP
174 response = (String) papServices.callPAP(deletePapPolicy,
175 new String[] {"groupId=" + pdpGroup, "apiflag=deletePapApi", "operation=delete"},
176 deletePolicyParameters.getRequestID(), clientScope);
177 } else if ("PDP".equalsIgnoreCase(deletePolicyParameters.getPolicyComponent())) {
178 if (deletePolicyParameters.getPdpGroup() == null || deletePolicyParameters.getPdpGroup().trim().isEmpty()) {
179 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No PDP Group given.";
180 LOGGER.error(message);
183 // send JSON object to PAP
185 (String) papServices.callPAP(
186 null, new String[] {"policyName=" + fullPolicyName, "groupId=" + pdpGroup,
187 "apiflag=deletePdpApi", "operation=delete"},
188 deletePolicyParameters.getRequestID(), clientScope);
190 message = XACMLErrorConstants.ERROR_DATA_ISSUE
191 + "Policy Component does not exist. Please enter either PAP or PDP to delete the policy from a specified Policy Component.";
192 LOGGER.error(message);
198 private boolean getValidation() {
199 // While Validating, extract the required values.
200 if (deletePolicyParameters.getPolicyName() == null || deletePolicyParameters.getPolicyName().trim().isEmpty()) {
201 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
204 if (!deletePolicyParameters.getPolicyName().contains("xml")) {
205 if (deletePolicyParameters.getPolicyName() != null
206 && deletePolicyParameters.getPolicyName().contains(".")) {
207 policyName = deletePolicyParameters.getPolicyName().substring(
208 deletePolicyParameters.getPolicyName().lastIndexOf('.') + 1,
209 deletePolicyParameters.getPolicyName().length());
210 policyScope = deletePolicyParameters.getPolicyName().substring(0,
211 deletePolicyParameters.getPolicyName().lastIndexOf('.'));
212 LOGGER.info("Name is " + policyName + " scope is " + policyScope);
214 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
218 policyName = deletePolicyParameters.getPolicyName();
220 policyType = deletePolicyParameters.getPolicyType();
221 if (policyType == null || policyType.trim().isEmpty()) {
222 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No PolicyType given.";
226 if (clientScope == null || clientScope.trim().isEmpty()) {
227 message = XACMLErrorConstants.ERROR_DATA_ISSUE + deletePolicyParameters.getPolicyType()
228 + " is not a valid Policy Type.";
229 LOGGER.error(message);
232 LOGGER.debug("clientScope is " + clientScope);
233 LOGGER.debug("filePrefix is " + filePrefix);
234 if (deletePolicyParameters.getPolicyComponent() == null) {
235 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Component given.";
241 private void setClientScope() {
242 if ("Firewall".equalsIgnoreCase(policyType)) {
243 clientScope = "ConfigFirewall";
244 filePrefix = "Config_FW_";
245 } else if ("Action".equalsIgnoreCase(policyType)) {
246 clientScope = "Action";
247 filePrefix = "Action_";
248 } else if ("Decision".equalsIgnoreCase(policyType)) {
249 clientScope = "Decision";
250 filePrefix = "Decision_";
251 } else if ("Base".equalsIgnoreCase(policyType)) {
252 clientScope = "Config";
253 filePrefix = "Config_";
254 } else if ("ClosedLoop_Fault".equalsIgnoreCase(policyType)) {
255 clientScope = "ConfigClosedLoop";
256 filePrefix = "Config_Fault_";
257 } else if ("ClosedLoop_PM".equalsIgnoreCase(policyType)) {
258 clientScope = "ConfigClosedLoop";
259 filePrefix = "Config_PM_";
260 } else if ("MicroService".equalsIgnoreCase(policyType)) {
261 clientScope = "ConfigMS";
262 filePrefix = "Config_MS_";
263 } else if ("Optimization".equalsIgnoreCase(policyType)) {
264 clientScope = "ConfigOptimization";
265 filePrefix = "Config_OOF_";
266 } else if ("BRMS_RAW".equalsIgnoreCase(policyType)) {
267 clientScope = "ConfigBrmsRaw";
268 filePrefix = "Config_BRMS_Raw_";
269 } else if ("BRMS_PARAM".equalsIgnoreCase(policyType)) {
270 clientScope = "ConfigBrmsParam";
271 filePrefix = "Config_BRMS_Param_";
274 message = XACMLErrorConstants.ERROR_DATA_ISSUE + policyType + " is not a valid Policy Type.";
278 public String getResult() {
282 public HttpStatus getResponseCode() {