* Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
* reserved.
* ================================================================================
+ * Modifications Copyright (c) 2019 Samsung
+ * ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
import org.onap.policy.api.PushPolicyParameters;
import org.onap.policy.api.RuleProvider;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Primary
public class PolicyClient {
- protected PolicyEngine policyEngine;
- protected static final String LOG_POLICY_PREFIX = "Response is ";
- protected static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyClient.class);
- protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
- public static final String POLICY_MSTYPE_PROPERTY_NAME = "policy.ms.type";
- public static final String POLICY_ONAPNAME_PROPERTY_NAME = "policy.onap.name";
- public static final String POLICY_BASENAME_PREFIX_PROPERTY_NAME = "policy.base.policyNamePrefix";
- public static final String POLICY_OP_NAME_PREFIX_PROPERTY_NAME = "policy.op.policyNamePrefix";
+ private PolicyEngine policyEngine;
+ private static final String LOG_POLICY_PREFIX = "Response is ";
+ private static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyClient.class);
+ private static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
+ private static final String POLICY_MSTYPE_PROPERTY_NAME = "policy.ms.type";
+ private static final String POLICY_ONAPNAME_PROPERTY_NAME = "policy.onap.name";
+ private static final String POLICY_BASENAME_PREFIX_PROPERTY_NAME = "policy.base.policyNamePrefix";
+ private static final String POLICY_OP_NAME_PREFIX_PROPERTY_NAME = "policy.op.policyNamePrefix";
public static final String POLICY_MS_NAME_PREFIX_PROPERTY_NAME = "policy.ms.policyNamePrefix";
- public static final String POLICY_OP_TYPE_PROPERTY_NAME = "policy.op.type";
- public static final String POLICY_GUARD_SUFFIX = "_Guard";
- public static final String TOSCA_FILE_TEMP_PATH = "tosca.filePath";
+ private static final String POLICY_OP_TYPE_PROPERTY_NAME = "policy.op.type";
+ private static final String TOSCA_FILE_TEMP_PATH = "tosca.filePath";
+ private static final String POLICY_COMMUNICATION_LOG_MESSAGE = "Exception occurred during policy communication";
+ private static final String POLICY_COMMUNICATION_EXC_MESSAGE = "Exception while communicating with Policy";
+ private static final String POLICY = "Policy";
@Autowired
- protected ApplicationContext appContext;
- @Autowired
- protected ClampProperties refProp;
+ private ClampProperties refProp;
@Autowired
private PolicyConfiguration policyConfiguration;
if ((PolicyClass.Decision.equals(policyParameters.getPolicyClass()) && !checkDecisionPolicyExists(prop))
|| (PolicyClass.Config.equals(policyParameters.getPolicyClass())
&& !checkPolicyExists(prop, policyPrefix, policyNameWithPrefix))) {
- LoggingUtils.setTargetContext("Policy", "createPolicy");
+ LoggingUtils.setTargetContext(POLICY, "createPolicy");
logger.info("Attempting to create policy for action=" + prop.getActionCd());
response = getPolicyEngine().createPolicy(policyParameters);
responseMessage = response.getResponseMessage();
} else {
- LoggingUtils.setTargetContext("Policy", "updatePolicy");
+ LoggingUtils.setTargetContext(POLICY, "updatePolicy");
logger.info("Attempting to update policy for action=" + prop.getActionCd());
response = getPolicyEngine().updatePolicy(policyParameters);
responseMessage = response.getResponseMessage();
} catch (Exception e) {
LoggingUtils.setResponseContext("900", "Policy send failed", this.getClass().getName());
LoggingUtils.setErrorContext("900", "Policy send error");
- logger.error("Exception occurred during policy communication", e);
- throw new PolicyClientException("Exception while communicating with Policy", e);
+ logger.error(POLICY_COMMUNICATION_LOG_MESSAGE, e);
+ throw new PolicyClientException(POLICY_COMMUNICATION_EXC_MESSAGE, e);
}
logger.info(LOG_POLICY_PREFIX + responseMessage);
LoggingUtils.setTimeContext(startTime, new Date());
PolicyChangeResponse response;
String responseMessage = "";
try {
- LoggingUtils.setTargetContext("Policy", "pushPolicy");
+ LoggingUtils.setTargetContext(POLICY, "pushPolicy");
logger.info("Attempting to push policy...");
response = getPolicyEngine().pushPolicy(pushPolicyParameters);
if (response != null) {
} catch (Exception e) {
LoggingUtils.setResponseContext("900", "Policy push failed", this.getClass().getName());
LoggingUtils.setErrorContext("900", "Policy push error");
- logger.error("Exception occurred during policy communication", e);
- throw new PolicyClientException("Exception while communicating with Policy", e);
+ logger.error(POLICY_COMMUNICATION_LOG_MESSAGE, e);
+ throw new PolicyClientException(POLICY_COMMUNICATION_EXC_MESSAGE, e);
}
logger.info(LOG_POLICY_PREFIX + responseMessage);
if (response != null && (response.getResponseCode() == 200 || response.getResponseCode() == 204)) {
* Use list Policy API to retrieve the policy. Return true if policy exists
* otherwise return false.
*
- * @param policyNamePrefix
- * The Policy Name Prefix
* @param prop
* The ModelProperties
+ * @param policyPrefix
+ * The Policy Name Prefix
+ * @param policyNameWithPrefix
+ * The Policy Full Name
* @return The response message from policy
* @throws PolicyConfigException
* In case of issues with policy engine
* @return The response message from Policy
*/
public String deleteMicrosService(ModelProperties prop) {
- String deletePolicyResponse = "";
- try {
- String policyNamePrefix = refProp.getStringValue(POLICY_MS_NAME_PREFIX_PROPERTY_NAME);
- if (checkPolicyExists(prop, policyNamePrefix, null)) {
- String policyType = refProp.getStringValue(POLICY_MSTYPE_PROPERTY_NAME);
- deletePolicyResponse = deletePolicy(prop, policyType, null);
- }
- } catch (Exception e) {
- logger.error("Exception occurred during policy communication", e);
- throw new PolicyClientException("Exception while communicating with Policy", e);
- }
- return deletePolicyResponse;
+ return findAndDelete(prop, POLICY_MS_NAME_PREFIX_PROPERTY_NAME, POLICY_MSTYPE_PROPERTY_NAME);
}
/**
deletePolicyResponse = deletePolicy(prop, DictionaryType.Decision.toString(), null);
}
} catch (Exception e) {
- logger.error("Exception occurred during policy communication", e);
- throw new PolicyClientException("Exception while communicating with Policy", e);
+ logger.error(POLICY_COMMUNICATION_LOG_MESSAGE, e);
+ throw new PolicyClientException(POLICY_COMMUNICATION_EXC_MESSAGE, e);
}
return deletePolicyResponse;
}
* @return The response message from policy
*/
public String deleteBrms(ModelProperties prop) {
+ return findAndDelete(prop, POLICY_OP_NAME_PREFIX_PROPERTY_NAME, POLICY_OP_TYPE_PROPERTY_NAME);
+ }
+
+ private String findAndDelete(ModelProperties prop, String policyPrefixProperty, String policyTypeProperty) {
String deletePolicyResponse = "";
try {
- String policyNamePrefix = refProp.getStringValue(POLICY_OP_NAME_PREFIX_PROPERTY_NAME);
+ String policyNamePrefix = refProp.getStringValue(policyPrefixProperty);
if (checkPolicyExists(prop, policyNamePrefix, null)) {
- String policyType = refProp.getStringValue(POLICY_OP_TYPE_PROPERTY_NAME);
+ String policyType = refProp.getStringValue(policyTypeProperty);
deletePolicyResponse = deletePolicy(prop, policyType, null);
}
} catch (Exception e) {
- logger.error("Exception occurred during policy communication", e);
- throw new PolicyClientException("Exception while communicating with Policy", e);
+ logger.error(POLICY_COMMUNICATION_LOG_MESSAGE, e);
+ throw new PolicyClientException(POLICY_COMMUNICATION_EXC_MESSAGE, e);
}
return deletePolicyResponse;
}
}
/**
- * Method to return correct policy name with prefix
- *
+ * Method to return correct policy name with prefix.
+ *
* @param prop
* The ModelProperties
* @param policyPrefix
* Policy Prefix
* @param policyNameWithPrefix
* Policy Name With Prefix
- * @return
+ * @return The policy name with the prefix
*/
protected String selectRightPolicyNameWithPrefix(ModelProperties prop, String policyPrefix,
String policyNameWithPrefix) {
deletePolicyParameters.setPdpGroup(refProp.getStringValue("policy.pdp.group"));
deletePolicyParameters.setPolicyType(policyType);
// send delete request
- StringBuilder responseMessage = new StringBuilder(sendDeletePolicy(deletePolicyParameters, prop));
logger.info("Deleting policy from PAP...");
+ StringBuilder responseMessage = new StringBuilder(sendDeletePolicy(deletePolicyParameters, prop));
deletePolicyParameters.setPolicyComponent("PAP");
deletePolicyParameters.setDeleteCondition(DeletePolicyCondition.ALL);
// send delete request
}
/**
- * Create a temp Tosca model file and perform import model to Policy Engine
+ * Create a temp Tosca model file and perform import model to Policy Engine.
*
* @param cldsToscaModel
* Policy model details
cldsToscaModel.getToscaModelYaml().getBytes().length);
}
} catch (IOException e) {
- logger.error("Exception caught when attempting to write Tosca files to disk", e);
+ logger.error("Exception caught when attempting to write Tosca files to disk", e);
throw new PolicyClientException("Exception caught when attempting to write Tosca files to disk", e);
}
}
/**
+ * Import the model.
* @param importParameters
* The ImportParameters
* @return The response message from policy
*/
- protected String importModel(ImportParameters importParameters) {
+ private String importModel(ImportParameters importParameters) {
PolicyChangeResponse response = null;
String responseMessage = "";
} catch (Exception e) {
LoggingUtils.setResponseContext("900", "Policy Model import failed", this.getClass().getName());
LoggingUtils.setErrorContext("900", "Policy Model import error");
- logger.error("Exception occurred during policy communication", e);
- throw new PolicyClientException("Exception while communicating with Policy", e);
+ logger.error(POLICY_COMMUNICATION_LOG_MESSAGE, e);
+ throw new PolicyClientException(POLICY_COMMUNICATION_EXC_MESSAGE, e);
}
logger.info(LOG_POLICY_PREFIX + responseMessage);
if (response != null && (response.getResponseCode() == 200 || response.getResponseCode() == 204)) {
}
/**
+ * Build file path for tosca file.
* @param clampToscaPath
* Temp directory path for writing tosca files
* @param toscaModelName