Push and unpush to support multiple policies
[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 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;
39
40     protected UUID populateRequestId(UUID paramReqId, String reqId) {
41         UUID requestUuid = paramReqId;
42         if (paramReqId == null) {
43             if (!StringUtils.isBlank(reqId)) {
44                 try {
45                     requestUuid = UUID.fromString(reqId);
46                 } catch (IllegalArgumentException e) {
47                     requestUuid = UUID.randomUUID();
48                     LOGGER.info("Generated Random UUID: " + requestUuid.toString(), e);
49                 }
50             } else {
51                 requestUuid = UUID.randomUUID();
52                 LOGGER.info("Generated Random UUID: " + requestUuid.toString());
53             }
54         }
55         this.requestId = requestUuid.toString();
56         return requestUuid;
57     }
58
59     /**
60      * Sets the client scope.
61      */
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_";
93         } else {
94             extendedClientScope(policyType);
95         }
96     }
97
98     protected void extendedClientScope(String policyType) {
99         clientScope = null;
100         message = XACMLErrorConstants.ERROR_DATA_ISSUE + policyType + " is not a valid Policy Type.";
101     }
102
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;
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                 if (polName.contains(".")) {
120                     policyName = polName.substring(polName.lastIndexOf('.') + 1);
121                     policyScope = polName.substring(0, polName.lastIndexOf('.'));
122                 } else {
123                     message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
124                     return false;
125                 }
126                 if (StringUtils.isBlank(policyName) || StringUtils.isBlank(policyScope)) {
127                     message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Policy Name.";
128                     return false;
129                 }
130
131                 clientScope = null;
132                 filePrefix = null;
133                 setClientScope(polType);
134                 if (StringUtils.isBlank(clientScope) || StringUtils.isBlank(filePrefix)) {
135                     message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Policy Name.";
136                     return false;
137                 }
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);
142
143             }
144         } catch (Exception e) {
145             message = XACMLErrorConstants.ERROR_DATA_ISSUE
146                     + "validatePolicies - Failed Validation. Please check the input parameters.";
147             return false;
148         }
149         return true;
150     }
151
152
153     /**
154      * Gets the response code.
155      *
156      * @return the response code
157      */
158     public HttpStatus getResponseCode() {
159         return status;
160     }
161
162 }