Fix to resolve unpushing(delete) policies issue
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / PdpApiService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pdp.rest.api.services;
22
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;
31
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;
40
41     protected UUID populateRequestId(UUID paramReqId, String reqId) {
42         UUID requestUuid = paramReqId;
43         if (paramReqId == null) {
44             if (!StringUtils.isBlank(reqId)) {
45                 try {
46                     requestUuid = UUID.fromString(reqId);
47                 } catch (IllegalArgumentException e) {
48                     requestUuid = UUID.randomUUID();
49                     LOGGER.info("Generated Random UUID: " + requestUuid.toString(), e);
50                 }
51             } else {
52                 requestUuid = UUID.randomUUID();
53                 LOGGER.info("Generated Random UUID: " + requestUuid.toString());
54             }
55         }
56         this.requestId = requestUuid.toString();
57         return requestUuid;
58     }
59
60     /**
61      * Sets the client scope.
62      */
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_";
94         } else {
95             extendedClientScope(policyType);
96         }
97     }
98
99     protected void extendedClientScope(String policyType) {
100         clientScope = null;
101         message = XACMLErrorConstants.ERROR_DATA_ISSUE + policyType + " is not a valid Policy Type.";
102     }
103
104     protected boolean validatePolicyNameAndScope(List<String> policyNames, List<String> policyTypes,
105             List<PolicyNameType> policyList) {
106
107         String polType = null;
108
109         if (policyTypes.size() == 1) {
110             polType = policyTypes.get(0).trim();
111         }
112
113         try {
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();
118                 }
119
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('.'));
128                 } else {
129                     message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
130                     return false;
131                 }
132
133                 if (StringUtils.isBlank(policyName) || StringUtils.isBlank(policyScope)) {
134                     message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Policy Name.";
135                     return false;
136                 }
137
138                 clientScope = null;
139                 filePrefix = null;
140                 setClientScope(polType);
141                 if (StringUtils.isBlank(clientScope) || StringUtils.isBlank(filePrefix)) {
142                     message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Policy Name.";
143                     return false;
144                 }
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);
149
150             }
151         } catch (Exception e) {
152             message = XACMLErrorConstants.ERROR_DATA_ISSUE
153                     + "validatePolicies - Failed Validation. Please check the input parameters.";
154             return false;
155         }
156         return true;
157     }
158
159
160     /**
161      * Gets the response code.
162      *
163      * @return the response code
164      */
165     public HttpStatus getResponseCode() {
166         return status;
167     }
168
169 }