cc4a059396b7e96b0d2fbe6eb5c3b0fcdecfd8ef
[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.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.UUID;
32 import java.util.function.UnaryOperator;
33 import lombok.AllArgsConstructor;
34 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
36 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
37 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeploy;
38 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
39 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
40 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
41 import org.onap.policy.models.base.PfUtils;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.stereotype.Component;
47
48 /**
49  * This class is used to send AutomationCompositionDeploy messages to participants on DMaaP.
50  */
51 @Component
52 @AllArgsConstructor
53 public class AutomationCompositionDeployPublisher extends AbstractParticipantPublisher<AutomationCompositionDeploy> {
54
55     private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionDeployPublisher.class);
56     private final AcDefinitionProvider acDefinitionProvider;
57
58     /**
59      * Send AutomationCompositionDeploy to Participant.
60      *
61      * @param automationComposition the AutomationComposition
62      */
63     @Timed(value = "publisher.automation_composition_deploy",
64             description = "AUTOMATION_COMPOSITION_DEPLOY messages published")
65     public void send(AutomationComposition automationComposition) {
66         send(automationComposition, 0);
67     }
68
69     /**
70      * Send AutomationCompositionDeploy to Participant.
71      *
72      * @param automationComposition the AutomationComposition
73      * @param startPhase the Start Phase
74      */
75     @Timed(value = "publisher.automation_composition_deploy",
76             description = "AUTOMATION_COMPOSITION_DEPLOY messages published")
77     public void send(AutomationComposition automationComposition, int startPhase) {
78         var acDeployMsg = new AutomationCompositionDeploy();
79         acDeployMsg.setCompositionId(automationComposition.getCompositionId());
80         acDeployMsg.setStartPhase(startPhase);
81         acDeployMsg.setAutomationCompositionId(automationComposition.getInstanceId());
82         acDeployMsg.setMessageId(UUID.randomUUID());
83         acDeployMsg.setTimestamp(Instant.now());
84         var toscaServiceTemplate =
85                 acDefinitionProvider.getAcDefinition(automationComposition.getCompositionId()).getServiceTemplate();
86
87         List<ParticipantDeploy> participantDeploys = new ArrayList<>();
88         for (var element : automationComposition.getElements().values()) {
89             element.setToscaServiceTemplateFragment(AcmUtils.getToscaServiceTemplateFragment(toscaServiceTemplate));
90             AcmUtils.prepareParticipantUpdate(element, participantDeploys);
91         }
92         acDeployMsg.setParticipantUpdatesList(participantDeploys);
93
94         LOGGER.debug("AutomationCompositionDeploy message sent {}", acDeployMsg);
95         super.send(acDeployMsg);
96     }
97
98     /**
99      * Send AutomationCompositionDeploy to Participant.
100      *
101      * @param automationComposition the AutomationComposition
102      * @param startPhase the Start Phase
103      */
104     @Timed(value = "publisher.automation_composition_deploy",
105             description = "AUTOMATION_COMPOSITION_DEPLOY messages published")
106     public void send(AutomationComposition automationComposition, ToscaServiceTemplate toscaServiceTemplate,
107             int startPhase, boolean firstStartPhase) {
108         var toscaServiceTemplateFragment = AcmUtils.getToscaServiceTemplateFragment(toscaServiceTemplate);
109         Map<UUID, List<AcElementDeploy>> map = new HashMap<>();
110         for (var element : automationComposition.getElements().values()) {
111             var acElementDeploy = new AcElementDeploy();
112             acElementDeploy.setId(element.getId());
113             acElementDeploy.setDefinition(new ToscaConceptIdentifier(element.getDefinition()));
114             acElementDeploy.setOrderedState(DeployOrder.DEPLOY);
115             acElementDeploy.setProperties(PfUtils.mapMap(element.getProperties(), UnaryOperator.identity()));
116             acElementDeploy.setToscaServiceTemplateFragment(toscaServiceTemplateFragment);
117
118             map.putIfAbsent(element.getParticipantId(), new ArrayList<>());
119             map.get(element.getParticipantId()).add(acElementDeploy);
120         }
121         List<ParticipantDeploy> participantDeploys = new ArrayList<>();
122         for (var entry : map.entrySet()) {
123             var participantDeploy = new ParticipantDeploy();
124             participantDeploy.setParticipantId(entry.getKey());
125             participantDeploy.setAcElementList(entry.getValue());
126             participantDeploys.add(participantDeploy);
127         }
128
129         var acDeployMsg = new AutomationCompositionDeploy();
130         acDeployMsg.setCompositionId(automationComposition.getCompositionId());
131         acDeployMsg.setStartPhase(startPhase);
132         acDeployMsg.setFirstStartPhase(firstStartPhase);
133         acDeployMsg.setAutomationCompositionId(automationComposition.getInstanceId());
134         acDeployMsg.setMessageId(UUID.randomUUID());
135         acDeployMsg.setTimestamp(Instant.now());
136         acDeployMsg.setParticipantUpdatesList(participantDeploys);
137
138         LOGGER.debug("AutomationCompositionDeploy message sent {}", acDeployMsg);
139         super.send(acDeployMsg);
140     }
141 }