Add PAP Policy parameter builder xacml policy
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / BRMSParamPolicyService.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.xacml.api.XACMLErrorConstants;
31 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
32
33 /**
34  * BRMS Param Policy Implementation.
35  *
36  * @version 0.1
37  */
38 public class BRMSParamPolicyService {
39     private static final Logger LOGGER = FlexLogger.getLogger(BRMSParamPolicyService.class.getName());
40     private PAPServices papServices = null;
41
42     private PolicyParameters policyParameters = null;
43     private String message = null;
44     private String policyName = null;
45     private String policyScope = null;
46     private String date = null;
47     private Map<AttributeType, Map<String, String>> drlRuleAndUIParams = null;
48
49     public BRMSParamPolicyService(String policyName, String policyScope,
50                                   PolicyParameters policyParameters, String date) {
51         this.policyParameters = policyParameters;
52         this.policyName = policyName;
53         this.policyScope = policyScope;
54         this.date = date;
55         papServices = new PAPServices();
56     }
57
58     public Boolean getValidation() {
59         boolean levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
60         if (!levelCheck) {
61             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
62             return false;
63         }
64         drlRuleAndUIParams = policyParameters.getAttributes();
65         if (drlRuleAndUIParams == null || drlRuleAndUIParams.isEmpty()) {
66             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Rule Attributes given.";
67             return false;
68         }
69         return true;
70     }
71
72     public String getMessage() {
73         return message;
74     }
75
76     public String getResult(boolean updateFlag) throws PolicyException {
77         String response = null;
78         String operation = null;
79         if (updateFlag) {
80             operation = "update";
81         } else {
82             operation = "create";
83         }
84         // Create Policy
85         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Param", policyName, policyParameters.getPolicyDescription(),
86                 "BRMS_PARAM_RULE", updateFlag, policyScope,
87                 drlRuleAndUIParams.get(AttributeType.MATCHING), 0, "DROOLS",
88                 null, drlRuleAndUIParams.get(AttributeType.RULE), policyParameters.getRiskLevel(),
89                 policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date,
90                 policyParameters.getControllerName(), policyParameters.getDependencyNames());
91         // Send JSON to PAP
92         response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
93                 "policyType=Config"}, policyParameters.getRequestID(), "ConfigBrmsParam");
94         LOGGER.info(response);
95         return response;
96     }
97
98 }