From 579204990a84fbd5593b2ce61ad3c2a5560ed894 Mon Sep 17 00:00:00 2001 From: jhh Date: Wed, 17 Aug 2022 19:02:20 -0500 Subject: Preloading onap.policies.monitoring.tcagen2 2.0.0 Issue-ID: POLICY-4317 Signed-off-by: jhh Change-Id: I8885ddbc942a40081583194e0ff03ff700b77975 --- .../api/main/startstop/ApiDatabaseInitializer.java | 52 ++++++++++++++++++++-- main/src/main/resources/application.yaml | 3 +- 2 files changed, 50 insertions(+), 5 deletions(-) (limited to 'main/src') diff --git a/main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java b/main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java index 87a53896..d9f4f777 100644 --- a/main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java +++ b/main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy API * ================================================================================ - * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019-2021 Nordix Foundation. * Modifications Copyright (C) 2022 Bell Canada. All rights reserved. * ================================================================================ @@ -24,9 +24,13 @@ package org.onap.policy.api.main.startstop; +import com.google.common.collect.Sets; +import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; +import java.util.Objects; import javax.annotation.PostConstruct; import lombok.RequiredArgsConstructor; import org.onap.policy.api.main.config.PolicyPreloadConfig; @@ -37,6 +41,7 @@ import org.onap.policy.common.utils.coder.StandardYamlCoder; import org.onap.policy.common.utils.resources.ResourceUtils; 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.ToscaEntityFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; @@ -112,11 +117,12 @@ public class ApiDatabaseInitializer { FunctionWithEx getter) throws PolicyApiException, CoderException, PfModelException { + var multiVersionTemplates = new ArrayList(); + for (String entity : entities) { var entityAsStringYaml = ResourceUtils.getResourceAsString(entity); if (entityAsStringYaml == null) { - LOGGER.warn("Preloading entity cannot be found: {}", entity); - continue; + throw new PolicyApiException("Preloaded entity cannot be found " + entity); } ToscaServiceTemplate singleEntity = coder.decode(entityAsStringYaml, ToscaServiceTemplate.class); @@ -124,6 +130,16 @@ public class ApiDatabaseInitializer { throw new PolicyApiException("Error deserializing entity from file: " + entity); } + if (isMultiVersion(serviceTemplate.getPolicyTypes(), singleEntity.getPolicyTypes())) { + // if this entity introduces a new policy version of an existing policy type, + // process it on its own as continuing here will override the existing policy type + // in a different version + + multiVersionTemplates.add(singleEntity); + LOGGER.warn("Detected multi-versioned type: {}", entity); + continue; + } + // Consolidate data types and policy types if (singleEntity.getDataTypes() != null) { serviceTemplate.getDataTypes().putAll(singleEntity.getDataTypes()); @@ -144,11 +160,39 @@ public class ApiDatabaseInitializer { // Preload the specified entities ToscaServiceTemplate createdServiceTemplate = getter.apply(serviceTemplate); LOGGER.debug("Created initial tosca service template in DB - {}", createdServiceTemplate); + + multiVersionTemplates + .forEach(mvServiceTemplate -> { + try { + LOGGER.info("Multi-versioned Service Template {}", mvServiceTemplate.getPolicyTypes().keySet()); + getter.apply(mvServiceTemplate); + } catch (PfModelException e) { + LOGGER.warn("ToscaServiceTemple cannot be preloaded: {}", mvServiceTemplate, e); + } + }); return createdServiceTemplate; } + // This method is templated, so it can be used with other derivations of ToscaEntity in the future, + // if multi-version are desired. + + protected boolean isMultiVersion(Map aggEntity, + Map singleEntity) { + if (aggEntity == null || singleEntity == null) { + return false; + } + + // There is a multi-version entity if both key sets have the same + // entity name but different version. + + return + Sets.intersection(aggEntity.keySet(), singleEntity.keySet()) + .stream() + .anyMatch(e -> !Objects.equals(aggEntity.get(e).getVersion(), singleEntity.get(e).getVersion())); + } + @FunctionalInterface protected interface FunctionWithEx { - public R apply(T value) throws PfModelException; + R apply(T value) throws PfModelException; } } \ No newline at end of file diff --git a/main/src/main/resources/application.yaml b/main/src/main/resources/application.yaml index 35ca4ed0..41a3afd4 100644 --- a/main/src/main/resources/application.yaml +++ b/main/src/main/resources/application.yaml @@ -28,6 +28,7 @@ policy-api: policy-preload: policyTypes: - policytypes/onap.policies.monitoring.tcagen2.yaml + - policytypes/onap.policies.monitoring.tcagen2.v2.yaml - policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml - policytypes/onap.policies.monitoring.dcae-restconfcollector.yaml - policytypes/onap.policies.monitoring.dcae-pm-subscription-handler.yaml @@ -67,4 +68,4 @@ management: base-path: / exposure: include: health,metrics,prometheus - path-mapping.prometheus: metrics \ No newline at end of file + path-mapping.prometheus: metrics -- cgit 1.2.3-korg