d1ded25d66521c89ebef86bee8c5cdf4644fdd49
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2024-2025 OpenInfra Foundation Europe. All rights reserved.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.acm.participant.intermediary.handler;
23
24 import java.util.List;
25 import java.util.UUID;
26 import java.util.stream.Collectors;
27 import lombok.RequiredArgsConstructor;
28 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto;
29 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
30 import org.onap.policy.clamp.acm.participant.intermediary.handler.cache.CacheProvider;
31 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
32 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
33 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
34 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
35 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
36 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrime;
37 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrimeAck;
38 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantSync;
39 import org.springframework.stereotype.Component;
40
41 @Component
42 @RequiredArgsConstructor
43 public class AcDefinitionHandler {
44
45     private final CacheProvider cacheProvider;
46     private final ParticipantMessagePublisher publisher;
47     private final ThreadHandler listener;
48
49     /**
50      * Handle a participant Prime message.
51      *
52      * @param participantPrimeMsg the ParticipantPrime message
53      */
54     public void handlePrime(ParticipantPrime participantPrimeMsg) {
55         if (!participantPrimeMsg.getParticipantDefinitionUpdates().isEmpty()) {
56             // prime
57             var list = collectAcElementDefinition(participantPrimeMsg.getParticipantDefinitionUpdates());
58             if (!list.isEmpty()) {
59                 cacheProvider.addElementDefinition(participantPrimeMsg.getCompositionId(), list,
60                         participantPrimeMsg.getRevisionIdComposition());
61                 prime(participantPrimeMsg.getMessageId(), participantPrimeMsg.getCompositionId(), list);
62             }
63         } else {
64             // deprime
65             deprime(participantPrimeMsg.getMessageId(), participantPrimeMsg.getCompositionId());
66         }
67     }
68
69     private List<AutomationCompositionElementDefinition> collectAcElementDefinition(
70             List<ParticipantDefinition> participantDefinitionList) {
71         return participantDefinitionList.stream()
72                 .filter(participantDefinition -> participantDefinition.getParticipantId()
73                         .equals(cacheProvider.getParticipantId()))
74                 .map(ParticipantDefinition::getAutomationCompositionElementDefinitionList)
75                 .flatMap(List::stream)
76                 .toList();
77     }
78
79     private void prime(UUID messageId, UUID compositionId, List<AutomationCompositionElementDefinition> list) {
80         var inPropertiesMap = list.stream().collect(Collectors.toMap(
81                 AutomationCompositionElementDefinition::getAcElementDefinitionId,
82                 el -> el.getAutomationCompositionElementToscaNodeTemplate().getProperties()));
83         var outPropertiesMap = list.stream().collect(Collectors.toMap(
84                 AutomationCompositionElementDefinition::getAcElementDefinitionId,
85                 AutomationCompositionElementDefinition::getOutProperties));
86         listener.prime(messageId, new CompositionDto(compositionId, inPropertiesMap, outPropertiesMap));
87     }
88
89     private void deprime(UUID messageId, UUID compositionId) {
90         var acDefinition = cacheProvider.getAcElementsDefinitions().get(compositionId);
91         if (acDefinition == null) {
92             // this participant does not handle this composition
93             var participantPrimeAck = new ParticipantPrimeAck();
94             participantPrimeAck.setCompositionId(compositionId);
95             participantPrimeAck.setMessage("Already deprimed or never primed");
96             participantPrimeAck.setResult(true);
97             participantPrimeAck.setResponseTo(messageId);
98             participantPrimeAck.setCompositionState(AcTypeState.COMMISSIONED);
99             participantPrimeAck.setStateChangeResult(StateChangeResult.NO_ERROR);
100             participantPrimeAck.setParticipantId(cacheProvider.getParticipantId());
101             participantPrimeAck.setReplicaId(cacheProvider.getReplicaId());
102             participantPrimeAck.setState(ParticipantState.ON_LINE);
103             publisher.sendParticipantPrimeAck(participantPrimeAck);
104             return;
105         }
106         var list = acDefinition.getElements().values();
107         var inPropertiesMap = list.stream().collect(Collectors.toMap(
108                 AutomationCompositionElementDefinition::getAcElementDefinitionId,
109                 el -> el.getAutomationCompositionElementToscaNodeTemplate().getProperties()));
110         var outPropertiesMap = list.stream().collect(Collectors.toMap(
111                 AutomationCompositionElementDefinition::getAcElementDefinitionId,
112                 AutomationCompositionElementDefinition::getOutProperties));
113         listener.deprime(messageId, new CompositionDto(compositionId, inPropertiesMap, outPropertiesMap));
114     }
115
116     /**
117      * Handle a Participant Sync message.
118      *
119      * @param participantSyncMsg the participantRestart message
120      */
121     public void handleParticipantSync(ParticipantSync participantSyncMsg) {
122
123         if (participantSyncMsg.isDelete()) {
124             deleteScenario(participantSyncMsg);
125             return;
126         }
127
128         if (!participantSyncMsg.getParticipantDefinitionUpdates().isEmpty()) {
129             if (StateChangeResult.TIMEOUT.equals(participantSyncMsg.getStateChangeResult())) {
130                 listener.cleanExecution(participantSyncMsg.getCompositionId(), participantSyncMsg.getMessageId());
131             }
132
133             var list = collectAcElementDefinition(participantSyncMsg.getParticipantDefinitionUpdates());
134             if (!list.isEmpty()) {
135                 cacheProvider.addElementDefinition(participantSyncMsg.getCompositionId(), list,
136                         participantSyncMsg.getRevisionIdComposition());
137             }
138         }
139
140         for (var automationcomposition : participantSyncMsg.getAutomationcompositionList()) {
141             cacheProvider.initializeAutomationComposition(
142                     participantSyncMsg.getCompositionId(), automationcomposition);
143             if (StateChangeResult.TIMEOUT.equals(automationcomposition.getStateChangeResult())) {
144                 for (var element : automationcomposition.getAcElementList()) {
145                     listener.cleanExecution(element.getId(), participantSyncMsg.getMessageId());
146                 }
147             }
148         }
149     }
150
151     private void deleteScenario(ParticipantSync participantSyncMsg) {
152         if (AcTypeState.COMMISSIONED.equals(participantSyncMsg.getState())) {
153             cacheProvider.removeElementDefinition(participantSyncMsg.getCompositionId());
154         }
155         for (var automationcomposition : participantSyncMsg.getAutomationcompositionList()) {
156             cacheProvider.removeAutomationComposition(automationcomposition.getAutomationCompositionId());
157         }
158     }
159 }