2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.clamp.controlloop.runtime.supervision.comm;
25 import java.time.Instant;
26 import java.util.ArrayList;
27 import java.util.List;
29 import lombok.AllArgsConstructor;
30 import org.onap.policy.clamp.controlloop.common.utils.CommonUtils;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUtils;
33 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ServiceTemplateProvider;
34 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
35 import org.onap.policy.models.base.PfModelException;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.stereotype.Component;
44 * This class is used to send ParticipantUpdate messages to participants on DMaaP.
48 public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<ParticipantUpdate> {
50 private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantUpdatePublisher.class);
52 private final ServiceTemplateProvider serviceTemplateProvider;
55 * Send ParticipantUpdate to all Participants.
57 * @param name the ToscaServiceTemplate name
58 * @param version the ToscaServiceTemplate version
60 public void sendComissioningBroadcast(String name, String version) {
61 sendCommissioning(name, version, null, null);
65 * Send ParticipantUpdate to Participant
66 * if participantType and participantId are null then message is broadcast.
68 * @param name the ToscaServiceTemplate name
69 * @param version the ToscaServiceTemplate version
70 * @param participantType the ParticipantType
71 * @param participantId the ParticipantId
73 public boolean sendCommissioning(String name, String version, ToscaConceptIdentifier participantType,
74 ToscaConceptIdentifier participantId) {
75 var message = new ParticipantUpdate();
76 message.setParticipantType(participantType);
77 message.setParticipantId(participantId);
78 message.setTimestamp(Instant.now());
80 ToscaServiceTemplate toscaServiceTemplate = null;
81 Map<String, ToscaNodeType> commonPropertiesMap = null;
83 var list = serviceTemplateProvider.getServiceTemplateList(name, version);
84 if (!list.isEmpty()) {
85 toscaServiceTemplate = list.get(0);
87 serviceTemplateProvider.getCommonOrInstancePropertiesFromNodeTypes(true, toscaServiceTemplate);
89 LOGGER.warn("No tosca service template found, cannot send participantupdate {} {}", name, version);
92 } catch (PfModelException pfme) {
93 LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme);
97 List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
98 for (var toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet()) {
99 if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(),
100 toscaServiceTemplate)) {
101 CommonUtils.prepareParticipantDefinitionUpdate(
102 ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()),
103 toscaInputEntry.getKey(), toscaInputEntry.getValue(),
104 participantDefinitionUpdates, commonPropertiesMap);
108 // Commission the controlloop but sending participantdefinitions to participants
109 message.setParticipantDefinitionUpdates(participantDefinitionUpdates);
110 LOGGER.debug("Participant Update sent {}", message);
116 * Send ParticipantUpdate to Participant after that commissioning has been removed.
118 public void sendDecomisioning() {
119 var message = new ParticipantUpdate();
120 message.setTimestamp(Instant.now());
121 // DeCommission the controlloop but deleting participantdefinitions on participants
122 message.setParticipantDefinitionUpdates(null);
124 LOGGER.debug("Participant Update sent {}", message);