5c4f06d2db7ca003a899ec9ad901f17b930e9e72
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.participant.policy.main.utils;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.FileNotFoundException;
26 import java.time.Instant;
27 import java.util.ArrayList;
28 import java.util.UUID;
29 import lombok.AccessLevel;
30 import lombok.NoArgsConstructor;
31 import org.onap.policy.clamp.acm.participant.policy.main.parameters.CommonTestData;
32 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrime;
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.ToscaServiceTemplate;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 @NoArgsConstructor(access = AccessLevel.PRIVATE)
40 public final class TestListenerUtils {
41
42     private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
43     private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
44
45     /**
46      * Method to create participantUpdateMsg.
47      *
48      * @return ParticipantPrime message
49      */
50     public static ParticipantPrime createParticipantPrimeMsg() {
51         final var participantPrimeMsg = new ParticipantPrime();
52         var participantId = CommonTestData.getParticipantId();
53
54         participantPrimeMsg.setParticipantId(participantId);
55         participantPrimeMsg.setCompositionId(UUID.randomUUID());
56         participantPrimeMsg.setTimestamp(Instant.now());
57         participantPrimeMsg.setTimestamp(Instant.ofEpochMilli(3000));
58         participantPrimeMsg.setMessageId(UUID.randomUUID());
59
60         var toscaServiceTemplate = testAutomationCompositionRead();
61         // Add policies to the toscaServiceTemplate
62         TestListenerUtils.addPoliciesToToscaServiceTemplate(toscaServiceTemplate);
63
64         participantPrimeMsg.setParticipantDefinitionUpdates(new ArrayList<>());
65         return participantPrimeMsg;
66     }
67
68     private static ToscaServiceTemplate testAutomationCompositionRead() {
69         var automationCompositionDirectoryContents = ResourceUtils.getDirectoryContents("clamp/acm/test");
70
71         boolean atLeastOneAutomationCompositionTested = false;
72         ToscaServiceTemplate toscaServiceTemplate = null;
73
74         for (String automationCompositionFilePath : automationCompositionDirectoryContents) {
75             if (!automationCompositionFilePath.endsWith(".yaml")) {
76                 continue;
77             }
78             atLeastOneAutomationCompositionTested = true;
79             toscaServiceTemplate = testAutomationCompositionYamlSerialization(automationCompositionFilePath);
80         }
81
82         // Add policy_types to the toscaServiceTemplate
83         addPolicyTypesToToscaServiceTemplate(toscaServiceTemplate);
84
85         assertTrue(atLeastOneAutomationCompositionTested);
86         return toscaServiceTemplate;
87     }
88
89     private static void addPolicyTypesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
90         var policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
91
92         for (String policyTypeFilePath : policyTypeDirectoryContents) {
93             String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
94
95             var foundPolicyTypeSt =
96                     yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
97
98             toscaServiceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
99             toscaServiceTemplate.setDescription(foundPolicyTypeSt.getDescription());
100             toscaServiceTemplate.setMetadata(foundPolicyTypeSt.getMetadata());
101             toscaServiceTemplate.setName(foundPolicyTypeSt.getName());
102             toscaServiceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion());
103             toscaServiceTemplate.setVersion(foundPolicyTypeSt.getVersion());
104
105             if (foundPolicyTypeSt.getDataTypes() != null) {
106                 if (toscaServiceTemplate.getDataTypes() == null) {
107                     toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
108                 } else {
109                     toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
110                 }
111             }
112
113             if (toscaServiceTemplate.getPolicyTypes() == null) {
114                 toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
115             } else {
116                 toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
117             }
118         }
119     }
120
121     /**
122      * Method to add polcies to the toscaServiceTemplate.
123      *
124      * @param toscaServiceTemplate to add policies
125      */
126     public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
127         var policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies");
128         toscaServiceTemplate.getToscaTopologyTemplate().setPolicies(new ArrayList<>());
129
130         for (var policiesFilePath : policiesDirectoryContents) {
131             if (!policiesFilePath.endsWith("yaml")) {
132                 continue;
133             }
134
135             var policiesString = ResourceUtils.getResourceAsString(policiesFilePath);
136
137             var foundPoliciesSt = yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class);
138             if (foundPoliciesSt.getToscaTopologyTemplate() != null
139                     && foundPoliciesSt.getToscaTopologyTemplate().getPolicies() != null) {
140                 toscaServiceTemplate.getToscaTopologyTemplate().getPolicies()
141                         .addAll(foundPoliciesSt.getToscaTopologyTemplate().getPolicies());
142             }
143         }
144     }
145
146     private static ToscaServiceTemplate testAutomationCompositionYamlSerialization(
147             String automationCompositionFilePath) {
148         try {
149             String automationCompositionString = ResourceUtils.getResourceAsString(automationCompositionFilePath);
150             if (automationCompositionString == null) {
151                 throw new FileNotFoundException(automationCompositionFilePath);
152             }
153
154             return yamlTranslator.fromYaml(automationCompositionString, ToscaServiceTemplate.class);
155         } catch (FileNotFoundException e) {
156             LOGGER.error("cannot find YAML file {}", automationCompositionFilePath);
157             throw new IllegalArgumentException(e);
158         }
159     }
160 }