Make clientAuth header optional and log request
[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         String policyName = null;
107         String policyScope = null;
108         String polType = null;
109
110         if (policyTypes.size() == 1) {
111             polType = policyTypes.get(0).trim();
112         }
113
114         try {
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();
119                 }
120                 if (polName.contains(".")) {
121                     policyName = polName.substring(polName.lastIndexOf('.') + 1);
122                     policyScope = polName.substring(0, polName.lastIndexOf('.'));
123                 } else {
124                     message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
125                     return false;
126                 }
127                 if (StringUtils.isBlank(policyName) || StringUtils.isBlank(policyScope)) {
128                     message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Policy Name.";
129                     return false;
130                 }
131
132                 clientScope = null;
133                 filePrefix = null;
134                 setClientScope(polType);
135                 if (StringUtils.isBlank(clientScope) || StringUtils.isBlank(filePrefix)) {
136                     message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Policy Name.";
137                     return false;
138                 }
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);
143
144             }
145         } catch (Exception e) {
146             message = XACMLErrorConstants.ERROR_DATA_ISSUE
147                     + "validatePolicies - Failed Validation. Please check the input parameters.";
148             return false;
149         }
150         return true;
151     }
152
153
154     /**
155      * Gets the response code.
156      *
157      * @return the response code
158      */
159     public HttpStatus getResponseCode() {
160         return status;
161     }
162
163 }