Add PAP Policy parameter builder xacml policy
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / ConfigPolicyService.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 java.util.Map;
23
24 import org.onap.policy.api.AttributeType;
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.utils.PolicyUtils;
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  * Config Base Policy Implementation.
37  *
38  * @version 0.1
39  */
40 public class ConfigPolicyService {
41     private static final Logger LOGGER = FlexLogger.getLogger(ConfigPolicyService.class.getName());
42     private PAPServices papServices = null;
43
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 String onapName = null;
50     private String configName = null;
51
52     public ConfigPolicyService(String policyName, String policyScope,
53                                PolicyParameters policyParameters, String date) {
54         this.policyParameters = policyParameters;
55         this.policyName = policyName;
56         this.policyScope = policyScope;
57         this.date = date;
58         papServices = new PAPServices();
59     }
60
61     public Boolean getValidation() {
62         if (policyParameters.getConfigBody() == null || policyParameters.getConfigBody().trim().isEmpty()) {
63             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Body given.";
64             return false;
65         }
66         if (policyParameters.getConfigBodyType() == null) {
67             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Body Type given.";
68             return false;
69         }
70         boolean levelCheck = false;
71         levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
72         if (!levelCheck) {
73             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
74             return false;
75         }
76         onapName = policyParameters.getOnapName();
77         configName = policyParameters.getConfigName();
78         if (onapName == null || onapName.trim().isEmpty()) {
79             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No ONAP Name given.";
80             return false;
81         }
82         if (configName == null || configName.trim().isEmpty()) {
83             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Name given.";
84             return false;
85         }
86         message = PolicyUtils.policySpecialCharValidator(onapName);
87         if (!message.contains("success")) {
88             message = XACMLErrorConstants.ERROR_DATA_ISSUE + message;
89             return false;
90         }
91         message = PolicyUtils.policySpecialCharValidator(configName);
92         if (!message.contains("success")) {
93             message = XACMLErrorConstants.ERROR_DATA_ISSUE + message;
94             return false;
95         }
96         return true;
97     }
98
99     public String getMessage() {
100         return message;
101     }
102
103     public String getResult(boolean updateFlag) throws PolicyException {
104         String response = null;
105         String operation = null;
106         if (updateFlag) {
107             operation = "update";
108         } else {
109             operation = "create";
110         }
111         String configType = policyParameters.getConfigBodyType().toString();
112         String body = policyParameters.getConfigBody();
113         String configBody = null;
114         //check body for JSON form and remove single quotes if present
115         if ("JSON".equalsIgnoreCase(configType)) {
116             if (body.contains("'")) {
117                 configBody = body.replace("'", "\"");
118             } else {
119                 configBody = body;
120             }
121         } else {
122             configBody = body;
123         }
124         Map<String, String> configAttributes = null;
125         if (policyParameters.getAttributes() != null) {
126             configAttributes = policyParameters.getAttributes().get(AttributeType.MATCHING);
127         }
128         // create Policy.
129         StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
130                 .configPolicyType("Base")
131                 .policyName(policyName)
132                 .description(policyParameters.getPolicyDescription())
133                 .onapName(onapName)
134                 .configName(configName)
135                 .attributes(configAttributes)
136                 .configType(configType)
137                 .configBodyData(configBody)
138                 .editPolicy(updateFlag)
139                 .domain(policyScope)
140                 .highestVersion(0)
141                 .riskLevel(policyParameters.getRiskLevel())
142                 .riskType(policyParameters.getRiskType())
143                 .guard(String.valueOf(policyParameters.getGuard()))
144                 .ttlDate(date)
145                 .build());
146         // Send Json to PAP.
147         response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
148                 "policyType=Config"}, policyParameters.getRequestID(), "Config");
149         LOGGER.info(response);
150         return response;
151     }
152
153 }