Allow handling of legact model artifacts
[aai/model-loader.git] / src / main / java / org / openecomp / modelloader / entity / model / ModelSorter.java
index 4dcda71..274b18f 100644 (file)
@@ -1,23 +1,25 @@
-/*-
+/**
  * ============LICENSE_START=======================================================
- * MODEL LOADER SERVICE
+ * Model Loader
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017 AT&T Intellectual Property.
+ * Copyright © 2017 Amdocs
+ * All rights reserved.
  * ================================================================================
  * 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
- * 
+ * 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=========================================================
+ *
+ * ECOMP and OpenECOMP are trademarks
+ * and service marks of AT&T Intellectual Property.
  */
-
 package org.openecomp.modelloader.entity.model;
 
 import jline.internal.Log;
@@ -42,11 +44,11 @@ public class ModelSorter {
    * Wraps a Model object to form dependencies other Models using Edges.
    */
   static class Node {
-    private final ModelArtifact model;
+    private final AbstractModelArtifact model;
     private final HashSet<Edge> inEdges;
     private final HashSet<Edge> outEdges;
 
-    public Node(ModelArtifact model) {
+    public Node(AbstractModelArtifact model) {
       this.model = model;
       inEdges = new HashSet<Edge>();
       outEdges = new HashSet<Edge>();
@@ -61,19 +63,18 @@ public class ModelSorter {
 
     @Override
     public String toString() {
-      return model.getNameVersionId();
+      return model.getUniqueIdentifier();
     }
 
     @Override
     public boolean equals(Object other) {
-      ModelArtifact otherModel = ((Node) other).model;
-      return this.model.getNameVersionId().equals(otherModel.getNameVersionId());
+      AbstractModelArtifact otherModel = ((Node) other).model;      
+      return this.model.getUniqueIdentifier().equals(otherModel.getUniqueIdentifier());
     }
 
     @Override
     public int hashCode() {
-      return this.model.getNameVersionId().hashCode();
-
+      return this.model.getUniqueIdentifier().hashCode();
     }
   }
 
@@ -111,7 +112,7 @@ public class ModelSorter {
 
     Collection<Node> nodes = createNodes(originalList);
     Collection<Node> sortedNodes = sortNodes(nodes);
-
+    
     List<Artifact> sortedModelsList = new ArrayList<Artifact>(sortedNodes.size());
     for (Node node : sortedNodes) {
       sortedModelsList.add(node.model);
@@ -131,23 +132,24 @@ public class ModelSorter {
 
     // load list of models into a map, so we can later replace referenceIds with
     // real Models
-    HashMap<String, ModelArtifact> versionIdToModelMap = new HashMap<String, ModelArtifact>();
+    HashMap<String, AbstractModelArtifact> versionIdToModelMap = new HashMap<String, AbstractModelArtifact>();
     for (Artifact art : models) {
-      ModelArtifact ma = (ModelArtifact) art;
-      versionIdToModelMap.put(ma.getNameVersionId(), ma);
+      AbstractModelArtifact ma = (AbstractModelArtifact) art;
+      versionIdToModelMap.put(ma.getUniqueIdentifier(), ma);
     }
 
     HashMap<String, Node> nodes = new HashMap<String, Node>();
     // create a node for each model and its referenced models
     for (Artifact art : models) {
-      ModelArtifact model = (ModelArtifact) art;
 
+      AbstractModelArtifact model = (AbstractModelArtifact) art;
+      
       // node might have been created by another model referencing it
-      Node node = nodes.get(model.getNameVersionId());
+      Node node = nodes.get(model.getUniqueIdentifier());
 
       if (null == node) {
         node = new Node(model);
-        nodes.put(model.getNameVersionId(), node);
+        nodes.put(model.getUniqueIdentifier(), node);
       }
 
       for (String referencedModelId : model.getDependentModelIds()) {
@@ -156,7 +158,7 @@ public class ModelSorter {
 
         if (null == referencedNode) {
           // create node
-          ModelArtifact referencedModel = versionIdToModelMap.get(referencedModelId);
+          AbstractModelArtifact referencedModel = versionIdToModelMap.get(referencedModelId);
           if (referencedModel == null) {
             Log.debug("ignoring " + referencedModelId);
             continue; // referenced model not supplied, no need to sort it
@@ -179,7 +181,6 @@ public class ModelSorter {
    * @return a sorted collection of the given nodes
    */
   private Collection<Node> sortNodes(Collection<Node> unsortedNodes) {
-
     // L <- Empty list that will contain the sorted elements
     ArrayList<Node> nodeList = new ArrayList<Node>();
 
@@ -230,4 +231,5 @@ public class ModelSorter {
     return nodeList;
   }
 
+
 }