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