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