504ddc28ceac02d4adfed071c7475df1954fce94
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021,2022 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.AutomationCompositionDefinition;
32 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
33 import org.onap.policy.clamp.models.acm.concepts.ParticipantUtils;
34 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdate;
35 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
36 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.stereotype.Component;
41
42 /**
43  * This class is used to send ParticipantUpdate messages to participants on DMaaP.
44  */
45 @Component
46 @AllArgsConstructor
47 public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<ParticipantUpdate> {
48
49     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantUpdatePublisher.class);
50
51     private final AcDefinitionProvider acDefinitionProvider;
52
53     /**
54      * Send ParticipantUpdate to all Participants.
55      *
56      * @param acmDefinition the AutomationComposition Definition
57      */
58     @Timed(value = "publisher.participant_update", description = "PARTICIPANT_UPDATE messages published")
59     public void sendComissioningBroadcast(AutomationCompositionDefinition acmDefinition) {
60         sendCommissioning(acmDefinition, null, null);
61     }
62
63     /**
64      * Send ParticipantUpdate to Participant
65      * if participantType and participantId are null then message is broadcast.
66      *
67      * @param participantType the ParticipantType
68      * @param participantId the ParticipantId
69      */
70     @Timed(value = "publisher.participant_update", description = "PARTICIPANT_UPDATE messages published")
71     public void sendCommissioning(ToscaConceptIdentifier participantType, UUID participantId) {
72         var list = acDefinitionProvider.getAllAcDefinitions();
73         if (list.isEmpty()) {
74             LOGGER.warn("No tosca service template found, cannot send participantupdate");
75         }
76         for (var acmDefinition : list) {
77             sendCommissioning(acmDefinition, participantType, participantId);
78         }
79     }
80
81     /**
82      * Send ParticipantUpdate to Participant
83      * if participantType and participantId are null then message is broadcast.
84      *
85      * @param acmDefinition the AutomationComposition Definition
86      * @param participantType the ParticipantType
87      * @param participantId the ParticipantId
88      */
89     @Timed(value = "publisher.participant_update", description = "PARTICIPANT_UPDATE messages published")
90     public void sendCommissioning(AutomationCompositionDefinition acmDefinition,
91             ToscaConceptIdentifier participantType, UUID participantId) {
92         var message = new ParticipantUpdate();
93         message.setCompositionId(acmDefinition.getCompositionId());
94         message.setParticipantType(participantType);
95         message.setParticipantId(participantId);
96         message.setTimestamp(Instant.now());
97
98         var toscaServiceTemplate = acmDefinition.getServiceTemplate();
99         List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
100         for (var toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet()) {
101             if (ParticipantUtils.checkIfNodeTemplateIsAutomationCompositionElement(toscaInputEntry.getValue(),
102                     toscaServiceTemplate)) {
103                 AcmUtils.prepareParticipantDefinitionUpdate(
104                         ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()),
105                         toscaInputEntry.getKey(), toscaInputEntry.getValue(), participantDefinitionUpdates);
106             }
107         }
108
109         // Commission the automation composition but sending participantdefinitions to participants
110         message.setParticipantDefinitionUpdates(participantDefinitionUpdates);
111         LOGGER.debug("Participant Update sent {}", message);
112         super.send(message);
113     }
114
115     /**
116      * Send ParticipantUpdate to Participant after that commissioning has been removed.
117      */
118     @Timed(value = "publisher.participant_update", description = "PARTICIPANT_UPDATE messages published")
119     public void sendDecomisioning(UUID compositionId) {
120         var message = new ParticipantUpdate();
121         message.setCompositionId(compositionId);
122         message.setTimestamp(Instant.now());
123         // DeCommission the automation composition but deleting participantdefinitions on participants
124         message.setParticipantDefinitionUpdates(null);
125
126         LOGGER.debug("Participant Update sent {}", message);
127         super.send(message);
128     }
129 }