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;
28 import java.util.UUID;
29 import lombok.AllArgsConstructor;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUpdates;
33 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.stereotype.Component;
39 * This class is used to send ControlLoopUpdate messages to participants on DMaaP.
43 public class ControlLoopUpdatePublisher extends AbstractParticipantPublisher<ControlLoopUpdate> {
45 private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopUpdatePublisher.class);
48 * Send ControlLoopUpdate to Participant.
50 * @param controlLoop the ControlLoop
52 public void send(ControlLoop controlLoop) {
53 var controlLoopUpdateMsg = new ControlLoopUpdate();
54 controlLoopUpdateMsg.setControlLoopId(controlLoop.getKey().asIdentifier());
55 controlLoopUpdateMsg.setMessageId(UUID.randomUUID());
56 controlLoopUpdateMsg.setTimestamp(Instant.now());
58 List<ParticipantUpdates> participantUpdates = new ArrayList<>();
59 for (ControlLoopElement element : controlLoop.getElements().values()) {
60 prepareParticipantUpdate(element, participantUpdates);
62 controlLoopUpdateMsg.setParticipantUpdatesList(participantUpdates);
64 LOGGER.debug("ControlLoopUpdate message sent {}", controlLoopUpdateMsg);
65 super.send(controlLoopUpdateMsg);
68 private void prepareParticipantUpdate(ControlLoopElement clElement,
69 List<ParticipantUpdates> participantUpdates) {
70 if (participantUpdates.isEmpty()) {
71 participantUpdates.add(getControlLoopElementList(clElement));
73 var participantExists = false;
74 for (ParticipantUpdates participantUpdate : participantUpdates) {
75 if (participantUpdate.getParticipantId().equals(clElement.getParticipantId())) {
76 participantUpdate.getControlLoopElementList().add(clElement);
77 participantExists = true;
80 if (!participantExists) {
81 participantUpdates.add(getControlLoopElementList(clElement));
86 private ParticipantUpdates getControlLoopElementList(ControlLoopElement clElement) {
87 var participantUpdate = new ParticipantUpdates();
88 List<ControlLoopElement> controlLoopElementList = new ArrayList<>();
89 participantUpdate.setParticipantId(clElement.getParticipantId());
90 controlLoopElementList.add(clElement);
91 participantUpdate.setControlLoopElementList(controlLoopElementList);
92 return participantUpdate;