ae7eda1eedc3571acf4d3f79135b8406ba3b2e56
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 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.runtime.supervision.comm;
22
23 import io.micrometer.core.annotation.Timed;
24 import java.time.Instant;
25 import java.util.List;
26 import java.util.Optional;
27 import java.util.UUID;
28 import org.onap.policy.clamp.acm.runtime.main.parameters.AcRuntimeParameterGroup;
29 import org.onap.policy.clamp.acm.runtime.main.parameters.Topics;
30 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
31 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
32 import org.onap.policy.clamp.models.acm.concepts.ParticipantRestartAc;
33 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantSync;
34 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
35 import org.onap.policy.common.endpoints.event.comm.TopicSink;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Component;
39
40
41 @Component
42 public class ParticipantSyncPublisher extends ParticipantRestartPublisher {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantSyncPublisher.class);
45
46     private final AcRuntimeParameterGroup acRuntimeParameterGroup;
47
48     public ParticipantSyncPublisher(AcRuntimeParameterGroup acRuntimeParameterGroup) {
49         super(acRuntimeParameterGroup);
50         this.acRuntimeParameterGroup = acRuntimeParameterGroup;
51     }
52
53
54     /**
55      * Send sync msg to Participant.
56      *
57      * @param participantId the ParticipantId
58      * @param acmDefinition the AutomationComposition Definition
59      * @param automationCompositions the list of automationCompositions
60      */
61     @Override
62     @Timed(value = "publisher.participant_sync_msg", description = "Participant Sync published")
63     public void send(UUID participantId, AutomationCompositionDefinition acmDefinition,
64                      List<AutomationComposition> automationCompositions) {
65
66         var message = new ParticipantSync();
67         message.setParticipantId(participantId);
68         message.setCompositionId(acmDefinition.getCompositionId());
69         message.setMessageId(UUID.randomUUID());
70         message.setTimestamp(Instant.now());
71         message.setState(acmDefinition.getState());
72         message.setParticipantDefinitionUpdates(prepareParticipantRestarting(participantId, acmDefinition));
73         var toscaServiceTemplateFragment = AcmUtils.getToscaServiceTemplateFragment(acmDefinition.getServiceTemplate());
74
75         for (var automationComposition : automationCompositions) {
76             var syncAc = new ParticipantRestartAc();
77             syncAc.setAutomationCompositionId(automationComposition.getInstanceId());
78             for (var element : automationComposition.getElements().values()) {
79                 if (participantId.equals(element.getParticipantId())) {
80                     var acElementSync = AcmUtils.createAcElementRestart(element);
81                     acElementSync.setToscaServiceTemplateFragment(toscaServiceTemplateFragment);
82                     syncAc.getAcElementList().add(acElementSync);
83                 }
84             }
85             message.getAutomationcompositionList().add(syncAc);
86         }
87
88         LOGGER.debug("Participant Sync sent {}", message);
89         super.send(message);
90     }
91
92     /**
93      * Is default topic.
94      * @return true if default
95      */
96     @Override
97     public boolean isDefaultTopic() {
98         return false;
99     }
100
101 }