Rework the policy refresh
[clamp.git] / src / main / java / org / onap / clamp / loop / LoopController.java
index d230eb9..9c2c71f 100644 (file)
@@ -29,9 +29,12 @@ import com.google.gson.reflect.TypeToken;
 import java.io.IOException;
 import java.lang.reflect.Type;
 import java.util.List;
+import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport;
 import org.onap.clamp.clds.util.JsonUtils;
 import org.onap.clamp.policy.microservice.MicroServicePolicy;
+import org.onap.clamp.policy.microservice.MicroServicePolicyService;
 import org.onap.clamp.policy.operational.OperationalPolicy;
+import org.onap.clamp.policy.operational.OperationalPolicyService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 
@@ -39,13 +42,36 @@ import org.springframework.stereotype.Controller;
 public class LoopController {
 
     private final LoopService loopService;
-    private static final Type OPERATIONAL_POLICY_TYPE = new TypeToken<List<OperationalPolicy>>() {}.getType();
 
-    private static final Type MICROSERVICE_POLICY_TYPE = new TypeToken<List<MicroServicePolicy>>() {}.getType();
+    private final ToscaConverterWithDictionarySupport toscaConverter;
 
+    private final OperationalPolicyService operationalPolicyService;
+
+    private final MicroServicePolicyService microServicePolicyService;
+
+    private static final Type OPERATIONAL_POLICY_TYPE = new TypeToken<List<OperationalPolicy>>() {
+    }.getType();
+
+    private static final Type MICROSERVICE_POLICY_TYPE = new TypeToken<List<MicroServicePolicy>>() {
+    }.getType();
+
+
+    /**
+     * Constructor.
+     *
+     * @param loopService               loopService
+     * @param operationalPolicyService  operationalPolicyService
+     * @param microServicePolicyService microServicePolicyService
+     * @param toscaConverter            toscaConverter
+     */
     @Autowired
-    public LoopController(LoopService loopService) {
+    public LoopController(LoopService loopService, OperationalPolicyService operationalPolicyService,
+                          MicroServicePolicyService microServicePolicyService,
+                          ToscaConverterWithDictionarySupport toscaConverter) {
         this.loopService = loopService;
+        this.toscaConverter = toscaConverter;
+        this.operationalPolicyService = operationalPolicyService;
+        this.microServicePolicyService = microServicePolicyService;
     }
 
     public Loop createLoop(String loopName, String templateName) {
@@ -156,10 +182,34 @@ public class LoopController {
     /**
      * Refresh the Operational Policy Json representation of the loop.
      *
-     * @param loopName The loop name
-     * @return The refreshed Loop
+     * @param loop                  The loop
+     * @param operationalPolicyName The operational policy name that needs a refresh
+     * @return The loop object
+     */
+    public Loop refreshOperationalPolicyJsonRepresentation(Loop loop, String operationalPolicyName) {
+        for (OperationalPolicy operationalPolicy : loop.getOperationalPolicies()) {
+            if (operationalPolicy.getName().equals(operationalPolicyName)) {
+                this.operationalPolicyService
+                        .refreshOperationalPolicyJsonRepresentation(operationalPolicy, toscaConverter);
+            }
+        }
+        return loop;
+    }
+
+    /**
+     * Refresh the Config Policy Json representation of the loop.
+     *
+     * @param loop                   The loop
+     * @param microServicePolicyName The microservice policy name that needs a refresh
+     * @return The loop object
      */
-    public Loop refreshOpPolicyJsonRepresentation(String loopName) {
-        return loopService.refreshOpPolicyJsonRepresentation(loopName);
+    public Loop refreshMicroServicePolicyJsonRepresentation(Loop loop, String microServicePolicyName) {
+        for (MicroServicePolicy microServicePolicy : loop.getMicroServicePolicies()) {
+            if (microServicePolicy.getName().equals(microServicePolicyName)) {
+                this.microServicePolicyService
+                        .refreshMicroServicePolicyJsonRepresentation(microServicePolicy, toscaConverter);
+            }
+        }
+        return loop;
     }
 }