import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
-import org.apache.camel.CamelContext;
-import org.onap.clamp.clds.client.DcaeInventoryServices;
-import org.onap.clamp.clds.config.ClampProperties;
+import java.util.LinkedHashMap;
+import java.util.Map.Entry;
+
+import org.onap.clamp.clds.client.PolicyEngineServices;
+import org.onap.clamp.loop.template.PolicyModel;
+import org.onap.clamp.loop.template.PolicyModelId;
+import org.onap.clamp.loop.template.PolicyModelsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.yaml.snakeyaml.Yaml;
/**
* This class implements a periodic job that is done in the background to
@Profile("clamp-policy-controller")
public class PolicyDownloader {
- protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeInventoryServices.class);
+ protected static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyDownloader.class);
protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
public static final String POLICY_RETRY_INTERVAL = "policy.retry.interval";
public static final String POLICY_RETRY_LIMIT = "policy.retry.limit";
- private final CamelContext camelContext;
-
- private final ClampProperties refProp;
+ private final PolicyEngineServices policyEngineServices;
+ private final PolicyModelsRepository policyModelsRepository;
@Autowired
- public PolicyDownloader(CamelContext camelContext, ClampProperties refProp) {
- this.refProp = refProp;
- this.camelContext = camelContext;
+ public PolicyDownloader(PolicyEngineServices policyEngineService, PolicyModelsRepository policyModelsRepository) {
+ this.policyEngineServices = policyEngineService;
+ this.policyModelsRepository = policyModelsRepository;
+ }
+
+ private void createPolicyInDbIfNeeded(PolicyModel policyModel) {
+ if (!policyModelsRepository
+ .existsById(new PolicyModelId(policyModel.getPolicyModelType(), policyModel.getVersion()))) {
+ policyModelsRepository.save(policyModel);
+ }
+ }
+
+ @Scheduled(fixedRate = 120000)
+ public void synchronizeAllPolicies() throws InterruptedException {
+ try {
+ LinkedHashMap<String, Object> loadedYaml = new Yaml().load(policyEngineServices.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<String, Object> policyTypesList = (LinkedHashMap<String, Object>) loadedYaml
+ .get("policy_types");
+ for (Entry<String, Object> policyType : policyTypesList.entrySet()) {
+ createPolicyInDbIfNeeded(policyEngineServices.createPolicyModelFromPolicyEngine(policyType.getKey(),
+ ((String) ((LinkedHashMap<String, Object>) policyType.getValue()).get("version"))));
+ }
+ } catch (InterruptedException e) {
+ logger.warn("query to policy engine has been interrupted", e);
+ throw e;
+ }
+
}
}