Optimize PAP policy constructor with builder
[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  * Modified 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.utils.PolicyUtils;
32 import org.onap.policy.xacml.api.XACMLErrorConstants;
33 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
34 import org.onap.policy.xacml.std.pap.StdPAPPolicyParams;
35
36 /**
37  * BRMS RAW Policy Implementation.
38  *
39  * @version 0.1
40  */
41 public class BRMSRawPolicyService {
42     private static Logger LOGGER = FlexLogger.getLogger(BRMSRawPolicyService.class.getName());
43     private static PAPServices papServices = null;
44
45     private PolicyParameters policyParameters = null;
46     private String message = null;
47     private String policyName = null;
48     private String policyScope = null;
49     private String date = null;
50     private boolean levelCheck = false;
51     private String brmsRawBody = null;
52
53     public BRMSRawPolicyService(String policyName, String policyScope,
54                                 PolicyParameters policyParameters, String date) {
55         this.policyParameters = policyParameters;
56         this.policyName = policyName;
57         this.policyScope = policyScope;
58         this.date = date;
59         papServices = new PAPServices();
60     }
61
62     public Boolean getValidation() {
63         brmsRawBody = policyParameters.getConfigBody();
64         if (brmsRawBody == null || brmsRawBody.trim().isEmpty()) {
65             message = XACMLErrorConstants.ERROR_DATA_ISSUE + " No Rule Body given";
66             return false;
67         }
68         message = PolicyUtils.brmsRawValidate(brmsRawBody);
69         if (message.contains("[ERR")) {
70             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Raw rule given is invalid" + message;
71             return false;
72         }
73         levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
74         if (!levelCheck) {
75             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
76             return false;
77         }
78         return true;
79     }
80
81     public String getMessage() {
82         return message;
83     }
84
85     public String getResult(boolean updateFlag) throws PolicyException {
86         String response = null;
87         String operation = null;
88         if (updateFlag) {
89             operation = "update";
90         } else {
91             operation = "create";
92         }
93         Map<String, String> ruleAttributes = null;
94         if (policyParameters.getAttributes() != null) {
95             ruleAttributes = policyParameters.getAttributes().get(AttributeType.RULE);
96         }
97         // Create Policy
98         StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
99                 .configPolicyType("BRMS_Raw")
100                 .policyName(policyName)
101                 .description(policyParameters.getPolicyDescription())
102                 .configName("BRMS_RAW_RULE")
103                 .editPolicy(updateFlag)
104                 .domain(policyScope)
105                 .dynamicFieldConfigAttributes(ruleAttributes)
106                 .highestVersion(0)
107                 .onapName("DROOLS")
108                 .configBodyData(brmsRawBody)
109                 .riskLevel(policyParameters.getRiskLevel())
110                 .riskType(policyParameters.getRiskType())
111                 .guard(String.valueOf(policyParameters.getGuard()))
112                 .ttlDate(date)
113                 .brmsController(policyParameters.getControllerName())
114                 .brmsDependency(policyParameters.getDependencyNames())
115                 .build());
116         // Send JSON to PAP
117         response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
118                 "policyType=Config"}, policyParameters.getRequestID(), "ConfigBrmsRaw");
119         LOGGER.info(response);
120         return response;
121     }
122
123 }