4a0abc1808070ae9ba9c8900415cebbcb2b1f9c4
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021,2023 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.List;
29 import java.util.UUID;
30 import lombok.AllArgsConstructor;
31 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
32 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
33 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeploy;
34 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
35 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Component;
39
40 /**
41  * This class is used to send AutomationCompositionDeploy messages to participants on DMaaP.
42  */
43 @Component
44 @AllArgsConstructor
45 public class AutomationCompositionDeployPublisher extends AbstractParticipantPublisher<AutomationCompositionDeploy> {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionDeployPublisher.class);
48     private final AcDefinitionProvider acDefinitionProvider;
49
50     /**
51      * Send AutomationCompositionDeploy to Participant.
52      *
53      * @param automationComposition the AutomationComposition
54      */
55     @Timed(value = "publisher.automation_composition_deploy",
56             description = "AUTOMATION_COMPOSITION_DEPLOY messages published")
57     public void send(AutomationComposition automationComposition) {
58         send(automationComposition, 0);
59     }
60
61     /**
62      * Send AutomationCompositionDeploy to Participant.
63      *
64      * @param automationComposition the AutomationComposition
65      * @param startPhase the Start Phase
66      */
67     @Timed(value = "publisher.automation_composition_deploy",
68             description = "AUTOMATION_COMPOSITION_DEPLOY messages published")
69     public void send(AutomationComposition automationComposition, int startPhase) {
70         var acDeployMsg = new AutomationCompositionDeploy();
71         acDeployMsg.setCompositionId(automationComposition.getCompositionId());
72         acDeployMsg.setStartPhase(startPhase);
73         acDeployMsg.setAutomationCompositionId(automationComposition.getInstanceId());
74         acDeployMsg.setMessageId(UUID.randomUUID());
75         acDeployMsg.setTimestamp(Instant.now());
76         var toscaServiceTemplate =
77                 acDefinitionProvider.getAcDefinition(automationComposition.getCompositionId()).getServiceTemplate();
78
79         List<ParticipantDeploy> participantDeploys = new ArrayList<>();
80         for (var element : automationComposition.getElements().values()) {
81             AcmUtils.setAcPolicyInfo(element, toscaServiceTemplate);
82             AcmUtils.prepareParticipantUpdate(element, participantDeploys);
83         }
84         acDeployMsg.setParticipantUpdatesList(participantDeploys);
85
86         LOGGER.debug("AutomationCompositionDeploy message sent {}", acDeployMsg);
87         super.send(acDeployMsg);
88     }
89 }