Improve metadata support
[clamp.git] / src / main / java / org / onap / clamp / policy / downloader / PolicyEngineController.java
index f3eaf0c..0c3f677 100644 (file)
@@ -26,18 +26,15 @@ package org.onap.clamp.policy.downloader;
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
 
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map.Entry;
+import java.time.Instant;
 
+import org.json.simple.parser.ParseException;
 import org.onap.clamp.clds.client.PolicyEngineServices;
 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
@@ -56,31 +53,26 @@ public class PolicyEngineController {
 
     private final PolicyEngineServices policyEngineServices;
 
+    private Instant lastInstantExecuted;
+
     @Autowired
     public PolicyEngineController(PolicyEngineServices policyEngineService,
-            PolicyModelsRepository policyModelsRepository) {
+                                  PolicyModelsRepository policyModelsRepository) {
         this.policyEngineServices = policyEngineService;
     }
 
-    @Scheduled(fixedRate = 120000)
-    public void synchronizeAllPolicies() {
-        LinkedHashMap<String, Object> loadedYaml;
-        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;
-        }
-
-        List<LinkedHashMap<String, Object>> policyTypesList = (List<LinkedHashMap<String, Object>>) loadedYaml
-                .get("policy_types");
-        policyTypesList.parallelStream().forEach(policyType -> {
-            Entry<String, Object> policyTypeEntry = (Entry<String, Object>) new ArrayList(policyType.entrySet()).get(0);
-
-            policyEngineServices.createPolicyInDbIfNeeded(
-                    policyEngineServices.createPolicyModelFromPolicyEngine(policyTypeEntry.getKey(),
-                            ((String) ((LinkedHashMap<String, Object>) policyTypeEntry.getValue()).get("version"))));
+    @Scheduled(fixedRate = 300000)
+    public synchronized void synchronizeAllPolicies() {
+        policyEngineServices.synchronizeAllPolicies();
+        lastInstantExecuted = Instant.now();
+    }
 
-        });
+    public Instant getLastInstantExecuted() {
+        return lastInstantExecuted;
     }
 
+    @Scheduled(fixedRate = 300000)
+    public synchronized void downloadPdpGroups() throws ParseException {
+        policyEngineServices.downloadPdpGroups();
+    }
 }