Add PAP Policy parameter builder xacml policy
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / ClosedLoopFaultPolicyService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
4  * ================================================================================
5  * Copyright (C) 2017 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  * Closed Loop Fault Policy Implementation.
35  *
36  * @version 0.1
37  */
38 public class ClosedLoopFaultPolicyService {
39     private static final Logger LOGGER = FlexLogger.getLogger(ClosedLoopFaultPolicyService.class.getName());
40     private PAPServices papServices = null;
41
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     private JsonObject configBody = null;
48
49     public ClosedLoopFaultPolicyService(String policyName, String policyScope,
50                                         PolicyParameters policyParameters, String date) {
51         this.policyParameters = policyParameters;
52         this.policyName = policyName;
53         this.policyScope = policyScope;
54         this.date = date;
55         papServices = new PAPServices();
56     }
57
58     public Boolean getValidation() {
59         if (policyParameters.getConfigBody() == null) {
60             message = XACMLErrorConstants.ERROR_DATA_ISSUE + " No Config Body Present";
61             return false;
62         }
63         if (!PolicyApiUtils.validateNONASCIICharactersAndAllowSpaces(policyParameters.getConfigBody())) {
64             message =
65                     XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " +
66                             policyParameters.getConfigBody();
67             return false;
68         }
69         try {
70             configBody = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody());
71         } catch (JsonException | IllegalStateException e) {
72             message =
73                     XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " +
74                             policyParameters.getConfigBody();
75             LOGGER.error("Json Parse Exception.", e);
76             return false;
77         }
78         return true;
79     }
80
81     public String getMessage() {
82         return message;
83     }
84
85     public String getResult(boolean updateFlag) throws PolicyException {
86         String response = null;
87         String operation = null;
88         String oldPolicyName = null;
89         if (updateFlag) {
90             operation = "update";
91             if (policyName.endsWith("_Draft")) {
92                 oldPolicyName = policyName + "_Draft.1";
93             }
94         } else {
95             operation = "create";
96         }
97         // get values and attributes from the JsonObject
98         if (!configBody.containsKey("onapname")) {
99             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given.";
100             LOGGER.error(message);
101             return message;
102         }
103         String onapName = configBody.get("onapname").toString().trim().replace("\"", "");
104         if (onapName == null || onapName.trim().isEmpty()) {
105             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given.";
106             LOGGER.error(message);
107             return message;
108         }
109         boolean levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
110         if (!levelCheck) {
111             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
112             LOGGER.error(message);
113             return message;
114         }
115         String jsonBody = configBody.toString();
116         // Create Policy.
117         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("ClosedLoop_Fault", policyName,
118                 policyParameters.getPolicyDescription(), onapName,
119                 jsonBody, false, oldPolicyName, null, updateFlag, policyScope, 0, policyParameters.getRiskLevel(),
120                 policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date);
121         //send JSON object to PAP
122         response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
123                 "policyType=Config"}, policyParameters.getRequestID(), "ConfigClosedLoop");
124         return response;
125     }
126
127 }