Changes for Checkstyle 8.32
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / authorative / concepts / ToscaEntity.java
index 87d0d9a..e588a59 100644 (file)
 package org.onap.policy.models.tosca.authorative.concepts;
 
 import com.google.gson.annotations.SerializedName;
-
+import io.swagger.annotations.ApiModelProperty;
 import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
-
+import javax.ws.rs.core.Response;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 import lombok.NonNull;
-
+import org.onap.policy.models.base.PfModelRuntimeException;
 import org.onap.policy.models.base.PfNameVersion;
 
 /**
@@ -46,6 +47,7 @@ public class ToscaEntity implements PfNameVersion {
 
     private String version;
 
+    @ApiModelProperty(name = "derived_from")
     @SerializedName("derived_from")
     private String derivedFrom;
 
@@ -54,7 +56,7 @@ public class ToscaEntity implements PfNameVersion {
     private String description;
 
     /**
-     * Copy COnstructor.
+     * Copy Constructor.
      *
      * @param copyObject object to copy from
      */
@@ -62,6 +64,7 @@ public class ToscaEntity implements PfNameVersion {
         this.name = copyObject.name;
         this.version = copyObject.version;
         this.derivedFrom = copyObject.derivedFrom;
+        this.description = copyObject.description;
 
         if (copyObject.metadata != null) {
             metadata = new LinkedHashMap<>();
@@ -71,4 +74,62 @@ public class ToscaEntity implements PfNameVersion {
         }
     }
 
+    /**
+     * Get a key for this entity.
+     *
+     * @return a ToscaEntityKey for this entry
+     */
+    public ToscaEntityKey getKey() {
+        return new ToscaEntityKey(name, version);
+    }
+
+    /**
+     * Convert a list of maps of TOSCA entities into a regular map.
+     *
+     * @param listOfMapsOfEntities the incoming list of maps of entities
+     * @return The entities on a regular map
+     * @throws PfModelException on duplicate entity entries
+     */
+    public static <T extends ToscaEntity> Map<ToscaEntityKey, T> getEntityListMapAsMap(
+            List<Map<String, T>> listOfMapsOfEntities) {
+        // Declare the return map
+        Map<ToscaEntityKey, T> entityMap = new LinkedHashMap<>();
+
+        for (Map<String, T> mapOfEntities : listOfMapsOfEntities) {
+            for (T entityEntry : mapOfEntities.values()) {
+                if (entityMap.containsKey(entityEntry.getKey())) {
+                    throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
+                            "list of map of entities contains more than one entity with key " + entityEntry.getKey());
+                }
+
+                entityMap.put(entityEntry.getKey(), entityEntry);
+            }
+        }
+
+        return entityMap;
+    }
+
+    /**
+     * Convert a map of TOSCA entities into a regular map.
+     *
+     * @param mapOfEntities the incoming list of maps of entities
+     * @return The entities on a regular map
+     * @throws PfModelException on duplicate entity entries
+     */
+    public static <T extends ToscaEntity> Map<ToscaEntityKey, T> getEntityMapAsMap(Map<String, T> mapOfEntities) {
+        // Declare the return map
+        Map<ToscaEntityKey, T> entityMap = new LinkedHashMap<>();
+
+        for (T entityEntry : mapOfEntities.values()) {
+            if (entityMap.containsKey(entityEntry.getKey())) {
+                throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
+                        "list of map of entities contains more than one entity with key " + entityEntry.getKey());
+            }
+
+            entityMap.put(entityEntry.getKey(), entityEntry);
+        }
+
+        return entityMap;
+    }
+
 }