2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.clamp.clds.client;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.Iterator;
30 import java.util.List;
32 import java.util.UUID;
34 import org.onap.clamp.clds.model.prop.ModelProperties;
35 import org.onap.clamp.clds.model.refprop.RefProp;
36 import org.onap.policy.api.AttributeType;
37 import org.onap.policy.api.ConfigRequestParameters;
38 import org.onap.policy.api.DeletePolicyCondition;
39 import org.onap.policy.api.DeletePolicyParameters;
40 import org.onap.policy.api.PolicyChangeResponse;
41 import org.onap.policy.api.PolicyConfig;
42 import org.onap.policy.api.PolicyConfigType;
43 import org.onap.policy.api.PolicyEngine;
44 import org.onap.policy.api.PolicyParameters;
45 import org.onap.policy.api.PolicyType;
46 import org.onap.policy.api.PushPolicyParameters;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.beans.factory.annotation.Value;
49 import org.springframework.context.ApplicationContext;
51 import com.att.eelf.configuration.EELFLogger;
52 import com.att.eelf.configuration.EELFManager;
55 * Policy utility methods - specifically, send the policy.
57 public class PolicyClient {
58 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyClient.class);
59 protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
61 @Value("${org.onap.clamp.config.files.cldsPolicyConfig:'classpath:/clds/clds-policy-config.properties'}")
62 protected String cldsPolicyConfigFile;
65 protected ApplicationContext appContext;
68 protected RefProp refProp;
70 public PolicyClient() {
75 * Perform send of microservice policy
79 * @param policyRequestUUID
83 public String sendBrms(Map<AttributeType, Map<String, String>> attributes, ModelProperties prop,
84 String policyRequestUUID) throws Exception {
86 PolicyParameters policyParameters = new PolicyParameters();
88 // Set Policy Type(Mandatory)
89 policyParameters.setPolicyConfigType(PolicyConfigType.BRMS_PARAM);
91 // Set Policy Name(Mandatory)
92 policyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId());
94 // documentation says this is options, but when tested, got the
95 // following failure: java.lang.Exception: Policy send failed: PE300 -
96 // Data Issue: No policyDescription given.
97 policyParameters.setPolicyDescription(refProp.getStringValue("op.policyDescription"));
99 policyParameters.setAttributes(attributes);
101 // Set a random UUID(Mandatory)
102 policyParameters.setRequestID(UUID.fromString(policyRequestUUID));
103 String policyNamePrefix = refProp.getStringValue("policy.op.policyNamePrefix");
104 String rtnMsg = send(policyParameters, prop, policyNamePrefix);
106 String policyType = refProp.getStringValue("policy.op.type");
107 push(policyType, prop);
113 * Perform send of microservice policy
117 * @param policyRequestUUID
121 public String sendMicroService(String policyJson, ModelProperties prop, String policyRequestUUID) throws Exception {
123 PolicyParameters policyParameters = new PolicyParameters();
126 policyParameters.setPolicyConfigType(PolicyConfigType.MicroService);
127 policyParameters.setOnapName(refProp.getStringValue("policy.ecomp.name"));
128 policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
130 policyParameters.setConfigBody(policyJson);
131 policyParameters.setConfigBodyType(PolicyType.JSON);
133 policyParameters.setRequestID(UUID.fromString(policyRequestUUID));
134 String policyNamePrefix = refProp.getStringValue("policy.ms.policyNamePrefix");
135 prop.setPolicyUniqueId("");//Adding this line to clear the policy id from policy name while pushing to policy engine
136 String rtnMsg = send(policyParameters, prop, policyNamePrefix);
137 String policyType = refProp.getStringValue("policy.ms.type");
138 push(policyType, prop);
144 * Perform send of policy.
146 * @param policyParameters
151 protected String send(PolicyParameters policyParameters, ModelProperties prop, String policyNamePrefix) throws Exception {
152 // Verify whether it is triggered by Validation Test button from UI
153 if ( prop.isTest() ) {
154 return "send not executed for test action";
157 PolicyEngine policyEngine = new PolicyEngine(
158 appContext.getResource(cldsPolicyConfigFile).getFile().getAbsolutePath());
160 // API method to create or update Policy.
161 PolicyChangeResponse response = null;
162 String responseMessage;
164 List<Integer> versions = getVersions(policyNamePrefix, prop);
165 if (versions.size() <= 0) {
166 logger.info("Attempting to create policy for action=" + prop.getActionCd());
167 response = policyEngine.createPolicy(policyParameters);
168 responseMessage = response.getResponseMessage();
170 logger.info("Attempting to update policy for action=" + prop.getActionCd());
171 response = policyEngine.updatePolicy(policyParameters);
172 responseMessage = response.getResponseMessage();
174 } catch (Exception e) {
175 responseMessage = e.toString();
177 logger.info("response is " + responseMessage);
179 if (response != null && response.getResponseCode() == 200) {
180 logger.info("Policy send successful");
182 logger.warn("Policy send failed: " + responseMessage);
183 throw new Exception("Policy send failed: " + responseMessage);
186 return responseMessage;
190 * Format and send push of policy.
197 protected String push(String policyType, ModelProperties prop) throws Exception {
198 // Verify whether it is triggered by Validation Test button from UI
199 if ( prop.isTest() ) {
200 return "push not executed for test action";
203 PushPolicyParameters pushPolicyParameters = new PushPolicyParameters();
205 // Parameter arguments
206 if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) {
207 pushPolicyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId());
209 pushPolicyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
211 logger.info("Policy Name in Push policy method - " + pushPolicyParameters.getPolicyName());
213 pushPolicyParameters.setPolicyType(policyType);
214 pushPolicyParameters.setPdpGroup(refProp.getStringValue("policy.pdp.group"));
215 pushPolicyParameters.setRequestID(null);
217 PolicyEngine policyEngine = new PolicyEngine(
218 appContext.getResource(cldsPolicyConfigFile).getFile().getAbsolutePath());
220 // API method to create or update Policy.
221 PolicyChangeResponse response = null;
222 String responseMessage;
224 logger.info("Attempting to push policy...");
225 response = policyEngine.pushPolicy(pushPolicyParameters);
226 responseMessage = response.getResponseMessage();
227 } catch (Exception e) {
228 responseMessage = e.toString();
230 logger.info("response is " + responseMessage);
232 if (response != null && (response.getResponseCode() == 200 || response.getResponseCode() == 204)) {
233 logger.info("Policy push successful");
235 logger.warn("Policy push failed: " + responseMessage);
236 throw new Exception("Policy push failed: " + responseMessage);
239 return responseMessage;
243 * Use Get Config Policy API to retrieve the versions for a policy. Return
244 * versions in sorted order. Return empty list if none found.
246 * @param policyNamePrefix
251 protected List<Integer> getVersions(String policyNamePrefix, ModelProperties prop) throws Exception {
253 ArrayList<Integer> versions = new ArrayList<>();
254 ConfigRequestParameters configRequestParameters = new ConfigRequestParameters();
255 String policyName = "";
257 if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) {
258 policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix) + "_" + prop.getPolicyUniqueId();
260 policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix);
263 logger.info("policyName=" + policyName);
264 configRequestParameters.setPolicyName(policyName);
266 PolicyEngine policyEngine = new PolicyEngine(
267 appContext.getResource(cldsPolicyConfigFile).getFile().getAbsolutePath());
270 Collection<PolicyConfig> response = policyEngine.getConfig(configRequestParameters);
271 Iterator<PolicyConfig> itrResp = response.iterator();
273 while (itrResp.hasNext()) {
274 PolicyConfig policyConfig = itrResp.next();
276 Integer version = new Integer(policyConfig.getPolicyVersion());
277 versions.add(version);
278 } catch (Exception e) {
279 // just print warning - if n;o policies, version may be null
281 "warning: failed to parse policyConfig.getPolicyVersion()=" + policyConfig.getPolicyVersion());
284 Collections.sort(versions);
285 logger.info("Policy versions.size()=" + versions.size());
286 } catch (Exception e) {
287 // just print warning - if no policy version found
288 logger.warn("warning: policy not found...policy name - " + policyName);
295 * Format and send delete Micro Service requests to Policy
301 public String deleteMicrosService(ModelProperties prop) throws Exception {
302 String policyNamePrefix = refProp.getStringValue("policy.ms.policyNamePrefix");
303 String policyType = refProp.getStringValue("policy.ms.type");
304 return deletePolicy(policyNamePrefix, prop, policyType);
308 * Format and send delete BRMS requests to Policy
314 public String deleteBrms(ModelProperties prop) throws Exception {
315 String policyNamePrefix = refProp.getStringValue("policy.op.policyNamePrefix");
316 String policyType = refProp.getStringValue("policy.op.type");
317 return deletePolicy(policyNamePrefix, prop, policyType);
321 * Format and send delete PAP and PDP requests to Policy
323 * @param policyNamePrefix
328 protected String deletePolicy(String policyNamePrefix, ModelProperties prop, String policyType) throws Exception {
329 DeletePolicyParameters deletePolicyParameters = new DeletePolicyParameters();
331 if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) {
332 deletePolicyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId());
334 deletePolicyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
336 logger.info("Policy Name in delete policy method - " + deletePolicyParameters.getPolicyName());
337 deletePolicyParameters.setPolicyComponent("PDP");
338 deletePolicyParameters.setDeleteCondition(DeletePolicyCondition.ALL);
339 deletePolicyParameters.setPdpGroup(refProp.getStringValue("policy.pdp.group"));
340 deletePolicyParameters.setPolicyType(policyType);
341 // send delete request
342 String responseMessage = null;
343 responseMessage = sendDeletePolicy(deletePolicyParameters, prop);
345 logger.info("Deleting policy from PAP...");
346 deletePolicyParameters.setPolicyComponent("PAP");
347 deletePolicyParameters.setDeleteCondition(DeletePolicyCondition.ALL);
349 // send delete request
350 responseMessage = sendDeletePolicy(deletePolicyParameters, prop);
352 return responseMessage;
356 * Send delete request to Policy
358 * @param deletePolicyParameters
363 protected String sendDeletePolicy(DeletePolicyParameters deletePolicyParameters, ModelProperties prop)
365 // Verify whether it is triggered by Validation Test button from UI
366 if ( prop.isTest() ) {
367 return "delete not executed for test action";
369 PolicyEngine policyEngine = new PolicyEngine(
370 appContext.getResource(cldsPolicyConfigFile).getFile().getAbsolutePath());
372 // API method to create or update Policy.
373 PolicyChangeResponse response = null;
374 String responseMessage;
376 logger.info("Attempting to delete policy...");
377 response = policyEngine.deletePolicy(deletePolicyParameters);
378 responseMessage = response.getResponseMessage();
379 } catch (Exception e) {
380 responseMessage = e.toString();
382 logger.info("response is " + responseMessage);
384 if (response != null && response.getResponseCode() == 200) {
385 logger.info("Policy delete successful");
387 logger.warn("Policy delete failed: " + responseMessage);
388 throw new Exception("Policy delete failed: " + responseMessage);
391 return responseMessage;