Improve the policy name
[clamp.git] / src / main / java / org / onap / clamp / clds / client / PolicyEngineServices.java
index 2302cc8..260bd1e 100644 (file)
@@ -25,44 +25,40 @@ package org.onap.clamp.clds.client;
 
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
-
-import java.util.ArrayList;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
 import java.util.LinkedHashMap;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
-
 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.PolicyModelId;
-import org.onap.clamp.loop.template.PolicyModelsRepository;
+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.springframework.transaction.annotation.Propagation;
-import org.springframework.transaction.annotation.Transactional;
+import org.yaml.snakeyaml.DumperOptions;
 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 PolicyModelsRepository policyModelsRepository;
+    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;
 
@@ -72,32 +68,39 @@ public class PolicyEngineServices {
     /**
      * Default constructor.
      *
-     * @param camelContext Camel context bean
-     * @param clampProperties ClampProperties bean
-     * @param policyModelsRepository policyModel repository bean
+     * @param camelContext        Camel context bean
+     * @param clampProperties     ClampProperties bean
+     * @param policyModelsService policyModel service
      */
     @Autowired
     public PolicyEngineServices(CamelContext camelContext, ClampProperties clampProperties,
-            PolicyModelsRepository policyModelsRepository) {
+                                PolicyModelsService policyModelsService) {
         this.camelContext = camelContext;
-        this.policyModelsRepository = policyModelsRepository;
+        this.policyModelsService = policyModelsService;
         if (clampProperties.getStringValue(POLICY_RETRY_LIMIT) != null) {
-            retryLimit = Integer.valueOf(clampProperties.getStringValue(POLICY_RETRY_LIMIT));
+            retryLimit = Integer.parseInt(clampProperties.getStringValue(POLICY_RETRY_LIMIT));
         }
         if (clampProperties.getStringValue(POLICY_RETRY_INTERVAL) != null) {
-            retryInterval = Integer.valueOf(clampProperties.getStringValue(POLICY_RETRY_INTERVAL));
+            retryInterval = Integer.parseInt(clampProperties.getStringValue(POLICY_RETRY_INTERVAL));
         }
     }
 
     /**
      * This method query Policy engine and create a PolicyModel object with type and version.
      *
-     * @param policyType The policyType id
+     * @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);
+        if (!policyModelsService.existsById(
+                new PolicyModelId(policyType, policyVersion))) {
+            return policyModelsService.savePolicyModelInNewTransaction(
+                    new PolicyModel(policyType, this.downloadOnePolicy(policyType, policyVersion), policyVersion));
+        }
+        logger.info("Skipping policy model download as it exists already in the database " + policyType
+                + "/" + policyVersion);
+        return null;
     }
 
     /**
@@ -110,19 +113,6 @@ public class PolicyEngineServices {
         return createPolicyModelFromPolicyEngine(microService.getModelType(), microService.getModelVersion());
     }
 
-    /**
-     * Thie method creates an PolicyModel in Db if it does not exist.
-     *
-     * @param policyModel The policyModel to save
-     */
-    @Transactional(propagation = Propagation.REQUIRES_NEW)
-    public void createPolicyInDbIfNeeded(PolicyModel policyModel) {
-        if (!policyModelsRepository
-                .existsById(new PolicyModelId(policyModel.getPolicyModelType(), policyModel.getVersion()))) {
-            policyModelsRepository.save(policyModel);
-        }
-    }
-
     /**
      * This method synchronize the clamp database and the policy engine.
      * So it creates the required PolicyModel.
@@ -135,48 +125,80 @@ public class PolicyEngineServices {
             return;
         }
 
-        List<LinkedHashMap<String, Object>> policyTypesList = (List<LinkedHashMap<String, Object>>) loadedYaml
+        LinkedHashMap<String, Object> policyTypesMap = (LinkedHashMap<String, Object>) loadedYaml
                 .get("policy_types");
-        policyTypesList.parallelStream().forEach(policyType -> {
-            Map.Entry<String, Object> policyTypeEntry = (Map.Entry<String, Object>) new ArrayList(policyType.entrySet()).get(0);
-
-            createPolicyInDbIfNeeded(
-                    createPolicyModelFromPolicyEngine(policyTypeEntry.getKey(),
-                            ((String) ((LinkedHashMap<String, Object>) policyTypeEntry.getValue()).get("version"))));
-        });
+        policyTypesMap.forEach((key, value) ->
+                this.createPolicyModelFromPolicyEngine(key,
+                        ((String) ((LinkedHashMap<String, Object>) value).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() {
-        return callCamelRoute(ExchangeBuilder.anExchange(camelContext).build(), "direct:get-all-policy-models");
+        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) {
-        return callCamelRoute(ExchangeBuilder.anExchange(camelContext).withProperty("policyModelName", policyType)
-                .withProperty("policyModelVersion", policyVersion).build(), "direct:get-policy-model");
+        logger.info("Downloading the policy model " + policyType + "/" + policyVersion);
+        DumperOptions options = new DumperOptions();
+        options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
+        options.setIndent(4);
+        options.setPrettyFlow(true);
+        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
+        Yaml yamlParser = new Yaml(options);
+        return yamlParser.dump((Map<String, Object>) yamlParser.load(callCamelRoute(
+                ExchangeBuilder.anExchange(camelContext).withProperty("policyModelName", policyType)
+                        .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<PdpGroup> pdpGroupList = new LinkedList<>();
+        JsonArray itemsArray = (JsonArray) jsonObj.get("groups");
+
+        for (com.google.gson.JsonElement jsonElement : itemsArray) {
+            JsonObject item = (JsonObject) jsonElement;
+            PdpGroup pdpGroup = JsonUtils.GSON.fromJson(item.toString(), PdpGroup.class);
+            pdpGroupList.add(pdpGroup);
+        }
+
+        policyModelsService.updatePdpGroupInfo(pdpGroupList);
     }
 
-    private String callCamelRoute(Exchange exchange, String camelFlow) {
+    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 ...");
+            }
+            else {
+                logger.info(logMsg + " query " + retryInterval + "ms before retrying ...");
                 // wait for a while and try to connect to DCAE again
                 try {
                     Thread.sleep(retryInterval);