31d62c70e3159270039f5748993b22204d97557f
[policy/clamp.git] /
1 /*-
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
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.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;
39
40 @NoArgsConstructor(access = AccessLevel.PRIVATE)
41 public final class TestListenerUtils {
42
43     private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
44     private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
45
46     /**
47      * Method to create participantUpdateMsg.
48      *
49      * @return ParticipantUpdate message
50      */
51     public static ParticipantUpdate createParticipantUpdateMsg() {
52         final var participantUpdateMsg = new ParticipantUpdate();
53         var participantId = CommonTestData.getParticipantId();
54         var participantType =
55                 new ToscaConceptIdentifier("org.onap.policy.acm.PolicyAutomationCompositionParticipant", "2.3.1");
56
57         participantUpdateMsg.setParticipantId(participantId);
58         participantUpdateMsg.setTimestamp(Instant.now());
59         participantUpdateMsg.setParticipantType(participantType);
60         participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
61         participantUpdateMsg.setMessageId(UUID.randomUUID());
62
63         var toscaServiceTemplate = testAutomationCompositionRead();
64         // Add policies to the toscaServiceTemplate
65         TestListenerUtils.addPoliciesToToscaServiceTemplate(toscaServiceTemplate);
66
67         participantUpdateMsg.setParticipantDefinitionUpdates(new ArrayList<>());
68         return participantUpdateMsg;
69     }
70
71     private static ToscaServiceTemplate testAutomationCompositionRead() {
72         var automationCompositionDirectoryContents = ResourceUtils.getDirectoryContents("clamp/acm/test");
73
74         boolean atLeastOneAutomationCompositionTested = false;
75         ToscaServiceTemplate toscaServiceTemplate = null;
76
77         for (String automationCompositionFilePath : automationCompositionDirectoryContents) {
78             if (!automationCompositionFilePath.endsWith(".yaml")) {
79                 continue;
80             }
81             atLeastOneAutomationCompositionTested = true;
82             toscaServiceTemplate = testAutomationCompositionYamlSerialization(automationCompositionFilePath);
83         }
84
85         // Add policy_types to the toscaServiceTemplate
86         addPolicyTypesToToscaServiceTemplate(toscaServiceTemplate);
87
88         assertTrue(atLeastOneAutomationCompositionTested);
89         return toscaServiceTemplate;
90     }
91
92     private static void addPolicyTypesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
93         var policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
94
95         for (String policyTypeFilePath : policyTypeDirectoryContents) {
96             String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
97
98             var foundPolicyTypeSt =
99                     yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
100
101             toscaServiceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
102             toscaServiceTemplate.setDescription(foundPolicyTypeSt.getDescription());
103             toscaServiceTemplate.setMetadata(foundPolicyTypeSt.getMetadata());
104             toscaServiceTemplate.setName(foundPolicyTypeSt.getName());
105             toscaServiceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion());
106             toscaServiceTemplate.setVersion(foundPolicyTypeSt.getVersion());
107
108             if (foundPolicyTypeSt.getDataTypes() != null) {
109                 if (toscaServiceTemplate.getDataTypes() == null) {
110                     toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
111                 } else {
112                     toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
113                 }
114             }
115
116             if (toscaServiceTemplate.getPolicyTypes() == null) {
117                 toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
118             } else {
119                 toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
120             }
121         }
122     }
123
124     /**
125      * Method to add polcies to the toscaServiceTemplate.
126      *
127      * @param toscaServiceTemplate to add policies
128      */
129     public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
130         var policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies");
131         toscaServiceTemplate.getToscaTopologyTemplate().setPolicies(new ArrayList<>());
132
133         for (var policiesFilePath : policiesDirectoryContents) {
134             if (!policiesFilePath.endsWith("yaml")) {
135                 continue;
136             }
137
138             var policiesString = ResourceUtils.getResourceAsString(policiesFilePath);
139
140             var foundPoliciesSt = yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class);
141             if (foundPoliciesSt.getToscaTopologyTemplate() != null
142                     && foundPoliciesSt.getToscaTopologyTemplate().getPolicies() != null) {
143                 toscaServiceTemplate.getToscaTopologyTemplate().getPolicies()
144                         .addAll(foundPoliciesSt.getToscaTopologyTemplate().getPolicies());
145             }
146         }
147     }
148
149     private static ToscaServiceTemplate testAutomationCompositionYamlSerialization(
150             String automationCompositionFilePath) {
151         try {
152             String automationCompositionString = ResourceUtils.getResourceAsString(automationCompositionFilePath);
153             if (automationCompositionString == null) {
154                 throw new FileNotFoundException(automationCompositionFilePath);
155             }
156
157             return yamlTranslator.fromYaml(automationCompositionString, ToscaServiceTemplate.class);
158         } catch (FileNotFoundException e) {
159             LOGGER.error("cannot find YAML file {}", automationCompositionFilePath);
160             throw new IllegalArgumentException(e);
161         }
162     }
163 }