Change code in appc dispatcher for new LCMs in R6
[appc.git] / appc-dg / appc-dg-shared / appc-dg-dependency-model / src / main / java / org / onap / appc / dg / dependencymanager / helper / DependencyModelParser.java
index 48d1a05..4b40ba2 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP : APPC
  * ================================================================================
- * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
  * ================================================================================
  * 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
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- * 
  * ============LICENSE_END=========================================================
  */
 
@@ -51,7 +50,7 @@ public class DependencyModelParser {
     private static final String PROPERTIES = "properties";
     private static final String ACTIVE_ACTIVE = "Active-Active";
     private static final String ACTIVE_PASSIVE = "Active-Passive";
-    private static final String HIGH_AVAILABLITY = "high_availablity";
+    private static final String HIGH_AVAILABLITY = "high_availablity"; // Deprecated original spelling
     private static final String HIGH_AVAILABILITY = "high_availability";
     private static final String MANDATORY = "mandatory";
     private static final String TOPOLOGY_TEMPLATE = "topology_template";
@@ -69,7 +68,7 @@ public class DependencyModelParser {
     }
 
     public VnfcDependencyModel generateDependencyModel(String vnfModel, String vnfType)
-        throws InvalidDependencyModelException {
+            throws InvalidDependencyModelException {
         Set<Node<Vnfc>> dependencies = new HashSet<>();
         ObjectMapper mapper = getMapper();
         boolean mandatory;
@@ -78,13 +77,15 @@ public class DependencyModelParser {
         try {
             ObjectNode root = (ObjectNode) mapper.readTree(vnfModel);
 
-            if (root.get(TOPOLOGY_TEMPLATE) == null || root.get(TOPOLOGY_TEMPLATE).get("node_templates") == null) {
+            JsonNode topologyTemplateNode = root.get(TOPOLOGY_TEMPLATE);
+            JsonNode nodeTemplateNode;
+
+            if (topologyTemplateNode == null
+                    || (nodeTemplateNode = topologyTemplateNode.get("node_templates")) == null) {
                 throw new InvalidDependencyModelException(
-                    "Dependency model is missing 'topology_template' or  'node_templates' elements");
+                        "Dependency model is missing 'topology_template' or 'node_templates' elements");
             }
 
-            JsonNode topologyTemplateNode = root.get(TOPOLOGY_TEMPLATE);
-            JsonNode nodeTemplateNode = topologyTemplateNode.get("node_templates");
             Iterator<Map.Entry<String, JsonNode>> iterator = nodeTemplateNode.fields();
             for (JsonNode yamlNode : nodeTemplateNode) {
                 logger.debug("Processing node: " + yamlNode);
@@ -92,7 +93,7 @@ public class DependencyModelParser {
                 String vnfcType = getQualifiedVnfcType(fullvnfcType);
                 String type = yamlNode.get("type").textValue();
                 type = type.substring(0, type.lastIndexOf('.') + 1);
-                if (type.concat(vnfcType).toLowerCase().startsWith(prefix.concat(vnfcType).toLowerCase())) {
+                if (type.toLowerCase().startsWith(prefix.toLowerCase())) {
 
                     resilienceType = resolveResilienceType(yamlNode);
                     mandatory = resolveMandatory(yamlNode);
@@ -103,23 +104,23 @@ public class DependencyModelParser {
                         logger.debug("Dependency node already exists for vnfc Type: " + vnfcType);
                         if (StringUtils.isEmpty(vnfcNode.getChild().getResilienceType())) {
                             logger.debug("Updating resilience type, "
-                                + "dependencies and mandatory attribute for VNFC type: " + vnfcType);
+                                    + "dependencies and mandatory attribute for VNFC type: " + vnfcType);
                             vnfcNode.getChild().setResilienceType(resilienceType);
                             tryFillNode(dependencies, parentList, vnfcNode);
                             vnfcNode.getChild().setMandatory(mandatory);
                         }
                     } else {
-                        logger.debug("Creating dependency node for  : " + vnfcType);
+                        logger.debug("Creating dependency node for: " + vnfcType);
                         vnfcNode = new Node<>(createVnfc(mandatory, resilienceType, vnfcType));
                         tryFillNode(dependencies, parentList, vnfcNode);
-                        logger.debug("Adding VNFC to dependency model : " + vnfcNode);
+                        logger.debug("Adding VNFC to dependency model: " + vnfcNode);
                         dependencies.add(vnfcNode);
                     }
                 }
             }
         } catch (IOException e) {
-            logger.error("Error parsing dependency model : " + vnfModel);
-            logger.error("Error message : " + e);
+            logger.error("Error parsing dependency model: " + vnfModel);
+            logger.error("Error message: " + e);
             throw new InvalidDependencyModelException("Error parsing dependency model. " + e.getMessage());
         }
         return new VnfcDependencyModel(dependencies);
@@ -136,8 +137,8 @@ public class DependencyModelParser {
     }
 
     private boolean mandatoryDoesNotExist(JsonNode yamlNode) {
-        return yamlNode.get(PROPERTIES).findValue(MANDATORY) == null ||
-            yamlNode.get(PROPERTIES).findValue(MANDATORY).asText().isEmpty();
+        return yamlNode.get(PROPERTIES).findValue(MANDATORY) == null
+                || yamlNode.get(PROPERTIES).findValue(MANDATORY).asText().isEmpty();
     }
 
     private String resolveResilienceType(JsonNode yamlNode) {
@@ -148,10 +149,10 @@ public class DependencyModelParser {
             resilienceType = dependencyMap
                     .get(yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABILITY).textValue());
         }
-        // The property name "high_availability" was misspelled in the code so leaving in the code to check for the 
+        // The property name "high_availability" was misspelled in the code so leaving in the code to check for the
         // incorrectly spelled version to avoid breaking existing configurations using the misspelled version
-        else if (yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY) == null ||
-                yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY).asText().isEmpty()) {
+        else if (yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY) == null
+                || yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY).asText().isEmpty()) {
             resilienceType = ACTIVE_ACTIVE;
         } else {
             resilienceType = dependencyMap
@@ -177,25 +178,27 @@ public class DependencyModelParser {
             String parentType = getVnfcType(type);
             Node<Vnfc> parentNode = getNode(nodes, parentType);
             if (parentNode != null) {
-                logger.debug("VNFC already exists for VNFC type: " + parentType + ". Adding it to parent list ");
+                logger.debug("VNFC already exists for VNFC type: " + parentType + ". Adding it to parent list");
                 node.addParent(parentNode.getChild());
             } else {
-                logger.debug("VNFC does not exist for VNFC type: " + parentType + ". Creating new VNFC ");
+                logger.debug("VNFC does not exist for VNFC type: " + parentType + ". Creating new VNFC");
                 parentNode = new Node<>(createVnfc(false, null, parentType));
                 node.addParent(parentNode.getChild());
-                logger.debug("Adding VNFC to dependency model : " + parentNode);
+                logger.debug("Adding VNFC to dependency model: " + parentNode);
                 nodes.add(parentNode);
             }
         }
     }
 
     private String[] getDependencyArray(JsonNode node, JsonNode nodeTemplateNode)
-        throws InvalidDependencyModelException {
+            throws InvalidDependencyModelException {
         JsonNode requirementsNode = node.get("requirements");
         Set<String> dependencyList = new HashSet<>();
         if (requirementsNode != null) {
             for (JsonNode internalNode : requirementsNode) {
-                //TODO : In this release we are supporting both relationship = tosca.capabilities.Node  and relationship =tosca.relationships.DependsOn we need to remove one of them in next release post confirming with SDC team
+                /* TODO: In this release we are supporting both relationship = tosca.capabilities.Node
+                   and relationship = tosca.relationships.DependsOn
+                   we need to remove one of them in next release post confirming with SDC team */
                 if (verifyNode(internalNode)) {
                     parseDependencyModel(node, nodeTemplateNode, dependencyList, internalNode);
                 }
@@ -207,7 +210,7 @@ public class DependencyModelParser {
     }
 
     private void parseDependencyModel(JsonNode node, JsonNode nodeTemplateNode, Set<String> dependencyList,
-        JsonNode internalNode) throws InvalidDependencyModelException {
+            JsonNode internalNode) throws InvalidDependencyModelException {
 
         if (internalNode.findValue("node") != null) {
             String nodeName = internalNode.findValue("node").asText();
@@ -215,20 +218,20 @@ public class DependencyModelParser {
             dependencyList.add(getQualifiedVnfcType(fullVnfcName));
         } else {
             throw new InvalidDependencyModelException(
-                "Error parsing dependency model. " + "Dependent Node not found for " + node.get("type"));
+                    "Error parsing dependency model. Dependent Node not found for " + node.get("type"));
         }
     }
 
     private boolean verifyNode(JsonNode internalNode) {
-        return nodeNullCheck(internalNode) &&
-            "tosca.capabilities.Node".equalsIgnoreCase(internalNode.findValue("capability").asText()) &&
-            ("tosca.relationships.DependsOn".equalsIgnoreCase(internalNode.findValue(RELATIONSHIP).asText()) ||
-                "tosca.capabilities.Node".equalsIgnoreCase(internalNode.findValue(RELATIONSHIP).asText()));
+        return nodeNullCheck(internalNode)
+                && "tosca.capabilities.Node".equalsIgnoreCase(internalNode.findValue("capability").asText())
+                && ("tosca.relationships.DependsOn".equalsIgnoreCase(internalNode.findValue(RELATIONSHIP).asText())
+                    || "tosca.capabilities.Node".equalsIgnoreCase(internalNode.findValue(RELATIONSHIP).asText()));
     }
 
     private boolean nodeNullCheck(JsonNode internalNode) {
         return internalNode.get("dependency") != null && internalNode.findValue("capability") != null
-            && internalNode.findValue(RELATIONSHIP) != null;
+                && internalNode.findValue(RELATIONSHIP) != null;
     }
 
     protected Node<Vnfc> getNode(Set<Node<Vnfc>> nodes, String vnfcType) {