163d3299d170e1d6c23dfc12d0d9b001a9f3ddbd
[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.acm.runtime.main.parameters.AcRuntimeParameterGroup;
33 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
35 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
36 import org.onap.policy.clamp.models.acm.concepts.ParticipantRestartAc;
37 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRestart;
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     private final AcRuntimeParameterGroup acRuntimeParameterGroup;
51
52     /**
53      * Send Restart to Participant.
54      *
55      * @param participantId the ParticipantId
56      * @param acmDefinition the AutomationComposition Definition
57      * @param automationCompositions the list of automationCompositions
58      */
59     @Timed(value = "publisher.participant_restart", description = "Participant Restart published")
60     public void send(UUID participantId, AutomationCompositionDefinition acmDefinition,
61             List<AutomationComposition> automationCompositions) {
62
63         var message = new ParticipantRestart();
64         message.setParticipantId(participantId);
65         message.setCompositionId(acmDefinition.getCompositionId());
66         message.setMessageId(UUID.randomUUID());
67         message.setTimestamp(Instant.now());
68         message.setState(acmDefinition.getState());
69         message.setParticipantDefinitionUpdates(prepareParticipantRestarting(participantId, acmDefinition));
70         var toscaServiceTemplateFragment = AcmUtils.getToscaServiceTemplateFragment(acmDefinition.getServiceTemplate());
71
72         for (var automationComposition : automationCompositions) {
73             var restartAc = new ParticipantRestartAc();
74             restartAc.setAutomationCompositionId(automationComposition.getInstanceId());
75             for (var element : automationComposition.getElements().values()) {
76                 if (participantId.equals(element.getParticipantId())) {
77                     var acElementRestart = AcmUtils.createAcElementRestart(element);
78                     acElementRestart.setToscaServiceTemplateFragment(toscaServiceTemplateFragment);
79                     restartAc.getAcElementList().add(acElementRestart);
80                 }
81             }
82             message.getAutomationcompositionList().add(restartAc);
83         }
84
85         LOGGER.debug("Participant Restart sent {}", message);
86         super.send(message);
87     }
88
89     private List<ParticipantDefinition> prepareParticipantRestarting(UUID participantId,
90             AutomationCompositionDefinition acmDefinition) {
91         var acElements = AcmUtils.extractAcElementsFromServiceTemplate(acmDefinition.getServiceTemplate(),
92                 acRuntimeParameterGroup.getAcmParameters().getToscaElementName());
93
94         // list of entry entry filtered by participantId
95         List<Entry<String, ToscaNodeTemplate>> elementList = new ArrayList<>();
96         Map<ToscaConceptIdentifier, UUID> supportedElementMap = new HashMap<>();
97         for (var elementEntry : acElements) {
98             var elementState = acmDefinition.getElementStateMap().get(elementEntry.getKey());
99             if (participantId.equals(elementState.getParticipantId())) {
100                 var type = new ToscaConceptIdentifier(elementEntry.getValue().getType(),
101                         elementEntry.getValue().getTypeVersion());
102                 supportedElementMap.put(type, participantId);
103                 elementList.add(elementEntry);
104             }
105         }
106         var list = AcmUtils.prepareParticipantPriming(elementList, supportedElementMap);
107         for (var participantDefinition : list) {
108             for (var elementDe : participantDefinition.getAutomationCompositionElementDefinitionList()) {
109                 var state = acmDefinition.getElementStateMap().get(elementDe.getAcElementDefinitionId().getName());
110                 if (state != null) {
111                     elementDe.setOutProperties(state.getOutProperties());
112                 }
113             }
114         }
115         return list;
116     }
117 }