New Optimization Policy
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / OptimizationPolicyService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20 package org.onap.policy.pdp.rest.api.services;
21
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;
31
32 /**
33  * Optimization Policy implementation. 
34  * 
35  * @version 0.1
36  */
37 public class OptimizationPolicyService{
38         private static final Logger LOGGER = FlexLogger.getLogger(OptimizationPolicyService.class.getName());
39         
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;
46
47         public OptimizationPolicyService(String policyName, String policyScope, PolicyParameters policyParameters, String date) {
48                 this.policyParameters = policyParameters;
49                 this.policyName = policyName;
50                 this.policyScope = policyScope;
51                 this.date = date;
52                 papServices = new PAPServices();
53         }
54
55         public String getMessage() {
56                 return message;
57         }
58         
59         public String getResult(boolean updateFlag) throws PolicyException{
60             String response = null;
61         String operation = null;
62         
63         if (updateFlag){
64             operation = "update";
65         } else {
66             operation = "create";
67         }
68         
69         // get values and attributes from the JsonObject
70         String servicModel = null;
71         String policyDescription=null;
72         String priority=null;
73         String version=null;
74         
75                 String onapName = policyParameters.getOnapName();
76                 JsonObject optimizationAttributes = null;
77                 try{
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);
82                         return null;
83                 }
84                 
85         if (optimizationAttributes.get("service")!=null){
86                 servicModel = optimizationAttributes.get("service").toString().replace("\"", "");
87         }
88         if(optimizationAttributes.containsKey("description")){
89                 policyDescription = optimizationAttributes.get("description").toString().replace("\"", "");
90         }
91         if(optimizationAttributes.containsKey("priority")){
92                 priority = optimizationAttributes.get("priority").toString().replace("\"", "");
93         }
94         if(optimizationAttributes.containsKey("version")){
95                 version = optimizationAttributes.get("version").toString().replace("\"", "");
96         }
97         
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); 
103         
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);
108         return response;
109         }
110 }