Add PAP Policy parameter builder xacml 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
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;
32
33 /**
34  * Optimization Policy implementation.
35  *
36  * @version 0.1
37  */
38 public class OptimizationPolicyService {
39     private static final Logger LOGGER = FlexLogger.getLogger(OptimizationPolicyService.class.getName());
40
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;
47
48     public OptimizationPolicyService(String policyName, String policyScope, PolicyParameters policyParameters,
49                                      String date) {
50         this.policyParameters = policyParameters;
51         this.policyName = policyName;
52         this.policyScope = policyScope;
53         this.date = date;
54         papServices = new PAPServices();
55     }
56
57     public String getMessage() {
58         return message;
59     }
60
61     public String getResult(boolean updateFlag) throws PolicyException {
62         String response = null;
63         String operation = null;
64
65         if (updateFlag) {
66             operation = "update";
67         } else {
68             operation = "create";
69         }
70
71         // get values and attributes from the JsonObject
72         String servicModel = null;
73         String policyDescription = null;
74         String priority = null;
75         String version = null;
76
77         String onapName = policyParameters.getOnapName();
78         JsonObject optimizationAttributes = null;
79         try {
80             optimizationAttributes = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody());
81         } catch (JsonException | IllegalStateException e) {
82             message =
83                     XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " +
84                             policyParameters.getConfigBody();
85             LOGGER.error("Error while parsing JSON body for MicroService Policy creation. ", e);
86             return null;
87         }
88
89         if (optimizationAttributes.get("service") != null) {
90             servicModel = optimizationAttributes.get("service").toString().replace("\"", "");
91         }
92         if (optimizationAttributes.containsKey("description")) {
93             policyDescription = optimizationAttributes.get("description").toString().replace("\"", "");
94         }
95         if (optimizationAttributes.containsKey("priority")) {
96             priority = optimizationAttributes.get("priority").toString().replace("\"", "");
97         }
98         if (optimizationAttributes.containsKey("version")) {
99             version = optimizationAttributes.get("version").toString().replace("\"", "");
100         }
101
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);
107
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);
113         return response;
114     }
115 }