Use builder for std pap 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  * 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.policy.pdp.rest.api.services;
22
23 import javax.json.JsonException;
24 import javax.json.JsonObject;
25
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;
34
35 /**
36  * Optimization Policy implementation.
37  *
38  * @version 0.1
39  */
40 public class OptimizationPolicyService {
41     private static final Logger LOGGER = FlexLogger.getLogger(OptimizationPolicyService.class.getName());
42
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;
49
50     public OptimizationPolicyService(String policyName, String policyScope, PolicyParameters policyParameters,
51                                      String date) {
52         this.policyParameters = policyParameters;
53         this.policyName = policyName;
54         this.policyScope = policyScope;
55         this.date = date;
56         papServices = new PAPServices();
57     }
58
59     public String getMessage() {
60         return message;
61     }
62
63     public String getResult(boolean updateFlag) throws PolicyException {
64         String response = null;
65         String operation = null;
66
67         if (updateFlag) {
68             operation = "update";
69         } else {
70             operation = "create";
71         }
72
73         // get values and attributes from the JsonObject
74         String servicModel = null;
75         String policyDescription = null;
76         String priority = null;
77         String version = null;
78
79         String onapName = policyParameters.getOnapName();
80         JsonObject optimizationAttributes = null;
81         try {
82             optimizationAttributes = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody());
83         } catch (JsonException | IllegalStateException e) {
84             message =
85                     XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " +
86                             policyParameters.getConfigBody();
87             LOGGER.error("Error while parsing JSON body for MicroService Policy creation. ", e);
88             return null;
89         }
90
91         if (optimizationAttributes.get("service") != null) {
92             servicModel = optimizationAttributes.get("service").toString().replace("\"", "");
93         }
94         if (optimizationAttributes.containsKey("description")) {
95             policyDescription = optimizationAttributes.get("description").toString().replace("\"", "");
96         }
97         if (optimizationAttributes.containsKey("priority")) {
98             priority = optimizationAttributes.get("priority").toString().replace("\"", "");
99         }
100         if (optimizationAttributes.containsKey("version")) {
101             version = optimizationAttributes.get("version").toString().replace("\"", "");
102         }
103
104         // Create Policy Object 
105         StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
106                 .configPolicyType("Optimization")
107                 .policyName(policyName)
108                 .description(policyDescription)
109                 .onapName(onapName)
110                 .configName(null)
111                 .serviceType(servicModel)
112                 .uuid(null)
113                 .msLocation(null)
114                 .jsonBody(optimizationAttributes.toString())
115                 .priority(priority)
116                 .version(version)
117                 .editPolicy(updateFlag)
118                 .domain(policyScope)
119                 .highestVersion(0)
120                 .riskLevel(policyParameters.getRiskLevel())
121                 .riskType(policyParameters.getRiskType())
122                 .guard(String.valueOf(policyParameters.getGuard()))
123                 .ttlDate(date)
124                 .build());
125
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);
131         return response;
132     }
133 }