X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fclds%2Fclient%2FPolicyEngineServices.java;h=5e6d9d98d011f2dd7fbe37f5df08472f3cf1f812;hb=e65d457a2dfd6ebb5e1f5a28b74f05c16c285dc3;hp=96294207a31a8e4e125cf604450e85fd8e9be8c6;hpb=67bb8026ac17792efc7c51e9f7905f918c965e01;p=clamp.git diff --git a/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java b/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java index 96294207..5e6d9d98 100644 --- a/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java +++ b/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java @@ -25,96 +25,171 @@ package org.onap.clamp.clds.client; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; - +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; import org.apache.camel.CamelContext; import org.apache.camel.Exchange; import org.apache.camel.builder.ExchangeBuilder; import org.onap.clamp.clds.config.ClampProperties; import org.onap.clamp.clds.sdc.controller.installer.BlueprintMicroService; +import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.loop.template.PolicyModel; +import org.onap.clamp.loop.template.PolicyModelsService; +import org.onap.clamp.policy.pdpgroup.PdpGroup; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import org.yaml.snakeyaml.Yaml; + /** * The class implements the communication with the Policy Engine to retrieve * policy models (tosca). It mainly delegates the physical calls to Camel * engine. - * */ @Component public class PolicyEngineServices { private final CamelContext camelContext; + private final PolicyModelsService policyModelsService; + private static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyEngineServices.class); - private static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger(); - private static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); private static int retryInterval = 0; private static int retryLimit = 1; public static final String POLICY_RETRY_INTERVAL = "policy.retry.interval"; public static final String POLICY_RETRY_LIMIT = "policy.retry.limit"; + /** + * Default constructor. + * + * @param camelContext Camel context bean + * @param clampProperties ClampProperties bean + * @param policyModelsService policyModel service + */ @Autowired - public PolicyEngineServices(CamelContext camelContext, ClampProperties refProp) { + public PolicyEngineServices(CamelContext camelContext, ClampProperties clampProperties, + PolicyModelsService policyModelsService) { this.camelContext = camelContext; - - if (refProp.getStringValue(POLICY_RETRY_LIMIT) != null) { - retryLimit = Integer.valueOf(refProp.getStringValue(POLICY_RETRY_LIMIT)); + this.policyModelsService = policyModelsService; + if (clampProperties.getStringValue(POLICY_RETRY_LIMIT) != null) { + retryLimit = Integer.parseInt(clampProperties.getStringValue(POLICY_RETRY_LIMIT)); } - if (refProp.getStringValue(POLICY_RETRY_INTERVAL) != null) { - retryInterval = Integer.valueOf(refProp.getStringValue(POLICY_RETRY_INTERVAL)); + if (clampProperties.getStringValue(POLICY_RETRY_INTERVAL) != null) { + retryInterval = Integer.parseInt(clampProperties.getStringValue(POLICY_RETRY_INTERVAL)); } } - public PolicyModel createPolicyModelFromPolicyEngine(String policyType, String policyVersion) - throws InterruptedException { - return new PolicyModel(policyType, this.downloadOnePolicy(policyType, policyVersion), policyVersion, - createPolicyAcronym(policyType)); + /** + * This method query Policy engine and create a PolicyModel object with type and version. + * + * @param policyType The policyType id + * @param policyVersion The policy version of that type + * @return A PolicyModel created from policyEngine data + */ + public PolicyModel createPolicyModelFromPolicyEngine(String policyType, String policyVersion) { + return new PolicyModel(policyType, this.downloadOnePolicy(policyType, policyVersion), policyVersion); } - public PolicyModel createPolicyModelFromPolicyEngine(BlueprintMicroService microService) - throws InterruptedException { + /** + * This method query Policy engine and create a PolicyModel object with type and version. + * + * @param microService microservice object instance + * @return A PolicyModel created from policyEngine data + */ + public PolicyModel createPolicyModelFromPolicyEngine(BlueprintMicroService microService) { return createPolicyModelFromPolicyEngine(microService.getModelType(), microService.getModelVersion()); } - private static String createPolicyAcronym(String policyType) { - String[] policyNameArray = policyType.split("\\."); - return policyNameArray[policyNameArray.length - 1]; + /** + * This method synchronize the clamp database and the policy engine. + * So it creates the required PolicyModel. + */ + public void synchronizeAllPolicies() { + LinkedHashMap loadedYaml; + loadedYaml = new Yaml().load(downloadAllPolicies()); + if (loadedYaml == null || loadedYaml.isEmpty()) { + logger.warn("getAllPolicyType yaml returned by policy engine could not be decoded, as it's null or empty"); + return; + } + + LinkedHashMap policyTypesMap = (LinkedHashMap) loadedYaml + .get("policy_types"); + policyTypesMap.entrySet().stream().forEach(entryPolicyType -> { + policyModelsService.createPolicyInDbIfNeeded( + createPolicyModelFromPolicyEngine(entryPolicyType.getKey(), + ((String) ((LinkedHashMap) entryPolicyType.getValue()).get("version")))); + }); } /** * This method can be used to download all policy types + data types defined in * policy engine. - * + * * @return A yaml containing all policy Types and all data types - * @throws InterruptedException In case of issue when sleeping during the retry */ - public String downloadAllPolicies() throws InterruptedException { - return callCamelRoute(ExchangeBuilder.anExchange(camelContext).build(), "direct:get-all-policy-models"); + public String downloadAllPolicies() { + return callCamelRoute(ExchangeBuilder.anExchange(camelContext).build(), "direct:get-all-policy-models", + "Get all policies"); } /** * This method can be used to download a policy tosca model on the engine. - * + * * @param policyType The policy type (id) * @param policyVersion The policy version * @return A string with the whole policy tosca model - * @throws InterruptedException In case of issue when sleeping during the retry */ - public String downloadOnePolicy(String policyType, String policyVersion) throws InterruptedException { + public String downloadOnePolicy(String policyType, String policyVersion) { return callCamelRoute(ExchangeBuilder.anExchange(camelContext).withProperty("policyModelName", policyType) - .withProperty("policyModelVersion", policyVersion).build(), "direct:get-policy-model"); + .withProperty("policyModelVersion", policyVersion).build(), "direct:get-policy-model", + "Get one policy"); + } + + /** + * This method can be used to download all Pdp Groups data from policy engine. + */ + public void downloadPdpGroups() { + String responseBody = + callCamelRoute(ExchangeBuilder.anExchange(camelContext).build(), "direct:get-all-pdp-groups", + "Get Pdp Groups"); + + if (responseBody == null || responseBody.isEmpty()) { + logger.warn("getPdpGroups returned by policy engine could not be decoded, as it's null or empty"); + return; + } + + JsonObject jsonObj = JsonUtils.GSON.fromJson(responseBody, JsonObject.class); + + List pdpGroupList = new LinkedList<>(); + JsonArray itemsArray = (JsonArray) jsonObj.get("groups"); + + Iterator it = itemsArray.iterator(); + while (it.hasNext()) { + JsonObject item = (JsonObject) it.next(); + PdpGroup pdpGroup = JsonUtils.GSON.fromJson(item.toString(), PdpGroup.class); + pdpGroupList.add(pdpGroup); + } + + policyModelsService.updatePdpGroupInfo(pdpGroupList); } - private String callCamelRoute(Exchange exchange, String camelFlow) throws InterruptedException { + private String callCamelRoute(Exchange exchange, String camelFlow, String logMsg) { for (int i = 0; i < retryLimit; i++) { Exchange exchangeResponse = camelContext.createProducerTemplate().send(camelFlow, exchange); if (Integer.valueOf(200).equals(exchangeResponse.getIn().getHeader("CamelHttpResponseCode"))) { return (String) exchangeResponse.getIn().getBody(); } else { - logger.info("Policy query " + retryInterval + "ms before retrying ..."); + logger.info(logMsg + " query " + retryInterval + "ms before retrying ..."); // wait for a while and try to connect to DCAE again - Thread.sleep(retryInterval); + try { + Thread.sleep(retryInterval); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } } } return "";