Use builder for std pap policy
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / ClosedLoopPMPolicyService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
4  * ================================================================================
5  * Copyright (C) 2017 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  * Closed Loop PM policy Implementation.
37  *
38  * @version 0.1
39  */
40 public class ClosedLoopPMPolicyService {
41     private static final Logger LOGGER = FlexLogger.getLogger(ClosedLoopPMPolicyService.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     private JsonObject configBody = null;
50
51     public ClosedLoopPMPolicyService(String policyName, String policyScope,
52                                      PolicyParameters policyParameters, String date) {
53         this.policyParameters = policyParameters;
54         this.policyName = policyName;
55         this.policyScope = policyScope;
56         this.date = date;
57         papServices = new PAPServices();
58     }
59
60     public Boolean getValidation() {
61         if (policyParameters.getConfigBody() == null) {
62             message = XACMLErrorConstants.ERROR_DATA_ISSUE + " No Config Body Present";
63             return false;
64         }
65         if (!PolicyApiUtils.validateNONASCIICharactersAndAllowSpaces(policyParameters.getConfigBody())) {
66             message =
67                     XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " +
68                             policyParameters.getConfigBody();
69             return false;
70         }
71         try {
72             configBody = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody());
73         } catch (JsonException | IllegalStateException e) {
74             message =
75                     XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " +
76                             policyParameters.getConfigBody();
77             LOGGER.error("Error during parsing JSON config body for Closed loop PM policy  ", e);
78             return false;
79         }
80         return true;
81     }
82
83     public String getMessage() {
84         return message;
85     }
86
87     public String getResult(boolean updateFlag) throws PolicyException {
88         String response = null;
89         String operation = null;
90         if (updateFlag) {
91             operation = "update";
92         } else {
93             operation = "create";
94         }
95         // get values and attributes from the JsonObject
96         if (!configBody.containsKey("onapname")) {
97             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given.";
98             LOGGER.error(message);
99             return message;
100         }
101         if (!configBody.containsKey("serviceTypePolicyName")) {
102             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Service Type Policy Name given.";
103             LOGGER.error(message);
104             return message;
105         }
106         String onapName = configBody.get("onapname").toString().trim().replace("\"", "");
107         if (onapName == null || onapName.trim().isEmpty()) {
108             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given.";
109             LOGGER.error(message);
110             return message;
111         }
112         boolean levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
113         if (!levelCheck) {
114             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
115             LOGGER.error(message);
116             return message;
117         }
118         String jsonBody = configBody.toString();
119         String serviceType = configBody.get("serviceTypePolicyName").toString().replace("\"", "");
120         // Create Policy.
121         // Creating CloseLoop_Fault and Performance Metric Policies
122         StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
123                 .configPolicyType("ClosedLoop_PM")
124                 .policyName(policyName)
125                 .description(policyParameters.getPolicyDescription())
126                 .onapName(onapName)
127                 .jsonBody(jsonBody)
128                 .draft(false)
129                 .oldPolicyFileName(null)
130                 .serviceType(serviceType)
131                 .editPolicy(updateFlag)
132                 .domain(policyScope)
133                 .highestVersion(0)
134                 .riskLevel(policyParameters.getRiskLevel())
135                 .riskType(policyParameters.getRiskType())
136                 .guard(String.valueOf(policyParameters.getGuard()))
137                 .ttlDate(date)
138                 .build());
139         //send JSON object to PAP
140         response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
141                 "policyType=Config"}, policyParameters.getRequestID(), "ConfigClosedLoop");
142         return response;
143     }
144 }