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 String requestId = null;
35 protected String filePrefix = null;
36 protected String clientScope = null;
37 protected String message = null;
38 protected HttpStatus status = HttpStatus.BAD_REQUEST;
40 protected UUID populateRequestId(UUID paramReqId, String reqId) {
41 UUID requestUuid = paramReqId;
42 if (paramReqId == null) {
43 if (!StringUtils.isBlank(reqId)) {
45 requestUuid = UUID.fromString(reqId);
46 } catch (IllegalArgumentException e) {
47 requestUuid = UUID.randomUUID();
48 LOGGER.info("Generated Random UUID: " + requestUuid.toString(), e);
51 requestUuid = UUID.randomUUID();
52 LOGGER.info("Generated Random UUID: " + requestUuid.toString());
55 this.requestId = requestUuid.toString();
60 * Sets the client scope.
62 protected void setClientScope(String policyType) {
63 if ("Firewall".equalsIgnoreCase(policyType)) {
64 clientScope = "ConfigFirewall";
65 filePrefix = "Config_FW_";
66 } else if ("Action".equalsIgnoreCase(policyType)) {
67 clientScope = "Action";
68 filePrefix = "Action_";
69 } else if ("Decision".equalsIgnoreCase(policyType)) {
70 clientScope = "Decision";
71 filePrefix = "Decision_";
72 } else if ("Base".equalsIgnoreCase(policyType)) {
73 clientScope = "Config";
74 filePrefix = "Config_";
75 } else if ("ClosedLoop_Fault".equalsIgnoreCase(policyType)) {
76 clientScope = "ConfigClosedLoop";
77 filePrefix = "Config_Fault_";
78 } else if ("ClosedLoop_PM".equalsIgnoreCase(policyType)) {
79 clientScope = "ConfigClosedLoop";
80 filePrefix = "Config_PM_";
81 } else if ("MicroService".equalsIgnoreCase(policyType)) {
82 clientScope = "ConfigMS";
83 filePrefix = "Config_MS_";
84 } else if ("Optimization".equalsIgnoreCase(policyType)) {
85 clientScope = "ConfigOptimization";
86 filePrefix = "Config_OOF_";
87 } else if ("BRMS_RAW".equalsIgnoreCase(policyType)) {
88 clientScope = "ConfigBrmsRaw";
89 filePrefix = "Config_BRMS_Raw_";
90 } else if ("BRMS_PARAM".equalsIgnoreCase(policyType)) {
91 clientScope = "ConfigBrmsParam";
92 filePrefix = "Config_BRMS_Param_";
94 extendedClientScope(policyType);
98 protected void extendedClientScope(String policyType) {
100 message = XACMLErrorConstants.ERROR_DATA_ISSUE + policyType + " is not a valid Policy Type.";
103 protected boolean validatePolicyNameAndScope(List<String> policyNames, List<String> policyTypes,
104 List<PolicyNameType> policyList) {
105 String policyName = null;
106 String policyScope = null;
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();
119 if (polName.contains(".")) {
120 policyName = polName.substring(polName.lastIndexOf('.') + 1);
121 policyScope = polName.substring(0, polName.lastIndexOf('.'));
123 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
126 if (StringUtils.isBlank(policyName) || StringUtils.isBlank(policyScope)) {
127 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Policy Name.";
133 setClientScope(polType);
134 if (StringUtils.isBlank(clientScope) || StringUtils.isBlank(filePrefix)) {
135 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Policy Name.";
138 PolicyNameType policyData = new PolicyNameType(policyName.trim(), policyScope.trim(), polType,
139 filePrefix, clientScope, null);
140 policyList.add(policyData);
141 LOGGER.info("RequestId - " + requestId + " , Policy - " + policyData);
144 } catch (Exception e) {
145 message = XACMLErrorConstants.ERROR_DATA_ISSUE
146 + "validatePolicies - Failed Validation. Please check the input parameters.";
154 * Gets the response code.
156 * @return the response code
158 public HttpStatus getResponseCode() {