X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-base%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fmodels%2Fbase%2FPfConceptContainer.java;h=b4d51d1736670a5d0bd1cb8800a215eefee824ee;hb=f98830a4ba01188538ca0b001143f7b573ecde77;hp=e197e13a48918f970dc639c91dac87b794b75d8b;hpb=0ac03692ecd0fbe69660b1246ca0c1fbac53e623;p=policy%2Fmodels.git 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 e197e13a4..b4d51d173 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 @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. 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. @@ -29,11 +30,15 @@ import java.util.Map.Entry; import java.util.NavigableMap; import java.util.Set; import java.util.TreeMap; +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; @@ -41,7 +46,7 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; -import org.onap.policy.common.utils.validation.Assertions; +import org.apache.commons.lang3.StringUtils; import org.onap.policy.models.base.PfValidationResult.ValidationResult; // @formatter:off @@ -57,19 +62,32 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult; * @param C the concept being contained */ //@formatter:on +@MappedSuperclass @Entity @Table(name = "PfConceptContainer") @Data @EqualsAndHashCode(callSuper = false) public class PfConceptContainer extends PfConcept - implements PfConceptGetter, PfAuthorative>> { + implements PfConceptGetter, PfAuthorative>> { private static final long serialVersionUID = -324211738823208318L; @EmbeddedId private PfConceptKey key; @ManyToMany(cascade = CascadeType.ALL) + // @formatter:off + @JoinTable( + joinColumns = { + @JoinColumn(name = "conceptContainerMapName", referencedColumnName = "name"), + @JoinColumn(name = "concpetContainerMapVersion", referencedColumnName = "version") + }, + inverseJoinColumns = { + @JoinColumn(name = "conceptContainerName", referencedColumnName = "name"), + @JoinColumn(name = "conceptContainerVersion", referencedColumnName = "version") + } + ) + // @formatter:on private Map conceptMap; /** @@ -110,6 +128,14 @@ public class PfConceptContainer ex */ public PfConceptContainer(@NonNull final PfConceptContainer copyConcept) { super(copyConcept); + this.key = new PfConceptKey(copyConcept.key); + + this.conceptMap = new TreeMap<>(); + for (final Entry conceptMapEntry : copyConcept.conceptMap.entrySet()) { + PfConceptKey newK = new PfConceptKey(conceptMapEntry.getKey()); + C newC = PfUtils.makeCopy(conceptMapEntry.getValue()); + this.conceptMap.put(newK, newC); + } } @Override @@ -125,16 +151,22 @@ public class PfConceptContainer ex @Override public List> toAuthorative() { - Map toscaPolicyMap = new LinkedHashMap<>(); + // The returned list is a list of map singletons with one map for each map + // entry in the concept container + List> toscaPolicyMapList = new ArrayList<>(); for (Entry conceptEntry : getConceptMap().entrySet()) { + // Create a map to hold this entry + Map toscaPolicyMap = new LinkedHashMap<>(1); + + // Add the concept container entry to the singleton map @SuppressWarnings("unchecked") PfAuthorative authoritiveImpl = (PfAuthorative) conceptEntry.getValue(); toscaPolicyMap.put(conceptEntry.getKey().getName(), authoritiveImpl.toAuthorative()); - } - List> toscaPolicyMapList = new ArrayList<>(); - toscaPolicyMapList.add(toscaPolicyMap); + // Add the map to the returned list + toscaPolicyMapList.add(toscaPolicyMap); + } return toscaPolicyMapList; } @@ -148,29 +180,32 @@ public class PfConceptContainer ex for (Map incomingConceptMap : authorativeList) { // Add the map entries one by one for (Entry 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 authoritiveImpl = (PfAuthorative) 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()); - } - // After all that, save the map entry conceptMap.put(conceptKey, jpaConcept); } @@ -178,7 +213,7 @@ public class PfConceptContainer 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"); } } @@ -197,15 +232,12 @@ public class PfConceptContainer 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); - if (conceptMap.isEmpty()) { - result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "conceptMap may not be empty")); - } else { + if (!conceptMap.isEmpty()) { result = validateConceptMap(result); } @@ -224,20 +256,18 @@ public class PfConceptContainer ex for (final Entry 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")); - } else - if (conceptEntry.getValue() == null) { - result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "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())); - result = conceptEntry.getValue().validate(result); - } else { - result = conceptEntry.getValue().validate(result); - } + "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")); + } 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())); + result = conceptEntry.getValue().validate(result); + } else { + result = conceptEntry.getValue().validate(result); + } } return result; } @@ -251,7 +281,7 @@ public class PfConceptContainer ex return 0; } if (getClass() != otherConcept.getClass()) { - return this.hashCode() - otherConcept.hashCode(); + return getClass().getName().compareTo(otherConcept.getClass().getName()); } @SuppressWarnings("unchecked") @@ -268,27 +298,13 @@ public class PfConceptContainer ex return 0; } - @Override - public PfConcept copyTo(@NonNull final PfConcept target) { - Assertions.instanceOf(target, PfConceptContainer.class); - - @SuppressWarnings("unchecked") - final PfConceptContainer copy = (PfConceptContainer) target; - copy.setKey(new PfConceptKey(key)); - final Map newConceptMap = new TreeMap<>(); - for (final Entry conceptMapEntry : conceptMap.entrySet()) { - C newC = getConceptNewInstance(); - conceptMapEntry.getValue().copyTo(newC); - newConceptMap.put(new PfConceptKey(conceptMapEntry.getKey()), newC); - } - copy.setConceptMap(newConceptMap); - - return copy; - } - @Override public C get(final PfConceptKey conceptKey) { - return new PfConceptGetterImpl<>((NavigableMap) conceptMap).get(conceptKey); + if (conceptKey.isNullVersion()) { + return get(conceptKey.getName()); + } else { + return new PfConceptGetterImpl<>((NavigableMap) conceptMap).get(conceptKey); + } } @Override @@ -299,7 +315,7 @@ public class PfConceptContainer ex @Override public C get(final String conceptKeyName, final String conceptKeyVersion) { return new PfConceptGetterImpl<>((NavigableMap) conceptMap).get(conceptKeyName, - conceptKeyVersion); + conceptKeyVersion); } @Override @@ -310,7 +326,7 @@ public class PfConceptContainer ex @Override public Set getAll(final String conceptKeyName, final String conceptKeyVersion) { return new PfConceptGetterImpl<>((NavigableMap) conceptMap).getAll(conceptKeyName, - conceptKeyVersion); + conceptKeyVersion); } /** @@ -322,11 +338,24 @@ public class PfConceptContainer ex private C getConceptNewInstance() { try { String conceptClassName = - ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0].getTypeName(); - return (C) Class.forName(conceptClassName).newInstance(); + ((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 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"); } } }