[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / BRMSRawPolicyService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
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
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 package org.onap.policy.pdp.rest.api.services;
21
22 import java.util.Map;
23
24 import org.onap.policy.api.AttributeType;
25 import org.onap.policy.api.PolicyException;
26 import org.onap.policy.api.PolicyParameters;
27 import org.onap.policy.common.logging.flexlogger.FlexLogger;
28 import org.onap.policy.common.logging.flexlogger.Logger;
29 import org.onap.policy.pdp.rest.api.utils.PolicyApiUtils;
30 import org.onap.policy.utils.PolicyUtils;
31 import org.onap.policy.xacml.api.XACMLErrorConstants;
32 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
33
34 /**
35  * BRMS RAW Policy Implementation. 
36  * 
37  * @version 0.1
38  */
39 public class BRMSRawPolicyService{
40         private static Logger LOGGER = FlexLogger.getLogger(BRMSRawPolicyService.class.getName());
41         private static PAPServices papServices = null;
42         
43         private PolicyParameters policyParameters = null;
44         private String message = null;
45         private String policyName = null;
46         private String policyScope = null;
47         private String date = null;
48         private boolean levelCheck = false;
49         private String brmsRawBody = null;
50         
51         public BRMSRawPolicyService(String policyName, String policyScope,
52                         PolicyParameters policyParameters, String date) {
53                 this.policyParameters = policyParameters;
54                 this.policyName = policyName;
55                 this.policyScope = policyScope;
56                 this.date = date;
57                 papServices = new PAPServices();
58         }
59
60         public Boolean getValidation() {
61                 brmsRawBody = policyParameters.getConfigBody();
62                 if(brmsRawBody==null || brmsRawBody.trim().isEmpty()){
63                         message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " No Rule Body given";
64                         return false;
65                 }
66                 message = PolicyUtils.brmsRawValidate(brmsRawBody);
67                 if(message.contains("[ERR")){
68                     message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Raw rule given is invalid" +message;
69                     return false;
70                 }
71                 levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
72                 if(!levelCheck){
73                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
74                         return false;
75                 }
76                 return true;
77         }
78
79         public String getMessage() {
80                 return message;
81         }
82
83         public String getResult(boolean updateFlag) throws PolicyException {
84                 String response = null;
85                 String operation = null;
86                 if (updateFlag){
87                         operation = "update";
88                 } else {
89                         operation = "create";
90                 }
91                 Map<String,String> ruleAttributes = null;
92                 if(policyParameters.getAttributes()!=null){
93                         ruleAttributes = policyParameters.getAttributes().get(AttributeType.RULE);
94                 }
95                 // Create Policy 
96                 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Raw",policyName,policyParameters.getPolicyDescription(),
97                                 "BRMS_RAW_RULE",updateFlag,policyScope, ruleAttributes, 0, "DROOLS", 
98                                 brmsRawBody, policyParameters.getRiskLevel(),
99                                 policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date,  policyParameters.getControllerName(), policyParameters.getDependencyNames());
100                 // Send JSON to PAP 
101                 response = (String) papServices.callPAP(newPAPPolicy, new String[] {"operation="+operation, "apiflag=api", "policyType=Config"}, policyParameters.getRequestID(), "ConfigBrmsRaw");
102                 LOGGER.info(response);
103                 return response;
104         }
105
106 }