2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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 * Optimization Policy implementation.
38 public class OptimizationPolicyService {
39 private static final Logger LOGGER = FlexLogger.getLogger(OptimizationPolicyService.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;
48 public OptimizationPolicyService(String policyName, String policyScope, PolicyParameters policyParameters,
50 this.policyParameters = policyParameters;
51 this.policyName = policyName;
52 this.policyScope = policyScope;
54 papServices = new PAPServices();
57 public String getMessage() {
61 public String getResult(boolean updateFlag) throws PolicyException {
62 String response = null;
63 String operation = null;
71 // get values and attributes from the JsonObject
72 String servicModel = null;
73 String policyDescription = null;
74 String priority = null;
75 String version = null;
77 String onapName = policyParameters.getOnapName();
78 JsonObject optimizationAttributes = null;
80 optimizationAttributes = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody());
81 } catch (JsonException | IllegalStateException e) {
83 XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " +
84 policyParameters.getConfigBody();
85 LOGGER.error("Error while parsing JSON body for MicroService Policy creation. ", e);
89 if (optimizationAttributes.get("service") != null) {
90 servicModel = optimizationAttributes.get("service").toString().replace("\"", "");
92 if (optimizationAttributes.containsKey("description")) {
93 policyDescription = optimizationAttributes.get("description").toString().replace("\"", "");
95 if (optimizationAttributes.containsKey("priority")) {
96 priority = optimizationAttributes.get("priority").toString().replace("\"", "");
98 if (optimizationAttributes.containsKey("version")) {
99 version = optimizationAttributes.get("version").toString().replace("\"", "");
102 // Create Policy Object
103 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Optimization", policyName, policyDescription, onapName,
104 null, servicModel, null, null, optimizationAttributes.toString(), priority,
105 version, updateFlag, policyScope, 0, policyParameters.getRiskLevel(),
106 policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date);
108 // Send JSON Object to PAP
109 response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
110 "policyType=Config"},
111 policyParameters.getRequestID(), "ConfigOptimization");
112 LOGGER.info("Response: " + response);