cb00c8e4d2daa2b3f28a26fe65ba7dbe8e66b3ca
[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.messages.rest.instantiation.DeployOrder;
38 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.stereotype.Component;
44
45 @Component
46 @AllArgsConstructor
47 public class ParticipantRestartPublisher extends AbstractParticipantPublisher<ParticipantRestart> {
48
49     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantRestartPublisher.class);
50
51     /**
52      * Send Restart to Participant.
53      *
54      */
55     @Timed(value = "publisher.participant_restart", description = "Participant Restart published")
56     public void send(UUID participantId, AutomationCompositionDefinition acmDefinition,
57             List<AutomationComposition> automationCompositions) {
58
59         var message = new ParticipantRestart();
60         message.setParticipantId(participantId);
61         message.setCompositionId(acmDefinition.getCompositionId());
62         message.setMessageId(UUID.randomUUID());
63         message.setTimestamp(Instant.now());
64         message.setParticipantDefinitionUpdates(prepareParticipantRestarting(participantId, acmDefinition));
65
66         for (var automationComposition : automationCompositions) {
67             var restartAc = new ParticipantRestartAc();
68             restartAc.setAutomationCompositionId(automationComposition.getInstanceId());
69             restartAc.setDeployState(automationComposition.getDeployState());
70             restartAc.setLockState(automationComposition.getLockState());
71             for (var element : automationComposition.getElements().values()) {
72                 if (participantId.equals(element.getParticipantId())) {
73                     var acElementDeploy = AcmUtils.createAcElementDeploy(element, DeployOrder.RESTARTING);
74                     acElementDeploy.setToscaServiceTemplateFragment(acmDefinition.getServiceTemplate());
75                     restartAc.getAcElementList().add(acElementDeploy);
76                 }
77             }
78             message.getAutocompositionList().add(restartAc);
79         }
80
81         LOGGER.debug("Participant Restart sent {}", message);
82         super.send(message);
83     }
84
85     private List<ParticipantDefinition> prepareParticipantRestarting(UUID participantId,
86             AutomationCompositionDefinition acmDefinition) {
87         var acElements = AcmUtils.extractAcElementsFromServiceTemplate(acmDefinition.getServiceTemplate());
88
89         // list of entry entry filtered by participantId
90         List<Entry<String, ToscaNodeTemplate>> elementList = new ArrayList<>();
91         Map<ToscaConceptIdentifier, UUID> supportedElementMap = new HashMap<>();
92         for (var elementEntry : acElements) {
93             var elementState = acmDefinition.getElementStateMap().get(elementEntry.getKey());
94             if (participantId.equals(elementState.getParticipantId())) {
95                 var type = new ToscaConceptIdentifier(elementEntry.getValue().getType(),
96                         elementEntry.getValue().getTypeVersion());
97                 supportedElementMap.put(type, participantId);
98                 elementList.add(elementEntry);
99             }
100         }
101         return AcmUtils.prepareParticipantPriming(elementList, supportedElementMap);
102     }
103 }