JPA concepts for TOSCA
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfConceptContainer.java
index b4d51d1..863a3ef 100644 (file)
@@ -21,6 +21,7 @@
 
 package org.onap.policy.models.base;
 
+import com.google.re2j.Pattern;
 import java.lang.reflect.ParameterizedType;
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
@@ -30,22 +31,19 @@ import java.util.Map.Entry;
 import java.util.NavigableMap;
 import java.util.Set;
 import java.util.TreeMap;
+import java.util.TreeSet;
 import java.util.function.Function;
-
 import javax.persistence.CascadeType;
 import javax.persistence.EmbeddedId;
-import javax.persistence.Entity;
 import javax.persistence.JoinColumn;
 import javax.persistence.JoinTable;
 import javax.persistence.ManyToMany;
 import javax.persistence.MappedSuperclass;
 import javax.persistence.Table;
 import javax.ws.rs.core.Response;
-
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
-
 import org.apache.commons.lang3.StringUtils;
 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
 
@@ -63,15 +61,16 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult;
  */
 //@formatter:on
 @MappedSuperclass
-@Entity
 @Table(name = "PfConceptContainer")
 @Data
 @EqualsAndHashCode(callSuper = false)
 
 public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> extends PfConcept
-    implements PfConceptGetter<C>, PfAuthorative<List<Map<String, A>>> {
+        implements PfConceptGetter<C>, PfAuthorative<List<Map<String, A>>> {
     private static final long serialVersionUID = -324211738823208318L;
 
+    private static final Pattern KEY_ID_PATTERN = Pattern.compile(PfKey.KEY_ID_REGEXP);
+
     @EmbeddedId
     private PfConceptKey key;
 
@@ -105,7 +104,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
      * @param key the concept key
      */
     public PfConceptContainer(@NonNull final PfConceptKey key) {
-        this(key, new TreeMap<PfConceptKey, C>());
+        this(key, new TreeMap<>());
     }
 
     /**
@@ -153,7 +152,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
     public List<Map<String, A>> toAuthorative() {
         // The returned list is a list of map singletons with one map for each map
         // entry in the concept container
-        List<Map<String, A>> toscaPolicyMapList = new ArrayList<>();
+        List<Map<String, A>> toscaConceptMapList = new ArrayList<>();
 
         for (Entry<PfConceptKey, C> conceptEntry : getConceptMap().entrySet()) {
             // Create a map to hold this entry
@@ -165,10 +164,10 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
             toscaPolicyMap.put(conceptEntry.getKey().getName(), authoritiveImpl.toAuthorative());
 
             // Add the map to the returned list
-            toscaPolicyMapList.add(toscaPolicyMap);
+            toscaConceptMapList.add(toscaPolicyMap);
         }
 
-        return toscaPolicyMapList;
+        return toscaConceptMapList;
     }
 
     @Override
@@ -182,7 +181,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
             for (Entry<String, A> incomingConceptEntry : incomingConceptMap.entrySet()) {
 
                 PfConceptKey conceptKey = new PfConceptKey();
-                if (incomingConceptEntry.getKey().matches(PfKey.KEY_ID_REGEXP)) {
+                if (KEY_ID_PATTERN.matches(incomingConceptEntry.getKey())) {
                     conceptKey = new PfConceptKey(incomingConceptEntry.getKey());
                 } else {
                     conceptKey.setName(incomingConceptEntry.getKey());
@@ -194,9 +193,9 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
                 }
 
                 incomingConceptEntry.getValue().setName(findConceptField(conceptKey, conceptKey.getName(),
-                    incomingConceptEntry.getValue(), PfNameVersion::getName));
+                        incomingConceptEntry.getValue(), PfNameVersion::getName));
                 incomingConceptEntry.getValue().setVersion(findConceptField(conceptKey, conceptKey.getVersion(),
-                    incomingConceptEntry.getValue(), PfNameVersion::getVersion));
+                        incomingConceptEntry.getValue(), PfNameVersion::getVersion));
 
                 C jpaConcept = getConceptNewInstance();
                 // This cast allows us to call the fromAuthorative method
@@ -213,8 +212,23 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
 
         if (conceptMap.isEmpty()) {
             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
-                "An incoming list of concepts must have at least one entry");
+                    "An incoming list of concepts must have at least one entry");
+        }
+    }
+
+    /**
+     * Get an authorative list of the concepts in this container.
+     *
+     * @return the authorative list of concepts
+     */
+    public List<A> toAuthorativeList() {
+        List<A> toscaConceptList = new ArrayList<>();
+
+        for (Map<String, A> toscaConceptMap : toAuthorative()) {
+            toscaConceptList.addAll(toscaConceptMap.values());
         }
+
+        return toscaConceptList;
     }
 
     @Override
@@ -232,7 +246,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
 
         if (key.equals(PfConceptKey.getNullKey())) {
             result.addValidationMessage(
-                new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
+                    new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
         }
 
         result = key.validate(result);
@@ -256,14 +270,14 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
         for (final Entry<PfConceptKey, C> conceptEntry : conceptMap.entrySet()) {
             if (conceptEntry.getKey().equals(PfConceptKey.getNullKey())) {
                 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "key on concept entry " + conceptEntry.getKey() + " may not be the null key"));
+                        "key on concept entry " + conceptEntry.getKey() + " may not be the null key"));
             } else if (conceptEntry.getValue() == null) {
                 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "value on concept entry " + conceptEntry.getKey() + " may not be null"));
+                        "value on concept entry " + conceptEntry.getKey() + " may not be null"));
             } else if (!conceptEntry.getKey().equals(conceptEntry.getValue().getKey())) {
                 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "key on concept entry key " + conceptEntry.getKey() + " does not equal concept value key "
-                        + conceptEntry.getValue().getKey()));
+                        "key on concept entry key " + conceptEntry.getKey() + " does not equal concept value key "
+                                + conceptEntry.getValue().getKey()));
                 result = conceptEntry.getValue().validate(result);
             } else {
                 result = conceptEntry.getValue().validate(result);
@@ -291,11 +305,27 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
             return retVal;
         }
 
-        if (!conceptMap.equals(other.conceptMap)) {
-            return (conceptMap.hashCode() - other.conceptMap.hashCode());
-        }
+        return PfUtils.compareMaps(conceptMap, other.conceptMap);
+    }
 
-        return 0;
+    /**
+     * Get all the concepts that match the given name and version.
+     *
+     * @param conceptKeyName the name of the concept, if null, return all names
+     * @param conceptKeyVersion the version of the concept, if null, return all versions
+     * @return conceptKeyVersion
+     */
+    public Set<C> getAllNamesAndVersions(final String conceptKeyName, final String conceptKeyVersion) {
+        if (conceptKeyName == null || conceptKeyVersion == null || PfKey.NULL_KEY_VERSION.equals(conceptKeyVersion)) {
+            return getAll(conceptKeyName, conceptKeyVersion);
+        } else {
+            final Set<C> returnSet = new TreeSet<>();
+            C foundConcept = get(conceptKeyName, conceptKeyVersion);
+            if (foundConcept != null) {
+                returnSet.add(foundConcept);
+            }
+            return returnSet;
+        }
     }
 
     @Override
@@ -315,7 +345,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
     @Override
     public C get(final String conceptKeyName, final String conceptKeyVersion) {
         return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).get(conceptKeyName,
-            conceptKeyVersion);
+                conceptKeyVersion);
     }
 
     @Override
@@ -326,7 +356,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
     @Override
     public Set<C> getAll(final String conceptKeyName, final String conceptKeyVersion) {
         return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).getAll(conceptKeyName,
-            conceptKeyVersion);
+                conceptKeyVersion);
     }
 
     /**
@@ -338,16 +368,16 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
     private C getConceptNewInstance() {
         try {
             String conceptClassName =
-                ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0].getTypeName();
+                    ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0].getTypeName();
             return (C) Class.forName(conceptClassName).getDeclaredConstructor().newInstance();
         } catch (Exception ex) {
             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
-                "failed to instantiate instance of container concept class", ex);
+                    "failed to instantiate instance of container concept class", ex);
         }
     }
 
     private String findConceptField(final PfConceptKey conceptKey, final String keyFieldValue,
-        final PfNameVersion concept, final Function<PfNameVersion, String> fieldGetterFunction) {
+            final PfNameVersion concept, final Function<PfNameVersion, String> fieldGetterFunction) {
 
         String conceptField = fieldGetterFunction.apply(concept);
 
@@ -355,7 +385,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
             return keyFieldValue;
         } else {
             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, "Key " + conceptKey.getId() + " field "
-                + keyFieldValue + " does not match the value " + conceptField + " in the concept field");
+                    + keyFieldValue + " does not match the value " + conceptField + " in the concept field");
         }
     }
 }