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.StdPDPPolicy;
30 import org.springframework.http.HttpStatus;
32 import com.att.research.xacml.api.pap.PAPException;
34 public class PushPolicyService {
35 private static final Logger LOGGER = FlexLogger.getLogger(PushPolicyService.class.getName());
37 private String pushResult = null;
38 private HttpStatus status = HttpStatus.BAD_REQUEST;
39 private PushPolicyParameters pushPolicyParameters = null;
40 private String message = null;
41 private String policyName = null;
42 private String policyScope = null;
43 private String pdpGroup = null;
44 private String policyType = null;
45 private String filePrefix = null;
46 private String clientScope = null;
48 public PushPolicyService(PushPolicyParameters pushPolicyParameters,
50 this.pushPolicyParameters = pushPolicyParameters;
51 if(pushPolicyParameters.getRequestID()==null){
52 UUID requestUUID = null;
53 if (requestID != null && !requestID.isEmpty()) {
55 requestUUID = UUID.fromString(requestID);
56 } catch (IllegalArgumentException e) {
57 requestUUID = UUID.randomUUID();
58 LOGGER.info("Generated Random UUID: " + requestUUID.toString(), e);
61 requestUUID = UUID.randomUUID();
62 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
64 this.pushPolicyParameters.setRequestID(requestUUID);
69 }catch(PolicyException e){
70 pushResult = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
71 status = HttpStatus.BAD_REQUEST;
75 private void specialCheck() {
76 if(pushResult.contains("BAD REQUEST") || pushResult.contains("PE300")){
77 status = HttpStatus.BAD_REQUEST;
81 private void run() throws PolicyException{
84 LOGGER.error(message);
85 throw new PolicyException(message);
89 status = HttpStatus.OK;
90 pushResult = processResult();
92 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
93 status = HttpStatus.BAD_REQUEST;
94 throw new PolicyException(e);
98 private String processResult() throws PolicyException{
99 PAPServices papServices = new PAPServices();
100 String response = null;
101 StdPDPPolicy selectedPolicy = papServices.pushPolicy(policyScope, filePrefix, policyName, clientScope, pdpGroup, pushPolicyParameters.getRequestID());
102 if(selectedPolicy==null){
103 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "response code of the URL is 404. "
104 + "This indicates a problem with getting the version from the PAP or the policy does not exist.";
105 LOGGER.error(response);
109 LOGGER.debug("StdPDPPolicy object contains: " + selectedPolicy.getId() + ", " + selectedPolicy.getName() + ", " + selectedPolicy.getLocation().toString());
110 response = (String) papServices.callPAP(selectedPolicy, new String[]{"groupId=" + pdpGroup, "policyId="+selectedPolicy.getId(), "apiflag=api", "operation=POST"}, pushPolicyParameters.getRequestID(), clientScope);
111 } catch (PAPException e) {
112 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+e.getMessage());
113 throw new PolicyException(e);
115 LOGGER.debug("Final API response: " + response);
119 private boolean getValidation() {
120 // While Validating, extract the required values.
121 if (pushPolicyParameters.getPolicyName() != null
122 && pushPolicyParameters.getPolicyName().contains(".")) {
123 policyName = pushPolicyParameters.getPolicyName().substring(pushPolicyParameters.getPolicyName().lastIndexOf('.') + 1,
124 pushPolicyParameters.getPolicyName().length());
125 policyScope = pushPolicyParameters.getPolicyName().substring(0,pushPolicyParameters.getPolicyName().lastIndexOf('.'));
126 LOGGER.info("Name is " + policyName + " scope is " + policyScope);
128 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
131 if (policyName==null||policyName.trim().isEmpty()){
132 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
135 policyType = pushPolicyParameters.getPolicyType();
136 if(policyType== null || policyType.trim().isEmpty()){
137 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No PolicyType given.";
142 if(clientScope==null){
146 pdpGroup = pushPolicyParameters.getPdpGroup();
147 if(pdpGroup==null || pdpGroup.trim().isEmpty()){
148 pdpGroup = "default";
151 LOGGER.debug("clientScope is " + clientScope);
152 LOGGER.debug("filePrefix is " + filePrefix);
157 private void setClientScope() {
158 if ("Firewall".equalsIgnoreCase(policyType)) {
159 clientScope = "ConfigFirewall";
160 filePrefix = "Config_FW_";
161 } else if ("Action".equalsIgnoreCase(policyType)) {
162 clientScope = "Action";
163 filePrefix = "Action_";
164 } else if ("Decision".equalsIgnoreCase(policyType)) {
165 clientScope = "Decision";
166 filePrefix = "Decision_";
167 } else if ("Base".equalsIgnoreCase(policyType)) {
168 clientScope = "Config";
169 filePrefix = "Config_";
170 } else if ("ClosedLoop_Fault".equalsIgnoreCase(policyType)) {
171 clientScope = "ConfigClosedLoop";
172 filePrefix = "Config_Fault_";
173 } else if ("ClosedLoop_PM".equalsIgnoreCase(policyType)) {
174 clientScope = "ConfigClosedLoop";
175 filePrefix = "Config_PM_";
176 } else if ("MicroService".equalsIgnoreCase(policyType)) {
177 clientScope = "ConfigMS";
178 filePrefix = "Config_MS_";
179 } else if ("BRMS_RAW".equalsIgnoreCase(policyType)) {
180 clientScope = "ConfigBrmsRaw";
181 filePrefix = "Config_BRMS_Raw_";
182 } else if ("BRMS_PARAM".equalsIgnoreCase(policyType)) {
183 clientScope = "ConfigBrmsParam";
184 filePrefix = "Config_BRMS_Param_";
187 message = XACMLErrorConstants.ERROR_DATA_ISSUE + policyType
188 + " is not a valid Policy Type.";
192 public String getResult() {
196 public HttpStatus getResponseCode() {