b086b191402645e668b73abe080413bf270bc747
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.runtime.supervision.comm;
22
23 import io.micrometer.core.annotation.Timed;
24 import java.time.Instant;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Map.Entry;
30 import java.util.UUID;
31 import lombok.AllArgsConstructor;
32 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
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.concepts.ParticipantRestartAc;
36 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRestart;
37 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.stereotype.Component;
43
44 @Component
45 @AllArgsConstructor
46 public class ParticipantRestartPublisher extends AbstractParticipantPublisher<ParticipantRestart> {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantRestartPublisher.class);
49
50     /**
51      * Send Restart to Participant.
52      *
53      * @param participantId the ParticipantId
54      * @param acmDefinition the AutomationComposition Definition
55      * @param automationCompositions the list of automationCompositions
56      */
57     @Timed(value = "publisher.participant_restart", description = "Participant Restart published")
58     public void send(UUID participantId, AutomationCompositionDefinition acmDefinition,
59             List<AutomationComposition> automationCompositions) {
60
61         var message = new ParticipantRestart();
62         message.setParticipantId(participantId);
63         message.setCompositionId(acmDefinition.getCompositionId());
64         message.setMessageId(UUID.randomUUID());
65         message.setTimestamp(Instant.now());
66         message.setState(acmDefinition.getState());
67         message.setParticipantDefinitionUpdates(prepareParticipantRestarting(participantId, acmDefinition));
68         var toscaServiceTemplateFragment = AcmUtils.getToscaServiceTemplateFragment(acmDefinition.getServiceTemplate());
69
70         for (var automationComposition : automationCompositions) {
71             var restartAc = new ParticipantRestartAc();
72             restartAc.setAutomationCompositionId(automationComposition.getInstanceId());
73             for (var element : automationComposition.getElements().values()) {
74                 if (participantId.equals(element.getParticipantId())) {
75                     var acElementRestart = AcmUtils.createAcElementRestart(element);
76                     acElementRestart.setToscaServiceTemplateFragment(toscaServiceTemplateFragment);
77                     restartAc.getAcElementList().add(acElementRestart);
78                 }
79             }
80             message.getAutomationcompositionList().add(restartAc);
81         }
82
83         LOGGER.debug("Participant Restart sent {}", message);
84         super.send(message);
85     }
86
87     private List<ParticipantDefinition> prepareParticipantRestarting(UUID participantId,
88             AutomationCompositionDefinition acmDefinition) {
89         var acElements = AcmUtils.extractAcElementsFromServiceTemplate(acmDefinition.getServiceTemplate());
90
91         // list of entry entry filtered by participantId
92         List<Entry<String, ToscaNodeTemplate>> elementList = new ArrayList<>();
93         Map<ToscaConceptIdentifier, UUID> supportedElementMap = new HashMap<>();
94         for (var elementEntry : acElements) {
95             var elementState = acmDefinition.getElementStateMap().get(elementEntry.getKey());
96             if (participantId.equals(elementState.getParticipantId())) {
97                 var type = new ToscaConceptIdentifier(elementEntry.getValue().getType(),
98                         elementEntry.getValue().getTypeVersion());
99                 supportedElementMap.put(type, participantId);
100                 elementList.add(elementEntry);
101             }
102         }
103         return AcmUtils.prepareParticipantPriming(elementList, supportedElementMap);
104     }
105 }