From b0e460abe43d40528da4856f2f3d5ee162ac1830 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Thu, 5 Sep 2019 21:10:46 +0000 Subject: Repair policy types in TOSCA service template Changed policy types from a list of maps to a map. Changed data types from a list of maps to a map. Made all knock on changes to adapters and tests. Updated Issue ID. Added onap.policies.controlloop.guard.coordination.FirstBlocksSecond.yaml policy type that is required to be preloaded in policy-api. Issue-ID: POLICY-2047 Change-Id: Ic8e809eab248fd7242f0e565fa2e3da75bcfbdeb Signed-off-by: liamfallon --- .../tosca/authorative/concepts/ToscaEntity.java | 24 +++++++++++ .../authorative/concepts/ToscaServiceTemplate.java | 9 ++--- .../provider/AuthorativeToscaProvider.java | 29 +++++++++++--- .../simple/concepts/JpaToscaServiceTemplate.java | 23 +++++++++-- .../concepts/ToscaPolicyTypeFilterTest.java | 40 ++++++++++--------- .../AuthorativeToscaProviderPolicyTypeTest.java | 46 +++++++++++----------- 6 files changed, 115 insertions(+), 56 deletions(-) (limited to 'models-tosca') diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntity.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntity.java index c0f40f159..0a8fa2788 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntity.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntity.java @@ -111,4 +111,28 @@ public class ToscaEntity implements PfNameVersion { 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 Map getEntityMapAsMap(Map mapOfEntities) { + // Declare the return map + Map 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; + } + } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplate.java index 21b15a8ae..17391964e 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplate.java @@ -25,7 +25,6 @@ package org.onap.policy.models.tosca.authorative.concepts; import com.google.gson.annotations.SerializedName; import io.swagger.annotations.ApiModelProperty; -import java.util.List; import java.util.Map; import lombok.Data; import lombok.EqualsAndHashCode; @@ -49,17 +48,17 @@ public class ToscaServiceTemplate extends ToscaEntity { @ApiModelProperty(name = "policy_types") @SerializedName("policy_types") - private List> policyTypes; + private Map policyTypes; @ApiModelProperty(name = "data_types") @SerializedName("data_types") - private List> dataTypes; + private Map dataTypes; public Map getPolicyTypesAsMap() { - return ToscaEntity.getEntityListMapAsMap(policyTypes); + return ToscaEntity.getEntityMapAsMap(policyTypes); } public Map getDataTypesAsMap() { - return ToscaEntity.getEntityListMapAsMap(dataTypes); + return ToscaEntity.getEntityMapAsMap(dataTypes); } } \ No newline at end of file diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java index e41281d7f..8c6e492f7 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java @@ -25,6 +25,8 @@ import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.TreeMap; + import lombok.NonNull; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; @@ -83,8 +85,8 @@ public class AuthorativeToscaProvider { LOGGER.debug("->getPolicyTypeList: name={}, version={}", name, version); - List policyTypeList = asConceptList( - new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative().getPolicyTypes()); + List policyTypeList = new ArrayList<>( + new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative().getPolicyTypes().values()); LOGGER.debug("<-getPolicyTypeList: name={}, version={}, policyTypeList={}", name, version, policyTypeList); return policyTypeList; @@ -106,7 +108,7 @@ public class AuthorativeToscaProvider { ToscaServiceTemplate serviceTemplate = new SimpleToscaProvider().getPolicyTypes(dao, null, null).toAuthorative(); - List filteredPolicyTypes = asConceptList(serviceTemplate.getPolicyTypes()); + List filteredPolicyTypes = new ArrayList<>(serviceTemplate.getPolicyTypes().values()); filteredPolicyTypes = filter.filter(filteredPolicyTypes); serviceTemplate.setPolicyTypes(asConceptMap(filteredPolicyTypes)); @@ -257,7 +259,7 @@ public class AuthorativeToscaProvider { List filteredPolicies = asConceptList(serviceTemplate.getToscaTopologyTemplate().getPolicies()); filteredPolicies = filter.filter(filteredPolicies); - serviceTemplate.getToscaTopologyTemplate().setPolicies(asConceptMap(filteredPolicies)); + serviceTemplate.getToscaTopologyTemplate().setPolicies(asConceptMapList(filteredPolicies)); LOGGER.debug("<-getFilteredPolicies: filter={}, serviceTemplate={}", filter, serviceTemplate); return serviceTemplate; @@ -368,14 +370,29 @@ public class AuthorativeToscaProvider { * @param conceptList the concept list * @return the list of concept map */ - private List> asConceptMap(List conceptList) { + private List> asConceptMapList(List conceptList) { List> toscaEntityMapList = new ArrayList<>(); for (T concept : conceptList) { - Map conceptMap = new LinkedHashMap<>(); + Map conceptMap = new TreeMap<>(); conceptMap.put(concept.getName(), concept); toscaEntityMapList.add(conceptMap); } return toscaEntityMapList; } + + /** + * Return the contents of a list of concepts as map of concepts. + * + * @param conceptList the concept list + * @return the list of concept map + */ + private Map asConceptMap(List conceptList) { + Map conceptMap = new LinkedHashMap<>(); + for (T concept : conceptList) { + conceptMap.put(concept.getName(), concept); + } + + return conceptMap; + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java index 83c9dc0cf..e07894cd8 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java @@ -22,7 +22,12 @@ package org.onap.policy.models.tosca.simple.concepts; import com.google.gson.annotations.SerializedName; + +import java.util.Collections; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; + import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; @@ -43,6 +48,8 @@ import org.onap.policy.models.base.PfKey; import org.onap.policy.models.base.PfValidationMessage; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; /** @@ -141,11 +148,19 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType()); + List> dataTypeMapList = dataTypes.toAuthorative(); + for (Map dataTypeMap : dataTypeMapList) { + toscaServiceTemplate.getDataTypes().putAll(dataTypeMap); + } } if (policyTypes != null) { - toscaServiceTemplate.setPolicyTypes(policyTypes.toAuthorative()); + toscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>()); + List> policyTypeMapList = policyTypes.toAuthorative(); + for (Map policyTypeMap : policyTypeMapList) { + toscaServiceTemplate.getPolicyTypes().putAll(policyTypeMap); + } } if (topologyTemplate != null) { @@ -171,12 +186,12 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType foundPolicyTypeMap : serviceTemplate.getPolicyTypes()) { - addPolicyTypes(foundPolicyTypeMap); - } + addPolicyTypes(serviceTemplate.getPolicyTypes()); } for (ToscaPolicyType type : typeList) { @@ -138,21 +142,21 @@ public class ToscaPolicyTypeFilterTest { ToscaPolicyTypeFilter.builder().version(ToscaPolicyTypeFilter.LATEST_VERSION).build(); List filteredList = filter.filter(typeList); - assertEquals(13, filteredList.size()); + assertEquals(20, filteredList.size()); assertEquals(VERSION_100, filteredList.get(0).getVersion()); - assertEquals(VERSION_000, filteredList.get(4).getVersion()); + assertEquals(VERSION_000, filteredList.get(11).getVersion()); typeList.get(12).setVersion("2.0.0"); filteredList = filter.filter(typeList); - assertEquals(13, filteredList.size()); - assertEquals("2.0.0", filteredList.get(0).getVersion()); - assertEquals(VERSION_000, filteredList.get(4).getVersion()); + assertEquals(20, filteredList.size()); + assertEquals("2.0.0", filteredList.get(11).getVersion()); + assertEquals(VERSION_000, filteredList.get(18).getVersion()); typeList.get(12).setVersion(VERSION_100); filteredList = filter.filter(typeList); - assertEquals(13, filteredList.size()); + assertEquals(20, filteredList.size()); assertEquals(VERSION_100, filteredList.get(0).getVersion()); - assertEquals(VERSION_000, filteredList.get(4).getVersion()); + assertEquals(VERSION_000, filteredList.get(18).getVersion()); } @Test @@ -174,7 +178,7 @@ public class ToscaPolicyTypeFilterTest { assertEquals(9, filteredList.size()); filter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.Vim_fit").version(VERSION_000) - .build(); + .build(); filteredList = filter.filter(typeList); assertEquals(1, filteredList.size()); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java index 8bed1e4a1..d77b52daa 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java @@ -27,7 +27,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import com.google.gson.GsonBuilder; -import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; import java.util.Properties; import org.apache.commons.lang3.ObjectUtils; @@ -137,15 +137,15 @@ public class AuthorativeToscaProviderPolicyTypeTest { PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0); - ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); - ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); + ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion()); - ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); @@ -160,11 +160,11 @@ public class AuthorativeToscaProviderPolicyTypeTest { assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null); - assertEquals(2, gotPolicyTypeList.size()); + assertEquals(1, gotPolicyTypeList.size()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, VERSION_000); - assertEquals(2, gotPolicyTypeList.size()); + assertEquals(1, gotPolicyTypeList.size()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); } @@ -203,29 +203,29 @@ public class AuthorativeToscaProviderPolicyTypeTest { PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0); - ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); - ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); + ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaPolicyTypeFilter.builder().build()); - ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); + ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription())); gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build()); - gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription())); gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version(VERSION_000).build()); - gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription())); @@ -236,7 +236,7 @@ public class AuthorativeToscaProviderPolicyTypeTest { gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, ToscaPolicyTypeFilter.builder().build()); - assertEquals(2, gotPolicyTypeList.size()); + assertEquals(1, gotPolicyTypeList.size()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, @@ -251,7 +251,7 @@ public class AuthorativeToscaProviderPolicyTypeTest { gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, ToscaPolicyTypeFilter.builder().version("1.0.0").build()); - assertEquals(1, gotPolicyTypeList.size()); + assertEquals(0, gotPolicyTypeList.size()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); } @@ -274,7 +274,7 @@ public class AuthorativeToscaProviderPolicyTypeTest { new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate); }).hasMessage(MISSING_POLICY_TYPES); - testToscaServiceTemplate.setPolicyTypes(new ArrayList<>()); + testToscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>()); assertThatThrownBy(() -> { new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate); }).hasMessage("An incoming list of concepts must have at least one entry"); @@ -287,8 +287,8 @@ public class AuthorativeToscaProviderPolicyTypeTest { PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0); - ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); - ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); + ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); } @@ -319,15 +319,15 @@ public class AuthorativeToscaProviderPolicyTypeTest { PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0); - ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); - ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); + ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); ToscaServiceTemplate updatedServiceTemplate = new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate); - ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); + ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription())); } @@ -370,15 +370,15 @@ public class AuthorativeToscaProviderPolicyTypeTest { PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0); - ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); - ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); + ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion()); - ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription())); @@ -405,7 +405,7 @@ public class AuthorativeToscaProviderPolicyTypeTest { new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate); }).hasMessage(MISSING_POLICY_TYPES); - testServiceTemplate.setPolicyTypes(new ArrayList<>()); + testServiceTemplate.setPolicyTypes(new LinkedHashMap<>()); assertThatThrownBy(() -> { new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate); }).hasMessage("An incoming list of concepts must have at least one entry"); -- cgit 1.2.3-korg