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 = (String) papServices.callPAP(selectedPolicy, new String[]{"groupId=" + pdpGroup, "policyId="+selectedPolicy.getId(), "apiflag=api", "operation=POST"}, pushPolicyParameters.getRequestID(), clientScope);
113 } catch (PAPException e) {
114 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+e.getMessage());
115 throw new PolicyException(e);
117 LOGGER.debug("Final API response: " + response);
121 private boolean getValidation() {
122 // While Validating, extract the required values.
123 if (pushPolicyParameters.getPolicyName() != null
124 && pushPolicyParameters.getPolicyName().contains(".")) {
125 policyName = pushPolicyParameters.getPolicyName().substring(pushPolicyParameters.getPolicyName().lastIndexOf('.') + 1,
126 pushPolicyParameters.getPolicyName().length());
127 policyScope = pushPolicyParameters.getPolicyName().substring(0,pushPolicyParameters.getPolicyName().lastIndexOf('.'));
128 LOGGER.info("Name is " + policyName + " scope is " + policyScope);
130 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
133 if (policyName==null||policyName.trim().isEmpty()){
134 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
137 policyType = pushPolicyParameters.getPolicyType();
138 if(policyType== null || policyType.trim().isEmpty()){
139 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No PolicyType given.";
144 if(clientScope==null){
148 pdpGroup = pushPolicyParameters.getPdpGroup();
149 if(pdpGroup==null || pdpGroup.trim().isEmpty()){
150 pdpGroup = "default";
153 LOGGER.debug("clientScope is " + clientScope);
154 LOGGER.debug("filePrefix is " + filePrefix);
159 private void setClientScope() {
160 if ("Firewall".equalsIgnoreCase(policyType)) {
161 clientScope = "ConfigFirewall";
162 filePrefix = "Config_FW_";
163 } else if ("Action".equalsIgnoreCase(policyType)) {
164 clientScope = "Action";
165 filePrefix = "Action_";
166 } else if ("Decision".equalsIgnoreCase(policyType)) {
167 clientScope = "Decision";
168 filePrefix = "Decision_";
169 } else if ("Base".equalsIgnoreCase(policyType)) {
170 clientScope = "Config";
171 filePrefix = "Config_";
172 } else if ("ClosedLoop_Fault".equalsIgnoreCase(policyType)) {
173 clientScope = "ConfigClosedLoop";
174 filePrefix = "Config_Fault_";
175 } else if ("ClosedLoop_PM".equalsIgnoreCase(policyType)) {
176 clientScope = "ConfigClosedLoop";
177 filePrefix = "Config_PM_";
178 } else if ("MicroService".equalsIgnoreCase(policyType)) {
179 clientScope = "ConfigMS";
180 filePrefix = "Config_MS_";
181 } else if ("BRMS_RAW".equalsIgnoreCase(policyType)) {
182 clientScope = "ConfigBrmsRaw";
183 filePrefix = "Config_BRMS_Raw_";
184 } else if ("BRMS_PARAM".equalsIgnoreCase(policyType)) {
185 clientScope = "ConfigBrmsParam";
186 filePrefix = "Config_BRMS_Param_";
189 message = XACMLErrorConstants.ERROR_DATA_ISSUE + policyType
190 + " is not a valid Policy Type.";
194 public String getResult() {
198 public HttpStatus getResponseCode() {