2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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.List;
24 import java.util.UUID;
25 import org.apache.commons.lang3.StringUtils;
26 import org.onap.policy.api.PolicyNameType;
27 import org.onap.policy.common.logging.flexlogger.FlexLogger;
28 import org.onap.policy.common.logging.flexlogger.Logger;
29 import org.onap.policy.xacml.api.XACMLErrorConstants;
30 import org.springframework.http.HttpStatus;
32 public abstract class PdpApiService {
33 private static final Logger LOGGER = FlexLogger.getLogger(PdpApiService.class.getName());
34 protected static final String PRINT_REQUESTID = " - RequestId - ";
35 protected String requestId = null;
36 protected String filePrefix = null;
37 protected String clientScope = null;
38 protected String message = null;
39 protected HttpStatus status = HttpStatus.BAD_REQUEST;
41 protected UUID populateRequestId(UUID paramReqId, String reqId) {
42 UUID requestUuid = paramReqId;
43 if (paramReqId == null) {
44 if (!StringUtils.isBlank(reqId)) {
46 requestUuid = UUID.fromString(reqId);
47 } catch (IllegalArgumentException e) {
48 requestUuid = UUID.randomUUID();
49 LOGGER.info("Generated Random UUID: " + requestUuid.toString(), e);
52 requestUuid = UUID.randomUUID();
53 LOGGER.info("Generated Random UUID: " + requestUuid.toString());
56 this.requestId = requestUuid.toString();
61 * Sets the client scope.
63 protected void setClientScope(String policyType) {
64 if ("Firewall".equalsIgnoreCase(policyType)) {
65 clientScope = "ConfigFirewall";
66 filePrefix = "Config_FW_";
67 } else if ("Action".equalsIgnoreCase(policyType)) {
68 clientScope = "Action";
69 filePrefix = "Action_";
70 } else if ("Decision".equalsIgnoreCase(policyType)) {
71 clientScope = "Decision";
72 filePrefix = "Decision_";
73 } else if ("Base".equalsIgnoreCase(policyType)) {
74 clientScope = "Config";
75 filePrefix = "Config_";
76 } else if ("ClosedLoop_Fault".equalsIgnoreCase(policyType)) {
77 clientScope = "ConfigClosedLoop";
78 filePrefix = "Config_Fault_";
79 } else if ("ClosedLoop_PM".equalsIgnoreCase(policyType)) {
80 clientScope = "ConfigClosedLoop";
81 filePrefix = "Config_PM_";
82 } else if ("MicroService".equalsIgnoreCase(policyType)) {
83 clientScope = "ConfigMS";
84 filePrefix = "Config_MS_";
85 } else if ("Optimization".equalsIgnoreCase(policyType)) {
86 clientScope = "ConfigOptimization";
87 filePrefix = "Config_OOF_";
88 } else if ("BRMS_RAW".equalsIgnoreCase(policyType)) {
89 clientScope = "ConfigBrmsRaw";
90 filePrefix = "Config_BRMS_Raw_";
91 } else if ("BRMS_PARAM".equalsIgnoreCase(policyType)) {
92 clientScope = "ConfigBrmsParam";
93 filePrefix = "Config_BRMS_Param_";
95 extendedClientScope(policyType);
99 protected void extendedClientScope(String policyType) {
101 message = XACMLErrorConstants.ERROR_DATA_ISSUE + policyType + " is not a valid Policy Type.";
104 protected boolean validatePolicyNameAndScope(List<String> policyNames, List<String> policyTypes,
105 List<PolicyNameType> policyList) {
107 String polType = null;
109 if (policyTypes.size() == 1) {
110 polType = policyTypes.get(0).trim();
114 for (int i = 0; i < policyNames.size(); i++) {
115 String polName = policyNames.get(i);
116 if (policyTypes.size() > 1) {
117 polType = policyTypes.get(i).trim();
120 String policyName = null;
121 String policyScope = null;
122 if (polName.endsWith("xml")) {
123 policyName = polName;
124 policyScope = policyName.substring(0, StringUtils.lastOrdinalIndexOf(policyName, ".", 3));
125 } else if (polName.contains(".")) {
126 policyName = polName.substring(polName.lastIndexOf('.') + 1);
127 policyScope = polName.substring(0, polName.lastIndexOf('.'));
129 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
133 if (StringUtils.isBlank(policyName) || StringUtils.isBlank(policyScope)) {
134 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Policy Name.";
140 setClientScope(polType);
141 if (StringUtils.isBlank(clientScope) || StringUtils.isBlank(filePrefix)) {
142 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Policy Name.";
145 PolicyNameType policyData = new PolicyNameType(policyName.trim(), policyScope.trim(), polType,
146 filePrefix, clientScope, null);
147 policyList.add(policyData);
148 LOGGER.info("RequestId - " + requestId + " , Policy - " + policyData);
151 } catch (Exception e) {
152 message = XACMLErrorConstants.ERROR_DATA_ISSUE
153 + "validatePolicies - Failed Validation. Please check the input parameters.";
161 * Gets the response code.
163 * @return the response code
165 public HttpStatus getResponseCode() {