2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.policy.pdp.rest.api.services;
23 import java.util.UUID;
25 import org.openecomp.policy.api.DeletePolicyParameters;
26 import org.openecomp.policy.api.PolicyException;
27 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
28 import org.openecomp.policy.common.logging.flexlogger.Logger;
29 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
30 import org.openecomp.policy.xacml.std.pap.StdPAPPolicy;
31 import org.springframework.http.HttpStatus;
33 public class DeletePolicyService {
34 private static Logger LOGGER = FlexLogger.getLogger(DeletePolicyService.class.getName());
36 private String deleteResult = null;
37 private HttpStatus status = HttpStatus.BAD_REQUEST;
38 private DeletePolicyParameters deletePolicyParameters = null;
39 private String message = null;
40 private String filePrefix = null;
41 private String clientScope = null;
42 private String policyType = null;
43 private String policyName = null;
44 private String policyScope = null;
46 public DeletePolicyService(DeletePolicyParameters deletePolicyParameters,
48 this.deletePolicyParameters = deletePolicyParameters;
49 if(deletePolicyParameters.getRequestID()==null){
50 UUID requestUUID = null;
51 if (requestID != null && !requestID.isEmpty()) {
53 requestUUID = UUID.fromString(requestID);
54 } catch (IllegalArgumentException e) {
55 requestUUID = UUID.randomUUID();
56 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
59 requestUUID = UUID.randomUUID();
60 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
62 this.deletePolicyParameters.setRequestID(requestUUID);
67 }catch(PolicyException e){
68 deleteResult = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
69 status = HttpStatus.BAD_REQUEST;
73 private void specialCheck() {
74 if(deleteResult==null){
77 if (deleteResult.contains("BAD REQUEST")||deleteResult.contains("PE300")||deleteResult.contains("not exist")||deleteResult.contains("Invalid policyName")) {
78 status = HttpStatus.BAD_REQUEST;
79 } else if (deleteResult.contains("locked down")){
80 status = HttpStatus.ACCEPTED;
81 } else if (deleteResult.contains("not Authorized")) {
82 status = HttpStatus.FORBIDDEN;
83 } else if (deleteResult.contains("groupId")) {
84 status = HttpStatus.NOT_FOUND;
85 } else if (deleteResult.contains("JPAUtils")||deleteResult.contains("database")||deleteResult.contains("policy file")||
86 deleteResult.contains("unknown")||deleteResult.contains("configuration")) {
87 status = HttpStatus.INTERNAL_SERVER_ERROR;
91 private void run() throws PolicyException{
94 LOGGER.error(message);
95 throw new PolicyException(message);
99 status = HttpStatus.OK;
100 deleteResult = processResult();
101 }catch (Exception e){
102 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
103 status = HttpStatus.BAD_REQUEST;
104 throw new PolicyException(e);
108 private String processResult() throws PolicyException{
109 String response = null;
110 String fullPolicyName = null;
111 String pdpGroup = deletePolicyParameters.getPdpGroup();
116 PAPServices papServices = new PAPServices();
117 if (!deletePolicyParameters.getPolicyName().contains("xml")) {
119 String activeVersion = papServices.getActiveVersion(policyScope, filePrefix, policyName, clientScope, deletePolicyParameters.getRequestID());
120 LOGGER.debug("The active version of " + policyScope + File.separator + filePrefix + policyName + " is " + activeVersion);
122 if ("pe100".equalsIgnoreCase(activeVersion)) {
123 response = XACMLErrorConstants.ERROR_PERMISSIONS + "response code of the URL is 403. PEP is not Authorized for making this Request!! "
124 + "Contact Administrator for this Scope. ";
125 LOGGER.error(response);
127 } else if ("pe300".equalsIgnoreCase(activeVersion)) {
128 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "response code of the URL is 404. "
129 + "This indicates a problem with getting the version from the PAP or the policy does not exist.";
130 LOGGER.error(response);
133 if (!activeVersion.equalsIgnoreCase("0")) {
134 id = policyScope + "." + filePrefix + policyName + "." + activeVersion + ".xml";
135 LOGGER.debug("The policyId is " + id);
137 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "could not retrieve the activeVersion for this policy. could not retrieve the activeVersion for this policy. "
138 + "This indicates the policy does not exist, please verify the policy exists.";
139 LOGGER.error(response);
143 fullPolicyName = policyScope + "." + filePrefix + policyName + "." + activeVersion + ".xml";
146 fullPolicyName = policyName;
149 if ("PAP".equalsIgnoreCase(deletePolicyParameters.getPolicyComponent())) {
150 if (deletePolicyParameters.getDeleteCondition()==null||deletePolicyParameters.getDeleteCondition().toString().trim().isEmpty()){
151 String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Delete Condition given.";
152 LOGGER.error(message);
156 StdPAPPolicy deletePapPolicy = new StdPAPPolicy(fullPolicyName, deletePolicyParameters.getDeleteCondition().toString());
157 //send JSON object to PAP
158 response = (String) papServices.callPAP(deletePapPolicy, new String[] {"groupId="+pdpGroup, "apiflag=deletePapApi", "operation=delete" }, deletePolicyParameters.getRequestID(), clientScope);
159 } else if ("PDP".equalsIgnoreCase(deletePolicyParameters.getPolicyComponent())) {
160 if (deletePolicyParameters.getPdpGroup()==null||deletePolicyParameters.getPdpGroup().trim().isEmpty()){
161 String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No PDP Group given.";
162 LOGGER.error(message);
165 //send JSON object to PAP
166 response = (String) papServices.callPAP(null, new String[] {"policyName="+fullPolicyName, "groupId="+pdpGroup, "apiflag=deletePdpApi", "operation=delete" }, deletePolicyParameters.getRequestID(), clientScope);
168 String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Policy Component does not exist. Please enter either PAP or PDP to delete the policy from a specified Policy Component.";
169 LOGGER.error(message);
175 private boolean getValidation() {
176 // While Validating, extract the required values.
177 if (!deletePolicyParameters.getPolicyName().contains("xml")) {
178 if (deletePolicyParameters.getPolicyName() != null
179 && deletePolicyParameters.getPolicyName().contains(".")) {
180 policyName = deletePolicyParameters.getPolicyName().substring(deletePolicyParameters.getPolicyName().lastIndexOf(".") + 1,
181 deletePolicyParameters.getPolicyName().length());
182 policyScope = deletePolicyParameters.getPolicyName().substring(0,deletePolicyParameters.getPolicyName().lastIndexOf("."));
183 LOGGER.info("Name is " + policyName + " scope is " + policyScope);
185 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
189 policyName = deletePolicyParameters.getPolicyName();
191 if (deletePolicyParameters.getPolicyName()==null||deletePolicyParameters.getPolicyName().trim().isEmpty()){
192 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
195 policyType = deletePolicyParameters.getPolicyType();
196 if(policyType== null || policyType.trim().isEmpty()){
197 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No PolicyType given.";
201 if (clientScope==null||clientScope.trim().isEmpty()){
202 message = XACMLErrorConstants.ERROR_DATA_ISSUE + deletePolicyParameters.getPolicyType() + " is not a valid Policy Type.";
203 LOGGER.error(message);
206 LOGGER.debug("clientScope is " + clientScope);
207 LOGGER.debug("filePrefix is " + filePrefix);
208 if(deletePolicyParameters.getPolicyComponent()==null){
209 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Component given.";
215 private void setClientScope() {
216 if ("Firewall".equalsIgnoreCase(policyType)) {
217 clientScope = "ConfigFirewall";
218 filePrefix = "Config_FW_";
219 } else if ("Action".equalsIgnoreCase(policyType)) {
220 clientScope = "Action";
221 filePrefix = "Action_";
222 } else if ("Decision".equalsIgnoreCase(policyType)) {
223 clientScope = "Decision";
224 filePrefix = "Decision_";
225 } else if ("Base".equalsIgnoreCase(policyType)) {
226 clientScope = "Config";
227 filePrefix = "Config_";
228 } else if ("ClosedLoop_Fault".equalsIgnoreCase(policyType)) {
229 clientScope = "ConfigClosedLoop";
230 filePrefix = "Config_Fault_";
231 } else if ("ClosedLoop_PM".equalsIgnoreCase(policyType)) {
232 clientScope = "ConfigClosedLoop";
233 filePrefix = "Config_PM_";
234 } else if ("MicroService".equalsIgnoreCase(policyType)) {
235 clientScope = "ConfigMS";
236 filePrefix = "Config_MS_";
237 }else if ("BRMS_RAW".equalsIgnoreCase(policyType)) {
238 clientScope = "ConfigBrmsRaw";
239 filePrefix = "Config_BRMS_Raw_";
240 } else if ("BRMS_PARAM".equalsIgnoreCase(policyType)) {
241 clientScope = "ConfigBrmsParam";
242 filePrefix = "Config_BRMS_Param_";
245 message = XACMLErrorConstants.ERROR_DATA_ISSUE + policyType
246 + " is not a valid Policy Type.";
250 public String getResult() {
254 public HttpStatus getResponseCode() {