2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.policy.pdp.rest.api.services;
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 import org.onap.policy.xacml.std.pap.StdPAPPolicyParams;
36 * Config Base Policy Implementation.
40 public class ConfigPolicyService {
41 private static final Logger LOGGER = FlexLogger.getLogger(ConfigPolicyService.class.getName());
42 private PAPServices papServices = null;
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 String onapName = null;
50 private String configName = null;
52 public ConfigPolicyService(String policyName, String policyScope,
53 PolicyParameters policyParameters, String date) {
54 this.policyParameters = policyParameters;
55 this.policyName = policyName;
56 this.policyScope = policyScope;
58 papServices = new PAPServices();
61 public Boolean getValidation() {
62 if (policyParameters.getConfigBody() == null || policyParameters.getConfigBody().trim().isEmpty()) {
63 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Body given.";
66 if (policyParameters.getConfigBodyType() == null) {
67 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Body Type given.";
70 boolean levelCheck = false;
71 levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
73 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
76 onapName = policyParameters.getOnapName();
77 configName = policyParameters.getConfigName();
78 if (onapName == null || onapName.trim().isEmpty()) {
79 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No ONAP Name given.";
82 if (configName == null || configName.trim().isEmpty()) {
83 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Name given.";
86 message = PolicyUtils.policySpecialCharValidator(onapName);
87 if (!message.contains("success")) {
88 message = XACMLErrorConstants.ERROR_DATA_ISSUE + message;
91 message = PolicyUtils.policySpecialCharValidator(configName);
92 if (!message.contains("success")) {
93 message = XACMLErrorConstants.ERROR_DATA_ISSUE + message;
99 public String getMessage() {
103 public String getResult(boolean updateFlag) throws PolicyException {
104 String response = null;
105 String operation = null;
107 operation = "update";
109 operation = "create";
111 String configType = policyParameters.getConfigBodyType().toString();
112 String body = policyParameters.getConfigBody();
113 String configBody = null;
114 //check body for JSON form and remove single quotes if present
115 if ("JSON".equalsIgnoreCase(configType)) {
116 if (body.contains("'")) {
117 configBody = body.replace("'", "\"");
124 Map<String, String> configAttributes = null;
125 if (policyParameters.getAttributes() != null) {
126 configAttributes = policyParameters.getAttributes().get(AttributeType.MATCHING);
129 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
130 .configPolicyType("Base")
131 .policyName(policyName)
132 .description(policyParameters.getPolicyDescription())
134 .configName(configName)
135 .attributes(configAttributes)
136 .configType(configType)
137 .configBodyData(configBody)
138 .editPolicy(updateFlag)
141 .riskLevel(policyParameters.getRiskLevel())
142 .riskType(policyParameters.getRiskType())
143 .guard(String.valueOf(policyParameters.getGuard()))
147 response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
148 "policyType=Config"}, policyParameters.getRequestID(), "Config");
149 LOGGER.info(response);