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;
22 import javax.json.JsonException;
23 import javax.json.JsonObject;
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.xacml.api.XACMLErrorConstants;
31 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
34 * Closed Loop PM policy Implementation.
38 public class ClosedLoopPMPolicyService {
39 private static final Logger LOGGER = FlexLogger.getLogger(ClosedLoopPMPolicyService.class.getName());
41 private PAPServices papServices = null;
42 private PolicyParameters policyParameters = null;
43 private String message = null;
44 private String policyName = null;
45 private String policyScope = null;
46 private String date = null;
47 private JsonObject configBody = null;
49 public ClosedLoopPMPolicyService(String policyName, String policyScope,
50 PolicyParameters policyParameters, String date) {
51 this.policyParameters = policyParameters;
52 this.policyName = policyName;
53 this.policyScope = policyScope;
55 papServices = new PAPServices();
58 public Boolean getValidation() {
59 if (policyParameters.getConfigBody() == null) {
60 message = XACMLErrorConstants.ERROR_DATA_ISSUE + " No Config Body Present";
63 if (!PolicyApiUtils.validateNONASCIICharactersAndAllowSpaces(policyParameters.getConfigBody())) {
65 XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " +
66 policyParameters.getConfigBody();
70 configBody = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody());
71 } catch (JsonException | IllegalStateException e) {
73 XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " +
74 policyParameters.getConfigBody();
75 LOGGER.error("Error during parsing JSON config body for Closed loop PM policy ", e);
81 public String getMessage() {
85 public String getResult(boolean updateFlag) throws PolicyException {
86 String response = null;
87 String operation = null;
93 // get values and attributes from the JsonObject
94 if (!configBody.containsKey("onapname")) {
95 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given.";
96 LOGGER.error(message);
99 if (!configBody.containsKey("serviceTypePolicyName")) {
100 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Service Type Policy Name given.";
101 LOGGER.error(message);
104 String onapName = configBody.get("onapname").toString().trim().replace("\"", "");
105 if (onapName == null || onapName.trim().isEmpty()) {
106 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given.";
107 LOGGER.error(message);
110 boolean levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
112 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
113 LOGGER.error(message);
116 String jsonBody = configBody.toString();
117 String serviceType = configBody.get("serviceTypePolicyName").toString().replace("\"", "");
119 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("ClosedLoop_PM", policyName,
120 policyParameters.getPolicyDescription(), onapName,
121 jsonBody, false, null, serviceType, updateFlag, policyScope, 0, policyParameters.getRiskLevel(),
122 policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date);
123 //send JSON object to PAP
124 response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
125 "policyType=Config"}, policyParameters.getRequestID(), "ConfigClosedLoop");