Fix pdp group info not there for legacy policy
[clamp.git] / src / main / java / org / onap / clamp / policy / operational / OperationalPolicy.java
index 355a889..1e721ce 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;
@@ -51,6 +50,7 @@ import javax.persistence.Transient;
 import org.apache.commons.lang3.RandomStringUtils;
 import org.hibernate.annotations.TypeDef;
 import org.hibernate.annotations.TypeDefs;
+import org.onap.clamp.clds.config.LegacyOperationalPolicyController;
 import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport;
 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
 import org.onap.clamp.loop.Loop;
@@ -120,64 +120,36 @@ 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(),
+                loopElementModel.getPolicyModels().first().getPolicyAcronym() + '_'
+                        + loopElementModel.getPolicyModels().first().getVersion(),
+                RandomStringUtils.randomAlphanumeric(3)), new JsonObject(),
                 new JsonObject(), loopElementModel.getPolicyModels().first(), loopElementModel, null, null);
         this.setLoop(loop);
-        this.setJsonRepresentation(generateJsonRepresentation(this, toscaConverter));
+        this.updateJsonRepresentation(toscaConverter, service);
     }
 
     /**
      * Create an operational policy from a policy model.
      *
-     * @param loop             The parent loop
-     * @param service          The loop service
-     * @param policyModel       The policy model
-     * @param toscaConverter   The tosca converter that must be used to create the Json representation
+     * @param loop           The parent loop
+     * @param service        The loop service
+     * @param policyModel    The policy 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, PolicyModel policyModel,
                              ToscaConverterWithDictionarySupport toscaConverter) throws IOException {
         this(Policy.generatePolicyName("OPERATIONAL", service.getName(), service.getVersion(),
-                RandomStringUtils.randomAlphanumeric(3), RandomStringUtils.randomAlphanumeric(3)), new JsonObject(),
+                policyModel.getPolicyAcronym() + '_' + policyModel.getVersion(),
+                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.
-     *
-     * @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(jsonReturned, 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, service);
     }
 
     public void setLoop(Loop loopName) {
@@ -203,6 +175,28 @@ public class OperationalPolicy extends Policy implements Serializable {
         this.name = name;
     }
 
+    @Override
+    public void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter, Service serviceModel) {
+        {
+            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(), serviceModel));
+            }
+        }
+    }
+
     @Override
     public int hashCode() {
         final int prime = 31;
@@ -235,7 +229,8 @@ public class OperationalPolicy extends Policy implements Serializable {
     }
 
     public Boolean isLegacy() {
-        return (this.getPolicyModel() != null) && this.getPolicyModel().getPolicyModelType().contains("legacy");
+        return (this.getPolicyModel() != null) && this.getPolicyModel().getPolicyModelType().contains(
+                LegacyOperationalPolicyController.OPERATIONAL_POLICY_LEGACY);
     }
 
     /**
@@ -319,17 +314,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());
-        }
-    }
 }