fa5e423d7aa6ef93154c4eb8662915966a1fa92b
[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 lombok.AllArgsConstructor;
30 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
31 import org.onap.policy.clamp.models.acm.concepts.ParticipantUtils;
32 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdate;
33 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
34 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
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 ParticipantUpdate messages to participants on DMaaP.
42  */
43 @Component
44 @AllArgsConstructor
45 public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<ParticipantUpdate> {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantUpdatePublisher.class);
48
49     private final AcDefinitionProvider acDefinitionProvider;
50
51     /**
52      * Send ParticipantUpdate to all Participants.
53      *
54      * @param name the ToscaServiceTemplate name
55      * @param version the ToscaServiceTemplate version
56      */
57     @Timed(value = "publisher.participant_update", description = "PARTICIPANT_UPDATE messages published")
58     public void sendComissioningBroadcast(String name, String version) {
59         sendCommissioning(name, version, null, null);
60     }
61
62     /**
63      * Send ParticipantUpdate to Participant
64      * if participantType and participantId are null then message is broadcast.
65      *
66      * @param name the ToscaServiceTemplate name
67      * @param version the ToscaServiceTemplate version
68      * @param participantType the ParticipantType
69      * @param participantId the ParticipantId
70      */
71     @Timed(value = "publisher.participant_update", description = "PARTICIPANT_UPDATE messages published")
72     public boolean sendCommissioning(String name, String version, ToscaConceptIdentifier participantType,
73             ToscaConceptIdentifier participantId) {
74         var message = new ParticipantUpdate();
75         message.setParticipantType(participantType);
76         message.setParticipantId(participantId);
77         message.setTimestamp(Instant.now());
78
79         var list = acDefinitionProvider.getServiceTemplateList(name, version);
80         if (list.isEmpty()) {
81             LOGGER.warn("No tosca service template found, cannot send participantupdate {} {}", name, version);
82             return false;
83         }
84         var toscaServiceTemplate = list.get(0);
85         var commonPropertiesMap = AcmUtils.getCommonOrInstancePropertiesFromNodeTypes(true, toscaServiceTemplate);
86
87         List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
88         for (var toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet()) {
89             if (ParticipantUtils.checkIfNodeTemplateIsAutomationCompositionElement(toscaInputEntry.getValue(),
90                     toscaServiceTemplate)) {
91                 AcmUtils.prepareParticipantDefinitionUpdate(
92                         ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()),
93                         toscaInputEntry.getKey(), toscaInputEntry.getValue(), participantDefinitionUpdates,
94                         commonPropertiesMap);
95             }
96         }
97
98         // Commission the automation composition but sending participantdefinitions to participants
99         message.setParticipantDefinitionUpdates(participantDefinitionUpdates);
100         LOGGER.debug("Participant Update sent {}", message);
101         super.send(message);
102         return true;
103     }
104
105     /**
106      * Send ParticipantUpdate to Participant after that commissioning has been removed.
107      */
108     @Timed(value = "publisher.participant_update", description = "PARTICIPANT_UPDATE messages published")
109     public void sendDecomisioning() {
110         var message = new ParticipantUpdate();
111         message.setTimestamp(Instant.now());
112         // DeCommission the automation composition but deleting participantdefinitions on participants
113         message.setParticipantDefinitionUpdates(null);
114
115         LOGGER.debug("Participant Update sent {}", message);
116         super.send(message);
117     }
118 }