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