summaryrefslogtreecommitdiffstats
path: root/models-base/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'models-base/src/main')
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java120
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfConceptGetterImpl.java2
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfKeyImpl.java37
3 files changed, 112 insertions, 47 deletions
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java b/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java
index 949cb96dc..d259fa260 100644
--- a/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java
@@ -30,6 +30,8 @@ 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;
@@ -45,6 +47,7 @@ import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
+import org.apache.commons.lang3.StringUtils;
import org.onap.policy.models.base.PfValidationResult.ValidationResult;
// @formatter:off
@@ -67,7 +70,7 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult;
@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;
@EmbeddedId
@@ -151,7 +154,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
@@ -163,10 +166,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
@@ -178,31 +181,32 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
for (Map<String, A> incomingConceptMap : authorativeList) {
// Add the map entries one by one
for (Entry<String, A> incomingConceptEntry : incomingConceptMap.entrySet()) {
- C jpaConcept = getConceptNewInstance();
+ PfConceptKey conceptKey = new PfConceptKey();
+ if (incomingConceptEntry.getKey().matches(PfKey.KEY_ID_REGEXP)) {
+ conceptKey = new PfConceptKey(incomingConceptEntry.getKey());
+ } else {
+ conceptKey.setName(incomingConceptEntry.getKey());
+ if (incomingConceptEntry.getValue().getVersion() != null) {
+ conceptKey.setVersion(incomingConceptEntry.getValue().getVersion());
+ } else {
+ conceptKey.setVersion(PfKey.NULL_KEY_VERSION);
+ }
+ }
+
+ incomingConceptEntry.getValue().setName(findConceptField(conceptKey, conceptKey.getName(),
+ incomingConceptEntry.getValue(), PfNameVersion::getName));
+ incomingConceptEntry.getValue().setVersion(findConceptField(conceptKey, conceptKey.getVersion(),
+ incomingConceptEntry.getValue(), PfNameVersion::getVersion));
+
+ C jpaConcept = getConceptNewInstance();
// This cast allows us to call the fromAuthorative method
@SuppressWarnings("unchecked")
PfAuthorative<A> authoritiveImpl = (PfAuthorative<A>) jpaConcept;
- if (incomingConceptEntry.getValue().getName() == null) {
- incomingConceptEntry.getValue().setName(incomingConceptEntry.getKey());
- }
-
// Set the key name and the rest of the values on the concept
authoritiveImpl.fromAuthorative(incomingConceptEntry.getValue());
- // This cast gets the key of the concept
- PfConceptKey conceptKey = (PfConceptKey) jpaConcept.getKey();
-
- // Set the concept key of the concept
- conceptKey.setName(incomingConceptEntry.getValue().getName());
-
- if (incomingConceptEntry.getValue().getVersion() != null) {
- conceptKey.setVersion(incomingConceptEntry.getValue().getVersion());
- } else {
- conceptKey.setVersion(PfKey.NULL_KEY_VERSION);
- }
-
// After all that, save the map entry
conceptMap.put(conceptKey, jpaConcept);
}
@@ -210,8 +214,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
@@ -229,7 +248,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);
@@ -253,14 +272,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);
@@ -295,9 +314,33 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
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) {
+ 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
public C get(final PfConceptKey conceptKey) {
- return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).get(conceptKey);
+ if (conceptKey.isNullVersion()) {
+ return get(conceptKey.getName());
+ } else {
+ return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).get(conceptKey);
+ }
}
@Override
@@ -308,7 +351,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
@@ -319,7 +362,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);
}
/**
@@ -331,11 +374,24 @@ 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) {
+
+ String conceptField = fieldGetterFunction.apply(concept);
+
+ if (StringUtils.isBlank(conceptField) || keyFieldValue.equals(conceptField)) {
+ 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");
}
}
}
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfConceptGetterImpl.java b/models-base/src/main/java/org/onap/policy/models/base/PfConceptGetterImpl.java
index c641a8035..033a7ddb2 100644
--- a/models-base/src/main/java/org/onap/policy/models/base/PfConceptGetterImpl.java
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfConceptGetterImpl.java
@@ -55,7 +55,7 @@ public class PfConceptGetterImpl<C> implements PfConceptGetter<C> {
Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
// The very fist key that could have this name
- final PfConceptKey lowestArtifactKey = new PfConceptKey(conceptKeyName, "0.0.1");
+ final PfConceptKey lowestArtifactKey = new PfConceptKey(conceptKeyName, PfKey.NULL_KEY_VERSION);
// Check if we found a key for our name
PfConceptKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfKeyImpl.java b/models-base/src/main/java/org/onap/policy/models/base/PfKeyImpl.java
index 461fd2495..61028f927 100644
--- a/models-base/src/main/java/org/onap/policy/models/base/PfKeyImpl.java
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfKeyImpl.java
@@ -1,6 +1,6 @@
/*
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 Nordix Foundation.
* Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -23,9 +23,11 @@ package org.onap.policy.models.base;
import java.util.ArrayList;
import java.util.List;
+
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;
+
import org.onap.policy.common.utils.validation.Assertions;
import org.onap.policy.models.base.PfValidationResult.ValidationResult;
@@ -116,6 +118,15 @@ public abstract class PfKeyImpl extends PfKey {
}
/**
+ * Determines if the name is "null".
+ *
+ * @return {@code true} if the name is null, {@code false} otherwise
+ */
+ public boolean isNullName() {
+ return PfKey.NULL_KEY_NAME.equals(getName());
+ }
+
+ /**
* Determines if the version is "null".
*
* @return {@code true} if the version is null, {@code false} otherwise
@@ -147,7 +158,7 @@ public abstract class PfKeyImpl extends PfKey {
}
if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
- && !thisVersionArray[1].equals(otherVersionArray[1])) {
+ && !thisVersionArray[1].equals(otherVersionArray[1])) {
return Compatibility.MINOR;
}
@@ -189,12 +200,12 @@ public abstract class PfKeyImpl extends PfKey {
}
if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
- && !thisVersionArray[1].equals(otherVersionArray[1])) {
+ && !thisVersionArray[1].equals(otherVersionArray[1])) {
return Integer.valueOf(thisVersionArray[1]) > Integer.valueOf(otherVersionArray[1]);
}
if (thisVersionArray.length >= 3 && otherVersionArray.length >= 3
- && !thisVersionArray[2].equals(otherVersionArray[2])) {
+ && !thisVersionArray[2].equals(otherVersionArray[2])) {
return Integer.valueOf(thisVersionArray[2]) > Integer.valueOf(otherVersionArray[2]);
}
@@ -215,8 +226,7 @@ public abstract class PfKeyImpl extends PfKey {
if (versionArray.length >= 2) {
return Integer.parseInt(versionArray[1]);
- }
- else {
+ } else {
return 0;
}
}
@@ -227,26 +237,25 @@ public abstract class PfKeyImpl extends PfKey {
if (versionArray.length >= 3) {
return Integer.parseInt(versionArray[2]);
- }
- else {
+ } else {
return 0;
}
}
@Override
public PfValidationResult validate(final PfValidationResult result) {
- final String nameValidationErrorMessage = Assertions.getStringParameterValidationMessage(NAME_TOKEN, getName(),
- getNameRegEx());
+ final String nameValidationErrorMessage =
+ Assertions.getStringParameterValidationMessage(NAME_TOKEN, getName(), getNameRegEx());
if (nameValidationErrorMessage != null) {
result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
- "name invalid-" + nameValidationErrorMessage));
+ "name invalid-" + nameValidationErrorMessage));
}
- final String versionValidationErrorMessage = Assertions.getStringParameterValidationMessage(VERSION_TOKEN,
- getVersion(), getVersionRegEx());
+ final String versionValidationErrorMessage =
+ Assertions.getStringParameterValidationMessage(VERSION_TOKEN, getVersion(), getVersionRegEx());
if (versionValidationErrorMessage != null) {
result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
- "version invalid-" + versionValidationErrorMessage));
+ "version invalid-" + versionValidationErrorMessage));
}
return result;