Add policy downloader
[clamp.git] / src / main / java / org / onap / clamp / clds / client / PolicyEngineServices.java
index d99e9b5..9629420 100644 (file)
@@ -30,6 +30,8 @@ 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.loop.template.PolicyModel;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
@@ -43,32 +45,52 @@ import org.springframework.stereotype.Component;
 public class PolicyEngineServices {
     private final CamelContext camelContext;
 
-    private final ClampProperties refProp;
+    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;
 
-    protected static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyEngineServices.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";
 
     @Autowired
     public PolicyEngineServices(CamelContext camelContext, ClampProperties refProp) {
-        this.refProp = refProp;
         this.camelContext = camelContext;
+
+        if (refProp.getStringValue(POLICY_RETRY_LIMIT) != null) {
+            retryLimit = Integer.valueOf(refProp.getStringValue(POLICY_RETRY_LIMIT));
+        }
+        if (refProp.getStringValue(POLICY_RETRY_INTERVAL) != null) {
+            retryInterval = Integer.valueOf(refProp.getStringValue(POLICY_RETRY_INTERVAL));
+        }
     }
 
-    private void downloadAllPolicies() {
-        /*
-         * Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)
-         * .withProperty("blueprintResourceId",
-         * resourceUuid).withProperty("blueprintServiceId", serviceUuid)
-         * .withProperty("blueprintName", artifactName).build();
-         * metricsLogger.info("Attempt n°" + i + " to contact DCAE inventory");
-         * 
-         * Exchange exchangeResponse =
-         * camelContext.createProducerTemplate().send("direct:get-all-policy-models",
-         * myCamelExchange);
-         */
+    public PolicyModel createPolicyModelFromPolicyEngine(String policyType, String policyVersion)
+            throws InterruptedException {
+        return new PolicyModel(policyType, this.downloadOnePolicy(policyType, policyVersion), policyVersion,
+                createPolicyAcronym(policyType));
+    }
+
+    public PolicyModel createPolicyModelFromPolicyEngine(BlueprintMicroService microService)
+            throws InterruptedException {
+        return createPolicyModelFromPolicyEngine(microService.getModelType(), microService.getModelVersion());
+    }
+
+    private static String createPolicyAcronym(String policyType) {
+        String[] policyNameArray = policyType.split("\\.");
+        return policyNameArray[policyNameArray.length - 1];
+    }
+
+    /**
+     * 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");
     }
 
     /**
@@ -77,34 +99,24 @@ public class PolicyEngineServices {
      * @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
+     * @throws InterruptedException In case of issue when sleeping during the retry
      */
     public String downloadOnePolicy(String policyType, String policyVersion) throws InterruptedException {
-        int retryInterval = 0;
-        int retryLimit = 1;
-        if (refProp.getStringValue(POLICY_RETRY_LIMIT) != null) {
-            retryLimit = Integer.valueOf(refProp.getStringValue(POLICY_RETRY_LIMIT));
-        }
-        if (refProp.getStringValue(POLICY_RETRY_INTERVAL) != null) {
-            retryInterval = Integer.valueOf(refProp.getStringValue(POLICY_RETRY_INTERVAL));
-        }
-        for (int i = 0; i < retryLimit; i++) {
-            Exchange paramExchange = ExchangeBuilder.anExchange(camelContext)
-                    .withProperty("policyModelName", policyType).withProperty("policyModelVersion", policyVersion)
-                    .build();
-
-            Exchange exchangeResponse = camelContext.createProducerTemplate().send("direct:get-policy-model",
-                    paramExchange);
+        return callCamelRoute(ExchangeBuilder.anExchange(camelContext).withProperty("policyModelName", policyType)
+                .withProperty("policyModelVersion", policyVersion).build(), "direct:get-policy-model");
+    }
 
+    private String callCamelRoute(Exchange exchange, String camelFlow) throws InterruptedException {
+        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 " + retryInterval + "ms before retrying ...");
+                logger.info("Policy query " + retryInterval + "ms before retrying ...");
                 // wait for a while and try to connect to DCAE again
                 Thread.sleep(retryInterval);
             }
         }
         return "";
     }
-
 }