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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.acm.participant.intermediary.handler;
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.ParticipantState;
34 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
35 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrime;
36 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrimeAck;
37 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRestart;
38 import org.springframework.stereotype.Component;
41 @RequiredArgsConstructor
42 public class AcDefinitionHandler {
44 private final CacheProvider cacheProvider;
45 private final ParticipantMessagePublisher publisher;
46 private final ThreadHandler listener;
49 * Handle a participant Prime message.
51 * @param participantPrimeMsg the ParticipantPrime message
53 public void handlePrime(ParticipantPrime participantPrimeMsg) {
54 if (!participantPrimeMsg.getParticipantDefinitionUpdates().isEmpty()) {
56 List<AutomationCompositionElementDefinition> list = new ArrayList<>();
57 for (var participantDefinition : participantPrimeMsg.getParticipantDefinitionUpdates()) {
58 if (participantDefinition.getParticipantId().equals(cacheProvider.getParticipantId())) {
59 list.addAll(participantDefinition.getAutomationCompositionElementDefinitionList());
62 if (!list.isEmpty()) {
63 cacheProvider.addElementDefinition(participantPrimeMsg.getCompositionId(), list);
64 prime(participantPrimeMsg.getMessageId(), participantPrimeMsg.getCompositionId(), list);
68 deprime(participantPrimeMsg.getMessageId(), participantPrimeMsg.getCompositionId());
72 private void prime(UUID messageId, UUID compositionId, List<AutomationCompositionElementDefinition> list) {
73 var inPropertiesMap = list.stream().collect(Collectors.toMap(
74 AutomationCompositionElementDefinition::getAcElementDefinitionId,
75 el -> el.getAutomationCompositionElementToscaNodeTemplate().getProperties()));
76 var outPropertiesMap = list.stream().collect(Collectors.toMap(
77 AutomationCompositionElementDefinition::getAcElementDefinitionId,
78 AutomationCompositionElementDefinition::getOutProperties));
79 listener.prime(messageId, new CompositionDto(compositionId, inPropertiesMap, outPropertiesMap));
82 private void deprime(UUID messageId, UUID compositionId) {
83 var acElementsDefinitions = cacheProvider.getAcElementsDefinitions().get(compositionId);
84 if (acElementsDefinitions == null) {
85 // this participant does not handle this composition
86 var participantPrimeAck = new ParticipantPrimeAck();
87 participantPrimeAck.setCompositionId(compositionId);
88 participantPrimeAck.setMessage("Already deprimed or never primed");
89 participantPrimeAck.setResult(true);
90 participantPrimeAck.setResponseTo(messageId);
91 participantPrimeAck.setCompositionState(AcTypeState.COMMISSIONED);
92 participantPrimeAck.setStateChangeResult(StateChangeResult.NO_ERROR);
93 participantPrimeAck.setParticipantId(cacheProvider.getParticipantId());
94 participantPrimeAck.setState(ParticipantState.ON_LINE);
95 publisher.sendParticipantPrimeAck(participantPrimeAck);
98 var list = new ArrayList<>(acElementsDefinitions.values());
99 var inPropertiesMap = list.stream().collect(Collectors.toMap(
100 AutomationCompositionElementDefinition::getAcElementDefinitionId,
101 el -> el.getAutomationCompositionElementToscaNodeTemplate().getProperties()));
102 var outPropertiesMap = list.stream().collect(Collectors.toMap(
103 AutomationCompositionElementDefinition::getAcElementDefinitionId,
104 AutomationCompositionElementDefinition::getOutProperties));
105 listener.deprime(messageId, new CompositionDto(compositionId, inPropertiesMap, outPropertiesMap));
109 * Handle a ParticipantRestart message.
111 * @param participantRestartMsg the participantRestart message
113 public void handleParticipantRestart(ParticipantRestart participantRestartMsg) {
114 List<AutomationCompositionElementDefinition> list = new ArrayList<>();
115 for (var participantDefinition : participantRestartMsg.getParticipantDefinitionUpdates()) {
116 list.addAll(participantDefinition.getAutomationCompositionElementDefinitionList());
118 if (!AcTypeState.COMMISSIONED.equals(participantRestartMsg.getState())) {
119 cacheProvider.addElementDefinition(participantRestartMsg.getCompositionId(), list);
122 for (var automationcomposition : participantRestartMsg.getAutomationcompositionList()) {
124 .initializeAutomationComposition(participantRestartMsg.getCompositionId(), automationcomposition);
126 var inPropertiesMap = list.stream().collect(Collectors.toMap(
127 AutomationCompositionElementDefinition::getAcElementDefinitionId,
128 el -> el.getAutomationCompositionElementToscaNodeTemplate().getProperties()));
129 var outPropertiesMap = list.stream().collect(Collectors.toMap(
130 AutomationCompositionElementDefinition::getAcElementDefinitionId,
131 AutomationCompositionElementDefinition::getOutProperties));
133 new CompositionDto(participantRestartMsg.getCompositionId(), inPropertiesMap, outPropertiesMap);
134 listener.restarted(participantRestartMsg.getMessageId(), composition, participantRestartMsg.getState(),
135 participantRestartMsg.getAutomationcompositionList());