2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2021-2022 Nordix Foundation.
 
   4  * ================================================================================
 
   5  * Licensed under the Apache License, Version 2.0 (the "License");
 
   6  * you may not use this file except in compliance with the License.
 
   7  * You may obtain a copy of the License at
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  11  * Unless required by applicable law or agreed to in writing, software
 
  12  * distributed under the License is distributed on an "AS IS" BASIS,
 
  13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  14  * See the License for the specific language governing permissions and
 
  15  * limitations under the License.
 
  17  * SPDX-License-Identifier: Apache-2.0
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.policy.clamp.acm.participant.policy.main.utils;
 
  23 import static org.junit.Assert.assertTrue;
 
  25 import java.io.FileNotFoundException;
 
  26 import java.time.Instant;
 
  27 import java.util.ArrayList;
 
  28 import java.util.List;
 
  31 import java.util.UUID;
 
  32 import lombok.AccessLevel;
 
  33 import lombok.NoArgsConstructor;
 
  34 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
 
  35 import org.onap.policy.clamp.models.acm.concepts.ParticipantUtils;
 
  36 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdate;
 
  37 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
 
  38 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
 
  39 import org.onap.policy.common.utils.resources.ResourceUtils;
 
  40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
 
  41 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
 
  42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 
  43 import org.slf4j.Logger;
 
  44 import org.slf4j.LoggerFactory;
 
  46 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 
  47 public final class TestListenerUtils {
 
  49     private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
 
  50     private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
 
  53      * Method to create participantUpdateMsg.
 
  55      * @return ParticipantUpdate message
 
  57     public static ParticipantUpdate createParticipantUpdateMsg() {
 
  58         final ParticipantUpdate participantUpdateMsg = new ParticipantUpdate();
 
  59         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
 
  60         ToscaConceptIdentifier participantType =
 
  61             new ToscaConceptIdentifier("org.onap.policy.acm.PolicyAutomationCompositionParticipant", "2.3.1");
 
  63         participantUpdateMsg.setParticipantId(participantId);
 
  64         participantUpdateMsg.setTimestamp(Instant.now());
 
  65         participantUpdateMsg.setParticipantType(participantType);
 
  66         participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
 
  67         participantUpdateMsg.setMessageId(UUID.randomUUID());
 
  69         ToscaServiceTemplate toscaServiceTemplate = testAutomationCompositionRead();
 
  70         // Add policies to the toscaServiceTemplate
 
  71         TestListenerUtils.addPoliciesToToscaServiceTemplate(toscaServiceTemplate);
 
  73         List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
 
  74         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate()
 
  75             .getNodeTemplates().entrySet()) {
 
  76             if (ParticipantUtils.checkIfNodeTemplateIsAutomationCompositionElement(toscaInputEntry.getValue(),
 
  77                 toscaServiceTemplate)) {
 
  78                 AcmUtils.prepareParticipantDefinitionUpdate(
 
  79                     ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()),
 
  80                     toscaInputEntry.getKey(), toscaInputEntry.getValue(),
 
  81                     participantDefinitionUpdates, null);
 
  85         participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates);
 
  86         return participantUpdateMsg;
 
  89     private static ToscaServiceTemplate testAutomationCompositionRead() {
 
  90         Set<String> automationCompositionDirectoryContents =
 
  91             ResourceUtils.getDirectoryContents("clamp/acm/test");
 
  93         boolean atLeastOneAutomationCompositionTested = false;
 
  94         ToscaServiceTemplate toscaServiceTemplate = null;
 
  96         for (String automationCompositionFilePath : automationCompositionDirectoryContents) {
 
  97             if (!automationCompositionFilePath.endsWith(".yaml")) {
 
 100             atLeastOneAutomationCompositionTested = true;
 
 101             toscaServiceTemplate = testAutomationCompositionYamlSerialization(automationCompositionFilePath);
 
 104         // Add policy_types to the toscaServiceTemplate
 
 105         addPolicyTypesToToscaServiceTemplate(toscaServiceTemplate);
 
 107         assertTrue(atLeastOneAutomationCompositionTested);
 
 108         return toscaServiceTemplate;
 
 111     private static void addPolicyTypesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
 
 112         Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
 
 114         for (String policyTypeFilePath : policyTypeDirectoryContents) {
 
 115             String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
 
 117             ToscaServiceTemplate foundPolicyTypeSt =
 
 118                 yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
 
 120             toscaServiceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
 
 121             toscaServiceTemplate.setDescription(foundPolicyTypeSt.getDescription());
 
 122             toscaServiceTemplate.setMetadata(foundPolicyTypeSt.getMetadata());
 
 123             toscaServiceTemplate.setName(foundPolicyTypeSt.getName());
 
 124             toscaServiceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion());
 
 125             toscaServiceTemplate.setVersion(foundPolicyTypeSt.getVersion());
 
 127             if (foundPolicyTypeSt.getDataTypes() != null) {
 
 128                 if (toscaServiceTemplate.getDataTypes() == null) {
 
 129                     toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
 
 131                     toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
 
 135             if (toscaServiceTemplate.getPolicyTypes() == null) {
 
 136                 toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
 
 138                 toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
 
 144      * Method to add polcies to the toscaServiceTemplate.
 
 146      * @param toscaServiceTemplate to add policies
 
 148     public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
 
 149         var policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies");
 
 150         toscaServiceTemplate.getToscaTopologyTemplate().setPolicies(new ArrayList<>());
 
 152         for (var policiesFilePath : policiesDirectoryContents) {
 
 153             if (!policiesFilePath.endsWith("yaml")) {
 
 157             var policiesString = ResourceUtils.getResourceAsString(policiesFilePath);
 
 159             var foundPoliciesSt =
 
 160                 yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class);
 
 161             if (foundPoliciesSt.getToscaTopologyTemplate() != null
 
 162                     && foundPoliciesSt.getToscaTopologyTemplate().getPolicies() != null) {
 
 163                 toscaServiceTemplate.getToscaTopologyTemplate().getPolicies()
 
 164                         .addAll(foundPoliciesSt.getToscaTopologyTemplate().getPolicies());
 
 169     private static ToscaServiceTemplate testAutomationCompositionYamlSerialization(
 
 170         String automationCompositionFilePath) {
 
 172             String automationCompositionString = ResourceUtils.getResourceAsString(automationCompositionFilePath);
 
 173             if (automationCompositionString == null) {
 
 174                 throw new FileNotFoundException(automationCompositionFilePath);
 
 177             return yamlTranslator.fromYaml(automationCompositionString, ToscaServiceTemplate.class);
 
 178         } catch (FileNotFoundException e) {
 
 179             LOGGER.error("cannot find YAML file {}", automationCompositionFilePath);
 
 180             throw new IllegalArgumentException(e);