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) {
106 String policyName = null;
107 String policyScope = null;
108 String polType = null;
110 if (policyTypes.size() == 1) {
111 polType = policyTypes.get(0).trim();
115 for (int i = 0; i < policyNames.size(); i++) {
116 String polName = policyNames.get(i);
117 if (policyTypes.size() > 1) {
118 polType = policyTypes.get(i).trim();
120 if (polName.contains(".")) {
121 policyName = polName.substring(polName.lastIndexOf('.') + 1);
122 policyScope = polName.substring(0, polName.lastIndexOf('.'));
124 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
127 if (StringUtils.isBlank(policyName) || StringUtils.isBlank(policyScope)) {
128 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Policy Name.";
134 setClientScope(polType);
135 if (StringUtils.isBlank(clientScope) || StringUtils.isBlank(filePrefix)) {
136 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Policy Name.";
139 PolicyNameType policyData = new PolicyNameType(policyName.trim(), policyScope.trim(), polType,
140 filePrefix, clientScope, null);
141 policyList.add(policyData);
142 LOGGER.info("RequestId - " + requestId + " , Policy - " + policyData);
145 } catch (Exception e) {
146 message = XACMLErrorConstants.ERROR_DATA_ISSUE
147 + "validatePolicies - Failed Validation. Please check the input parameters.";
155 * Gets the response code.
157 * @return the response code
159 public HttpStatus getResponseCode() {