Preloading onap.policies.monitoring.tcagen2 2.0.0 43/130343/1
authorjhh <jorge.hernandez-herrero@att.com>
Thu, 18 Aug 2022 00:02:20 +0000 (19:02 -0500)
committerjhh <jorge.hernandez-herrero@att.com>
Thu, 18 Aug 2022 00:03:32 +0000 (19:03 -0500)
Issue-ID: POLICY-4317
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Change-Id: I8885ddbc942a40081583194e0ff03ff700b77975

main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java
main/src/main/resources/application.yaml

index 87a5389..d9f4f77 100644 (file)
@@ -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.
  * ================================================================================
 
 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<ToscaServiceTemplate, ToscaServiceTemplate> getter)
             throws PolicyApiException, CoderException, PfModelException {
 
+        var multiVersionTemplates = new ArrayList<ToscaServiceTemplate>();
+
         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 <T extends ToscaEntity> boolean isMultiVersion(Map<String, T> aggEntity,
+                                                             Map<String, T> 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<T, R> {
-        public R apply(T value) throws PfModelException;
+        R apply(T value) throws PfModelException;
     }
 }
\ No newline at end of file
index 35ca4ed..41a3afd 100644 (file)
@@ -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