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;
29 import java.util.UUID;
30 import lombok.AccessLevel;
31 import lombok.NoArgsConstructor;
32 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdate;
33 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
34 import org.onap.policy.common.utils.resources.ResourceUtils;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 @NoArgsConstructor(access = AccessLevel.PRIVATE)
41 public final class TestListenerUtils {
43 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
44 private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
47 * Method to create participantUpdateMsg.
49 * @return ParticipantUpdate message
51 public static ParticipantUpdate createParticipantUpdateMsg() {
52 final ParticipantUpdate participantUpdateMsg = new ParticipantUpdate();
53 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
54 ToscaConceptIdentifier participantType =
55 new ToscaConceptIdentifier("org.onap.policy.acm.PolicyAutomationCompositionParticipant", "2.3.1");
57 participantUpdateMsg.setParticipantId(participantId);
58 participantUpdateMsg.setTimestamp(Instant.now());
59 participantUpdateMsg.setParticipantType(participantType);
60 participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
61 participantUpdateMsg.setMessageId(UUID.randomUUID());
63 ToscaServiceTemplate toscaServiceTemplate = testAutomationCompositionRead();
64 // Add policies to the toscaServiceTemplate
65 TestListenerUtils.addPoliciesToToscaServiceTemplate(toscaServiceTemplate);
67 participantUpdateMsg.setParticipantDefinitionUpdates(new ArrayList<>());
68 return participantUpdateMsg;
71 private static ToscaServiceTemplate testAutomationCompositionRead() {
72 Set<String> automationCompositionDirectoryContents =
73 ResourceUtils.getDirectoryContents("clamp/acm/test");
75 boolean atLeastOneAutomationCompositionTested = false;
76 ToscaServiceTemplate toscaServiceTemplate = null;
78 for (String automationCompositionFilePath : automationCompositionDirectoryContents) {
79 if (!automationCompositionFilePath.endsWith(".yaml")) {
82 atLeastOneAutomationCompositionTested = true;
83 toscaServiceTemplate = testAutomationCompositionYamlSerialization(automationCompositionFilePath);
86 // Add policy_types to the toscaServiceTemplate
87 addPolicyTypesToToscaServiceTemplate(toscaServiceTemplate);
89 assertTrue(atLeastOneAutomationCompositionTested);
90 return toscaServiceTemplate;
93 private static void addPolicyTypesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
94 Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
96 for (String policyTypeFilePath : policyTypeDirectoryContents) {
97 String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
99 ToscaServiceTemplate foundPolicyTypeSt =
100 yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
102 toscaServiceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
103 toscaServiceTemplate.setDescription(foundPolicyTypeSt.getDescription());
104 toscaServiceTemplate.setMetadata(foundPolicyTypeSt.getMetadata());
105 toscaServiceTemplate.setName(foundPolicyTypeSt.getName());
106 toscaServiceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion());
107 toscaServiceTemplate.setVersion(foundPolicyTypeSt.getVersion());
109 if (foundPolicyTypeSt.getDataTypes() != null) {
110 if (toscaServiceTemplate.getDataTypes() == null) {
111 toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
113 toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
117 if (toscaServiceTemplate.getPolicyTypes() == null) {
118 toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
120 toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
126 * Method to add polcies to the toscaServiceTemplate.
128 * @param toscaServiceTemplate to add policies
130 public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
131 var policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies");
132 toscaServiceTemplate.getToscaTopologyTemplate().setPolicies(new ArrayList<>());
134 for (var policiesFilePath : policiesDirectoryContents) {
135 if (!policiesFilePath.endsWith("yaml")) {
139 var policiesString = ResourceUtils.getResourceAsString(policiesFilePath);
141 var foundPoliciesSt =
142 yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class);
143 if (foundPoliciesSt.getToscaTopologyTemplate() != null
144 && foundPoliciesSt.getToscaTopologyTemplate().getPolicies() != null) {
145 toscaServiceTemplate.getToscaTopologyTemplate().getPolicies()
146 .addAll(foundPoliciesSt.getToscaTopologyTemplate().getPolicies());
151 private static ToscaServiceTemplate testAutomationCompositionYamlSerialization(
152 String automationCompositionFilePath) {
154 String automationCompositionString = ResourceUtils.getResourceAsString(automationCompositionFilePath);
155 if (automationCompositionString == null) {
156 throw new FileNotFoundException(automationCompositionFilePath);
159 return yamlTranslator.fromYaml(automationCompositionString, ToscaServiceTemplate.class);
160 } catch (FileNotFoundException e) {
161 LOGGER.error("cannot find YAML file {}", automationCompositionFilePath);
162 throw new IllegalArgumentException(e);