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