Use builder for std pap 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  * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.policy.pdp.rest.api.services;
22
23 import java.util.Map;
24
25 import org.onap.policy.api.AttributeType;
26 import org.onap.policy.api.PolicyException;
27 import org.onap.policy.api.PolicyParameters;
28 import org.onap.policy.common.logging.flexlogger.FlexLogger;
29 import org.onap.policy.common.logging.flexlogger.Logger;
30 import org.onap.policy.pdp.rest.api.utils.PolicyApiUtils;
31 import org.onap.policy.xacml.api.XACMLErrorConstants;
32 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
33 import org.onap.policy.xacml.std.pap.StdPAPPolicyParams;
34
35 /**
36  * BRMS Param Policy Implementation.
37  *
38  * @version 0.1
39  */
40 public class BRMSParamPolicyService {
41     private static final Logger LOGGER = FlexLogger.getLogger(BRMSParamPolicyService.class.getName());
42     private PAPServices papServices = null;
43
44     private PolicyParameters policyParameters = null;
45     private String message = null;
46     private String policyName = null;
47     private String policyScope = null;
48     private String date = null;
49     private Map<AttributeType, Map<String, String>> drlRuleAndUIParams = null;
50
51     public BRMSParamPolicyService(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         boolean levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
62         if (!levelCheck) {
63             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
64             return false;
65         }
66         drlRuleAndUIParams = policyParameters.getAttributes();
67         if (drlRuleAndUIParams == null || drlRuleAndUIParams.isEmpty()) {
68             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Rule Attributes given.";
69             return false;
70         }
71         return true;
72     }
73
74     public String getMessage() {
75         return message;
76     }
77
78     public String getResult(boolean updateFlag) throws PolicyException {
79         String response = null;
80         String operation = null;
81         if (updateFlag) {
82             operation = "update";
83         } else {
84             operation = "create";
85         }
86         // Create Policy
87         // Creating BRMS Param Policies from the Admin Console
88         StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
89                 .configPolicyType("BRMS_Param")
90                 .policyName(policyName)
91                 .description(policyParameters.getPolicyDescription())
92                 .configName("BRMS_PARAM_RULE")
93                 .editPolicy(updateFlag)
94                 .domain(policyScope)
95                 .dynamicFieldConfigAttributes(drlRuleAndUIParams.get(AttributeType.MATCHING))
96                 .highestVersion(0)
97                 .onapName("DROOLS")
98                 .configBodyData(null)
99                 .drlRuleAndUIParams(drlRuleAndUIParams.get(AttributeType.RULE))
100                 .riskLevel(policyParameters.getRiskLevel())
101                 .riskType(policyParameters.getRiskType())
102                 .guard(String.valueOf(policyParameters.getGuard()))
103                 .ttlDate(date)
104                 .brmsController(policyParameters.getControllerName())
105                 .brmsDependency(policyParameters.getDependencyNames())
106                 .build());
107         // Send JSON to PAP
108         response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
109                 "policyType=Config"}, policyParameters.getRequestID(), "ConfigBrmsParam");
110         LOGGER.info(response);
111         return response;
112     }
113
114 }