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;
22 import java.util.UUID;
24 import org.openecomp.policy.api.PolicyException;
25 import org.openecomp.policy.api.PushPolicyParameters;
26 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
27 import org.openecomp.policy.common.logging.flexlogger.Logger;
28 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
29 import org.openecomp.policy.xacml.std.pap.StdPAPPolicy;
30 import org.openecomp.policy.xacml.std.pap.StdPDPPolicy;
31 import org.springframework.http.HttpStatus;
33 import com.att.research.xacml.api.pap.PAPException;
34 import com.att.research.xacml.api.pap.PDPPolicy;
36 public class PushPolicyService {
37 private static Logger LOGGER = FlexLogger.getLogger(PushPolicyService.class.getName());
39 private String pushResult = null;
40 private HttpStatus status = HttpStatus.BAD_REQUEST;
41 private PushPolicyParameters pushPolicyParameters = null;
42 private String message = null;
43 private String policyName = null;
44 private String policyScope = null;
45 private String pdpGroup = null;
46 private String policyType = null;
47 private String filePrefix = null;
48 private String clientScope = null;
50 public PushPolicyService(PushPolicyParameters pushPolicyParameters,
52 this.pushPolicyParameters = pushPolicyParameters;
53 if(pushPolicyParameters.getRequestID()==null){
54 UUID requestUUID = null;
55 if (requestID != null && !requestID.isEmpty()) {
57 requestUUID = UUID.fromString(requestID);
58 } catch (IllegalArgumentException e) {
59 requestUUID = UUID.randomUUID();
60 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
63 requestUUID = UUID.randomUUID();
64 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
66 this.pushPolicyParameters.setRequestID(requestUUID);
71 }catch(PolicyException e){
72 pushResult = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
73 status = HttpStatus.BAD_REQUEST;
77 private void specialCheck() {
78 if(pushResult.contains("BAD REQUEST") || pushResult.contains("PE300")){
79 status = HttpStatus.BAD_REQUEST;
83 private void run() throws PolicyException{
86 LOGGER.error(message);
87 throw new PolicyException(message);
91 status = HttpStatus.OK;
92 pushResult = processResult();
94 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
95 status = HttpStatus.BAD_REQUEST;
96 throw new PolicyException(e);
100 private String processResult() throws PolicyException{
101 PAPServices papServices = new PAPServices();
102 String response = null;
103 StdPDPPolicy selectedPolicy = papServices.pushPolicy(policyScope, filePrefix, policyName, clientScope, pdpGroup, pushPolicyParameters.getRequestID());
104 if(selectedPolicy==null){
105 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "response code of the URL is 404. "
106 + "This indicates a problem with getting the version from the PAP or the policy does not exist.";
107 LOGGER.error(response);
110 /* //String activeVersion = papServices.getActiveVersion(policyScope, filePrefix, policyName, clientScope, pushPolicyParameters.getRequestID());
111 LOGGER.debug("The active version of " + policyScope + File.separator + filePrefix + policyName + " is " + activeVersion);
113 if ("pe100".equalsIgnoreCase(activeVersion)) {
114 response = XACMLErrorConstants.ERROR_PERMISSIONS + "response code of the URL is 403. PEP is not Authorized for making this Request!! "
115 + "Contact Administrator for this Scope. ";
116 LOGGER.error(response);
118 } else if ("pe300".equalsIgnoreCase(activeVersion)) {
119 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "response code of the URL is 404. "
120 + "This indicates a problem with getting the version from the PAP or the policy does not exist.";
121 LOGGER.error(response);
124 if (!activeVersion.equalsIgnoreCase("0")) {
125 id = policyScope + "." + filePrefix + policyName + "." + activeVersion + ".xml";
126 LOGGER.debug("The policyId is " + id);
128 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "could not retrieve the activeVersion for this policy. "
129 + "This indicates the policy does not exist, please verify the policy exists.";
130 LOGGER.error(response);
133 StdPDPPolicy selectedPolicy = papServices.getGitPath(policyScope, filePrefix, policyName, activeVersion, clientScope, pushPolicyParameters.getRequestID(), id);
136 LOGGER.debug("StdPDPPolicy object contains: " + selectedPolicy.getId() + ", " + selectedPolicy.getName() + ", " + selectedPolicy.getLocation().toString());
137 response = copyPolicy(selectedPolicy, pdpGroup, clientScope, pushPolicyParameters.getRequestID());
138 } catch (PAPException e) {
139 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+e.getMessage());
140 throw new PolicyException(e);
142 LOGGER.debug("copyPolicy response: " + response);
143 if(response.contains("successfully")){
144 response = (String) papServices.callPAP(selectedPolicy, new String[]{"groupId=" + pdpGroup, "policyId="+selectedPolicy.getId(), "apiflag=addPolicyToGroup", "operation=PUT"}, pushPolicyParameters.getRequestID(), clientScope);
146 LOGGER.debug("Final API response: " + response);
150 private String copyPolicy(PDPPolicy policy, String group, String policyType, UUID requestID) throws PAPException {
151 String response = null;
152 if (policy == null || group == null) {
153 throw new PAPException("Null input policy="+policy+" group="+group);
156 StdPAPPolicy location = new StdPAPPolicy(policy.getLocation());
157 response = copyFile(policy.getId(), group, location, policyType, requestID);
158 } catch (Exception e) {
159 String message = "Unable to PUT policy '" + policy.getId() + "', e:" + e;
160 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
161 throw new PAPException(message);
166 private String copyFile(String policyId, String group, StdPAPPolicy location, String clientScope, UUID requestID) throws PAPException {
167 String response = null;
168 // send the policy file to the PAP Servlet
169 PAPServices papService = new PAPServices();
171 response = (String) papService.callPAP(location, new String[] {"groupId=" + group, "policyId="+policyId, "apiflag=api", "operation=post"}, requestID, clientScope);
172 } catch (Exception e) {
173 String message = "Unable to PUT policy '" + policyId + "', e:" + e;
174 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
175 throw new PAPException(message);
180 private boolean getValidation() {
181 // While Validating, extract the required values.
182 if (pushPolicyParameters.getPolicyName() != null
183 && pushPolicyParameters.getPolicyName().contains(".")) {
184 policyName = pushPolicyParameters.getPolicyName().substring(pushPolicyParameters.getPolicyName().lastIndexOf(".") + 1,
185 pushPolicyParameters.getPolicyName().length());
186 policyScope = pushPolicyParameters.getPolicyName().substring(0,pushPolicyParameters.getPolicyName().lastIndexOf("."));
187 LOGGER.info("Name is " + policyName + " scope is " + policyScope);
189 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
192 if (policyName==null||policyName.trim().isEmpty()){
193 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
196 policyType = pushPolicyParameters.getPolicyType();
197 if(policyType== null || policyType.trim().isEmpty()){
198 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No PolicyType given.";
203 if(clientScope==null){
207 pdpGroup = pushPolicyParameters.getPdpGroup();
208 if(pdpGroup==null || pdpGroup.trim().isEmpty()){
209 pdpGroup = "default";
212 LOGGER.debug("clientScope is " + clientScope);
213 LOGGER.debug("filePrefix is " + filePrefix);
218 private void setClientScope() {
219 if ("Firewall".equalsIgnoreCase(policyType)) {
220 clientScope = "ConfigFirewall";
221 filePrefix = "Config_FW_";
222 } else if ("Action".equalsIgnoreCase(policyType)) {
223 clientScope = "Action";
224 filePrefix = "Action_";
225 } else if ("Decision".equalsIgnoreCase(policyType)) {
226 clientScope = "Decision";
227 filePrefix = "Decision_";
228 } else if ("Base".equalsIgnoreCase(policyType)) {
229 clientScope = "Config";
230 filePrefix = "Config_";
231 } else if ("ClosedLoop_Fault".equalsIgnoreCase(policyType)) {
232 clientScope = "ConfigClosedLoop";
233 filePrefix = "Config_Fault_";
234 } else if ("ClosedLoop_PM".equalsIgnoreCase(policyType)) {
235 clientScope = "ConfigClosedLoop";
236 filePrefix = "Config_PM_";
237 } else if ("MicroService".equalsIgnoreCase(policyType)) {
238 clientScope = "ConfigMS";
239 filePrefix = "Config_MS_";
240 } else if ("BRMS_RAW".equalsIgnoreCase(policyType)) {
241 clientScope = "ConfigBrmsRaw";
242 filePrefix = "Config_BRMS_Raw_";
243 } else if ("BRMS_PARAM".equalsIgnoreCase(policyType)) {
244 clientScope = "ConfigBrmsParam";
245 filePrefix = "Config_BRMS_Param_";
248 message = XACMLErrorConstants.ERROR_DATA_ISSUE + policyType
249 + " is not a valid Policy Type.";
253 public String getResult() {
257 public HttpStatus getResponseCode() {