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