2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ===================================================================
24 package org.onap.clamp.clds.client.req.policy;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
29 import java.util.Collection;
30 import java.util.Date;
32 import java.util.UUID;
34 import javax.ws.rs.BadRequestException;
36 import org.onap.clamp.clds.config.ClampProperties;
37 import org.onap.clamp.clds.config.PolicyConfiguration;
38 import org.onap.clamp.clds.exception.policy.PolicyClientException;
39 import org.onap.clamp.clds.model.properties.ModelProperties;
40 import org.onap.clamp.clds.model.properties.PolicyItem;
41 import org.onap.clamp.clds.util.LoggingUtils;
42 import org.onap.policy.api.AttributeType;
43 import org.onap.policy.api.ConfigRequestParameters;
44 import org.onap.policy.api.DeletePolicyCondition;
45 import org.onap.policy.api.DeletePolicyParameters;
46 import org.onap.policy.api.DictionaryType;
47 import org.onap.policy.api.PolicyChangeResponse;
48 import org.onap.policy.api.PolicyClass;
49 import org.onap.policy.api.PolicyConfigException;
50 import org.onap.policy.api.PolicyConfigType;
51 import org.onap.policy.api.PolicyEngine;
52 import org.onap.policy.api.PolicyEngineException;
53 import org.onap.policy.api.PolicyParameters;
54 import org.onap.policy.api.PolicyType;
55 import org.onap.policy.api.PushPolicyParameters;
56 import org.onap.policy.api.RuleProvider;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.context.ApplicationContext;
59 import org.springframework.context.annotation.Primary;
60 import org.springframework.stereotype.Component;
63 * Policy utility methods - specifically, send the policy.
67 public class PolicyClient {
69 protected PolicyEngine policyEngine;
70 protected static final String LOG_POLICY_PREFIX = "Response is ";
71 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyClient.class);
72 protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
73 public static final String POLICY_MSTYPE_PROPERTY_NAME = "policy.ms.type";
74 public static final String POLICY_ONAPNAME_PROPERTY_NAME = "policy.onap.name";
75 public static final String POLICY_BASENAME_PREFIX_PROPERTY_NAME = "policy.base.policyNamePrefix";
76 public static final String POLICY_OP_NAME_PREFIX_PROPERTY_NAME = "policy.op.policyNamePrefix";
77 public static final String POLICY_MS_NAME_PREFIX_PROPERTY_NAME = "policy.ms.policyNamePrefix";
78 public static final String POLICY_OP_TYPE_PROPERTY_NAME = "policy.op.type";
79 public static final String POLICY_GUARD_SUFFIX = "_Guard";
82 protected ApplicationContext appContext;
84 protected ClampProperties refProp;
86 private PolicyConfiguration policyConfiguration;
89 * Perform Guard policy type.
95 * @param policyRequestUuid
97 * @return The response message of policy
99 public String sendGuardPolicy(Map<AttributeType, Map<String, String>> attributes, ModelProperties prop,
100 String policyRequestUuid, PolicyItem policyItem) {
101 PolicyParameters policyParameters = new PolicyParameters();
102 // Set Policy Type(Mandatory)
103 policyParameters.setPolicyClass(PolicyClass.Decision);
104 // Set Policy Name(Mandatory)
105 policyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueGuardId());
106 // documentation says this is options, but when tested, got the
107 // following failure: java.lang.Exception: Policy send failed: PE300 -
108 // Data Issue: No policyDescription given.
109 policyParameters.setPolicyDescription(refProp.getStringValue("op.policyDescription"));
110 policyParameters.setOnapName("PDPD");
111 policyParameters.setRuleProvider(RuleProvider.valueOf(policyItem.getGuardPolicyType()));
112 policyParameters.setAttributes(attributes);
113 // Set a random UUID(Mandatory)
114 policyParameters.setRequestID(UUID.fromString(policyRequestUuid));
116 String rtnMsg = send(policyParameters, prop, null);
117 push(DictionaryType.Decision.toString(), prop);
122 * Perform BRMS policy type.
125 * A map of attributes
127 * The ModelProperties
128 * @param policyRequestUuid
130 * @return The response message of policy
132 public String sendBrmsPolicy(Map<AttributeType, Map<String, String>> attributes, ModelProperties prop,
133 String policyRequestUuid) {
134 PolicyParameters policyParameters = new PolicyParameters();
135 // Set Policy Type(Mandatory)
136 policyParameters.setPolicyConfigType(PolicyConfigType.BRMS_PARAM);
137 // Set Policy Name(Mandatory)
138 policyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId());
139 // documentation says this is options, but when tested, got the
140 // following failure: java.lang.Exception: Policy send failed: PE300 -
141 // Data Issue: No policyDescription given.
142 policyParameters.setPolicyDescription(refProp.getStringValue("op.policyDescription"));
143 policyParameters.setAttributes(attributes);
144 // Set a random UUID(Mandatory)
145 policyParameters.setRequestID(UUID.fromString(policyRequestUuid));
146 String policyNamePrefix = refProp.getStringValue(POLICY_OP_NAME_PREFIX_PROPERTY_NAME);
147 String rtnMsg = send(policyParameters, prop, policyNamePrefix);
148 String policyType = refProp.getStringValue(POLICY_OP_TYPE_PROPERTY_NAME);
149 push(policyType, prop);
154 * Perform send of microservice policy in JSON.
159 * The ModelProperties
160 * @param policyRequestUuid
161 * The policy Request UUID
162 * @return The response message of policy
164 public String sendMicroServiceInJson(String policyJson, ModelProperties prop, String policyRequestUuid) {
165 PolicyParameters policyParameters = new PolicyParameters();
167 policyParameters.setPolicyConfigType(PolicyConfigType.MicroService);
168 policyParameters.setOnapName(refProp.getStringValue(POLICY_ONAPNAME_PROPERTY_NAME));
169 policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
170 policyParameters.setConfigBody(policyJson);
171 policyParameters.setConfigBodyType(PolicyType.JSON);
172 policyParameters.setRequestID(UUID.fromString(policyRequestUuid));
173 String policyNamePrefix = refProp.getStringValue(POLICY_MS_NAME_PREFIX_PROPERTY_NAME);
174 // Adding this line to clear the policy id from policy name while
175 // pushing to policy engine
176 prop.setPolicyUniqueId("");
177 String rtnMsg = send(policyParameters, prop, policyNamePrefix);
178 String policyType = refProp.getStringValue(POLICY_MSTYPE_PROPERTY_NAME);
179 push(policyType, prop);
184 * Perform send of base policy in OTHER type.
187 * The config policy string body
188 * @param configPolicyName
189 * The config policy name of the component that has been pre-deployed in
192 * The ModelProperties
193 * @param policyRequestUuid
194 * The policy request UUID
195 * @return The answer from policy call
197 public String sendBasePolicyInOther(String configBody, String configPolicyName, ModelProperties prop,
198 String policyRequestUuid) {
199 PolicyParameters policyParameters = new PolicyParameters();
201 policyParameters.setPolicyConfigType(PolicyConfigType.Base);
202 policyParameters.setOnapName(refProp.getStringValue(POLICY_ONAPNAME_PROPERTY_NAME));
203 policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
204 policyParameters.setConfigBody(configBody);
205 policyParameters.setConfigBodyType(PolicyType.OTHER);
206 policyParameters.setConfigName("HolmesPolicy");
207 policyParameters.setPolicyName(configPolicyName);
208 policyParameters.setRequestID(UUID.fromString(policyRequestUuid));
209 // Adding this line to clear the policy id from policy name while
210 // pushing to policy engine
211 prop.setPolicyUniqueId("");
212 String rtnMsg = send(policyParameters, prop, refProp.getStringValue(POLICY_BASENAME_PREFIX_PROPERTY_NAME));
213 push(PolicyConfigType.Base.toString(), prop);
218 * Perform send of Microservice policy in OTHER type.
221 * The config policy string body
223 * The ModelProperties
224 * @return The answer from policy call
226 public String sendMicroServiceInOther(String configBody, ModelProperties prop) {
227 PolicyParameters policyParameters = new PolicyParameters();
229 policyParameters.setPolicyConfigType(PolicyConfigType.MicroService);
230 policyParameters.setOnapName(refProp.getStringValue(POLICY_ONAPNAME_PROPERTY_NAME));
231 policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
232 policyParameters.setConfigBody(configBody);
233 String policyNamePrefix = refProp.getStringValue(POLICY_MS_NAME_PREFIX_PROPERTY_NAME);
234 // Adding this line to clear the policy id from policy name while
235 // pushing to policy engine
236 prop.setPolicyUniqueId("");
237 String rtnMsg = send(policyParameters, prop, policyNamePrefix);
238 String policyType = refProp.getStringValue(POLICY_MSTYPE_PROPERTY_NAME);
239 push(policyType, prop);
244 * Perform send of Configuration or Decision policies.
246 * @param policyParameters
247 * The PolicyParameters
249 * The ModelProperties
250 * @return The response message of Policy
252 protected String send(PolicyParameters policyParameters, ModelProperties prop, String policyNamePrefix) {
253 // Verify whether it is triggered by Validation Test button from UI
254 if (prop.isTestOnly()) {
255 return "send not executed for test action";
257 // API method to create or update Policy.
258 PolicyChangeResponse response = null;
259 String responseMessage = "";
260 Date startTime = new Date();
262 if ((PolicyClass.Decision.equals(policyParameters.getPolicyClass()) && !checkDecisionPolicyExists(prop))
263 || (PolicyClass.Config.equals(policyParameters.getPolicyClass())
264 && !checkPolicyExists(policyNamePrefix, prop))) {
265 LoggingUtils.setTargetContext("Policy", "createPolicy");
266 logger.info("Attempting to create policy for action=" + prop.getActionCd());
267 response = getPolicyEngine().createPolicy(policyParameters);
268 responseMessage = response.getResponseMessage();
270 LoggingUtils.setTargetContext("Policy", "updatePolicy");
271 logger.info("Attempting to update policy for action=" + prop.getActionCd());
272 response = getPolicyEngine().updatePolicy(policyParameters);
273 responseMessage = response.getResponseMessage();
275 } catch (Exception e) {
276 LoggingUtils.setResponseContext("900", "Policy send failed", this.getClass().getName());
277 LoggingUtils.setErrorContext("900", "Policy send error");
278 logger.error("Exception occurred during policy communication", e);
279 throw new PolicyClientException("Exception while communicating with Policy", e);
281 logger.info(LOG_POLICY_PREFIX + responseMessage);
282 LoggingUtils.setTimeContext(startTime, new Date());
283 if (response.getResponseCode() == 200) {
284 LoggingUtils.setResponseContext("0", "Policy send success", this.getClass().getName());
285 logger.info("Policy send successful");
286 metricsLogger.info("Policy send success");
288 LoggingUtils.setResponseContext("900", "Policy send failed", this.getClass().getName());
289 logger.warn("Policy send failed: " + responseMessage);
290 metricsLogger.info("Policy send failure");
291 throw new BadRequestException("Policy send failed: " + responseMessage);
293 return responseMessage;
297 * Format and send push of policy.
302 * The ModelProperties
303 * @return The response message of policy
305 protected String push(String policyType, ModelProperties prop) {
306 // Verify whether it is triggered by Validation Test button from UI
307 if (prop.isTestOnly()) {
308 return "push not executed for test action";
310 PushPolicyParameters pushPolicyParameters = new PushPolicyParameters();
311 // Parameter arguments
312 if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) {
313 if (DictionaryType.Decision.toString().equals(policyType)) {
314 pushPolicyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueGuardId());
316 pushPolicyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId());
319 pushPolicyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
321 logger.info("Policy Name in Push policy method - " + pushPolicyParameters.getPolicyName());
322 pushPolicyParameters.setPolicyType(policyType);
323 pushPolicyParameters.setPdpGroup(refProp.getStringValue("policy.pdp.group"));
324 pushPolicyParameters.setRequestID(null);
325 // API method to create or update Policy.
326 PolicyChangeResponse response;
327 String responseMessage = "";
329 LoggingUtils.setTargetContext("Policy", "pushPolicy");
330 logger.info("Attempting to push policy...");
331 response = getPolicyEngine().pushPolicy(pushPolicyParameters);
332 if (response != null) {
333 responseMessage = response.getResponseMessage();
335 } catch (Exception e) {
336 LoggingUtils.setResponseContext("900", "Policy push failed", this.getClass().getName());
337 LoggingUtils.setErrorContext("900", "Policy push error");
338 logger.error("Exception occurred during policy communication", e);
339 throw new PolicyClientException("Exception while communicating with Policy", e);
341 logger.info(LOG_POLICY_PREFIX + responseMessage);
342 if (response != null && (response.getResponseCode() == 200 || response.getResponseCode() == 204)) {
343 LoggingUtils.setResponseContext("0", "Policy push success", this.getClass().getName());
344 logger.info("Policy push successful");
345 metricsLogger.info("Policy push success");
347 LoggingUtils.setResponseContext("900", "Policy push failed", this.getClass().getName());
348 logger.warn("Policy push failed: " + responseMessage);
349 metricsLogger.info("Policy push failure");
350 throw new BadRequestException("Policy push failed: " + responseMessage);
352 return responseMessage;
356 * Use list Decision policy to know if the decision policy exists.
359 * The model properties
360 * @return true if it exists, false otherwise
362 protected boolean checkDecisionPolicyExists(ModelProperties prop) {
363 boolean policyexists = false;
365 logger.info("Search in Policy Engine for DecisionpolicyName=" + prop.getPolicyScopeAndNameWithUniqueGuardId());
367 // No other choice than pushing to see if it exists or not
368 String response = push(DictionaryType.Decision.toString(), prop);
369 if (response != null) {
372 } catch (BadRequestException e) {
373 // just print warning - if no policy version found
374 logger.warn("Policy not found...policy name - " + prop.getPolicyScopeAndNameWithUniqueGuardId(), e);
380 * Use list Policy API to retrieve the policy. Return true if policy exists
381 * otherwise return false.
383 * @param policyNamePrefix
384 * The Policy Name Prefix
386 * The ModelProperties
387 * @return The response message from policy
388 * @throws PolicyConfigException
389 * In case of issues with policy engine
391 protected boolean checkPolicyExists(String policyNamePrefix, ModelProperties prop) {
392 boolean policyexists = false;
393 String policyName = "";
396 if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) {
397 policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix) + "_"
398 + prop.getPolicyUniqueId();
400 policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix);
402 logger.info("Search in Policy Engine for policyName=" + policyName);
404 ConfigRequestParameters configRequestParameters = new ConfigRequestParameters();
405 configRequestParameters.setPolicyName(policyName);
406 Collection<String> response = getPolicyEngine().listConfig(configRequestParameters);
407 if (response != null && !response.isEmpty() && !response.contains("Policy Name: null")) {
410 } catch (PolicyConfigException e1) {
411 // just print warning - if no policy version found
412 logger.warn("Policy not found...policy name - " + policyName, e1);
418 * This method create a new policy engine.
420 * @return A new policy engine
422 private synchronized PolicyEngine getPolicyEngine() {
424 if (policyEngine == null) {
425 policyEngine = new PolicyEngine(policyConfiguration.getProperties());
427 } catch (PolicyEngineException e) {
428 throw new PolicyClientException("Exception when creating a new policy engine", e);
434 * Format and send delete Micro Service requests to Policy.
437 * The ModelProperties
438 * @return The response message from Policy
440 public String deleteMicrosService(ModelProperties prop) {
441 String deletePolicyResponse = "";
443 String policyNamePrefix = refProp.getStringValue(POLICY_MS_NAME_PREFIX_PROPERTY_NAME);
444 if (checkPolicyExists(policyNamePrefix, prop)) {
445 String policyType = refProp.getStringValue(POLICY_MSTYPE_PROPERTY_NAME);
446 deletePolicyResponse = deletePolicy(prop, policyType);
448 } catch (Exception e) {
449 logger.error("Exception occurred during policy communication", e);
450 throw new PolicyClientException("Exception while communicating with Policy", e);
452 return deletePolicyResponse;
456 * This method delete the Base policy.
459 * The model Properties
460 * @return A string with the answer from policy
462 public String deleteBasePolicy(ModelProperties prop) {
463 return deletePolicy(prop, PolicyConfigType.Base.toString());
467 * Format and send delete Guard requests to Policy.
470 * The ModelProperties
471 * @return The response message from policy
473 public String deleteGuard(ModelProperties prop) {
474 String deletePolicyResponse = "";
477 if (checkDecisionPolicyExists(prop)) {
478 deletePolicyResponse = deletePolicy(prop, DictionaryType.Decision.toString());
480 } catch (Exception e) {
481 logger.error("Exception occurred during policy communication", e);
482 throw new PolicyClientException("Exception while communicating with Policy", e);
484 return deletePolicyResponse;
488 * Format and send delete BRMS requests to Policy.
491 * The ModelProperties
492 * @return The response message from policy
494 public String deleteBrms(ModelProperties prop) {
495 String deletePolicyResponse = "";
497 String policyNamePrefix = refProp.getStringValue(POLICY_OP_NAME_PREFIX_PROPERTY_NAME);
498 if (checkPolicyExists(policyNamePrefix, prop)) {
499 String policyType = refProp.getStringValue(POLICY_OP_TYPE_PROPERTY_NAME);
500 deletePolicyResponse = deletePolicy(prop, policyType);
502 } catch (Exception e) {
503 logger.error("Exception occurred during policy communication", e);
504 throw new PolicyClientException("Exception while communicating with Policy", e);
506 return deletePolicyResponse;
510 * Format and send delete PAP and PDP requests to Policy.
513 * The ModelProperties
514 * @return The response message from policy
516 protected String deletePolicy(ModelProperties prop, String policyType) {
517 DeletePolicyParameters deletePolicyParameters = new DeletePolicyParameters();
518 if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) {
519 if (DictionaryType.Decision.toString().equals(policyType)) {
520 deletePolicyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueGuardId());
522 deletePolicyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId());
525 deletePolicyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
527 logger.info("Policy Name in delete policy method - " + deletePolicyParameters.getPolicyName());
528 deletePolicyParameters.setPolicyComponent("PDP");
529 deletePolicyParameters.setDeleteCondition(DeletePolicyCondition.ALL);
530 deletePolicyParameters.setPdpGroup(refProp.getStringValue("policy.pdp.group"));
531 deletePolicyParameters.setPolicyType(policyType);
532 // send delete request
533 StringBuilder responseMessage = new StringBuilder(sendDeletePolicy(deletePolicyParameters, prop));
534 logger.info("Deleting policy from PAP...");
535 deletePolicyParameters.setPolicyComponent("PAP");
536 deletePolicyParameters.setDeleteCondition(DeletePolicyCondition.ALL);
537 // send delete request
538 responseMessage.append(sendDeletePolicy(deletePolicyParameters, prop));
539 return responseMessage.toString();
543 * Send delete request to Policy.
545 * @param deletePolicyParameters
546 * The DeletePolicyParameters
548 * The ModelProperties
549 * @return The response message from policy
551 protected String sendDeletePolicy(DeletePolicyParameters deletePolicyParameters, ModelProperties prop) {
552 // Verify whether it is triggered by Validation Test button from UI
553 if (prop.isTestOnly()) {
554 return "delete not executed for test action";
556 // API method to create or update Policy.
557 PolicyChangeResponse response = null;
558 String responseMessage = "";
560 logger.info("Attempting to delete policy...");
561 response = getPolicyEngine().deletePolicy(deletePolicyParameters);
562 responseMessage = response.getResponseMessage();
563 } catch (Exception e) {
564 logger.error("Exception occurred during policy communnication", e);
566 logger.info(LOG_POLICY_PREFIX + responseMessage);
567 if (response != null && response.getResponseCode() == 200) {
568 logger.info("Policy delete successful");
570 logger.warn("Policy delete failed: " + responseMessage);
571 throw new BadRequestException("Policy delete failed: " + responseMessage);
573 return responseMessage;