2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pdp.rest.api.services;
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;
37 * Config Base Policy Implementation.
41 public class ConfigPolicyService {
42 private static final Logger LOGGER = FlexLogger.getLogger(ConfigPolicyService.class.getName());
43 private PAPServices papServices = null;
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;
53 public ConfigPolicyService(String policyName, String policyScope,
54 PolicyParameters policyParameters, String date) {
55 this.policyParameters = policyParameters;
56 this.policyName = policyName;
57 this.policyScope = policyScope;
59 papServices = new PAPServices();
62 public Boolean getValidation() {
63 if (policyParameters.getConfigBody() == null || policyParameters.getConfigBody().trim().isEmpty()) {
64 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Body given.";
67 if (policyParameters.getConfigBodyType() == null) {
68 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Body Type given.";
71 boolean levelCheck = false;
72 levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
74 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
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.";
83 if (configName == null || configName.trim().isEmpty()) {
84 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Name given.";
87 message = PolicyUtils.policySpecialCharValidator(onapName);
88 if (!message.contains("success")) {
89 message = XACMLErrorConstants.ERROR_DATA_ISSUE + message;
92 message = PolicyUtils.policySpecialCharValidator(configName);
93 if (!message.contains("success")) {
94 message = XACMLErrorConstants.ERROR_DATA_ISSUE + message;
100 public String getMessage() {
104 public String getResult(boolean updateFlag) throws PolicyException {
105 String response = null;
106 String operation = null;
108 operation = "update";
110 operation = "create";
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("'", "\"");
125 Map<String, String> configAttributes = null;
126 if (policyParameters.getAttributes() != null) {
127 configAttributes = policyParameters.getAttributes().get(AttributeType.MATCHING);
130 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
131 .configPolicyType("Base")
132 .policyName(policyName)
133 .description(policyParameters.getPolicyDescription())
135 .configName(configName)
136 .dynamicFieldConfigAttributes(configAttributes)
137 .configType(configType)
138 .configBodyData(configBody)
139 .editPolicy(updateFlag)
142 .riskLevel(policyParameters.getRiskLevel())
143 .riskType(policyParameters.getRiskType())
144 .guard(String.valueOf(policyParameters.getGuard()))
148 response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
149 "policyType=Config"}, policyParameters.getRequestID(), "Config");
150 LOGGER.info(response);