2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.runtime.supervision.comm;
23 import java.time.Instant;
24 import java.util.ArrayList;
25 import java.util.List;
27 import lombok.AllArgsConstructor;
28 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition;
30 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
31 import org.onap.policy.common.utils.coder.Coder;
32 import org.onap.policy.common.utils.coder.CoderException;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34 import org.onap.policy.models.base.PfModelException;
35 import org.onap.policy.models.provider.PolicyModelsProvider;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplates;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.stereotype.Component;
45 * This class is used to send ParticipantUpdate messages to participants on DMaaP.
49 public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<ParticipantUpdate> {
51 private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantUpdatePublisher.class);
52 private static final String CONTROL_LOOP_ELEMENT = "ControlLoopElement";
53 private final PolicyModelsProvider modelsProvider;
54 private static final Coder CODER = new StandardCoder();
57 * Send ParticipantUpdate to Participant.
59 * @param participantId the participant Id
60 * @param participantType the participant Type
62 public void send(ToscaConceptIdentifier participantId, ToscaConceptIdentifier participantType,
63 boolean commissionFlag) {
64 var message = new ParticipantUpdate();
65 message.setParticipantId(participantId);
66 message.setParticipantType(participantType);
67 message.setTimestamp(Instant.now());
69 ToscaServiceTemplate toscaServiceTemplate;
71 toscaServiceTemplate = modelsProvider.getServiceTemplateList(null, null).get(0);
72 } catch (PfModelException pfme) {
73 LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme);
77 List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
78 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry :
79 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet()) {
80 if (toscaInputEntry.getValue().getType().contains(CONTROL_LOOP_ELEMENT)) {
81 ToscaConceptIdentifier clParticipantId;
83 clParticipantId = CODER.decode(
84 toscaInputEntry.getValue().getProperties().get("participant_id").toString(),
85 ToscaConceptIdentifier.class);
86 } catch (CoderException e) {
87 throw new RuntimeException("cannot get ParticipantId from toscaNodeTemplate", e);
89 prepareParticipantDefinitionUpdate(clParticipantId, toscaInputEntry.getKey(),
90 toscaInputEntry.getValue(), participantDefinitionUpdates);
95 // Commission the controlloop but sending participantdefinitions to participants
96 message.setParticipantDefinitionUpdates(participantDefinitionUpdates);
97 message.setToscaServiceTemplate(toscaServiceTemplate);
99 // DeCommission the controlloop but deleting participantdefinitions on participants
100 message.setParticipantDefinitionUpdates(null);
101 message.setToscaServiceTemplate(null);
103 LOGGER.debug("Participant Update sent {}", message);
107 private void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantId, String entryKey,
108 ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) {
110 var clDefinition = new ControlLoopElementDefinition();
111 clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(
112 entryKey, entryValue.getVersion()));
113 clDefinition.setControlLoopElementToscaNodeTemplate(entryValue);
114 List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>();
116 if (participantDefinitionUpdates.isEmpty()) {
117 participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantId,
118 controlLoopElementDefinitionList));
120 boolean participantExists = false;
121 for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) {
122 if (participantDefinitionUpdate.getParticipantId().equals(clParticipantId)) {
123 participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition);
124 participantExists = true;
127 if (!participantExists) {
128 participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantId,
129 controlLoopElementDefinitionList));
134 private ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition,
135 ToscaConceptIdentifier clParticipantId,
136 List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
137 var participantDefinition = new ParticipantDefinition();
138 participantDefinition.setParticipantId(clParticipantId);
139 controlLoopElementDefinitionList.add(clDefinition);
140 participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList);
141 return participantDefinition;