944aaa6b7d8258aee8a8fe3ea59a8c988e79b53b
[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.simulator.utils;
22
23 import java.io.FileNotFoundException;
24 import java.time.Instant;
25 import java.util.ArrayList;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.UUID;
30 import lombok.AccessLevel;
31 import lombok.NoArgsConstructor;
32 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
33 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
36 import org.onap.policy.clamp.models.acm.concepts.ParticipantUpdates;
37 import org.onap.policy.clamp.models.acm.concepts.ParticipantUtils;
38 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionUpdate;
39 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
40 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
41 import org.onap.policy.common.utils.resources.ResourceUtils;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 @NoArgsConstructor(access = AccessLevel.PRIVATE)
49 public final class TestListenerUtils {
50
51     private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
52     private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
53     private static final String TOSCA_TEMPLATE = "clamp/acm/test/pm_simple_ac_tosca.yaml";
54
55     /**
56      * Method to create a automationComposition from a yaml file.
57      *
58      * @return AutomationComposition automation composition
59      */
60     public static AutomationComposition createAutomationComposition() {
61         AutomationComposition automationComposition = new AutomationComposition();
62         Map<UUID, AutomationCompositionElement> elements = new LinkedHashMap<>();
63         ToscaServiceTemplate toscaServiceTemplate = testAutomationCompositionYamlSerialization();
64         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
65             toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
66         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
67             AutomationCompositionElement acElement = new AutomationCompositionElement();
68             acElement.setId(UUID.randomUUID());
69
70             ToscaConceptIdentifier acElementParticipantId = new ToscaConceptIdentifier();
71             acElementParticipantId.setName(toscaInputEntry.getKey());
72             acElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
73             acElement.setParticipantId(acElementParticipantId);
74             acElement.setParticipantType(acElementParticipantId);
75
76             acElement.setDefinition(acElementParticipantId);
77             acElement.setState(AutomationCompositionState.UNINITIALISED);
78             acElement.setDescription(toscaInputEntry.getValue().getDescription());
79             acElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
80             elements.put(acElement.getId(), acElement);
81         }
82         automationComposition.setElements(elements);
83         automationComposition.setName("PMSHInstance0");
84         automationComposition.setVersion("1.0.0");
85
86         ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
87         definition.setName("PMSHInstance0");
88         definition.setVersion("1.0.0");
89         automationComposition.setDefinition(definition);
90
91         return automationComposition;
92     }
93
94     /**
95      * Method to create AutomationCompositionUpdateMsg.
96      *
97      * @return AutomationCompositionUpdate message
98      */
99     public static AutomationCompositionUpdate createAutomationCompositionUpdateMsg() {
100         final AutomationCompositionUpdate acUpdateMsg = new AutomationCompositionUpdate();
101         ToscaConceptIdentifier automationCompositionId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
102         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "0.0.0");
103
104         acUpdateMsg.setAutomationCompositionId(automationCompositionId);
105         acUpdateMsg.setParticipantId(participantId);
106         acUpdateMsg.setMessageId(UUID.randomUUID());
107         acUpdateMsg.setTimestamp(Instant.now());
108
109         Map<UUID, AutomationCompositionElement> elements = new LinkedHashMap<>();
110         ToscaServiceTemplate toscaServiceTemplate = testAutomationCompositionYamlSerialization();
111         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
112             toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
113         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
114             if (ParticipantUtils.checkIfNodeTemplateIsAutomationCompositionElement(toscaInputEntry.getValue(),
115                 toscaServiceTemplate)) {
116                 AutomationCompositionElement acElement = new AutomationCompositionElement();
117                 acElement.setId(UUID.randomUUID());
118                 var acParticipantType =
119                     ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties());
120
121                 acElement.setParticipantId(acParticipantType);
122                 acElement.setParticipantType(acParticipantType);
123
124                 acElement.setDefinition(
125                     new ToscaConceptIdentifier(toscaInputEntry.getKey(), toscaInputEntry.getValue().getVersion()));
126                 acElement.setState(AutomationCompositionState.UNINITIALISED);
127                 acElement.setDescription(toscaInputEntry.getValue().getDescription());
128                 acElement.setOrderedState(AutomationCompositionOrderedState.PASSIVE);
129                 elements.put(acElement.getId(), acElement);
130             }
131         }
132
133         List<ParticipantUpdates> participantUpdates = new ArrayList<>();
134         for (AutomationCompositionElement element : elements.values()) {
135             AcmUtils.prepareParticipantUpdate(element, participantUpdates);
136         }
137         acUpdateMsg.setParticipantUpdatesList(participantUpdates);
138         return acUpdateMsg;
139     }
140
141     private static ToscaServiceTemplate testAutomationCompositionYamlSerialization() {
142         try {
143             String automationCompositionString = ResourceUtils.getResourceAsString(TestListenerUtils.TOSCA_TEMPLATE);
144             if (automationCompositionString == null) {
145                 throw new FileNotFoundException(TestListenerUtils.TOSCA_TEMPLATE);
146             }
147
148             return yamlTranslator.fromYaml(automationCompositionString, ToscaServiceTemplate.class);
149         } catch (FileNotFoundException e) {
150             LOGGER.error("cannot find YAML file {}", TestListenerUtils.TOSCA_TEMPLATE);
151             throw new IllegalArgumentException(e);
152         }
153     }
154 }