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.openecomp.policy.pdp.rest.api.services;
24 import org.openecomp.policy.api.AttributeType;
25 import org.openecomp.policy.api.PolicyException;
26 import org.openecomp.policy.api.PolicyParameters;
27 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
28 import org.openecomp.policy.common.logging.flexlogger.Logger;
29 import org.openecomp.policy.pdp.rest.api.utils.PolicyApiUtils;
30 import org.openecomp.policy.utils.PolicyUtils;
31 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
32 import org.openecomp.policy.xacml.std.pap.StdPAPPolicy;
35 * Config Base Policy Implementation.
39 public class ConfigPolicyService {
40 private static Logger LOGGER = FlexLogger.getLogger(ConfigPolicyService.class.getName());
41 private static PAPServices papServices = null;
43 private PolicyParameters policyParameters = null;
44 private String message = null;
45 private String policyName = null;
46 private String policyScope = null;
47 private String date = null;
48 private boolean levelCheck = false;
49 private String ecompName = 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 levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
72 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
75 ecompName = policyParameters.getEcompName();
76 configName = policyParameters.getConfigName();
77 if(ecompName==null || ecompName.trim().isEmpty()){
78 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No ECOMP Name given.";
81 if(configName==null || configName.trim().isEmpty()){
82 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Name given.";
85 message = PolicyUtils.emptyPolicyValidator(ecompName);
86 if(!message.contains("success")){
87 message = XACMLErrorConstants.ERROR_DATA_ISSUE+ message;
90 message = PolicyUtils.emptyPolicyValidator(configName);
91 if(!message.contains("success")){
92 message = XACMLErrorConstants.ERROR_DATA_ISSUE+ message;
98 public String getMessage() {
102 public String getResult(boolean updateFlag) throws PolicyException {
103 String response = null;
104 String operation = null;
106 operation = "update";
108 operation = "create";
110 String configType = policyParameters.getConfigBodyType().toString();
111 String body = policyParameters.getConfigBody();
112 String configBody = null;
113 //check body for JSON form and remove single quotes if present
114 if (configType.equalsIgnoreCase("JSON")) {
115 if (body.contains("'")) {
116 configBody = body.replace("'", "\"");
123 Map<String,String> configAttributes = null;
124 if(policyParameters.getAttributes()!=null){
125 configAttributes = policyParameters.getAttributes().get(AttributeType.MATCHING);
128 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Base", policyName, policyParameters.getPolicyDescription(), ecompName, configName, configAttributes, configType,
129 configBody, updateFlag, policyScope,0, policyParameters.getRiskLevel(),policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date);
131 response = (String) papServices.callPAP(newPAPPolicy, new String[] {"operation="+operation, "apiflag=api", "policyType=Config"}, policyParameters.getRequestID(), "Config");
132 LOGGER.info(response);