2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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 * Optimization Policy implementation.
40 public class OptimizationPolicyService {
41 private static final Logger LOGGER = FlexLogger.getLogger(OptimizationPolicyService.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;
50 public OptimizationPolicyService(String policyName, String policyScope, PolicyParameters policyParameters,
52 this.policyParameters = policyParameters;
53 this.policyName = policyName;
54 this.policyScope = policyScope;
56 papServices = new PAPServices();
59 public String getMessage() {
63 public String getResult(boolean updateFlag) throws PolicyException {
64 String response = null;
65 String operation = null;
73 // get values and attributes from the JsonObject
74 String servicModel = null;
75 String policyDescription = null;
76 String priority = null;
77 String version = null;
79 String onapName = policyParameters.getOnapName();
80 JsonObject optimizationAttributes = null;
82 optimizationAttributes = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody());
83 } catch (JsonException | IllegalStateException e) {
85 XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " +
86 policyParameters.getConfigBody();
87 LOGGER.error("Error while parsing JSON body for MicroService Policy creation. ", e);
91 if (optimizationAttributes.get("service") != null) {
92 servicModel = optimizationAttributes.get("service").toString().replace("\"", "");
94 if (optimizationAttributes.containsKey("description")) {
95 policyDescription = optimizationAttributes.get("description").toString().replace("\"", "");
97 if (optimizationAttributes.containsKey("priority")) {
98 priority = optimizationAttributes.get("priority").toString().replace("\"", "");
100 if (optimizationAttributes.containsKey("version")) {
101 version = optimizationAttributes.get("version").toString().replace("\"", "");
104 // Create Policy Object
105 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
106 .configPolicyType("Optimization")
107 .policyName(policyName)
108 .description(policyDescription)
111 .serviceType(servicModel)
114 .jsonBody(optimizationAttributes.toString())
117 .editPolicy(updateFlag)
120 .riskLevel(policyParameters.getRiskLevel())
121 .riskType(policyParameters.getRiskType())
122 .guard(String.valueOf(policyParameters.getGuard()))
126 // Send JSON Object to PAP
127 response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
128 "policyType=Config"},
129 policyParameters.getRequestID(), "ConfigOptimization");
130 LOGGER.info("Response: " + response);