Fix Checkstyle issues
[clamp.git] / src / main / java / org / onap / clamp / clds / model / properties / ModelProperties.java
index f38e8c1..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
@@ -25,24 +27,21 @@ 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.
@@ -52,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;
@@ -66,6 +65,7 @@ public class ModelProperties {
     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());
@@ -75,22 +75,22 @@ 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) {
@@ -100,55 +100,55 @@ public class ModelProperties {
             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());
             // Parse the list of base Model Elements and build up the
             // ModelElements
             modelElementClasses.entrySet().stream().parallel()
-            .filter(entry -> (AbstractModelElement.class.isAssignableFrom(entry.getKey())
-                && missingTypes.contains(entry.getValue())))
-            .forEach(entry -> {
-                try {
-                    modelElements.put(entry.getValue(),
-                        (entry.getKey()
-                            .getConstructor(ModelProperties.class, ModelBpmn.class, JsonNode.class)
-                            .newInstance(this, modelBpmn, modelJson)));
-                } catch (InstantiationException | NoSuchMethodException | IllegalAccessException
-                    | InvocationTargetException e) {
-                    logger.warn("Unable to instantiate a ModelElement, exception follows: ", e);
-                }
-            });
+                    .filter(entry -> (AbstractModelElement.class.isAssignableFrom(entry.getKey())
+                            && missingTypes.contains(entry.getValue())))
+                    .forEach(entry -> {
+                        try {
+                            modelElements.put(entry.getValue(),
+                                    (entry.getKey().getConstructor(ModelBpmn.class, JsonObject.class)
+                                            .newInstance(modelBpmn, modelJson)));
+                        } catch (InstantiationException | NoSuchMethodException | IllegalAccessException
+                                | InvocationTargetException e) {
+                            logger.warn("Unable to instantiate a ModelElement " + entry.getValue()
+                                    + ", exception follows: ", e);
+                        }
+                    });
         }
     }
 
     /**
      * 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;
@@ -162,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");
@@ -178,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() {
@@ -199,6 +185,7 @@ public class ModelProperties {
     }
 
     /**
+     * Get the control name.
      * @return the controlName
      */
     public String getControlName() {
@@ -206,6 +193,7 @@ public class ModelProperties {
     }
 
     /**
+     * Get the control name qnd policy uniqueId.
      * @return the controlNameAndPolicyUniqueId
      */
     public String getControlNameAndPolicyUniqueId() {
@@ -213,6 +201,7 @@ public class ModelProperties {
     }
 
     /**
+     * Get the current policy name.
      * @return the currentPolicyName
      */
     private String getCurrentPolicyName() {
@@ -224,6 +213,7 @@ public class ModelProperties {
     }
 
     /**
+     * Get the current policy scope and policy name.
      * @return the currentPolicyScopeAndPolicyName
      */
     public String getCurrentPolicyScopeAndPolicyName() {
@@ -231,15 +221,16 @@ public class ModelProperties {
     }
 
     /**
-     * @return The policyName that wil be used in input parameters of DCAE
-     *         deploy
+     * 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());
+            + refProp.getStringValue(PolicyClient.POLICY_MS_NAME_PREFIX_PROPERTY_NAME) + getCurrentPolicyName());
     }
 
     /**
+     * Get policy scope and name with uniqueid.
      * @return the policyScopeAndNameWithUniqueId
      */
     public String getPolicyScopeAndNameWithUniqueId() {
@@ -248,14 +239,16 @@ public class ModelProperties {
     }
 
     /**
+     * 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);
+        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) {
@@ -263,17 +256,38 @@ public class ModelProperties {
             modelName + createScopeSeparator(modelName) + policyNamePrefix + getCurrentPolicyName());
     }
 
+    /**
+     * Get policy name with scope context.
+     * @return the PolicyNameWithScopeContext
+     */
+    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() {
@@ -281,17 +295,18 @@ 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() {
@@ -307,17 +322,18 @@ public class ModelProperties {
     }
 
     /**
-     * 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() {
@@ -325,6 +341,7 @@ public class ModelProperties {
     }
 
     /**
+     * Get the testOnly flag value.
      * @return the testOnly
      */
     public boolean isTestOnly() {
@@ -332,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() {
@@ -361,6 +359,12 @@ 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) {
         if (!modelElementClasses.containsKey(modelElementClass.getClass())) {