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.onap.policy.pdp.rest.api.services;
22 import java.util.UUID;
24 import org.onap.policy.api.PolicyException;
25 import org.onap.policy.api.PushPolicyParameters;
26 import org.onap.policy.common.logging.flexlogger.FlexLogger;
27 import org.onap.policy.common.logging.flexlogger.Logger;
28 import org.onap.policy.xacml.api.XACMLErrorConstants;
29 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
30 import org.onap.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 final 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(), e);
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);
111 LOGGER.debug("StdPDPPolicy object contains: " + selectedPolicy.getId() + ", " + selectedPolicy.getName() + ", " + selectedPolicy.getLocation().toString());
112 response = copyPolicy(selectedPolicy, pdpGroup, clientScope, pushPolicyParameters.getRequestID());
113 } catch (PAPException e) {
114 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+e.getMessage());
115 throw new PolicyException(e);
117 LOGGER.debug("copyPolicy response: " + response);
118 if(response.contains("successfully")){
119 response = (String) papServices.callPAP(selectedPolicy, new String[]{"groupId=" + pdpGroup, "policyId="+selectedPolicy.getId(), "apiflag=addPolicyToGroup", "operation=PUT"}, pushPolicyParameters.getRequestID(), clientScope);
121 LOGGER.debug("Final API response: " + response);
125 private String copyPolicy(PDPPolicy policy, String group, String policyType, UUID requestID) throws PAPException {
126 String response = null;
127 if (policy == null || group == null) {
128 throw new PAPException("Null input policy="+policy+" group="+group);
131 StdPAPPolicy location = new StdPAPPolicy(policy.getLocation());
132 response = copyFile(policy.getId(), group, location, policyType, requestID);
133 } catch (Exception e) {
134 String message = "Unable to PUT policy '" + policy.getId() + "', e:" + e;
135 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
136 throw new PAPException(message);
141 private String copyFile(String policyId, String group, StdPAPPolicy location, String clientScope, UUID requestID) throws PAPException {
142 String response = null;
143 // send the policy file to the PAP Servlet
144 PAPServices papService = new PAPServices();
146 response = (String) papService.callPAP(location, new String[] {"groupId=" + group, "policyId="+policyId, "apiflag=api", "operation=post"}, requestID, clientScope);
147 } catch (Exception e) {
148 String message = "Unable to PUT policy '" + policyId + "', e:" + e;
149 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
150 throw new PAPException(message);
155 private boolean getValidation() {
156 // While Validating, extract the required values.
157 if (pushPolicyParameters.getPolicyName() != null
158 && pushPolicyParameters.getPolicyName().contains(".")) {
159 policyName = pushPolicyParameters.getPolicyName().substring(pushPolicyParameters.getPolicyName().lastIndexOf('.') + 1,
160 pushPolicyParameters.getPolicyName().length());
161 policyScope = pushPolicyParameters.getPolicyName().substring(0,pushPolicyParameters.getPolicyName().lastIndexOf('.'));
162 LOGGER.info("Name is " + policyName + " scope is " + policyScope);
164 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
167 if (policyName==null||policyName.trim().isEmpty()){
168 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
171 policyType = pushPolicyParameters.getPolicyType();
172 if(policyType== null || policyType.trim().isEmpty()){
173 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No PolicyType given.";
178 if(clientScope==null){
182 pdpGroup = pushPolicyParameters.getPdpGroup();
183 if(pdpGroup==null || pdpGroup.trim().isEmpty()){
184 pdpGroup = "default";
187 LOGGER.debug("clientScope is " + clientScope);
188 LOGGER.debug("filePrefix is " + filePrefix);
193 private void setClientScope() {
194 if ("Firewall".equalsIgnoreCase(policyType)) {
195 clientScope = "ConfigFirewall";
196 filePrefix = "Config_FW_";
197 } else if ("Action".equalsIgnoreCase(policyType)) {
198 clientScope = "Action";
199 filePrefix = "Action_";
200 } else if ("Decision".equalsIgnoreCase(policyType)) {
201 clientScope = "Decision";
202 filePrefix = "Decision_";
203 } else if ("Base".equalsIgnoreCase(policyType)) {
204 clientScope = "Config";
205 filePrefix = "Config_";
206 } else if ("ClosedLoop_Fault".equalsIgnoreCase(policyType)) {
207 clientScope = "ConfigClosedLoop";
208 filePrefix = "Config_Fault_";
209 } else if ("ClosedLoop_PM".equalsIgnoreCase(policyType)) {
210 clientScope = "ConfigClosedLoop";
211 filePrefix = "Config_PM_";
212 } else if ("MicroService".equalsIgnoreCase(policyType)) {
213 clientScope = "ConfigMS";
214 filePrefix = "Config_MS_";
215 } else if ("BRMS_RAW".equalsIgnoreCase(policyType)) {
216 clientScope = "ConfigBrmsRaw";
217 filePrefix = "Config_BRMS_Raw_";
218 } else if ("BRMS_PARAM".equalsIgnoreCase(policyType)) {
219 clientScope = "ConfigBrmsParam";
220 filePrefix = "Config_BRMS_Param_";
223 message = XACMLErrorConstants.ERROR_DATA_ISSUE + policyType
224 + " is not a valid Policy Type.";
228 public String getResult() {
232 public HttpStatus getResponseCode() {