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;
24 import org.onap.policy.api.PolicyException;
25 import org.onap.policy.api.PolicyParameters;
26 import org.onap.policy.common.logging.flexlogger.FlexLogger;
27 import org.onap.policy.common.logging.flexlogger.Logger;
28 import org.onap.policy.pdp.rest.api.utils.PolicyApiUtils;
29 import org.onap.policy.xacml.api.XACMLErrorConstants;
30 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
33 * Optimization Policy implementation.
37 public class OptimizationPolicyService{
38 private static final Logger LOGGER = FlexLogger.getLogger(OptimizationPolicyService.class.getName());
40 private PAPServices papServices = null;
41 private PolicyParameters policyParameters = null;
42 private String message = null;
43 private String policyName = null;
44 private String policyScope = null;
45 private String date = null;
47 public OptimizationPolicyService(String policyName, String policyScope, PolicyParameters policyParameters, String date) {
48 this.policyParameters = policyParameters;
49 this.policyName = policyName;
50 this.policyScope = policyScope;
52 papServices = new PAPServices();
55 public String getMessage() {
59 public String getResult(boolean updateFlag) throws PolicyException{
60 String response = null;
61 String operation = null;
69 // get values and attributes from the JsonObject
70 String servicModel = null;
71 String policyDescription=null;
75 String onapName = policyParameters.getOnapName();
76 JsonObject optimizationAttributes = null;
78 optimizationAttributes = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody());
79 } catch(JsonException| IllegalStateException e){
80 message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper JSON object : " + policyParameters.getConfigBody();
81 LOGGER.error("Error while parsing JSON body for MicroService Policy creation. ", e);
85 if (optimizationAttributes.get("service")!=null){
86 servicModel = optimizationAttributes.get("service").toString().replace("\"", "");
88 if(optimizationAttributes.containsKey("description")){
89 policyDescription = optimizationAttributes.get("description").toString().replace("\"", "");
91 if(optimizationAttributes.containsKey("priority")){
92 priority = optimizationAttributes.get("priority").toString().replace("\"", "");
94 if(optimizationAttributes.containsKey("version")){
95 version = optimizationAttributes.get("version").toString().replace("\"", "");
98 // Create Policy Object
99 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Optimization", policyName, policyDescription, onapName,
100 null, servicModel, null, null, optimizationAttributes.toString(), priority,
101 version, updateFlag, policyScope, 0, policyParameters.getRiskLevel(),
102 policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date);
104 // Send JSON Object to PAP
105 response = (String) papServices.callPAP(newPAPPolicy, new String[] {"operation="+operation, "apiflag=api", "policyType=Config"},
106 policyParameters.getRequestID(), "ConfigOptimization");
107 LOGGER.info("Response: " + response);