d0ad72f8373d07865b5e1832f02e83364b9cf532
[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.List;
29 import java.util.Map;
30 import java.util.Set;
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;
45
46 @NoArgsConstructor(access = AccessLevel.PRIVATE)
47 public final class TestListenerUtils {
48
49     private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
50     private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
51
52     /**
53      * Method to create participantUpdateMsg.
54      *
55      * @return ParticipantUpdate message
56      */
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");
62
63         participantUpdateMsg.setParticipantId(participantId);
64         participantUpdateMsg.setTimestamp(Instant.now());
65         participantUpdateMsg.setParticipantType(participantType);
66         participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
67         participantUpdateMsg.setMessageId(UUID.randomUUID());
68
69         ToscaServiceTemplate toscaServiceTemplate = testAutomationCompositionRead();
70         // Add policies to the toscaServiceTemplate
71         TestListenerUtils.addPoliciesToToscaServiceTemplate(toscaServiceTemplate);
72
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);
82             }
83         }
84
85         participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates);
86         return participantUpdateMsg;
87     }
88
89     private static ToscaServiceTemplate testAutomationCompositionRead() {
90         Set<String> automationCompositionDirectoryContents =
91             ResourceUtils.getDirectoryContents("clamp/acm/test");
92
93         boolean atLeastOneAutomationCompositionTested = false;
94         ToscaServiceTemplate toscaServiceTemplate = null;
95
96         for (String automationCompositionFilePath : automationCompositionDirectoryContents) {
97             if (!automationCompositionFilePath.endsWith(".yaml")) {
98                 continue;
99             }
100             atLeastOneAutomationCompositionTested = true;
101             toscaServiceTemplate = testAutomationCompositionYamlSerialization(automationCompositionFilePath);
102         }
103
104         // Add policy_types to the toscaServiceTemplate
105         addPolicyTypesToToscaServiceTemplate(toscaServiceTemplate);
106
107         assertTrue(atLeastOneAutomationCompositionTested);
108         return toscaServiceTemplate;
109     }
110
111     private static void addPolicyTypesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
112         Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
113
114         for (String policyTypeFilePath : policyTypeDirectoryContents) {
115             String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
116
117             ToscaServiceTemplate foundPolicyTypeSt =
118                 yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
119
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());
126
127             if (foundPolicyTypeSt.getDataTypes() != null) {
128                 if (toscaServiceTemplate.getDataTypes() == null) {
129                     toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
130                 } else {
131                     toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
132                 }
133             }
134
135             if (toscaServiceTemplate.getPolicyTypes() == null) {
136                 toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
137             } else {
138                 toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
139             }
140         }
141     }
142
143     /**
144      * Method to add polcies to the toscaServiceTemplate.
145      *
146      * @param toscaServiceTemplate to add policies
147      */
148     public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
149         var policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies");
150         toscaServiceTemplate.getToscaTopologyTemplate().setPolicies(new ArrayList<>());
151
152         for (var policiesFilePath : policiesDirectoryContents) {
153             if (!policiesFilePath.endsWith("yaml")) {
154                 continue;
155             }
156
157             var policiesString = ResourceUtils.getResourceAsString(policiesFilePath);
158
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());
165             }
166         }
167     }
168
169     private static ToscaServiceTemplate testAutomationCompositionYamlSerialization(
170         String automationCompositionFilePath) {
171         try {
172             String automationCompositionString = ResourceUtils.getResourceAsString(automationCompositionFilePath);
173             if (automationCompositionString == null) {
174                 throw new FileNotFoundException(automationCompositionFilePath);
175             }
176
177             return yamlTranslator.fromYaml(automationCompositionString, ToscaServiceTemplate.class);
178         } catch (FileNotFoundException e) {
179             LOGGER.error("cannot find YAML file {}", automationCompositionFilePath);
180             throw new IllegalArgumentException(e);
181         }
182     }
183 }