diff options
author | Jorge Hernandez <jorge.hernandez-herrero@att.com> | 2019-06-17 13:43:19 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-06-17 13:43:19 +0000 |
commit | f59ec395bf1e41df894f884e70ff3185280668c0 (patch) | |
tree | da740e2fdb029e8c649c9492f689089cf2c7c7a0 /models-tosca/src | |
parent | aa0526fc9a995457aac1b803d428d253bef9116a (diff) | |
parent | 10c11e142feb1d040612fed695aaf76d007294da (diff) |
Merge "Allow multiple versions of entities to be returned"
Diffstat (limited to 'models-tosca/src')
8 files changed, 146 insertions, 9 deletions
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 f5a178a37..c0f40f159 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 @@ -25,12 +25,17 @@ 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; /** @@ -71,4 +76,39 @@ 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; + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntityKey.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntityKey.java new file mode 100644 index 000000000..adde63b60 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntityKey.java @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.authorative.concepts; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * This class is a compound key for Tosca entity keying on maps. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class ToscaEntityKey { + private String name; + private String version; +} 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 0b19708dc..21b15a8ae 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 @@ -54,4 +54,12 @@ public class ToscaServiceTemplate extends ToscaEntity { @ApiModelProperty(name = "data_types") @SerializedName("data_types") private List<Map<String, ToscaDataType>> dataTypes; + + public Map<ToscaEntityKey, ToscaPolicyType> getPolicyTypesAsMap() { + return ToscaEntity.getEntityListMapAsMap(policyTypes); + } + + public Map<ToscaEntityKey, ToscaDataType> getDataTypesAsMap() { + return ToscaEntity.getEntityListMapAsMap(dataTypes); + } }
\ No newline at end of file diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaTopologyTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaTopologyTemplate.java index ebb53e177..74ebf07e0 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaTopologyTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaTopologyTemplate.java @@ -37,4 +37,8 @@ public class ToscaTopologyTemplate { private String description; private List<Map<String, ToscaPolicy>> policies; + + public Map<ToscaEntityKey, ToscaPolicy> getPoliciesAsMap() { + return ToscaEntity.getEntityListMapAsMap(policies); + } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTest.java index f5be66c4a..8ee74233b 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTest.java @@ -46,6 +46,8 @@ public class ToscaPolicyTest { policy.setType("my_type"); policy.setTypeVersion("3.2.1"); + assertEquals("ToscaEntityKey(name=my_name, version=1.2.3)", policy.getKey().toString()); + ToscaPolicyIdentifier ident = policy.getIdentifier(); assertEquals("my_name", ident.getName()); assertEquals("1.2.3", ident.getVersion()); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java index a7f3761a5..10f3b0db2 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java @@ -27,6 +27,7 @@ import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Properties; import org.eclipse.persistence.config.PersistenceUnitProperties; @@ -382,7 +383,7 @@ public class AuthorativeToscaProviderPolicyTest { ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()); - assertEquals(0, gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).size()); + assertTrue(gotServiceTemplate.getToscaTopologyTemplate().getPolicies().isEmpty()); } @Test @@ -408,6 +409,49 @@ public class AuthorativeToscaProviderPolicyTest { }).hasMessage("An incoming list of concepts must have at least one entry"); } + @Test + public void testEntityMaps() throws CoderException, PfModelException { + Object yamlObject = new Yaml().load( + ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml")); + String yamlAsJsonString = new StandardCoder().encode(yamlObject); + + ToscaServiceTemplate toscaServiceTemplatePolicyType = + standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplatePolicyType); + new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType); + + assertEquals(3, toscaServiceTemplatePolicyType.getDataTypesAsMap().size()); + assertEquals(2, toscaServiceTemplatePolicyType.getPolicyTypesAsMap().size()); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), + ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + + PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + + ToscaPolicy beforePolicy = + toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + ToscaPolicy createdPolicy = + createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + + assertEquals(1, toscaServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size()); + assertEquals(1, createdServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size()); + + Map<String, ToscaPolicy> policyMapItem = createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0); + createdServiceTemplate.getToscaTopologyTemplate().getPolicies().add(policyMapItem); + + assertThatThrownBy(() -> { + createdServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap(); + }).hasMessageContaining("list of map of entities contains more than one entity with key"); + } + private void createPolicyTypes() throws CoderException, PfModelException { Object yamlObject = new Yaml().load( ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml")); 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 6a925bcf3..ded2cdee2 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 @@ -23,6 +23,7 @@ package org.onap.policy.models.tosca.authorative.provider; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import com.google.gson.GsonBuilder; @@ -133,7 +134,7 @@ public class AuthorativeToscaProviderPolicyTypeTest { PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0"); ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); - ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); @@ -199,7 +200,7 @@ public class AuthorativeToscaProviderPolicyTypeTest { PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0"); ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); - ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); @@ -283,7 +284,7 @@ public class AuthorativeToscaProviderPolicyTypeTest { PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0"); ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); - ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); } @@ -315,14 +316,14 @@ public class AuthorativeToscaProviderPolicyTypeTest { PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0"); ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); - ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).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(0).get(policyTypeKey.getName()); + ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription())); } @@ -366,7 +367,7 @@ public class AuthorativeToscaProviderPolicyTypeTest { PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0"); ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); - ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); @@ -380,7 +381,7 @@ public class AuthorativeToscaProviderPolicyTypeTest { ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion()); - assertEquals(0, gotServiceTemplate.getPolicyTypes().get(0).size()); + assertTrue(gotServiceTemplate.getPolicyTypes().isEmpty()); } @Test diff --git a/models-tosca/src/test/resources/logback-test.xml b/models-tosca/src/test/resources/logback-test.xml index 6e2737da6..a7a583827 100644 --- a/models-tosca/src/test/resources/logback-test.xml +++ b/models-tosca/src/test/resources/logback-test.xml @@ -25,7 +25,7 @@ <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" /> <property name="LOG_DIR" value="${java.io.tmpdir}/pf_logging/" /> - <!-- USE FOR STD OUT ONLY --> + <!-- USE FOR STD OUTPUT ONLY --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <Pattern>%d %contextName [%t] %level %logger{36} - %msg%n</Pattern> |