7fe63a7c55bfcb304eb26108ef743903fdea53f4
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021,2023-2024 Nordix Foundation.
4  * ================================================================================
5  * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.clamp.acm.runtime.supervision.comm;
24
25 import io.micrometer.core.annotation.Timed;
26 import java.time.Instant;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.UUID;
32 import lombok.AllArgsConstructor;
33 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
35 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
36 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionDeploy;
37 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
38 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.stereotype.Component;
43
44 /**
45  * This class is used to send AutomationCompositionDeploy messages to participants on Kafka.
46  */
47 @Component
48 @AllArgsConstructor
49 public class AutomationCompositionDeployPublisher extends AbstractParticipantPublisher<AutomationCompositionDeploy> {
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionDeployPublisher.class);
52
53     /**
54      * Send AutomationCompositionDeploy to Participant.
55      *
56      * @param automationComposition the AutomationComposition
57      * @param startPhase the Start Phase
58      * @param firstStartPhase true if the first StartPhase
59      */
60     @Timed(value = "publisher.automation_composition_deploy",
61             description = "AUTOMATION_COMPOSITION_DEPLOY messages published")
62     public void send(AutomationComposition automationComposition, ToscaServiceTemplate toscaServiceTemplate,
63             int startPhase, boolean firstStartPhase) {
64         var toscaServiceTemplateFragment = AcmUtils.getToscaServiceTemplateFragment(toscaServiceTemplate);
65         Map<UUID, List<AcElementDeploy>> map = new HashMap<>();
66         for (var element : automationComposition.getElements().values()) {
67             var acElementDeploy = AcmUtils.createAcElementDeploy(element, DeployOrder.DEPLOY);
68             acElementDeploy.setToscaServiceTemplateFragment(toscaServiceTemplateFragment);
69
70             map.putIfAbsent(element.getParticipantId(), new ArrayList<>());
71             map.get(element.getParticipantId()).add(acElementDeploy);
72         }
73         List<ParticipantDeploy> participantDeploys = new ArrayList<>();
74         for (var entry : map.entrySet()) {
75             var participantDeploy = new ParticipantDeploy();
76             participantDeploy.setParticipantId(entry.getKey());
77             participantDeploy.setAcElementList(entry.getValue());
78             participantDeploys.add(participantDeploy);
79         }
80
81         var acDeployMsg = new AutomationCompositionDeploy();
82         acDeployMsg.setCompositionId(automationComposition.getCompositionId());
83         acDeployMsg.setStartPhase(startPhase);
84         acDeployMsg.setFirstStartPhase(firstStartPhase);
85         acDeployMsg.setAutomationCompositionId(automationComposition.getInstanceId());
86         acDeployMsg.setMessageId(UUID.randomUUID());
87         acDeployMsg.setTimestamp(Instant.now());
88         acDeployMsg.setParticipantUpdatesList(participantDeploys);
89
90         LOGGER.debug("AutomationCompositionDeploy message sent {}", acDeployMsg.getMessageId());
91         super.send(acDeployMsg);
92     }
93 }