2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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=========================================================
21 package org.onap.policy.pdp.rest.api.services;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
26 import org.apache.commons.lang3.StringUtils;
27 import org.onap.policy.api.PolicyException;
28 import org.onap.policy.api.PolicyNameType;
29 import org.onap.policy.api.PushPolicyParameters;
30 import org.onap.policy.common.logging.flexlogger.FlexLogger;
31 import org.onap.policy.common.logging.flexlogger.Logger;
32 import org.onap.policy.xacml.api.XACMLErrorConstants;
33 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
34 import org.springframework.http.HttpStatus;
36 public class PushPolicyService extends PdpApiService {
37 private static final Logger LOGGER = FlexLogger.getLogger(PushPolicyService.class.getName());
39 private PushPolicyParameters pushPolicyParameters = null;
40 private List<PolicyNameType> policyList = new ArrayList<>();
41 private String pdpGroup = null;
42 private String result = null;
45 * Instantiates a new push policy service.
47 * @param pushPolicyParameters the push policy parameters
48 * @param reqId the request ID
50 public PushPolicyService(final PushPolicyParameters pushPolicyParameters, final String reqId) {
51 this.pushPolicyParameters = pushPolicyParameters;
52 pushPolicyParameters.setRequestID(populateRequestId(pushPolicyParameters.getRequestID(), reqId));
53 LOGGER.info("Operation: pushPolicy - Request - " + pushPolicyParameters + ", RequestId - " + requestId);
57 } catch (PolicyException e) {
58 result = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
59 status = HttpStatus.BAD_REQUEST;
62 "PushPolicyService - RequestId - " + requestId + " , Response - " + result + ", Status - " + status);
65 private void run() throws PolicyException {
67 if (!getValidation()) {
68 LOGGER.error(message);
69 throw new PolicyException(message);
73 status = HttpStatus.OK;
74 result = processResult();
75 } catch (Exception e) {
76 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
77 status = HttpStatus.BAD_REQUEST;
78 throw new PolicyException(e);
82 private String processResult() throws PolicyException {
83 PAPServices papServices = new PAPServices();
84 String response = null;
87 // for each policy to be pushed, we need to call pap to get the active version
88 // and construct the full policy name. Then the list of policy names are sent to pap
89 // to do the actual push.
90 String policyId = constructPolicyNames(papServices);
94 new String[] {"groupId=" + pdpGroup, "policyId=" + policyId, "apiflag=api",
95 "operation=POST", "userId=API"},
96 pushPolicyParameters.getRequestID(), clientScope);
97 } catch (Exception e) {
98 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e.getMessage());
99 throw new PolicyException(e);
101 LOGGER.debug("Final API response: " + response);
105 private void specialCheck() {
106 if (result.contains("BAD REQUEST") || result.contains("PE300")) {
107 status = HttpStatus.BAD_REQUEST;
112 private String constructPolicyNames(PAPServices papServices) throws PolicyException {
113 List<String> pushPolicyList = new ArrayList<>(policyList.size());
114 for (PolicyNameType policyData : policyList) {
115 LOGGER.info("PushPolicyService - constructPolicyNames - Policy Name is " + policyData.getPolicyName()
116 + " scope is " + policyData.getPolicyScope());
117 // for each policy we need to get the active version to construct policyId
118 StdPDPPolicy selectedPolicy = papServices.pushPolicy(policyData.getPolicyScope(),
119 policyData.getFilePrefix(), policyData.getPolicyName(), policyData.getClientScope(),
120 pdpGroup.trim(), pushPolicyParameters.getRequestID());
121 if (selectedPolicy != null) {
122 pushPolicyList.add(selectedPolicy.getId());
124 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "response code of the URL is 404. "
125 + "This indicates a problem with getting the version "
126 + "from the PAP or the policy does not exist - " + policyData);
129 return String.join(",", pushPolicyList);
132 private boolean getValidation() {
133 // While Validating, extract the required values.
134 if (StringUtils.isBlank(pushPolicyParameters.getPolicyName())) {
135 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
139 if (StringUtils.isBlank(pushPolicyParameters.getPolicyType())) {
140 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No PolicyType given.";
144 List<String> policyNames = Arrays.asList(pushPolicyParameters.getPolicyName().split(","));
145 List<String> policyTypes = Arrays.asList(pushPolicyParameters.getPolicyType().split(","));
147 if (policyTypes.size() != 1 && (policyNames.size() != policyTypes.size())) {
148 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid number of PolicyTypes";
152 if ((policyNames.stream().anyMatch(StringUtils::isBlank))
153 || (policyTypes.stream().anyMatch(StringUtils::isBlank))) {
154 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid PolicyName or PolicyType";
158 if (!validatePolicyNameAndScope(policyNames, policyTypes, policyList)) {
159 LOGGER.warn("PushPolicyService - Failed validation in validatePolicyNameAndScope - " + message);
163 pdpGroup = pushPolicyParameters.getPdpGroup();
164 if (StringUtils.isBlank(pdpGroup)) {
165 pdpGroup = "default";
171 public String getResult() {