f3fcfe67aed919ea26285bdb06bf835ec0245cbe
[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.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.messages.dmaap.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.models.tosca.authorative.concepts.ToscaConceptIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.stereotype.Component;
45
46 /**
47  * This class is used to send ParticipantPrime messages to participants on DMaaP.
48  */
49 @Component
50 @AllArgsConstructor
51 public class ParticipantPrimePublisher extends AbstractParticipantPublisher<ParticipantPrime> {
52
53     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantPrimePublisher.class);
54
55     private final ParticipantProvider participantProvider;
56     private final AcmParticipantProvider acmParticipantProvider;
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.setState(AcTypeState.PRIMING);
88         var acElements = AcmUtils.extractAcElementsFromServiceTemplate(acmDefinition.getServiceTemplate(),
89                 acRuntimeParameterGroup.getAcmParameters().getToscaElementName());
90         Map<ToscaConceptIdentifier, UUID> supportedElementMap = new HashMap<>();
91         var participantIds = new HashSet<UUID>();
92         if (AcTypeState.PRIMED.equals(acmDefinition.getState())) {
93             // scenario Prime again, participants already assigned
94             for (var elementEntry : acElements) {
95                 var elementState = acmDefinition.getElementStateMap().get(elementEntry.getKey());
96                 elementState.setState(AcTypeState.PRIMING);
97                 participantIds.add(elementState.getParticipantId());
98                 var type = new ToscaConceptIdentifier(elementEntry.getValue().getType(),
99                         elementEntry.getValue().getTypeVersion());
100                 supportedElementMap.put(type, 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 type = new ToscaConceptIdentifier(elementEntry.getValue().getType(),
109                         elementEntry.getValue().getTypeVersion());
110                 var participantId = supportedElementMap.get(type);
111                 if (participantId != null) {
112                     elementState.setParticipantId(participantId);
113                     participantIds.add(participantId);
114                 }
115             }
116         }
117         acmParticipantProvider.verifyParticipantState(participantIds);
118         return AcmUtils.prepareParticipantPriming(acElements, supportedElementMap);
119     }
120
121     /**
122      * Send ParticipantPrime to Participant after that commissioning has been removed.
123      */
124     @Timed(value = "publisher.participant_update", description = "PARTICIPANT_UPDATE messages published")
125     public void sendDepriming(UUID compositionId) {
126         var message = new ParticipantPrime();
127         message.setCompositionId(compositionId);
128         message.setTimestamp(Instant.now());
129         // DeCommission the automation composition but deleting participantdefinitions on participants
130         message.setParticipantDefinitionUpdates(null);
131
132         LOGGER.debug("Participant Update sent {}", message);
133         super.send(message);
134     }
135 }