diff options
author | rameshiyer27 <ramesh.murugan.iyer@est.tech> | 2022-02-07 17:07:38 +0000 |
---|---|---|
committer | rameshiyer27 <ramesh.murugan.iyer@est.tech> | 2022-02-10 14:20:42 +0000 |
commit | 45b653fc5a8d641452247eca5c80cf580609e9bf (patch) | |
tree | 06cfd4529db05dc23cbd63d2fe2e53799eb07bdf /models-tosca | |
parent | 303d42453fbfcf2381f810b10a496b69aa8bc34b (diff) |
Add policy metadataSet handling as node templates
Each metadataSet is represented as a 'node_template' in Tosca
mapped to a specific node type.
Support added for db operations of node templates as
independent entities.
Detailed documentation available here :
https://wiki.onap.org/display/DW/Enable+Handling+of+Policy+Type+Metadata
Issue-ID: POLICY-3832
Signed-off-by: zrrmmua <ramesh.murugan.iyer@est.tech>
Change-Id: I6c189142b1778ba858aae27cd92d4f136d950208
Diffstat (limited to 'models-tosca')
9 files changed, 785 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 919d187de..c339f721e 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy Model * ================================================================================ - * Copyright (C) 2019-2021 Nordix Foundation. + * Copyright (C) 2019-2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -51,7 +51,7 @@ public class ToscaEntity implements PfNameVersion { @SerializedName("derived_from") private String derivedFrom; - private Map<String, String> metadata; + private Map<String, Object> metadata; private String description; /** @@ -67,7 +67,7 @@ public class ToscaEntity implements PfNameVersion { if (copyObject.metadata != null) { metadata = new LinkedHashMap<>(); - for (final Entry<String, String> metadataEntry : copyObject.metadata.entrySet()) { + for (final Entry<String, Object> metadataEntry : copyObject.metadata.entrySet()) { metadata.put(metadataEntry.getKey(), metadataEntry.getValue()); } } 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 8171b7d78..2ec9ee2c8 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019-2021 Nordix Foundation. + * Copyright (C) 2019-2022 Nordix Foundation. * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -35,6 +35,8 @@ import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.dao.PfDao; import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityFilter; +import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; @@ -549,6 +551,125 @@ public class AuthorativeToscaProvider { } /** + * Create tosca node templates. + * + * @param dao the DAO to use to access the database + * @param toscaServiceTemplate the template with node templates entities to be created. + * @return the toscaServiceTemplate with node templates that were created + * @throws PfModelException on errors creating node templates + */ + public ToscaServiceTemplate createToscaNodeTemplates(@NonNull final PfDao dao, + @NonNull final ToscaServiceTemplate toscaServiceTemplate) + throws PfModelException { + LOGGER.debug("createToscaNodeTemplates ={}", toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate; + + synchronized (providerLockObject) { + createdServiceTemplate = new SimpleToscaProvider() + .createToscaNodeTemplates(dao, new JpaToscaServiceTemplate(toscaServiceTemplate)).toAuthorative(); + } + + LOGGER.debug("<-createToscaNodeTemplates: createdServiceTemplate={}", createdServiceTemplate); + return createdServiceTemplate; + } + + /** + * Update tosca node templates. + * + * @param dao the DAO to use to access the database + * @param serviceTemplate the service template containing the definitions of the nodeTemplates to be updated. + * @return the TOSCA service template containing the nodeTemplates that were updated + * @throws PfModelRuntimeException on errors updating node templates + */ + public ToscaServiceTemplate updateToscaNodeTemplates(@NonNull final PfDao dao, + @NonNull final ToscaServiceTemplate serviceTemplate) + throws PfModelException { + LOGGER.debug("->updateToscaNodeTemplates: serviceTemplate={}", serviceTemplate); + ToscaServiceTemplate updatedServiceTemplate; + + synchronized (providerLockObject) { + updatedServiceTemplate = new SimpleToscaProvider() + .updateToscaNodeTemplates(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative(); + } + + LOGGER.debug("<-updateToscaNodeTemplates: updatedServiceTemplate={}", updatedServiceTemplate); + return updatedServiceTemplate; + } + + + /** + * Delete a tosca node template. + * + * @param dao the DAO to use to access the database + * @param name the name of the node template to delete. + * @param version the version of the node template to delete. + * @return the TOSCA service template containing the node template that was deleted + * @throws PfModelException on errors deleting node template + */ + public ToscaServiceTemplate deleteToscaNodeTemplate(@NonNull final PfDao dao, @NonNull final String name, + @NonNull final String version) throws PfModelException { + LOGGER.debug("->deleteToscaNodeTemplate: name={}, version={}", name, version); + ToscaServiceTemplate deletedServiceTemplate; + + synchronized (providerLockObject) { + deletedServiceTemplate = + new SimpleToscaProvider().deleteToscaNodeTemplate(dao, new PfConceptKey(name, version)).toAuthorative(); + } + LOGGER.debug("<-deleteToscaNodeTemplate: name={}, version={}, deletedServiceTemplate={}", name, version, + deletedServiceTemplate); + return deletedServiceTemplate; + } + + /** + * Get node template metadataSet. + * + * @param dao the DAO to use to access the database + * @param name the name of the metadataSet to get, null to get all metadataSets + * @param version the version of the metadataSet to get, null to get all versions of a metadataSets + * @return the metadataSets found + * @throws PfModelException on errors getting policy metadataSet + */ + public List<Map<ToscaEntityKey, Map<String, Object>>> getNodeTemplateMetadataSet( + @NonNull final PfDao dao, final String name, final String version) + throws PfModelException { + LOGGER.debug("->getNodeTemplateMetadataSet: name={}, version={}", name, version); + List<Map<ToscaEntityKey, Map<String, Object>>> metadataSets; + + synchronized (providerLockObject) { + metadataSets = new SimpleToscaProvider().getNodeTemplateMetadata(dao, name, version); + } + LOGGER.debug("<-getNodeTemplateMetadataSet: name={}, version={}, metadataSets={}", name, version, + metadataSets); + return metadataSets; + } + + /** + * Get tosca node templates. + * + * @param dao the DAO to use to access the database + * @param name the name of the node template to get, null to get all node templates + * @param version the version of the node template to get, null to get all versions of node template + * @return the node templates found + * @throws PfModelException on errors getting tosca node templates + */ + public List<Map<PfConceptKey, ToscaNodeTemplate>> getToscaNodeTemplate( + @NonNull final PfDao dao, final String name, final String version) + throws PfModelException { + LOGGER.debug("->getNodeTemplate: name={}, version={}", name, version); + List<Map<PfConceptKey, ToscaNodeTemplate>> nodeTemplates = new ArrayList<>(); + + synchronized (providerLockObject) { + new SimpleToscaProvider().getToscaNodeTemplates(dao, name, version) + .getConceptMap().forEach((key, value) -> nodeTemplates.add(Map.of(key, value.toAuthorative()))); + } + + LOGGER.debug("<-getNodeTemplate: name={}, version={}, nodeTemplates={}", name, version, + nodeTemplates); + return nodeTemplates; + } + + + /** * Return the contents of a list of maps as a plain list. * * @param listOfMaps the list of maps diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java index 2416bab69..84381fb50 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019-2020 Nordix Foundation. + * Copyright (C) 2019-2020,2022 Nordix Foundation. * Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -29,6 +29,7 @@ import javax.persistence.AttributeOverride; import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.EmbeddedId; +import javax.persistence.Lob; import javax.persistence.MappedSuperclass; import lombok.Data; import lombok.EqualsAndHashCode; @@ -66,6 +67,7 @@ public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept impleme private PfConceptKey derivedFrom; @ElementCollection + @Lob private Map<@NotNull @NotBlank String, @NotNull @NotBlank String> metadata; @Column @@ -156,7 +158,7 @@ public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept impleme description = toscaEntity.getDescription(); } - metadata = PfUtils.mapMap(toscaEntity.getMetadata(), item -> item); + metadata = PfUtils.mapMap(toscaEntity.getMetadata(), Object::toString); } @Override diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java index 862108b15..e2f61c464 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019-2020 Nordix Foundation. + * Copyright (C) 2019-2020,2022 Nordix Foundation. * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,6 +24,8 @@ package org.onap.policy.models.tosca.simple.provider; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Map; +import java.util.Optional; import javax.ws.rs.core.Response; import lombok.NonNull; import org.apache.commons.collections4.CollectionUtils; @@ -36,9 +38,15 @@ import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.dao.PfDao; import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; +import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes; import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplates; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeType; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTypes; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType; @@ -641,6 +649,195 @@ public class SimpleToscaProvider { return deletedServiceTemplate; } + + /** + * Get metadata of tosca node templates. + * + * @param dao the DAO to use to access the database + * @param name the name of the nodeTemplate to get, set to null to get all node templates + * @param version the version of the metadataSet to get, set to null to get all versions + * @return the list of maps with node template key and metadata values found + * @throws PfModelException on errors getting metadataSets + */ + public List<Map<ToscaEntityKey, Map<String, Object>>> getNodeTemplateMetadata( + @NonNull final PfDao dao, final String name, final String version) + throws PfModelException { + LOGGER.debug("->getNodeTemplateMetadataSet: name={}, version={}", name, version); + + List<Map<ToscaEntityKey, Map<String, Object>>> metadataSets = new ArrayList<>(); + + JpaToscaServiceTemplate dbServiceTemplate = getServiceTemplate(dao); + + //Return empty list if no node templates present in db + if (!ToscaUtils.doNodeTemplatesExist(dbServiceTemplate)) { + return metadataSets; + } + var returnServiceTemplate = new JpaToscaServiceTemplate(dbServiceTemplate); + List<ToscaNodeTemplate> toscaNodeTemplates = new ArrayList<>(); + returnServiceTemplate.getTopologyTemplate() + .getNodeTemplates().getConceptMap().forEach((key, value) -> toscaNodeTemplates.add(value + .toAuthorative())); + + //Filter metadataSet for specific node template + if (name != null && version != null) { + var filterKey = new ToscaEntityKey(name, version); + toscaNodeTemplates.removeIf(entity -> !entity.getKey().equals(filterKey)); + } + toscaNodeTemplates.forEach(e -> metadataSets.add(Map.of(e.getKey(), e.getMetadata()))); + LOGGER.debug("<-getNodeTemplateMetadataSet: name={}, version={}, metadataSets={}", name, version, + metadataSets); + + return metadataSets; + } + + /** + * Get tosca node templates. + * + * @param dao the DAO to use to access the database + * @param name the name of the node template to get, set to null to get all node templates + * @param version the version of the node template to get, set to null to get all versions + * @return the node templates with the specified key + * @throws PfModelException on errors getting node templates + */ + public JpaToscaNodeTemplates getToscaNodeTemplates(@NonNull final PfDao dao, final String name, + final String version) + throws PfModelException { + LOGGER.debug("->getNodeTemplate: name={}, version={}", name, version); + + var nodeTemplates = new JpaToscaNodeTemplates(); + + JpaToscaServiceTemplate dbServiceTemplate = getServiceTemplate(dao); + + //Return empty if no nodeTemplates present in db + if (!ToscaUtils.doNodeTemplatesExist(dbServiceTemplate)) { + return nodeTemplates; + } + var returnServiceTemplate = new JpaToscaServiceTemplate(dbServiceTemplate); + nodeTemplates = returnServiceTemplate.getTopologyTemplate().getNodeTemplates(); + + //Filter specific nodeTemplates + if (name != null && version != null) { + var filterKey = new PfConceptKey(name, version); + nodeTemplates.getConceptMap().entrySet().removeIf(entity -> !entity.getKey().equals(filterKey)); + } + LOGGER.debug("<-getNodeTemplateMetadataSet: name={}, version={}, nodeTemplates={}", name, version, + nodeTemplates); + + return nodeTemplates; + } + + + /** + * Update tosca node template. + * + * @param dao the DAO to use to access the database + * @param serviceTemplate the service template containing the definitions of the node templates to be updated. + * @return the TOSCA service template containing the node templates that were updated + * @throws PfModelRuntimeException on errors updating node templates + */ + public JpaToscaServiceTemplate updateToscaNodeTemplates(@NonNull final PfDao dao, + @NonNull final JpaToscaServiceTemplate serviceTemplate) + throws PfModelException { + LOGGER.debug("->updateToscaNodeTemplates: serviceTemplate={}", serviceTemplate); + + ToscaUtils.assertNodeTemplatesExist(serviceTemplate); + for (JpaToscaNodeTemplate nodeTemplate : serviceTemplate.getTopologyTemplate().getNodeTemplates() + .getAll(null)) { + + //verify if the node template is referenced in the metadata of created policies + assertNodeTemplateNotUsedInPolicy(dao, nodeTemplate.getName(), nodeTemplate.getVersion()); + verifyNodeTypeInDbTemplate(dao, nodeTemplate); + + dao.update(nodeTemplate); + } + // Return the service template with updated node templates + var updatedNodeTemplates = new JpaToscaNodeTemplates(); + updatedNodeTemplates.setKey(serviceTemplate.getTopologyTemplate().getNodeTemplates().getKey()); + + for (PfConceptKey metadataSetKey : serviceTemplate.getTopologyTemplate().getNodeTemplates().getConceptMap() + .keySet()) { + updatedNodeTemplates.getConceptMap().put(metadataSetKey, dao.get(JpaToscaNodeTemplate.class, + metadataSetKey)); + } + serviceTemplate.getTopologyTemplate().setNodeTemplates(updatedNodeTemplates); + + LOGGER.debug("<-updatedToscaNodeTemplates: serviceTemplate={}", serviceTemplate); + return serviceTemplate; + } + + /** + * Delete a tosca node template. + * + * @param dao the DAO to use to access the database + * @param nodeTemplateKey the node template key + * @return the TOSCA service template containing the node templates that were deleted + * @throws PfModelException on errors deleting node templates + */ + public JpaToscaServiceTemplate deleteToscaNodeTemplate(@NonNull final PfDao dao, + @NonNull final PfConceptKey nodeTemplateKey) + throws PfModelException { + LOGGER.debug("->deleteToscaNodeTemplate: key={}", nodeTemplateKey); + + JpaToscaServiceTemplate dbServiceTemplate = getServiceTemplate(dao); + + if (!ToscaUtils.doNodeTemplatesExist(dbServiceTemplate)) { + throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "no node templates found"); + } + JpaToscaNodeTemplate nodeTemplate4Deletion = dbServiceTemplate.getTopologyTemplate().getNodeTemplates() + .get(nodeTemplateKey); + if (nodeTemplate4Deletion == null) { + throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "node template " + nodeTemplateKey.getId() + + NOT_FOUND); + } + + //Verify if the node template is referenced in the metadata of created policies + assertNodeTemplateNotUsedInPolicy(dao, nodeTemplateKey.getName(), nodeTemplateKey.getVersion()); + + dbServiceTemplate.getTopologyTemplate().getNodeTemplates().getConceptMap().remove(nodeTemplateKey); + new SimpleToscaServiceTemplateProvider().write(dao, dbServiceTemplate); + dao.delete(nodeTemplate4Deletion); + + var deletedServiceTemplate = new JpaToscaServiceTemplate(); + deletedServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); + deletedServiceTemplate.getTopologyTemplate().setNodeTemplates(new JpaToscaNodeTemplates()); + deletedServiceTemplate.getTopologyTemplate().getNodeTemplates().getConceptMap() + .put(nodeTemplateKey, nodeTemplate4Deletion); + + LOGGER.debug("<-deleteToscaNodeTemplate: key={}, serviceTemplate={}", nodeTemplateKey, deletedServiceTemplate); + return deletedServiceTemplate; + } + + /** + * Write a node template to the database. + * + * @param dao the DAO to use to access the database + * @param serviceTemplate the service template to be written + * @return the service template created by this method + * @throws PfModelException on errors writing the metadataSets + */ + public JpaToscaServiceTemplate createToscaNodeTemplates(@NonNull final PfDao dao, + @NonNull final JpaToscaServiceTemplate serviceTemplate) + throws PfModelException { + + LOGGER.debug("->write: tosca nodeTemplates={}", serviceTemplate); + + ToscaUtils.assertNodeTemplatesExist(serviceTemplate); + + Optional<JpaToscaNodeTypes> nodeTypes = Optional.ofNullable(serviceTemplate.getNodeTypes()); + for (JpaToscaNodeTemplate nodeTemplate : serviceTemplate.getTopologyTemplate().getNodeTemplates() + .getAll(null)) { + + // verify node types in the db if mismatch/empty entities in the template + if (! (nodeTypes.isPresent() && nodeTypes.get().getKeys().contains(nodeTemplate.getType()))) { + verifyNodeTypeInDbTemplate(dao, nodeTemplate); + } + } + JpaToscaServiceTemplate writtenServiceTemplate = appendToServiceTemplate(dao, serviceTemplate); + LOGGER.debug("<-createdToscaNodeTemplates: writtenServiceTemplate={}", writtenServiceTemplate); + + return serviceTemplate; + } + /** * Verify the policy type for a policy exists. * @@ -670,6 +867,28 @@ public class SimpleToscaProvider { } /** + * Verify the node type for a toscaNodeTemplate metadataSet. + * + * @param dao the DAO to use to access toscaNodeTemplate types in the database + * @param toscaNodeTemplate the toscaNodeTemplate to check the toscaNodeTemplate type for + */ + private void verifyNodeTypeInDbTemplate(final PfDao dao, final JpaToscaNodeTemplate toscaNodeTemplate) throws + PfModelException { + PfConceptKey nodeTypeKey = toscaNodeTemplate.getType(); + + JpaToscaNodeType nodeType = null; + + nodeType = dao.get(JpaToscaNodeType.class, nodeTypeKey); + + if (nodeType == null) { + String errorMessage = + "NODE_TYPE " + nodeTypeKey + " for toscaNodeTemplate " + toscaNodeTemplate.getId() + + " does not exist"; + throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage); + } + } + + /** * Get the latest version of the policy type for the given policy type name. * * @param dao the DAO to use to access policy types in the database @@ -699,4 +918,31 @@ public class SimpleToscaProvider { return (JpaToscaPolicyType) filterdPolicyTypeList.get(0); } + + /** + * Assert that the node template is not referenced in any Tosca policy. + * + * @param name the name of node template + * @param version the version of node template + * @throws PfModelException if node template referenced in a policy + */ + private void assertNodeTemplateNotUsedInPolicy(PfDao dao, String name, String version) + throws PfModelException { + JpaToscaServiceTemplate dbTemplate; + try { + //Retrieve all the policies from db, return if policies doesn't exist + dbTemplate = getPolicies(dao, null, null); + } catch (PfModelRuntimeException e) { + LOGGER.debug("Could not verify the node template reference in created policies ", e); + return; + } + for (JpaToscaPolicy policy : dbTemplate.getTopologyTemplate().getPolicies().getConceptMap().values()) { + if (policy.getMetadata().getOrDefault("metadataSetName", "").equals(name) + && policy.getMetadata().getOrDefault("metadataSetVersion", "").equals(version)) { + throw new PfModelException(Response.Status.NOT_ACCEPTABLE, + "Node template is in use, it is referenced in Tosca Policy " + policy.getName() + " version " + + policy.getVersion()); + } + } + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaServiceTemplateUtils.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaServiceTemplateUtils.java index 7f6fbb2f3..6f4b2993f 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaServiceTemplateUtils.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaServiceTemplateUtils.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2020 Nordix Foundation. + * Copyright (C) 2020,2022 Nordix Foundation. * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -67,6 +67,8 @@ public final class ToscaServiceTemplateUtils { addFragmentEntitites(compositeTemplate.getDataTypes(), fragmentTemplate.getDataTypes(), result)); compositeTemplate.setPolicyTypes( addFragmentEntitites(compositeTemplate.getPolicyTypes(), fragmentTemplate.getPolicyTypes(), result)); + compositeTemplate.setNodeTypes( + addFragmentEntitites(compositeTemplate.getNodeTypes(), fragmentTemplate.getNodeTypes(), result)); if (originalTemplate.getTopologyTemplate() != null && fragmentTemplate.getTopologyTemplate() != null) { if (originalTemplate.getTopologyTemplate() @@ -74,6 +76,9 @@ public final class ToscaServiceTemplateUtils { compositeTemplate.getTopologyTemplate() .setPolicies(addFragmentEntitites(compositeTemplate.getTopologyTemplate().getPolicies(), fragmentTemplate.getTopologyTemplate().getPolicies(), result)); + compositeTemplate.getTopologyTemplate() + .setNodeTemplates(addFragmentEntitites(compositeTemplate.getTopologyTemplate().getNodeTemplates(), + fragmentTemplate.getTopologyTemplate().getNodeTemplates(), result)); } else { Validated.addResult(result, "topology template", originalTemplate.getTopologyTemplate().getKey(), diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java index 5ec8247ac..be0d41d9b 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019-2021 Nordix Foundation. + * Copyright (C) 2019-2022 Nordix Foundation. * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -107,6 +107,15 @@ public final class ToscaUtils { } /** + * Assert that node templates have been specified correctly. + * + * @param serviceTemplate the service template containing node templates to be checked + */ + public static void assertNodeTemplatesExist(final JpaToscaServiceTemplate serviceTemplate) { + assertExist(serviceTemplate, ToscaUtils::checkNodeTemplateExist); + } + + /** * Check that data types have been specified correctly. * * @param serviceTemplate the service template containing data types to be checked @@ -135,6 +144,16 @@ public final class ToscaUtils { } /** + * Check that tosca node templates have been specified correctly. + * + * @param serviceTemplate the service template containing node templates to be checked + */ + public static boolean doNodeTemplatesExist(final JpaToscaServiceTemplate serviceTemplate) { + + return doExist(serviceTemplate, ToscaUtils::checkNodeTemplateExist); + } + + /** * Assert that something have been specified correctly. * * @param serviceTemplate the service template containing policy types to be checked @@ -207,6 +226,20 @@ public final class ToscaUtils { } /** + * Check if node templates have been specified correctly. + */ + public static String checkNodeTemplateExist(final JpaToscaServiceTemplate serviceTemplate) { + if (serviceTemplate.getTopologyTemplate().getNodeTemplates() == null) { + return "node templates not present on the service template"; + } + + if (serviceTemplate.getTopologyTemplate().getNodeTemplates().getConceptMap().isEmpty()) { + return "no parameters present on the node templates"; + } + return null; + } + + /** * getLatestPolicyTypeVersion Find all the ancestors of an entity type. * * @param entityTypes the set of entity types that exist diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderNodeTemplateTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderNodeTemplateTest.java new file mode 100644 index 000000000..21559344f --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderNodeTemplateTest.java @@ -0,0 +1,315 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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.provider; + +import static org.assertj.core.api.Assertions.assertThat; +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 java.util.List; +import java.util.Map; +import java.util.Properties; +import org.eclipse.persistence.config.PersistenceUnitProperties; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.coder.YamlJsonTranslator; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.dao.DaoParameters; +import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.dao.PfDaoFactory; +import org.onap.policy.models.dao.impl.DefaultPfDao; +import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.yaml.snakeyaml.Yaml; + +/** + * Test of the {@link AuthorativeToscaProvider} class. + */ +public class AuthorativeToscaProviderNodeTemplateTest { + + + private static final String NODE_TEMPLATES_JSON = "nodetemplates/nodetemplates.metadatasets.input.tosca.json"; + private static final String UPDATED_METADATA_SET_JSON = "nodetemplates/TestUpdateMetadataSet.json"; + private static final String CREATE_METADATA_SET_JSON = "nodetemplates/TestCreateMetadataSet.json"; + private static final String POLICY_WITH_METADATA_SET_REF = "policies/apex.policy.decisionmaker.input.tosca.yaml"; + private static final String APEX_POLICY_TYPE_YAML = "policytypes/onap.policies.native.Apex.yaml"; + private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$"; + private static ToscaServiceTemplate toscaServiceTemplate; + private static ToscaServiceTemplate updatedToscaServiceTemplate; + private static ToscaServiceTemplate createToscaNodeTemplate; + private PfDao pfDao; + private StandardCoder standardCoder; + private AuthorativeToscaProvider authorativeToscaProvider = new AuthorativeToscaProvider(); + private YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator(); + + /** + * Read policy metadataSet input json. + * @throws Exception Coder exception + */ + @Before + public void fetchToscaNodeTemplatesJson() throws Exception { + standardCoder = new StandardCoder(); + toscaServiceTemplate = + standardCoder.decode(ResourceUtils.getResourceAsString(NODE_TEMPLATES_JSON), ToscaServiceTemplate.class); + updatedToscaServiceTemplate = + standardCoder.decode(ResourceUtils.getResourceAsString(UPDATED_METADATA_SET_JSON), + ToscaServiceTemplate.class); + createToscaNodeTemplate = standardCoder.decode(ResourceUtils.getResourceAsString( + CREATE_METADATA_SET_JSON), ToscaServiceTemplate.class); + } + + /** + * Set up DAO towards the database. + * + * @throws Exception on database errors + */ + @Before + public void setupDao() throws Exception { + final DaoParameters daoParameters = new DaoParameters(); + daoParameters.setPluginClass(DefaultPfDao.class.getName()); + + daoParameters.setPersistenceUnit("ToscaConceptTest"); + + Properties jdbcProperties = new Properties(); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy"); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY"); + + if (System.getProperty("USE-MARIADB") != null) { + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.mariadb.jdbc.Driver"); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:mariadb://localhost:3306/policy"); + } else { + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver"); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, + "jdbc:h2:mem:AuthorativeToscaProviderNodeTemplatesTest"); + } + + daoParameters.setJdbcProperties(jdbcProperties); + + pfDao = new PfDaoFactory().createPfDao(daoParameters); + pfDao.init(daoParameters); + } + + @After + public void teardown() { + pfDao.close(); + } + + @Test + public void testPolicyMetadataSetsGet() throws Exception { + + assertThatThrownBy(() -> { + authorativeToscaProvider.getNodeTemplateMetadataSet(null, null, null); + }).hasMessageMatching(DAO_IS_NULL); + + assertNotNull(toscaServiceTemplate); + authorativeToscaProvider.createToscaNodeTemplates(pfDao, toscaServiceTemplate); + List<Map<PfConceptKey, ToscaNodeTemplate>> gotPolicyMetadataSets1 = authorativeToscaProvider + .getToscaNodeTemplate(pfDao, null, null); + assertEquals(3, gotPolicyMetadataSets1.size()); + + //Fetch all metadataSet if id is null + List<Map<ToscaEntityKey, Map<String, Object>>> gotPolicyMetadataSets = authorativeToscaProvider + .getNodeTemplateMetadataSet(pfDao, null, null); + assertEquals(3, gotPolicyMetadataSets.size()); + + // Get filtered metadataSet + List<Map<ToscaEntityKey, Map<String, Object>>> filteredPolicyMetadataSet = authorativeToscaProvider + .getNodeTemplateMetadataSet(pfDao, "apexMetadata_adaptive", "2.3.1"); + assertEquals(1, filteredPolicyMetadataSet.size()); + + //Get invalid metadataSet + List<Map<ToscaEntityKey, Map<String, Object>>> filteredMetadataSetInvalid = authorativeToscaProvider + .getNodeTemplateMetadataSet(pfDao, "invalidname", "1.0.0"); + assertThat(filteredMetadataSetInvalid).isEmpty(); + } + + @Test + public void testToscaNodeTemplatesGet() throws Exception { + + assertThatThrownBy(() -> { + authorativeToscaProvider.getToscaNodeTemplate(null, null, null); + }).hasMessageMatching(DAO_IS_NULL); + + assertNotNull(toscaServiceTemplate); + authorativeToscaProvider.createToscaNodeTemplates(pfDao, toscaServiceTemplate); + + //Fetch all node templates if id is null + List<Map<PfConceptKey, ToscaNodeTemplate>> gotToscaNodeTemplates = authorativeToscaProvider + .getToscaNodeTemplate(pfDao, null, null); + assertEquals(3, gotToscaNodeTemplates.size()); + + // Get filtered node templates + List<Map<PfConceptKey, ToscaNodeTemplate>> filteredNodeTemplates = authorativeToscaProvider + .getToscaNodeTemplate(pfDao, "apexMetadata_adaptive", "2.3.1"); + assertEquals(1, filteredNodeTemplates.size()); + + //Get invalid node template + List<Map<PfConceptKey, ToscaNodeTemplate>> filteredNodeTemplatesInvalid = authorativeToscaProvider + .getToscaNodeTemplate(pfDao, "invalidname", "1.0.0"); + assertThat(filteredNodeTemplatesInvalid).isEmpty(); + } + + @Test + public void testToscaNodeTemplatesCreate() throws Exception { + assertThatThrownBy(() -> { + authorativeToscaProvider.createToscaNodeTemplates(null, null); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + authorativeToscaProvider.createToscaNodeTemplates(null, new ToscaServiceTemplate()); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + authorativeToscaProvider.createToscaNodeTemplates(pfDao, null); + }).hasMessageMatching("^toscaServiceTemplate is marked .*on.*ull but is null$"); + + ToscaServiceTemplate createdNodeTemplates = + authorativeToscaProvider.createToscaNodeTemplates(pfDao, toscaServiceTemplate); + assertThat(createdNodeTemplates.getToscaTopologyTemplate().getNodeTemplates()).hasSize(3); + assertThat(createdNodeTemplates.getToscaTopologyTemplate().getNodeTemplates().get("apexMetadata_grpc") + .getMetadata()).containsKey("threshold"); + + authorativeToscaProvider.createToscaNodeTemplates(pfDao, createToscaNodeTemplate); + assertThat(authorativeToscaProvider.getNodeTemplateMetadataSet(pfDao, null, null)).hasSize(4); + + //Create node template with invalid node type + createToscaNodeTemplate.getToscaTopologyTemplate().getNodeTemplates().get("apexMetadata_adaptive") + .setType("invalid.type"); + assertThatThrownBy(() -> { + authorativeToscaProvider.createToscaNodeTemplates(pfDao, createToscaNodeTemplate); + }).hasMessageMatching("^NODE_TYPE .* for toscaNodeTemplate .* does not exist$"); + + } + + @Test + public void testToscaNodeTemplateUpdate() throws Exception { + assertThatThrownBy(() -> { + authorativeToscaProvider.updateToscaNodeTemplates(null, null); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + authorativeToscaProvider.updateToscaNodeTemplates(null, new ToscaServiceTemplate()); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + authorativeToscaProvider.updateToscaNodeTemplates(pfDao, null); + }).hasMessageMatching("^serviceTemplate is marked non-null but is null$"); + + authorativeToscaProvider.createToscaNodeTemplates(pfDao, toscaServiceTemplate); + ToscaServiceTemplate updatedTemplate = + authorativeToscaProvider.updateToscaNodeTemplates(pfDao, updatedToscaServiceTemplate); + assertEquals("Updated Metadata set for GRPC", + updatedTemplate.getToscaTopologyTemplate().getNodeTemplates().get("apexMetadata_grpc") + .getDescription()); + + //Update nodeTemplate with invalid node type + updatedToscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().get("apexMetadata_grpc") + .setTypeVersion("0.0.0"); + assertThatThrownBy(() -> { + authorativeToscaProvider.updateToscaNodeTemplates(pfDao, updatedToscaServiceTemplate); + }).hasMessageMatching("^NODE_TYPE .* for toscaNodeTemplate .* does not exist$"); + } + + @Test + public void testToscaNodeTemplatetDelete() throws Exception { + assertThatThrownBy(() -> { + authorativeToscaProvider.deleteToscaNodeTemplate(null, null, null); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + authorativeToscaProvider.deleteToscaNodeTemplate(null, null, "0.0.1"); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + authorativeToscaProvider.deleteToscaNodeTemplate(pfDao, null, null); + }).hasMessageMatching("^name is marked .*on.*ull but is null$"); + + assertThatThrownBy(() -> { + authorativeToscaProvider.deleteToscaNodeTemplate(pfDao, "name", null); + }).hasMessageMatching("^version is marked .*on.*ull but is null$"); + + authorativeToscaProvider.createToscaNodeTemplates(pfDao, toscaServiceTemplate); + assertThatThrownBy(() -> { + authorativeToscaProvider.deleteToscaNodeTemplate(pfDao, "dummyname", "1.0.1"); + }).hasMessage("node template dummyname:1.0.1 not found"); + + + ToscaServiceTemplate responseTemplate = + authorativeToscaProvider.deleteToscaNodeTemplate(pfDao, "apexMetadata_decisionMaker", + "1.0.0"); + + assertTrue(responseTemplate.getToscaTopologyTemplate().getNodeTemplates() + .containsKey("apexMetadata_decisionMaker")); + assertThat(responseTemplate.getToscaTopologyTemplate().getNodeTemplates()).hasSize(1); + + assertThat(authorativeToscaProvider.getNodeTemplateMetadataSet(pfDao, null, null)).hasSize(2); + + } + + @Test + public void testNodeTemplatesWithExistingPolicy() throws Exception { + String policyString = ResourceUtils.getResourceAsString(POLICY_WITH_METADATA_SET_REF); + ToscaServiceTemplate policyServiceTemplate = + yamlJsonTranslator.fromYaml(policyString, ToscaServiceTemplate.class); + + createPolicyTypes(); + //Create policy with metadataSet reference in it + authorativeToscaProvider.createPolicies(pfDao, policyServiceTemplate); + assertThat(authorativeToscaProvider.getPolicyList(pfDao, null, null)).hasSize(1); + assertEquals("apexMetadata_decisionMaker", authorativeToscaProvider + .getPolicyList(pfDao, null, null).get(0).getMetadata().get("metadataSetName")); + + //Create node templates + authorativeToscaProvider.createToscaNodeTemplates(pfDao, toscaServiceTemplate); + + //delete node templates referenced in existing policy + assertThatThrownBy(() -> { + authorativeToscaProvider + .deleteToscaNodeTemplate(pfDao, "apexMetadata_decisionMaker", "1.0.0"); + }).hasMessageEndingWith("Node template is in use, it is referenced in Tosca Policy " + + "operational.apex.decisionMaker version 1.0.0"); + + //delete unreferenced node template + authorativeToscaProvider.deleteToscaNodeTemplate(pfDao, "apexMetadata_adaptive", "2.3.1"); + assertThat(authorativeToscaProvider.getNodeTemplateMetadataSet(pfDao, null, null)).hasSize(2); + } + + private void createPolicyTypes() throws CoderException, PfModelException { + Object yamlObject = + new Yaml().load(ResourceUtils.getResourceAsString(APEX_POLICY_TYPE_YAML)); + String yamlAsJsonString = new StandardCoder().encode(yamlObject); + + ToscaServiceTemplate toscaServiceTemplatePolicyType = + standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplatePolicyType); + new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType); + } + +} diff --git a/models-tosca/src/test/resources/nodetemplates/TestCreateMetadataSet.json b/models-tosca/src/test/resources/nodetemplates/TestCreateMetadataSet.json new file mode 100644 index 000000000..8620d3ef0 --- /dev/null +++ b/models-tosca/src/test/resources/nodetemplates/TestCreateMetadataSet.json @@ -0,0 +1,31 @@ +{ + "tosca_definitions_version": "tosca_simple_yaml_1_1_0", + "node_types": { + "org.onap.nodetypes.policy.MetadataSet2": { + "derived_from": "tosca.nodetypes.Root", + "version": "1.0.1" + } + }, + "topology_template": { + "node_templates": { + "apexMetadata_adaptive": { + "version": "2.3.3", + "type": "org.onap.nodetypes.policy.MetadataSet2", + "type_version": "1.0.1", + "description": "Metadata set for an Adaptive Policy", + "metadata": { + "policyModel": { + "key": { + "name": "AdaptivePolicyModel", + "version": "1.2.2" + } + }, + "radius": 1.23, + "height": 2.13, + "length": 46 + } + } + + } + } +}
\ No newline at end of file diff --git a/models-tosca/src/test/resources/nodetemplates/TestUpdateMetadataSet.json b/models-tosca/src/test/resources/nodetemplates/TestUpdateMetadataSet.json new file mode 100644 index 000000000..7fbad818c --- /dev/null +++ b/models-tosca/src/test/resources/nodetemplates/TestUpdateMetadataSet.json @@ -0,0 +1,23 @@ +{ + "tosca_definitions_version": "tosca_simple_yaml_1_1_0", + "topology_template": { + "node_templates": { + "apexMetadata_grpc": { + "version": "1.2.1", + "type": "org.onap.nodetypes.policy.MetadataSet", + "type_version": "1.0.0", + "description": "Updated Metadata set for GRPC", + "metadata": { + "policyModel": { + "key": { + "name": "GrpcPolicyModel", + "version": "1.0.1" + } + }, + "threshold": 3.15, + "state": "active" + } + } + } + } +}
\ No newline at end of file |