diff options
author | rameshiyer27 <ramesh.murugan.iyer@est.tech> | 2022-02-18 17:27:13 +0000 |
---|---|---|
committer | rameshiyer27 <ramesh.murugan.iyer@est.tech> | 2022-02-23 16:11:33 +0000 |
commit | aba3f2a1c64d755bfed2f278d40a74f9a4f9ab1e (patch) | |
tree | dc1f5bc7ee0ae1541aeb3aa990c7cc5f6b8653e3 /main/src/main/java/org/onap | |
parent | beef773a297cbeb5c5abfb5e75e678e39d3d1523 (diff) |
Add support for populating metadataSet in PAP tosca policy
MetadataSet fetched from the db and added to the policy metadata map
with the key "metadataSet" before sending to PDPs
Issue-ID: POLICY-3832
Signed-off-by: zrrmmua <ramesh.murugan.iyer@est.tech>
Change-Id: I14064d82ba6877bd7560af7de7572b4813a98385
Diffstat (limited to 'main/src/main/java/org/onap')
3 files changed, 126 insertions, 0 deletions
diff --git a/main/src/main/java/org/onap/policy/pap/main/repository/ToscaNodeTemplateRepository.java b/main/src/main/java/org/onap/policy/pap/main/repository/ToscaNodeTemplateRepository.java new file mode 100644 index 00000000..c132e388 --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/repository/ToscaNodeTemplateRepository.java @@ -0,0 +1,31 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * Copyright (C) 2022, Nordix Foundation. 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. + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.repository; + +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ToscaNodeTemplateRepository extends JpaRepository<JpaToscaNodeTemplate, PfConceptKey> { + +} diff --git a/main/src/main/java/org/onap/policy/pap/main/service/ToscaNodeTemplateService.java b/main/src/main/java/org/onap/policy/pap/main/service/ToscaNodeTemplateService.java new file mode 100644 index 00000000..9b097ccb --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/service/ToscaNodeTemplateService.java @@ -0,0 +1,70 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * Copyright (C) 2022, Nordix Foundation. 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. + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.service; + +import java.util.Optional; +import javax.ws.rs.core.Response; +import lombok.RequiredArgsConstructor; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate; +import org.onap.policy.pap.main.repository.ToscaNodeTemplateRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional(readOnly = true) +@RequiredArgsConstructor +public class ToscaNodeTemplateService { + + private static final Logger LOGGER = LoggerFactory.getLogger(ToscaNodeTemplateService.class); + + private final ToscaNodeTemplateRepository nodeTemplateRepository; + + /** + * Get node templates. + * + * @param name the name of the node template to get + * @param version the version of the node template to get + * @return the node templates found + * @throws PfModelRuntimeException on errors getting node templates + */ + public ToscaNodeTemplate getToscaNodeTemplate(final String name, final String version) + throws PfModelRuntimeException { + + LOGGER.debug("->getNodeTemplate: name={}, version={}", name, version); + + Optional<JpaToscaNodeTemplate> jpaToscaNodeTemplate = nodeTemplateRepository + .findById(new PfConceptKey(name, version)); + if (jpaToscaNodeTemplate.isPresent()) { + var nodeTemplate = jpaToscaNodeTemplate.get().toAuthorative(); + LOGGER.debug("<-NodeTemplate: name={}, version={}, nodeTemplate={}", name, version, nodeTemplate); + return nodeTemplate; + } else { + throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, + "node template for " + name + ":" + version + " do not exist in the database"); + } + } + +} diff --git a/main/src/main/java/org/onap/policy/pap/main/service/ToscaServiceTemplateService.java b/main/src/main/java/org/onap/policy/pap/main/service/ToscaServiceTemplateService.java index e71045f6..ef0fc603 100644 --- a/main/src/main/java/org/onap/policy/pap/main/service/ToscaServiceTemplateService.java +++ b/main/src/main/java/org/onap/policy/pap/main/service/ToscaServiceTemplateService.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2022 Bell Canada. All rights reserved. + * Modifications 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. @@ -32,6 +33,7 @@ import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; +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.ToscaTypedEntityFilter; @@ -51,8 +53,14 @@ public class ToscaServiceTemplateService { private static final Logger LOGGER = LoggerFactory.getLogger(ToscaServiceTemplateService.class); + private static final String METADATASET_NAME = "metadataSetName"; + private static final String METADATASET_VERSION = "metadataSetVersion"; + private static final String METADATASET = "metadataSet"; + private final ToscaServiceTemplateRepository serviceTemplateRepository; + private final ToscaNodeTemplateService nodeTemplateService; + /** * Get policies. * @@ -71,6 +79,7 @@ public class ToscaServiceTemplateService { List<Map<String, ToscaPolicy>> policies = getToscaServiceTemplate(name, version, "policy").toAuthorative() .getToscaTopologyTemplate().getPolicies(); policyList = policies.stream().flatMap(policy -> policy.values().stream()).collect(Collectors.toList()); + populateMetadataSet(policyList); } catch (PfModelRuntimeException pfme) { return handlePfModelRuntimeException(pfme); } catch (Exception exc) { @@ -186,4 +195,20 @@ public class ToscaServiceTemplateService { } } + /** + * Populates metadataSet in policy->metadata if metadataSet reference is provided. + * + * @param policies List of policies + */ + private void populateMetadataSet(List<ToscaPolicy> policies) { + for (ToscaPolicy policy : policies) { + if (policy.getMetadata().keySet().containsAll(List.of(METADATASET_NAME, METADATASET_VERSION))) { + var name = String.valueOf(policy.getMetadata().get(METADATASET_NAME)); + var version = String.valueOf(policy.getMetadata().get(METADATASET_VERSION)); + policy.getMetadata().putIfAbsent(METADATASET, + nodeTemplateService.getToscaNodeTemplate(name, version).getMetadata()); + } + } + } + } |