Fix Checkstyle issues
[clamp.git] / src / main / java / org / onap / clamp / clds / model / properties / ModelProperties.java
index cc6f02d..9b7f85d 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
  *                             reserved.
  * ================================================================================
+ * Modifications Copyright (c) 2019 Samsung
+ * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * limitations under the License.
  * ============LICENSE_END============================================
  * ===================================================================
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
+ *
  */
 
 package org.onap.clamp.clds.model.properties;
 
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
-import com.fasterxml.jackson.databind.JsonNode;
-
-import java.io.IOException;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParseException;
 import java.lang.reflect.InvocationTargetException;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
-
 import org.apache.camel.Exchange;
+import org.onap.clamp.clds.client.req.policy.PolicyClient;
+import org.onap.clamp.clds.config.ClampProperties;
 import org.onap.clamp.clds.exception.ModelBpmnException;
-import org.onap.clamp.clds.model.CldsEvent;
 import org.onap.clamp.clds.model.CldsModel;
 import org.onap.clamp.clds.service.CldsService;
-import org.onap.clamp.clds.util.JacksonUtils;
+import org.onap.clamp.clds.util.JsonUtils;
 
 /**
  * Parse model properties.
@@ -50,7 +51,7 @@ public class ModelProperties {
     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsService.class);
     protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
     private ModelBpmn modelBpmn;
-    private JsonNode modelJson;
+    private JsonObject modelJson;
     private final String modelName;
     private final String controlName;
     private final String actionCd;
@@ -60,8 +61,11 @@ public class ModelProperties {
     private final Map<String, AbstractModelElement> modelElements = new ConcurrentHashMap<>();
     private String currentModelElementId;
     private String policyUniqueId;
+    private String guardUniqueId;
+    public static final String POLICY_GUARD_SUFFIX = "_Guard_";
     private static final Object lock = new Object();
     private static Map<Class<? extends AbstractModelElement>, String> modelElementClasses = new ConcurrentHashMap<>();
+
     static {
         synchronized (lock) {
             modelElementClasses.put(Policy.class, Policy.getType());
@@ -71,45 +75,45 @@ public class ModelProperties {
     }
 
     /**
-     * Retain data required to parse the ModelElement objects. (Rather than
-     * parse them all - parse them on demand if requested.)
+     * Retain data required to parse the ModelElement objects. (Rather than parse
+     * them all - parse them on demand if requested.)
      *
      * @param modelName
-     *            The model name coming form the UI
+     *        The model name coming form the UI
      * @param controlName
-     *            The closed loop name coming from the UI
+     *        The closed loop name coming from the UI
      * @param actionCd
-     *            Type of operation PUT,UPDATE,DELETE
+     *        Type of operation PUT,UPDATE,DELETE
      * @param isATest
-     *            The test flag coming from the UI (for validation only, no
-     *            query are physically executed)
+     *        The test flag coming from the UI (for validation only, no query are
+     *        physically executed)
      * @param modelBpmnText
-     *            The BPMN flow in JSON from the UI
+     *        The BPMN flow in JSON from the UI
      * @param modelPropText
-     *            The BPMN parameters for all boxes defined in modelBpmnTest
+     *        The BPMN parameters for all boxes defined in modelBpmnTest
      */
     public ModelProperties(String modelName, String controlName, String actionCd, boolean isATest, String modelBpmnText,
-            String modelPropText) {
+        String modelPropText) {
         try {
             this.modelName = modelName;
             this.controlName = controlName;
             this.actionCd = actionCd;
             this.testOnly = isATest;
             modelBpmn = ModelBpmn.create(modelBpmnText);
-            modelJson = JacksonUtils.getObjectMapperInstance().readTree(modelPropText);
+            modelJson = JsonUtils.GSON.fromJson(modelPropText, JsonObject.class);
             instantiateMissingModelElements();
-        } catch (IOException e) {
+        } catch (JsonParseException e) {
             throw new ModelBpmnException("Exception occurred when trying to decode the BPMN Properties JSON", e);
         }
     }
 
     /**
-     * This method is meant to ensure that one ModelElement instance exists for
-     * each ModelElement class. As new ModelElement classes could have been
-     * registered after instantiation of this ModelProperties, we need to build
-     * the missing ModelElement instances.
+     * This method is meant to ensure that one ModelElement instance exists for each
+     * ModelElement class. As new ModelElement classes could have been registered
+     * after instantiation of this ModelProperties, we need to build the missing
+     * ModelElement instances.
      */
-    private final void instantiateMissingModelElements() {
+    private void instantiateMissingModelElements() {
         if (modelElementClasses.size() != modelElements.size()) {
             Set<String> missingTypes = new HashSet<>(modelElementClasses.values());
             missingTypes.removeAll(modelElements.keySet());
@@ -121,12 +125,12 @@ public class ModelProperties {
                     .forEach(entry -> {
                         try {
                             modelElements.put(entry.getValue(),
-                                    (entry.getKey()
-                                            .getConstructor(ModelProperties.class, ModelBpmn.class, JsonNode.class)
-                                            .newInstance(this, modelBpmn, modelJson)));
+                                    (entry.getKey().getConstructor(ModelBpmn.class, JsonObject.class)
+                                            .newInstance(modelBpmn, modelJson)));
                         } catch (InstantiationException | NoSuchMethodException | IllegalAccessException
                                 | InvocationTargetException e) {
-                            logger.warn("Unable to instantiate a ModelElement, exception follows: ", e);
+                            logger.warn("Unable to instantiate a ModelElement " + entry.getValue()
+                                    + ", exception follows: ", e);
                         }
                     });
         }
@@ -135,16 +139,16 @@ public class ModelProperties {
     /**
      * Get the VF for a model. If return null if there is no VF.
      *
-     * @param model
-     * @return
+     * @param model The clds model
+     * @return The vf of the model
      */
     public static String getVf(CldsModel model) {
         List<String> vfs = null;
         try {
-            JsonNode modelJson = JacksonUtils.getObjectMapperInstance().readTree(model.getPropText());
+            JsonObject modelJson = JsonUtils.GSON.fromJson(model.getPropText(), JsonObject.class);
             Global global = new Global(modelJson);
             vfs = global.getResourceVf();
-        } catch (IOException e) {
+        } catch (JsonParseException e) {
             logger.warn("no VF found", e);
         }
         String vf = null;
@@ -158,10 +162,9 @@ public class ModelProperties {
      * Create ModelProperties extracted from a CamelExchange.
      *
      * @param camelExchange
-     *            The camel Exchange object that contains all info provided to
-     *            the flow
-     * @return A model Properties created from the parameters found in
-     *         camelExchange object
+     *        The camel Exchange object that contains all info provided to the flow
+     * @return A model Properties created from the parameters found in camelExchange
+     *         object
      */
     public static ModelProperties create(Exchange camelExchange) {
         String modelProp = (String) camelExchange.getProperty("modelProp");
@@ -174,20 +177,7 @@ public class ModelProperties {
     }
 
     /**
-     * return appropriate model element given the type
-     *
-     * @param type
-     * @return
-     */
-    public AbstractModelElement getModelElementByType(String type) {
-        AbstractModelElement modelElement = modelElements.get(type);
-        if (modelElement == null) {
-            throw new IllegalArgumentException("Invalid or not found ModelElement type: " + type);
-        }
-        return modelElement;
-    }
-
-    /**
+     * Get the model name.
      * @return the modelName
      */
     public String getModelName() {
@@ -195,6 +185,7 @@ public class ModelProperties {
     }
 
     /**
+     * Get the control name.
      * @return the controlName
      */
     public String getControlName() {
@@ -202,6 +193,7 @@ public class ModelProperties {
     }
 
     /**
+     * Get the control name qnd policy uniqueId.
      * @return the controlNameAndPolicyUniqueId
      */
     public String getControlNameAndPolicyUniqueId() {
@@ -209,52 +201,93 @@ public class ModelProperties {
     }
 
     /**
+     * Get the current policy name.
      * @return the currentPolicyName
      */
     private String getCurrentPolicyName() {
         return normalizePolicyScopeName(controlName + "_" + currentModelElementId);
     }
 
+    private String createScopeSeparator(String policyScope) {
+        return policyScope.contains(".") ? "" : ".";
+    }
+
     /**
+     * Get the current policy scope and policy name.
      * @return the currentPolicyScopeAndPolicyName
      */
     public String getCurrentPolicyScopeAndPolicyName() {
-        return normalizePolicyScopeName(modelName + "." + getCurrentPolicyName());
+        return normalizePolicyScopeName(modelName + createScopeSeparator(modelName) + getCurrentPolicyName());
+    }
+
+    /**
+     * Get policy name for dcae deploy.
+     * @return The policyName that wil be used in input parameters of DCAE deploy
+     */
+    public String getPolicyNameForDcaeDeploy(ClampProperties refProp) {
+        return normalizePolicyScopeName(modelName + createScopeSeparator(modelName)
+            + refProp.getStringValue(PolicyClient.POLICY_MS_NAME_PREFIX_PROPERTY_NAME) + getCurrentPolicyName());
     }
 
     /**
+     * Get policy scope and name with uniqueid.
      * @return the policyScopeAndNameWithUniqueId
      */
     public String getPolicyScopeAndNameWithUniqueId() {
-        return normalizePolicyScopeName(modelName + "." + getCurrentPolicyName() + "_" + policyUniqueId);
+        return normalizePolicyScopeName(
+            modelName + createScopeSeparator(modelName) + getCurrentPolicyName() + "_" + policyUniqueId);
+    }
+
+    /**
+     * Get policy scope and name with unique guardid.
+     * @return the policyScopeAndNameWithUniqueId
+     */
+    public String getPolicyScopeAndNameWithUniqueGuardId() {
+        return normalizePolicyScopeName(modelName + createScopeSeparator(modelName) + getCurrentPolicyName() + "_"
+            + policyUniqueId + POLICY_GUARD_SUFFIX + guardUniqueId);
     }
 
     /**
+     * Get current policy scope and full policy name.
      * @return the currentPolicyScopeAndFullPolicyName
      */
     public String getCurrentPolicyScopeAndFullPolicyName(String policyNamePrefix) {
-        return normalizePolicyScopeName(modelName + "." + policyNamePrefix + getCurrentPolicyName());
+        return normalizePolicyScopeName(
+            modelName + createScopeSeparator(modelName) + policyNamePrefix + getCurrentPolicyName());
     }
 
     /**
-     * @return the currentPolicyScopeAndFullPolicyNameWithVersion
+     * Get policy name with scope context.
+     * @return the PolicyNameWithScopeContext
      */
-    public String getCurrentPolicyScopeAndFullPolicyNameWithVersion(String policyNamePrefix, int version) {
-        return normalizePolicyScopeName(
-                modelName + "." + policyNamePrefix + getCurrentPolicyName() + "." + version + ".xml");
+    public String getPolicyNameWithScopeContext(String policyScope, String policyType, String vnfScope, String context,
+        String userDefinedName) {
+        return normalizePolicyScopeName(policyScope + createScopeSeparator(policyScope) + policyType + "_" + vnfScope
+            + "_" + context + "_" + modelName + "_" + userDefinedName);
+    }
+
+    /**
+     * Get policy name with prefix scope context.
+     * @return the PolicyNameWithPrefixScopeContext
+     */
+    public String getPolicyNameWithPrefixScopeContext(String policyScope, String policyType, String vnfScope,
+        String context, String userDefinedName, String policyPrefix) {
+        return normalizePolicyScopeName(policyScope + createScopeSeparator(policyScope) + policyPrefix + policyType
+            + "_" + vnfScope + "_" + context + "_" + modelName + "_" + userDefinedName);
     }
 
     /**
      * Replace all '-' with '_' within policy scope and name.
      *
-     * @param inName
-     * @return
+     * @param inName policy scope and name
+     * @return policy scope and name with "-" replaced with "_"
      */
     private String normalizePolicyScopeName(String inName) {
         return inName.replaceAll("-", "_");
     }
 
     /**
+     * Get current model element id.
      * @return the currentModelElementId
      */
     public String getCurrentModelElementId() {
@@ -262,35 +295,45 @@ public class ModelProperties {
     }
 
     /**
-     * When generating a policy request for a model element, must set the id of
-     * that model element using this method. Used to generate the policy name.
+     * When generating a policy request for a model element, must set the id of that
+     * model element using this method. Used to generate the policy name.
      *
      * @param currentModelElementId
-     *            the currentModelElementId to set
+     *        the currentModelElementId to set
      */
     public void setCurrentModelElementId(String currentModelElementId) {
         this.currentModelElementId = currentModelElementId;
     }
 
     /**
+     * Get policy uniqueId.
      * @return the policyUniqueId
      */
     public String getPolicyUniqueId() {
         return policyUniqueId;
     }
 
+    public String getGuardUniqueId() {
+        return guardUniqueId;
+    }
+
+    public void setGuardUniqueId(String guardUniqueId) {
+        this.guardUniqueId = guardUniqueId;
+    }
+
     /**
-     * When generating a policy request for a model element, must set the unique
-     * id of that policy using this method. Used to generate the policy name.
+     * When generating a policy request for a model element, must set the unique id
+     * of that policy using this method. Used to generate the policy name.
      *
      * @param policyUniqueId
-     *            the policyUniqueId to set
+     *        the policyUniqueId to set
      */
     public void setPolicyUniqueId(String policyUniqueId) {
         this.policyUniqueId = policyUniqueId;
     }
 
     /**
+     * Get actioncd.
      * @return the actionCd
      */
     public String getActionCd() {
@@ -298,6 +341,7 @@ public class ModelProperties {
     }
 
     /**
+     * Get the testOnly flag value.
      * @return the testOnly
      */
     public boolean isTestOnly() {
@@ -305,26 +349,7 @@ public class ModelProperties {
     }
 
     /**
-     * @return the isCreateRequest
-     */
-    public boolean isCreateRequest() {
-        switch (actionCd) {
-            case CldsEvent.ACTION_SUBMIT:
-            case CldsEvent.ACTION_RESTART:
-                return true;
-        }
-        return false;
-    }
-
-    public boolean isStopRequest() {
-        switch (actionCd) {
-            case CldsEvent.ACTION_STOP:
-                return true;
-        }
-        return false;
-    }
-
-    /**
+     * Get the global value.
      * @return the global
      */
     public Global getGlobal() {
@@ -334,8 +359,14 @@ public class ModelProperties {
         return global;
     }
 
+    /**
+     * Registers model element.
+     *
+     * @param modelElementClass model element class
+     * @param type model element type
+     */
     public static final synchronized void registerModelElement(Class<? extends AbstractModelElement> modelElementClass,
-            String type) {
+        String type) {
         if (!modelElementClasses.containsKey(modelElementClass.getClass())) {
             modelElementClasses.put(modelElementClass, type);
         }