2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Modifications 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;
23 import javax.json.JsonException;
24 import javax.json.JsonObject;
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.xacml.api.XACMLErrorConstants;
32 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
33 import org.onap.policy.xacml.std.pap.StdPAPPolicyParams;
36 * Closed Loop PM policy Implementation.
40 public class ClosedLoopPMPolicyService {
41 private static final Logger LOGGER = FlexLogger.getLogger(ClosedLoopPMPolicyService.class.getName());
43 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 JsonObject configBody = null;
51 public ClosedLoopPMPolicyService(String policyName, String policyScope,
52 PolicyParameters policyParameters, String date) {
53 this.policyParameters = policyParameters;
54 this.policyName = policyName;
55 this.policyScope = policyScope;
57 papServices = new PAPServices();
60 public Boolean getValidation() {
61 if (policyParameters.getConfigBody() == null) {
62 message = XACMLErrorConstants.ERROR_DATA_ISSUE + " No Config Body Present";
65 if (!PolicyApiUtils.validateNONASCIICharactersAndAllowSpaces(policyParameters.getConfigBody())) {
67 XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " +
68 policyParameters.getConfigBody();
72 configBody = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody());
73 } catch (JsonException | IllegalStateException e) {
75 XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " +
76 policyParameters.getConfigBody();
77 LOGGER.error("Error during parsing JSON config body for Closed loop PM policy ", e);
83 public String getMessage() {
87 public String getResult(boolean updateFlag) throws PolicyException {
88 String response = null;
89 String operation = null;
95 // get values and attributes from the JsonObject
96 if (!configBody.containsKey("onapname")) {
97 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given.";
98 LOGGER.error(message);
101 if (!configBody.containsKey("serviceTypePolicyName")) {
102 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Service Type Policy Name given.";
103 LOGGER.error(message);
106 String onapName = configBody.get("onapname").toString().trim().replace("\"", "");
107 if (onapName == null || onapName.trim().isEmpty()) {
108 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given.";
109 LOGGER.error(message);
112 boolean levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
114 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
115 LOGGER.error(message);
118 String jsonBody = configBody.toString();
119 String serviceType = configBody.get("serviceTypePolicyName").toString().replace("\"", "");
121 // Creating CloseLoop_Fault and Performance Metric Policies
122 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
123 .configPolicyType("ClosedLoop_PM")
124 .policyName(policyName)
125 .description(policyParameters.getPolicyDescription())
129 .oldPolicyFileName(null)
130 .serviceType(serviceType)
131 .editPolicy(updateFlag)
134 .riskLevel(policyParameters.getRiskLevel())
135 .riskType(policyParameters.getRiskType())
136 .guard(String.valueOf(policyParameters.getGuard()))
139 //send JSON object to PAP
140 response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
141 "policyType=Config"}, policyParameters.getRequestID(), "ConfigClosedLoop");