Modify std pap policy to use builder in constr
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / ConfigPolicyService.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  * Config Base Policy Implementation.
38  *
39  * @version 0.1
40  */
41 public class ConfigPolicyService {
42     private static final Logger LOGGER = FlexLogger.getLogger(ConfigPolicyService.class.getName());
43     private 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 String onapName = null;
51     private String configName = null;
52
53     public ConfigPolicyService(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         if (policyParameters.getConfigBody() == null || policyParameters.getConfigBody().trim().isEmpty()) {
64             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Body given.";
65             return false;
66         }
67         if (policyParameters.getConfigBodyType() == null) {
68             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Body Type given.";
69             return false;
70         }
71         boolean levelCheck = false;
72         levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
73         if (!levelCheck) {
74             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
75             return false;
76         }
77         onapName = policyParameters.getOnapName();
78         configName = policyParameters.getConfigName();
79         if (onapName == null || onapName.trim().isEmpty()) {
80             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No ONAP Name given.";
81             return false;
82         }
83         if (configName == null || configName.trim().isEmpty()) {
84             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Name given.";
85             return false;
86         }
87         message = PolicyUtils.policySpecialCharValidator(onapName);
88         if (!message.contains("success")) {
89             message = XACMLErrorConstants.ERROR_DATA_ISSUE + message;
90             return false;
91         }
92         message = PolicyUtils.policySpecialCharValidator(configName);
93         if (!message.contains("success")) {
94             message = XACMLErrorConstants.ERROR_DATA_ISSUE + message;
95             return false;
96         }
97         return true;
98     }
99
100     public String getMessage() {
101         return message;
102     }
103
104     public String getResult(boolean updateFlag) throws PolicyException {
105         String response = null;
106         String operation = null;
107         if (updateFlag) {
108             operation = "update";
109         } else {
110             operation = "create";
111         }
112         String configType = policyParameters.getConfigBodyType().toString();
113         String body = policyParameters.getConfigBody();
114         String configBody = null;
115         //check body for JSON form and remove single quotes if present
116         if ("JSON".equalsIgnoreCase(configType)) {
117             if (body.contains("'")) {
118                 configBody = body.replace("'", "\"");
119             } else {
120                 configBody = body;
121             }
122         } else {
123             configBody = body;
124         }
125         Map<String, String> configAttributes = null;
126         if (policyParameters.getAttributes() != null) {
127             configAttributes = policyParameters.getAttributes().get(AttributeType.MATCHING);
128         }
129         // create Policy.
130         StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
131                 .configPolicyType("Base")
132                 .policyName(policyName)
133                 .description(policyParameters.getPolicyDescription())
134                 .onapName(onapName)
135                 .configName(configName)
136                 .dyanamicFieldConfigAttributes(configAttributes)
137                 .configType(configType)
138                 .configBodyData(configBody)
139                 .editPolicy(updateFlag)
140                 .domain(policyScope)
141                 .highestVersion(0)
142                 .riskLevel(policyParameters.getRiskLevel())
143                 .riskType(policyParameters.getRiskType())
144                 .guard(String.valueOf(policyParameters.getGuard()))
145                 .ttlDate(date)
146                 .build());
147         // Send Json to PAP.
148         response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
149                 "policyType=Config"}, policyParameters.getRequestID(), "Config");
150         LOGGER.info(response);
151         return response;
152     }
153
154 }