2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
20 package org.onap.policy.pdp.rest.api.services;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 import java.util.UUID;
26 import org.glassfish.jersey.spi.Contract;
27 import org.onap.policy.api.PolicyException;
28 import org.onap.policy.api.PolicyParameters;
29 import org.onap.policy.common.logging.flexlogger.FlexLogger;
30 import org.onap.policy.common.logging.flexlogger.Logger;
31 import org.onap.policy.pdp.rest.api.utils.PolicyApiUtils;
32 import org.onap.policy.rest.adapter.PolicyRestAdapter;
33 import org.onap.policy.rest.util.PolicyValidation;
34 import org.onap.policy.rest.util.PolicyValidationRequestWrapper;
35 import org.onap.policy.xacml.api.XACMLErrorConstants;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.stereotype.Controller;
39 import com.google.common.base.Strings;
41 public class CreateUpdatePolicyServiceImpl implements CreateUpdatePolicyService {
42 private static final Logger LOGGER = FlexLogger.getLogger(CreateUpdatePolicyServiceImpl.class.getName());
44 private String policyResult = null;
45 private HttpStatus status = HttpStatus.BAD_REQUEST;
46 private Boolean updateFlag = false;
47 private String message = null;
48 private PolicyParameters policyParameters = new PolicyParameters();
49 private String policyName = null;
50 private String policyScope = null;
51 private String date = null;
53 public CreateUpdatePolicyServiceImpl(PolicyParameters policyParameters,
54 String requestID, boolean updateFlag) {
55 this.updateFlag = updateFlag;
56 this.policyParameters = policyParameters;
57 if(policyParameters.getRequestID()==null){
58 UUID requestUUID = null;
59 if (requestID != null && !requestID.isEmpty()) {
61 requestUUID = UUID.fromString(requestID);
62 } catch (IllegalArgumentException e) {
63 requestUUID = UUID.randomUUID();
64 LOGGER.info("Generated Random UUID: " + requestUUID.toString(), e);
67 requestUUID = UUID.randomUUID();
68 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
70 this.policyParameters.setRequestID(requestUUID);
75 }catch(PolicyException e){
76 policyResult = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
77 status = HttpStatus.BAD_REQUEST;
81 public void run() throws PolicyException{
84 LOGGER.error(message);
85 throw new PolicyException(message);
89 status = HttpStatus.OK;
90 policyResult = processResult();
92 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
93 status = HttpStatus.BAD_REQUEST;
94 throw new PolicyException(e);
98 @SuppressWarnings("incomplete-switch")
99 public String processResult() throws PolicyException{
100 String response = null;
101 if(policyParameters.getPolicyConfigType()!=null){
102 // This is a Config Type Policy.
103 switch(policyParameters.getPolicyConfigType()){
105 BRMSParamPolicyService bRMSParamPolicyService = new BRMSParamPolicyService(policyName, policyScope, policyParameters, date);
107 if(!bRMSParamPolicyService.getValidation()){
108 LOGGER.error(bRMSParamPolicyService.getMessage());
109 status = HttpStatus.BAD_REQUEST;
110 return bRMSParamPolicyService.getMessage();
113 response = bRMSParamPolicyService.getResult(updateFlag);
116 BRMSRawPolicyService bRMSRawPolicyService = new BRMSRawPolicyService(policyName, policyScope, policyParameters, date);
118 if(!bRMSRawPolicyService.getValidation()){
119 LOGGER.error(bRMSRawPolicyService.getMessage());
120 status = HttpStatus.BAD_REQUEST;
121 return bRMSRawPolicyService.getMessage();
124 response = bRMSRawPolicyService.getResult(updateFlag);
127 ConfigPolicyService configPolicyService = new ConfigPolicyService(policyName, policyScope, policyParameters, date);
129 if(!configPolicyService.getValidation()){
130 LOGGER.error(configPolicyService.getMessage());
131 status = HttpStatus.BAD_REQUEST;
132 return configPolicyService.getMessage();
135 response = configPolicyService.getResult(updateFlag);
137 case ClosedLoop_Fault:
138 ClosedLoopFaultPolicyService closedLoopFaultPolicyService = new ClosedLoopFaultPolicyService(policyName, policyScope, policyParameters, date);
140 if(!closedLoopFaultPolicyService.getValidation()){
141 LOGGER.error(closedLoopFaultPolicyService.getMessage());
142 status = HttpStatus.BAD_REQUEST;
143 return closedLoopFaultPolicyService.getMessage();
146 response = closedLoopFaultPolicyService.getResult(updateFlag);
149 ClosedLoopPMPolicyService closedLoopPMPolicyService = new ClosedLoopPMPolicyService(policyName, policyScope, policyParameters, date);
151 if(!closedLoopPMPolicyService.getValidation()){
152 LOGGER.error(closedLoopPMPolicyService.getMessage());
153 status = HttpStatus.BAD_REQUEST;
154 return closedLoopPMPolicyService.getMessage();
157 response = closedLoopPMPolicyService.getResult(updateFlag);
160 FirewallPolicyService firewallPolicyService = new FirewallPolicyService(policyName, policyScope, policyParameters, date);
162 if(!firewallPolicyService.getValidation()){
163 LOGGER.error(firewallPolicyService.getMessage());
164 status = HttpStatus.BAD_REQUEST;
165 return firewallPolicyService.getMessage();
168 response = firewallPolicyService.getResult(updateFlag);
171 MicroServicesPolicyService microServicesPolicyService = new MicroServicesPolicyService(policyName, policyScope, policyParameters, date);
173 if(!microServicesPolicyService.getValidation()){
174 LOGGER.error(microServicesPolicyService.getMessage());
175 status = HttpStatus.BAD_REQUEST;
176 return microServicesPolicyService.getMessage();
179 response = microServicesPolicyService.getResult(updateFlag);
182 String message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " Invalid Config Type Present";
183 LOGGER.error(message);
184 status = HttpStatus.BAD_REQUEST;
187 }else if (policyParameters.getPolicyClass()!=null){
188 switch (policyParameters.getPolicyClass()){
190 ActionPolicyService actionPolicyService = new ActionPolicyService(policyScope, policyName, policyParameters);
192 if(!actionPolicyService.getValidation()){
193 LOGGER.error(actionPolicyService.getMessage());
194 status = HttpStatus.BAD_REQUEST;
195 return actionPolicyService.getMessage();
198 response = actionPolicyService.getResult(updateFlag);
201 DecisionPolicyService decisionPolicyService = new DecisionPolicyService(policyScope, policyName, policyParameters);
203 if(!decisionPolicyService.getValidation()){
204 LOGGER.error(decisionPolicyService.getMessage());
205 status = HttpStatus.BAD_REQUEST;
206 return decisionPolicyService.getMessage();
209 response = decisionPolicyService.getResult(updateFlag);
213 String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Class found.";
214 LOGGER.error(message);
215 status = HttpStatus.BAD_REQUEST;
221 protected boolean getValidation() {
223 PolicyValidation validation = new PolicyValidation();
225 StringBuilder responseString;
227 if (policyParameters != null) {
229 if (!Strings.isNullOrEmpty(policyParameters.getPolicyName())){
230 if (policyParameters.getPolicyName().contains(".")) {
231 policyName = policyParameters.getPolicyName().substring(policyParameters.getPolicyName().lastIndexOf('.') + 1,
232 policyParameters.getPolicyName().length());
233 policyScope = policyParameters.getPolicyName().substring(0,policyParameters.getPolicyName().lastIndexOf('.'));
234 policyParameters.setPolicyName(policyName);
235 LOGGER.info("Name is " + policyName + " scope is " + policyScope);
237 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Policy Scope: No Policy Scope given";
238 LOGGER.error("Common validation did not return success: " + message);
242 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "PolicyName: PolicyName Should not be empty";
243 LOGGER.error("Common validation did not return success: " + message);
247 if(policyParameters.getPolicyClass() != null && "Config".equals(policyParameters.getPolicyClass().toString())){
248 String policyConfigType = policyParameters.getPolicyConfigType().toString();
249 if(!"BRMS_Param".equalsIgnoreCase(policyConfigType) && Strings.isNullOrEmpty(policyParameters.getConfigBody())){
250 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "ConfigBody: No Config Body given";
251 LOGGER.error("Common validation did not return success: " + message);
257 PolicyValidationRequestWrapper wrapper = new PolicyValidationRequestWrapper();
258 PolicyRestAdapter policyData = wrapper.populateRequestParameters(policyParameters);
259 responseString = validation.validatePolicy(policyData);
260 } catch (Exception e) {
261 LOGGER.error("Exception Occured during Policy Validation" +e);
262 if(e.getMessage()!=null){
263 if("Action".equals(policyParameters.getPolicyClass().toString()) && e.getMessage().contains("Index:")){
264 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Rule Algorithms: One or more Fields in Rule Algorithms is Empty.";
266 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Exception Occured During Policy Validation: " + e;
272 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy parameters given. ";
276 // Set some default Values.
277 if (policyParameters.getTtlDate()!=null){
278 date = convertDate(policyParameters.getTtlDate());
281 if (responseString!=null){
282 if("success".equals(responseString.toString())||"success@#".equals(responseString.toString())){
285 message = XACMLErrorConstants.ERROR_DATA_ISSUE + PolicyApiUtils.formatResponse(responseString);
286 LOGGER.error("Common validation did not return success: " + message);
290 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Unknown Error Occured During Policy Validation";
291 LOGGER.error(message);
297 protected String convertDate(Date date) {
298 String strDate = null;
300 SimpleDateFormat dateformatJava = new SimpleDateFormat("dd-MM-yyyy");
301 strDate = dateformatJava.format(date);
303 return (strDate==null) ? "NA": strDate;
306 protected void specialCheck() {
307 if(policyResult== null || policyResult.contains("BAD REQUEST")||policyResult.contains("PE300")){
308 status = HttpStatus.BAD_REQUEST;
309 } else if (policyResult.contains("Policy Exist Error")) {
310 status = HttpStatus.CONFLICT;
311 } else if (policyResult.contains("PE200")||policyResult.contains("PE900")){
312 status = HttpStatus.INTERNAL_SERVER_ERROR;
316 public String getResult() {
320 public HttpStatus getResponseCode() {