d6246773e9503ff167a46858ce8cc11e1f92e372
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Nordix Foundation.
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.ArrayList;
25 import java.util.List;
26 import java.util.UUID;
27 import java.util.stream.Collectors;
28 import lombok.RequiredArgsConstructor;
29 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto;
30 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
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                 prime(participantPrimeMsg.getMessageId(), participantPrimeMsg.getCompositionId(), list);
61             }
62         } else {
63             // deprime
64             deprime(participantPrimeMsg.getMessageId(), participantPrimeMsg.getCompositionId());
65         }
66     }
67
68     private List<AutomationCompositionElementDefinition> collectAcElementDefinition(
69             List<ParticipantDefinition> participantDefinitionList) {
70         List<AutomationCompositionElementDefinition> list = new ArrayList<>();
71         for (var participantDefinition : participantDefinitionList) {
72             if (participantDefinition.getParticipantId().equals(cacheProvider.getParticipantId())) {
73                 list.addAll(participantDefinition.getAutomationCompositionElementDefinitionList());
74             }
75         }
76         return list;
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 acElementsDefinitions = cacheProvider.getAcElementsDefinitions().get(compositionId);
91         if (acElementsDefinitions == 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 = new ArrayList<>(acElementsDefinitions.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             }
137         }
138
139         for (var automationcomposition : participantSyncMsg.getAutomationcompositionList()) {
140             cacheProvider
141                     .initializeAutomationComposition(participantSyncMsg.getCompositionId(), automationcomposition);
142             if (StateChangeResult.TIMEOUT.equals(automationcomposition.getStateChangeResult())) {
143                 for (var element : automationcomposition.getAcElementList()) {
144                     listener.cleanExecution(element.getId(), participantSyncMsg.getMessageId());
145                 }
146             }
147         }
148     }
149
150     private void deleteScenario(ParticipantSync participantSyncMsg) {
151         if (AcTypeState.COMMISSIONED.equals(participantSyncMsg.getState())) {
152             cacheProvider.removeElementDefinition(participantSyncMsg.getCompositionId());
153         }
154         for (var automationcomposition : participantSyncMsg.getAutomationcompositionList()) {
155             cacheProvider.removeAutomationComposition(automationcomposition.getAutomationCompositionId());
156         }
157     }
158 }