Rework the policy refresh 60/103760/2
authorsebdet <sebastien.determe@intl.att.com>
Mon, 16 Mar 2020 18:04:34 +0000 (11:04 -0700)
committersebdet <sebastien.determe@intl.att.com>
Mon, 16 Mar 2020 21:00:24 +0000 (14:00 -0700)
Rework the policy refresh for the new unique dialog policyModel

Issue-ID: CLAMP-578
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
Change-Id: Ie8c91223e92c1e344d7ead5784ffea33d4f6a00f

16 files changed:
src/main/java/org/onap/clamp/loop/Loop.java
src/main/java/org/onap/clamp/loop/LoopController.java
src/main/java/org/onap/clamp/loop/LoopService.java
src/main/java/org/onap/clamp/loop/template/LoopElementModel.java
src/main/java/org/onap/clamp/policy/Policy.java
src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java
src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java
src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java
src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java
src/main/java/org/onap/clamp/policy/operational/OperationalPolicyService.java
src/main/resources/clds/camel/rest/clamp-api-v2.xml
src/test/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilderTest.java
ui-react/src/api/LoopCache.js
ui-react/src/api/LoopCache.test.js
ui-react/src/api/LoopService.js
ui-react/src/components/dialogs/OperationalPolicy/OperationalPolicyModal.js

index dd6fbf0..605e42f 100644 (file)
 
 package org.onap.clamp.loop;
 
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
 import com.google.gson.JsonObject;
 import com.google.gson.annotations.Expose;
-import java.io.IOException;
 import java.io.Serializable;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -77,9 +74,6 @@ public class Loop extends AuditEntity implements Serializable {
      */
     private static final long serialVersionUID = -286522707701388642L;
 
-    @Transient
-    private static final EELFLogger logger = EELFManager.getInstance().getLogger(Loop.class);
-
     @Id
     @Expose
     @Column(nullable = false, name = "name", unique = true)
@@ -170,23 +164,13 @@ public class Loop extends AuditEntity implements Serializable {
         this.setModelService(loopTemplate.getModelService());
         loopTemplate.getLoopElementModelsUsed().forEach(element -> {
             if (LoopElementModel.MICRO_SERVICE_TYPE.equals(element.getLoopElementModel().getLoopElementType())) {
-                try {
-                    this.addMicroServicePolicy((MicroServicePolicy) element.getLoopElementModel()
-                            .createPolicyInstance(this, toscaConverter));
-                } catch (IOException e) {
-                    logger.error("Exception caught when creating the microservice policy instance of the loop "
-                            + "instance", e);
-                }
+                this.addMicroServicePolicy((MicroServicePolicy) element.getLoopElementModel()
+                        .createPolicyInstance(this, toscaConverter));
             }
             else if (LoopElementModel.OPERATIONAL_POLICY_TYPE
                     .equals(element.getLoopElementModel().getLoopElementType())) {
-                try {
-                    this.addOperationalPolicy((OperationalPolicy) element.getLoopElementModel()
-                            .createPolicyInstance(this, toscaConverter));
-                } catch (IOException e) {
-                    logger.error("Exception caught when creating the operational policy instance of the loop instance",
-                            e);
-                }
+                this.addOperationalPolicy((OperationalPolicy) element.getLoopElementModel()
+                        .createPolicyInstance(this, toscaConverter));
             }
         });
     }
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;
     }
 }
index acd125b..af1f58b 100644 (file)
@@ -122,15 +122,15 @@ public class LoopService {
             return null;
         }
         loop.addOperationalPolicy(
-                new OperationalPolicy(loop,loop.getModelService(), policyModel, toscaConverter));
+                new OperationalPolicy(loop, loop.getModelService(), policyModel, toscaConverter));
         return loopsRepository.saveAndFlush(loop);
     }
 
     /**
      * This method remove an operational policy to a loop instance.
      *
-     * @param loopName The loop name
-     * @param policyType The policy model type
+     * @param loopName      The loop name
+     * @param policyType    The policy model type
      * @param policyVersion The policy model  version
      * @return The loop modified
      */
@@ -141,8 +141,8 @@ public class LoopService {
             return null;
         }
         for (OperationalPolicy opPolicy : loop.getOperationalPolicies()) {
-            if (opPolicy.getPolicyModel().getPolicyModelType().equals(policyType) &&
-                    opPolicy.getPolicyModel().getVersion().equals(policyVersion)) {
+            if (opPolicy.getPolicyModel().getPolicyModelType().equals(policyType)
+                    && opPolicy.getPolicyModel().getVersion().equals(policyVersion)) {
                 loop.removeOperationalPolicy(opPolicy);
                 break;
             }
@@ -179,20 +179,5 @@ public class LoopService {
         return loopsRepository.findById(loopName)
                 .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName));
     }
-
-    /**
-     * Api to refresh the Operational Policy UI window.
-     *
-     * @param loopName The loop Name
-     * @return The refreshed loop object
-     */
-    public Loop refreshOpPolicyJsonRepresentation(String loopName) {
-        Loop loop = findClosedLoopByName(loopName);
-        Set<OperationalPolicy> policyList = loop.getOperationalPolicies();
-        for (OperationalPolicy policy : policyList) {
-            policy.updateJsonRepresentation();
-        }
-        loop.setOperationalPolicies(policyList);
-        return loopsRepository.save(loop);
-    }
 }
+
index dfdfc42..4a46a95 100644 (file)
@@ -24,7 +24,6 @@
 package org.onap.clamp.loop.template;
 
 import com.google.gson.annotations.Expose;
-import java.io.IOException;
 import java.io.Serializable;
 import java.util.HashSet;
 import java.util.Set;
@@ -251,16 +250,15 @@ public class LoopElementModel extends AuditEntity implements Serializable {
      * Create a policy instance from the current loop element model.
      *
      * @return A Policy object.
-     * @throws IOException in case of failure when creating an operational policy
      */
-    public Policy createPolicyInstance(Loop loop, ToscaConverterWithDictionarySupport toscaConverter)
-            throws IOException {
+    public Policy createPolicyInstance(Loop loop, ToscaConverterWithDictionarySupport toscaConverter) {
         if (LoopElementModel.MICRO_SERVICE_TYPE.equals(this.getLoopElementType())) {
             return new MicroServicePolicy(loop, loop.getModelService(), this, toscaConverter);
         }
         else if (LoopElementModel.OPERATIONAL_POLICY_TYPE.equals(this.getLoopElementType())) {
             return new OperationalPolicy(loop, loop.getModelService(), this, toscaConverter);
-        } else {
+        }
+        else {
             return null;
         }
     }
index 3b22064..87d36f3 100644 (file)
@@ -43,6 +43,7 @@ import org.hibernate.annotations.Type;
 import org.hibernate.annotations.TypeDef;
 import org.hibernate.annotations.TypeDefs;
 import org.json.JSONObject;
+import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport;
 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
 import org.onap.clamp.loop.common.AuditEntity;
 import org.onap.clamp.loop.template.LoopElementModel;
@@ -175,6 +176,13 @@ public abstract class Policy extends AuditEntity {
         this.jsonRepresentation = jsonRepresentation;
     }
 
+    /**
+     * Regenerate the Policy Json Representation.
+     *
+     * @param toscaConverter The tosca converter required to regenerate the json schema
+     */
+    public abstract void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter);
+
     /**
      * policyModel getter.
      *
index 321c12f..47b3a4f 100644 (file)
@@ -23,8 +23,6 @@
 
 package org.onap.clamp.policy.microservice;
 
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
 import com.google.gson.JsonObject;
 import com.google.gson.annotations.Expose;
 import java.io.Serializable;
@@ -36,7 +34,6 @@ import javax.persistence.FetchType;
 import javax.persistence.Id;
 import javax.persistence.ManyToMany;
 import javax.persistence.Table;
-import javax.persistence.Transient;
 import org.apache.commons.lang3.RandomStringUtils;
 import org.hibernate.annotations.TypeDef;
 import org.hibernate.annotations.TypeDefs;
@@ -57,9 +54,6 @@ public class MicroServicePolicy extends Policy implements Serializable {
      */
     private static final long serialVersionUID = 6271238288583332616L;
 
-    @Transient
-    private static final EELFLogger logger = EELFManager.getInstance().getLogger(MicroServicePolicy.class);
-
     @Expose
     @Id
     @Column(nullable = false, name = "name", unique = true)
@@ -126,20 +120,19 @@ public class MicroServicePolicy extends Policy implements Serializable {
     /**
      * Constructor with tosca converter.
      *
-     * @param loop The loop instance
-     * @param service The service model object
+     * @param loop             The loop instance
+     * @param service          The service model object
      * @param loopElementModel The loop element model from which this microservice instance is created
-     * @param toscaConverter The tosca converter that will used to convert the tosca policy model
+     * @param toscaConverter   The tosca converter that will used to convert the tosca policy model
      */
     public MicroServicePolicy(Loop loop, Service service, LoopElementModel loopElementModel,
                               ToscaConverterWithDictionarySupport toscaConverter) {
         this(Policy.generatePolicyName("MICROSERVICE", service.getName(), service.getVersion(),
                 RandomStringUtils.randomAlphanumeric(3), RandomStringUtils.randomAlphanumeric(3)),
                 loopElementModel.getPolicyModels().first(), false,
-                toscaConverter.convertToscaToJsonSchemaObject(
-                        loopElementModel.getPolicyModels().first().getPolicyModelTosca(),
-                        loopElementModel.getPolicyModels().first().getPolicyModelType()),
+                new JsonObject(),
                 loopElementModel, null, null);
+        this.updateJsonRepresentation(toscaConverter);
     }
 
     @Override
@@ -157,6 +150,12 @@ public class MicroServicePolicy extends Policy implements Serializable {
         this.name = name;
     }
 
+    @Override
+    public void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter) {
+        toscaConverter.convertToscaToJsonSchemaObject(this.getPolicyModel().getPolicyModelTosca(),
+                this.getPolicyModel().getPolicyModelType());
+    }
+
     public Boolean getShared() {
         return shared;
     }
index 9bc641c..0631380 100644 (file)
@@ -26,6 +26,7 @@ package org.onap.clamp.policy.microservice;
 import java.util.List;
 import java.util.Set;
 import java.util.stream.Collectors;
+import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport;
 import org.onap.clamp.loop.Loop;
 import org.onap.clamp.policy.PolicyService;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -34,11 +35,11 @@ import org.springframework.stereotype.Service;
 @Service
 public class MicroServicePolicyService implements PolicyService<MicroServicePolicy> {
 
-    private final MicroServicePolicyRepository repository;
+    private final MicroServicePolicyRepository microServiceRepository;
 
     @Autowired
-    public MicroServicePolicyService(MicroServicePolicyRepository repository) {
-        this.repository = repository;
+    public MicroServicePolicyService(MicroServicePolicyRepository microServiceRepository) {
+        this.microServiceRepository = microServiceRepository;
     }
 
     @Override
@@ -49,7 +50,7 @@ public class MicroServicePolicyService implements PolicyService<MicroServicePoli
 
     @Override
     public boolean isExisting(String policyName) {
-        return repository.existsById(policyName);
+        return microServiceRepository.existsById(policyName);
     }
 
     /**
@@ -60,8 +61,9 @@ public class MicroServicePolicyService implements PolicyService<MicroServicePoli
      * @return The updated MicroService policy
      */
     public MicroServicePolicy getAndUpdateMicroServicePolicy(Loop loop, MicroServicePolicy policy) {
-        return repository.save(
-                repository.findById(policy.getName()).map(p -> updateMicroservicePolicyProperties(p, policy, loop))
+        return microServiceRepository.save(
+                microServiceRepository
+                        .findById(policy.getName()).map(p -> updateMicroservicePolicyProperties(p, policy, loop))
                         .orElse(new MicroServicePolicy(policy.getName(), policy.getPolicyModel(),
                                 policy.getShared(), policy.getJsonRepresentation(), null, policy.getPdpGroup(),
                                 policy.getPdpSubgroup())));
@@ -89,6 +91,20 @@ public class MicroServicePolicyService implements PolicyService<MicroServicePoli
                                            String deploymentUrl) {
         microServicePolicy.setDcaeDeploymentId(deploymentId);
         microServicePolicy.setDcaeDeploymentStatusUrl(deploymentUrl);
-        repository.save(microServicePolicy);
+        microServiceRepository.save(microServicePolicy);
+    }
+
+
+    /**
+     * Api to refresh the MicroService Policy UI window.
+     *
+     * @param microServicePolicy The micro Service policy object
+     * @param toscaConverter     the tosca converter required to convert the tosca model to json schema
+     */
+    public void refreshMicroServicePolicyJsonRepresentation(MicroServicePolicy microServicePolicy,
+                                                            ToscaConverterWithDictionarySupport toscaConverter) {
+        microServicePolicy.updateJsonRepresentation(toscaConverter);
+        this.microServiceRepository.saveAndFlush(microServicePolicy);
+
     }
 }
index 528d695..492c9b9 100644 (file)
@@ -31,7 +31,6 @@ import com.google.gson.GsonBuilder;
 import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
-import com.google.gson.JsonSyntaxException;
 import com.google.gson.annotations.Expose;
 import java.io.IOException;
 import java.io.Serializable;
@@ -120,15 +119,14 @@ public class OperationalPolicy extends Policy implements Serializable {
      * @param service          The loop service
      * @param loopElementModel The loop element model
      * @param toscaConverter   The tosca converter that must be used to create the Json representation
-     * @throws IOException In case of issues with the legacy files (generated from resource files
      */
     public OperationalPolicy(Loop loop, Service service, LoopElementModel loopElementModel,
-                             ToscaConverterWithDictionarySupport toscaConverter) throws IOException {
+                             ToscaConverterWithDictionarySupport toscaConverter) {
         this(Policy.generatePolicyName("OPERATIONAL", service.getName(), service.getVersion(),
                 RandomStringUtils.randomAlphanumeric(3), RandomStringUtils.randomAlphanumeric(3)), new JsonObject(),
                 new JsonObject(), loopElementModel.getPolicyModels().first(), loopElementModel, null, null);
         this.setLoop(loop);
-        this.setJsonRepresentation(generateJsonRepresentation(this, toscaConverter));
+        this.updateJsonRepresentation(toscaConverter);
     }
 
     /**
@@ -146,39 +144,7 @@ public class OperationalPolicy extends Policy implements Serializable {
                 RandomStringUtils.randomAlphanumeric(3), RandomStringUtils.randomAlphanumeric(3)), new JsonObject(),
                 new JsonObject(), policyModel, null, null, null);
         this.setLoop(loop);
-        this.setJsonRepresentation(generateJsonRepresentation(this, toscaConverter));
-    }
-
-    /**
-     * This method can generate a Json representation (json schema) for an operational policy.
-     * This is mainly to support a legacy case and a generic case.
-     * For the legacy case the operational policy given is modified (configurationJson).
-     *
-     * @param operationalPolicy The operational policy
-     * @param toscaConverter    The tosca converter
-     * @return The Json Object with Json schema
-     */
-    public static JsonObject generateJsonRepresentation(OperationalPolicy operationalPolicy,
-                                                        ToscaConverterWithDictionarySupport toscaConverter)
-            throws IOException {
-        JsonObject jsonReturned = new JsonObject();
-        if (operationalPolicy.getPolicyModel() == null) {
-            return new JsonObject();
-        }
-        if (operationalPolicy.isLegacy()) {
-            // Op policy Legacy case
-            LegacyOperationalPolicy.preloadConfiguration(operationalPolicy.getConfigurationsJson(), operationalPolicy.loop);
-            jsonReturned = OperationalPolicyRepresentationBuilder
-                    .generateOperationalPolicySchema(operationalPolicy.loop.getModelService());
-        }
-        else {
-            // Generic Case
-            jsonReturned = toscaConverter.convertToscaToJsonSchemaObject(
-                    operationalPolicy.getPolicyModel().getPolicyModelTosca(),
-                    operationalPolicy.getPolicyModel().getPolicyModelType());
-        }
-
-        return jsonReturned;
+        this.updateJsonRepresentation(toscaConverter);
     }
 
     public void setLoop(Loop loopName) {
@@ -204,6 +170,28 @@ public class OperationalPolicy extends Policy implements Serializable {
         this.name = name;
     }
 
+    @Override
+    public void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter) {
+        {
+            this.setJsonRepresentation(new JsonObject());
+            if (this.getPolicyModel() == null) {
+                return;
+            }
+            if (this.isLegacy()) {
+                // Op policy Legacy case
+                LegacyOperationalPolicy.preloadConfiguration(this.getConfigurationsJson(), this.loop);
+                this.setJsonRepresentation(OperationalPolicyRepresentationBuilder
+                        .generateOperationalPolicySchema(this.loop.getModelService()));
+            }
+            else {
+                // Generic Case
+                this.setJsonRepresentation(toscaConverter.convertToscaToJsonSchemaObject(
+                        this.getPolicyModel().getPolicyModelTosca(),
+                        this.getPolicyModel().getPolicyModelType()));
+            }
+        }
+    }
+
     @Override
     public int hashCode() {
         final int prime = 31;
@@ -320,17 +308,4 @@ public class OperationalPolicy extends Policy implements Serializable {
         logger.info("Guard policy payload: " + result);
         return result;
     }
-
-    /**
-     * Regenerate the Operational Policy Json Representation.
-     */
-    public void updateJsonRepresentation() {
-        try {
-            this.setJsonRepresentation(
-                    OperationalPolicyRepresentationBuilder.generateOperationalPolicySchema(loop.getModelService()));
-        } catch (JsonSyntaxException | IOException | NullPointerException e) {
-            logger.error("Unable to generate the operational policy Schema ... ", e);
-            this.setJsonRepresentation(new JsonObject());
-        }
-    }
 }
index c88c1b9..7559851 100644 (file)
 
 package org.onap.clamp.policy.operational;
 
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
 import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
-import com.google.gson.JsonSyntaxException;
 import java.io.IOException;
 import java.util.Map.Entry;
 import org.onap.clamp.clds.util.JsonUtils;
@@ -36,6 +37,9 @@ import org.onap.clamp.loop.service.Service;
 
 public class OperationalPolicyRepresentationBuilder {
 
+    private static final EELFLogger logger =
+            EELFManager.getInstance().getLogger(OperationalPolicyRepresentationBuilder.class);
+
     /**
      * This method generates the operational policy json representation that will be
      * used by ui for rendering. It uses the model (VF and VFModule) defined in the
@@ -44,34 +48,38 @@ public class OperationalPolicyRepresentationBuilder {
      *
      * @param modelJson The loop model json
      * @return The json representation
-     * @throws JsonSyntaxException If the schema template cannot be parsed
-     * @throws IOException         In case of issue when opening the schema template
      */
-    public static JsonObject generateOperationalPolicySchema(Service modelJson)
-            throws JsonSyntaxException, IOException {
-        JsonObject jsonSchema = JsonUtils.GSON.fromJson(
-                ResourceFileUtil.getResourceAsString("clds/json-schema/operational_policies/operational_policy.json"),
-                JsonObject.class);
-        jsonSchema.get("properties").getAsJsonObject()
-                .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies")
-                .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("target")
-                .getAsJsonObject().get("anyOf").getAsJsonArray().addAll(createAnyOfArray(modelJson));
-
-        // update CDS recipe and payload information to schema
-        JsonArray actors = jsonSchema.get("properties").getAsJsonObject()
-                .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies")
-                .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("actor")
-                .getAsJsonObject().get("anyOf").getAsJsonArray();
-
-        for (JsonElement actor : actors) {
-            if ("CDS".equalsIgnoreCase(actor.getAsJsonObject().get("title").getAsString())) {
-                actor.getAsJsonObject().get("properties").getAsJsonObject().get("type").getAsJsonObject()
-                        .get("anyOf").getAsJsonArray()
-                        .addAll(createAnyOfArrayForCdsRecipe(modelJson.getResourceDetails()));
+    public static JsonObject generateOperationalPolicySchema(Service modelJson) {
+
+        JsonObject jsonSchema = null;
+        try {
+            jsonSchema = JsonUtils.GSON.fromJson(
+                    ResourceFileUtil
+                            .getResourceAsString("clds/json-schema/operational_policies/operational_policy.json"),
+                    JsonObject.class);
+            jsonSchema.get("properties").getAsJsonObject()
+                    .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies")
+                    .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("target")
+                    .getAsJsonObject().get("anyOf").getAsJsonArray().addAll(createAnyOfArray(modelJson));
+
+            // update CDS recipe and payload information to schema
+            JsonArray actors = jsonSchema.get("properties").getAsJsonObject()
+                    .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies")
+                    .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("actor")
+                    .getAsJsonObject().get("anyOf").getAsJsonArray();
+
+            for (JsonElement actor : actors) {
+                if ("CDS".equalsIgnoreCase(actor.getAsJsonObject().get("title").getAsString())) {
+                    actor.getAsJsonObject().get("properties").getAsJsonObject().get("type").getAsJsonObject()
+                            .get("anyOf").getAsJsonArray()
+                            .addAll(createAnyOfArrayForCdsRecipe(modelJson.getResourceDetails()));
+                }
             }
+            return jsonSchema;
+        } catch (IOException e) {
+            logger.error("Unable to generate the json schema because of an exception",e);
+            return new JsonObject();
         }
-
-        return jsonSchema;
     }
 
     private static JsonObject createSchemaProperty(String title, String type, String defaultValue, String readOnlyFlag,
index ad6cbd9..9c0cbe9 100644 (file)
 
 package org.onap.clamp.policy.operational;
 
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
 import java.util.List;
 import java.util.Set;
 import java.util.stream.Collectors;
+import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport;
 import org.onap.clamp.loop.Loop;
 import org.onap.clamp.loop.template.PolicyModelsRepository;
 import org.onap.clamp.policy.PolicyService;
@@ -39,6 +42,8 @@ public class OperationalPolicyService implements PolicyService<OperationalPolicy
 
     private final PolicyModelsRepository policyModelsRepository;
 
+    private static final EELFLogger logger = EELFManager.getInstance().getLogger(OperationalPolicyService.class);
+
     @Autowired
     public OperationalPolicyService(OperationalPolicyRepository repository,
                                     PolicyModelsRepository policyModelsRepository) {
@@ -54,7 +59,7 @@ public class OperationalPolicyService implements PolicyService<OperationalPolicy
                         operationalPolicyRepository
                                 .findById(policy.getName())
                                 .map(p -> setConfiguration(p, policy))
-                                .orElse(initializeMissingFields(loop,policy)))
+                                .orElse(initializeMissingFields(loop, policy)))
                 .collect(Collectors.toSet());
     }
 
@@ -74,4 +79,16 @@ public class OperationalPolicyService implements PolicyService<OperationalPolicy
         policy.setPdpSubgroup(newPolicy.getPdpSubgroup());
         return policy;
     }
+
+    /**
+     * Api to refresh the Operational Policy UI window.
+     *
+     * @param operationalPolicy The operational policy object
+     * @param toscaConverter    the tosca converter required to convert the tosca model to json schema
+     */
+    public void refreshOperationalPolicyJsonRepresentation(OperationalPolicy operationalPolicy,
+                                                           ToscaConverterWithDictionarySupport toscaConverter) {
+        operationalPolicy.updateJsonRepresentation(toscaConverter);
+        this.operationalPolicyRepository.saveAndFlush(operationalPolicy);
+    }
 }
index d1c191d..d81c164 100644 (file)
                                </doTry>
                        </route>
                </put>
-               <put uri="/v2/loop/refreshOpPolicyJsonSchema/{loopName}"
+               <put uri="/v2/loop/refreshMicroServicePolicyJsonSchema/{loopName}/{microServicePolicyName}"
+                        outType="org.onap.clamp.loop.Loop" produces="application/json">
+                       <route>
+                               <removeHeaders pattern="*" excludePattern="loopName|microServicePolicyName" />
+                               <doTry>
+                                       <log loggingLevel="INFO"
+                                                message="Refresh Micro Service Policy UI for loop: ${header.loopName} and ${header.microServicePolicyName}" />
+                                       <to
+                                                       uri="bean:org.onap.clamp.flow.log.FlowLogOperation?method=startLog(*, 'REFRESH Micro Service Policy UI request')" />
+                                       <to
+                                                       uri="bean:org.onap.clamp.authorization.AuthorizationController?method=authorize(*,'cl','','update')" />
+                                       <to uri="direct:load-loop" />
+                                       <to
+                                                       uri="bean:org.onap.clamp.loop.LoopController?method=refreshMicroServicePolicyJsonRepresentation(${exchangeProperty[loopObject]},${header.microServicePolicyName}})" />
+                                       <log loggingLevel="INFO"
+                                                message="REFRESH Micro Service policy request successfully executed for loop: ${header.loopName}" />
+                                       <to
+                                                       uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('REFRESH Micro Service policy request successfully executed','INFO',${exchangeProperty[loopObject]})" />
+                                       <to
+                                                       uri="bean:org.onap.clamp.flow.log.FlowLogOperation?method=endLog()" />
+                                       <doCatch>
+                                               <exception>java.lang.Exception</exception>
+                                               <handled>
+                                                       <constant>false</constant>
+                                               </handled>
+                                               <to
+                                                               uri="bean:org.onap.clamp.flow.log.FlowLogOperation?method=errorLog()" />
+                                               <log loggingLevel="ERROR"
+                                                        message="REFRESH Micro Service policy request failed for loop: ${header.loopName}" />
+                                               <to
+                                                               uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('REFRESH Micro Service policy request failed, Error reported: ${exception} - Body: ${exception.responseBody}','ERROR',${exchangeProperty[loopObject]})" />
+                                       </doCatch>
+                               </doTry>
+                       </route>
+               </put>
+               <put uri="/v2/loop/refreshOperationalPolicyJsonSchema/{loopName}/{operationalPolicyName}"
                        outType="org.onap.clamp.loop.Loop" produces="application/json">
                        <route>
-                               <removeHeaders pattern="*" excludePattern="loopName" />
+                               <removeHeaders pattern="*" excludePattern="loopName|operationalPolicyName" />
                                <doTry>
                                        <log loggingLevel="INFO"
-                                               message="Refresh Operational Policy UI for loop: ${header.loopName}" />
+                                               message="Refresh Operational Policy UI for loop: ${header.loopName} and ${header.operationalPolicyName}" />
                                        <to
-                                               uri="bean:org.onap.clamp.flow.log.FlowLogOperation?method=startLog(*, 'REFRESH OP Policy UI request')" />
+                                               uri="bean:org.onap.clamp.flow.log.FlowLogOperation?method=startLog(*, 'REFRESH Operational Policy UI request')" />
                                        <to
                                                uri="bean:org.onap.clamp.authorization.AuthorizationController?method=authorize(*,'cl','','update')" />
                                        <to uri="direct:load-loop" />
                                        <to
-                                               uri="bean:org.onap.clamp.loop.LoopController?method=refreshOpPolicyJsonRepresentation(${header.loopName})" />
+                                               uri="bean:org.onap.clamp.loop.LoopController?method=refreshOperationalPolicyJsonRepresentation(${exchangeProperty[loopObject]},${header.operationalPolicyName}})" />
                                        <log loggingLevel="INFO"
-                                               message="REFRESH request successfully executed for loop: ${header.loopName}" />
+                                               message="REFRESH operational policy request successfully executed for loop: ${header.loopName}" />
                                        <to
-                                               uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('REFRESH request successfully executed','INFO',${exchangeProperty[loopObject]})" />
+                                               uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('REFRESH operational policy request successfully executed','INFO',${exchangeProperty[loopObject]})" />
                                        <to
                                                uri="bean:org.onap.clamp.flow.log.FlowLogOperation?method=endLog()" />
                                        <doCatch>
                                                <to
                                                        uri="bean:org.onap.clamp.flow.log.FlowLogOperation?method=errorLog()" />
                                                <log loggingLevel="ERROR"
-                                                       message="REFRESH request failed for loop: ${header.loopName}" />
+                                                       message="REFRESH operational policy request failed for loop: ${header.loopName}" />
                                                <to
-                                                       uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('REFRESH request failed, Error reported: ${exception} - Body: ${exception.responseBody}','ERROR',${exchangeProperty[loopObject]})" />
+                                                       uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('REFRESH operational policy request failed, Error reported: ${exception} - Body: ${exception.responseBody}','ERROR',${exchangeProperty[loopObject]})" />
                                        </doCatch>
                                </doTry>
                        </route>
index 12ddbaa..4e9b562 100644 (file)
@@ -27,9 +27,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 
 import com.google.gson.GsonBuilder;
 import com.google.gson.JsonObject;
-
 import java.io.IOException;
-
 import org.junit.Test;
 import org.onap.clamp.clds.util.ResourceFileUtil;
 import org.onap.clamp.loop.service.Service;
index 5eaa79a..c54337f 100644 (file)
@@ -70,10 +70,6 @@ export default class LoopCache {
                return this.loopJsonCache["name"];
        }
 
-       getOperationalPolicyConfigurationJson() {
-               return this.loopJsonCache["operationalPolicies"]["0"]["configurationsJson"];
-       }
-
        getOperationalPolicyJsonSchema() {
                return this.loopJsonCache["operationalPolicies"]["0"]["jsonRepresentation"];
        }
index fc75681..4642ff5 100644 (file)
@@ -30,17 +30,6 @@ describe('Verify LoopCache functions', () => {
       expect(loopCache.getLoopName()).toBe("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca");
                });
 
-    it('getOperationalPolicyConfigurationJson', () => {
-      const opPolicyConfig = {
-          "guard_policies": {},
-          "operational_policy": {
-            "controlLoop": {},
-            "policies": []
-          }
-      };
-      expect(loopCache.getOperationalPolicyConfigurationJson()).toStrictEqual(opPolicyConfig);
-    });
-
     it('getOperationalPolicies', () => {
       const opPolicy = [{
         "name": "OPERATIONAL_h2NMX_v1_0_ResourceInstanceName1_tca",
index 2750763..698ee28 100644 (file)
@@ -171,8 +171,8 @@ export default class LoopService {
                        });
        }
 
-       static refreshOpPolicyJson(loopName) {
-               return fetch('/restservices/clds/v2/loop/refreshOpPolicyJsonSchema/' + loopName, {
+       static refreshOperationalPolicyJson(loopName,operationalPolicyName) {
+               return fetch('/restservices/clds/v2/loop/refreshOperationalPolicyJsonSchema/' + loopName + '/' + operationalPolicyName, {
                        method: 'PUT',
                        headers: {
                                "Content-Type": "application/json"
@@ -194,6 +194,29 @@ export default class LoopService {
                        });
        }
 
+               static refreshMicroServicePolicyJson(loopName,microServicePolicyName) {
+               return fetch('/restservices/clds/v2/loop/refreshMicroServicePolicyJsonSchema/' + loopName + '/' + microServicePolicyName, {
+                       method: 'PUT',
+                       headers: {
+                               "Content-Type": "application/json"
+                       },
+                       credentials: 'same-origin'
+               })
+                       .then(function (response) {
+                               console.debug("Refresh Operational Policy Json Schema response received: ", response.status);
+                               if (response.ok) {
+                                       return response.json();
+                               } else {
+                                       console.error("Refresh Operational Policy Json Schema query failed");
+                                       return {};
+                               }
+                       })
+                       .catch(function (error) {
+                               console.error("Refresh Operational Policy Json Schema error received", error);
+                               return {};
+                       });
+    }
+
        static addOperationalPolicyType(loopName, policyType, policyVersion) {
                return fetch('/restservices/clds/v2/loop/addOperationaPolicy/' + loopName + '/policyModel/' + policyType +'/' + policyVersion , {
                        method: 'PUT',
index 7ed8ba6..89e7079 100644 (file)
@@ -127,7 +127,7 @@ export default class OperationalPolicyModal extends React.Component {
        }
 
        handleRefresh() {
-               LoopService.refreshOpPolicyJson(this.state.loopCache.getLoopName()).then(data => {
+               LoopService.refreshOperationalPolicyJson(this.state.loopCache.getLoopName(), this.state.loopCache.getOperationalPolicies()[0]).then(data => {
                        var newLoopCache =  new LoopCache(data);
                        var schema_json = newLoopCache.getOperationalPolicyJsonSchema();
                        var operationalPoliciesData = newLoopCache.getOperationalPoliciesNoJsonSchema();