34bcb014d2d5be5babb8d0c2a7781df9c7011918
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved.
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.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.UUID;
33 import java.util.stream.Collectors;
34 import lombok.AllArgsConstructor;
35 import org.onap.policy.clamp.acm.runtime.main.parameters.AcRuntimeParameterGroup;
36 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
38 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
39 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
40 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrime;
41 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
42 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
43 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.stereotype.Component;
48
49 /**
50  * This class is used to send ParticipantPrime messages to participants on Kafka.
51  */
52 @Component
53 @AllArgsConstructor
54 public class ParticipantPrimePublisher extends AbstractParticipantPublisher<ParticipantPrime> {
55
56     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantPrimePublisher.class);
57
58     private final ParticipantProvider participantProvider;
59     private final AcRuntimeParameterGroup acRuntimeParameterGroup;
60
61     /**
62      * Send ParticipantPrime to Participant
63      * if participantId is null then message is broadcast.
64      *
65      * @param participantDefinitions the list of ParticipantDefinition to send
66      * @param compositionId the compositionId
67      * @param revisionId last update
68      */
69     @Timed(value = "publisher.participant_update", description = "PARTICIPANT_UPDATE messages published")
70     public void sendPriming(List<ParticipantDefinition> participantDefinitions, UUID compositionId,
71             UUID revisionId) {
72         var message = new ParticipantPrime();
73         message.setCompositionId(compositionId);
74         message.setParticipantIdList(participantDefinitions.stream()
75                 .map(ParticipantDefinition::getParticipantId).collect(Collectors.toSet()));
76         message.setTimestamp(Instant.now());
77         message.setRevisionIdComposition(revisionId);
78         message.setParticipantDefinitionUpdates(participantDefinitions);
79         LOGGER.debug("Participant Update sent {}", message.getMessageId());
80         super.send(message);
81     }
82
83     /**
84      * Prepare the Priming message creating the list of ParticipantDefinition to send
85      * and fill the ElementState map of the AC Definition.
86      *
87      * @param acmDefinition the AutomationComposition Definition
88      * @return list of ParticipantDefinition
89      */
90     public List<ParticipantDefinition> prepareParticipantPriming(AutomationCompositionDefinition acmDefinition) {
91         acmDefinition.setStateChangeResult(StateChangeResult.NO_ERROR);
92         acmDefinition.setState(AcTypeState.PRIMING);
93         acmDefinition.setRevisionId(UUID.randomUUID());
94         acmDefinition.setLastMsg(TimestampHelper.now());
95         var acElements = AcmUtils.extractAcElementsFromServiceTemplate(acmDefinition.getServiceTemplate(),
96                 acRuntimeParameterGroup.getAcmParameters().getToscaElementName());
97         Map<ToscaConceptIdentifier, UUID> supportedElementMap = new HashMap<>();
98         var participantIds = new HashSet<UUID>();
99         if (AcTypeState.PRIMED.equals(acmDefinition.getState())) {
100             // scenario Prime again, participants already assigned
101             for (var elementEntry : acElements) {
102                 var elementState = acmDefinition.getElementStateMap().get(elementEntry.getKey());
103                 elementState.setState(AcTypeState.PRIMING);
104                 participantIds.add(elementState.getParticipantId());
105                 supportedElementMap.put(AcmUtils.getType(elementEntry.getValue()), elementState.getParticipantId());
106             }
107         } else {
108             // scenario Prime participants not assigned yet
109             supportedElementMap = participantProvider.getSupportedElementMap();
110             for (var elementEntry : acElements) {
111                 var elementState = acmDefinition.getElementStateMap().get(elementEntry.getKey());
112                 elementState.setState(AcTypeState.PRIMING);
113                 var participantId = supportedElementMap.get(AcmUtils.getType(elementEntry.getValue()));
114                 if (participantId != null) {
115                     elementState.setParticipantId(participantId);
116                     participantIds.add(participantId);
117                 }
118             }
119         }
120         participantProvider.verifyParticipantState(participantIds);
121         return AcmUtils.prepareParticipantPriming(acElements, supportedElementMap);
122     }
123
124     /**
125      * Send ParticipantPrime to Participant after that commissioning has been removed.
126      */
127     @Timed(value = "publisher.participant_update", description = "PARTICIPANT_UPDATE messages published")
128     public void sendDepriming(UUID compositionId, Set<UUID> participantIds, UUID revisionId) {
129         var message = new ParticipantPrime();
130         message.setCompositionId(compositionId);
131         message.setParticipantIdList(participantIds);
132         message.setTimestamp(Instant.now());
133         message.setRevisionIdComposition(revisionId);
134         // DeCommission the automation composition but deleting participantdefinitions on participants
135         message.setParticipantDefinitionUpdates(null);
136
137         LOGGER.debug("Participant Update sent {}", message.getMessageId());
138         super.send(message);
139     }
140 }