6df030d32e77e0ec146097d856ed1fc34728f280
[policy/clamp.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.clamp.controlloop.runtime.supervision.comm;
24
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;
37
38 /**
39  * This class is used to send ControlLoopUpdate messages to participants on DMaaP.
40  */
41 @Component
42 @AllArgsConstructor
43 public class ControlLoopUpdatePublisher extends AbstractParticipantPublisher<ControlLoopUpdate> {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopUpdatePublisher.class);
46
47     /**
48      * Send ControlLoopUpdate to Participant.
49      *
50      * @param controlLoop the ControlLoop
51      */
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());
57
58         List<ParticipantUpdates> participantUpdates = new ArrayList<>();
59         for (ControlLoopElement element : controlLoop.getElements().values()) {
60             prepareParticipantUpdate(element, participantUpdates);
61         }
62         controlLoopUpdateMsg.setParticipantUpdatesList(participantUpdates);
63
64         LOGGER.debug("ControlLoopUpdate message sent {}", controlLoopUpdateMsg);
65         super.send(controlLoopUpdateMsg);
66     }
67
68     private void prepareParticipantUpdate(ControlLoopElement clElement,
69         List<ParticipantUpdates> participantUpdates) {
70         if (participantUpdates.isEmpty()) {
71             participantUpdates.add(getControlLoopElementList(clElement));
72         } else {
73             var participantExists = false;
74             for (ParticipantUpdates participantUpdate : participantUpdates) {
75                 if (participantUpdate.getParticipantId().equals(clElement.getParticipantId())) {
76                     participantUpdate.getControlLoopElementList().add(clElement);
77                     participantExists = true;
78                 }
79             }
80             if (!participantExists) {
81                 participantUpdates.add(getControlLoopElementList(clElement));
82             }
83         }
84     }
85
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;
93     }
94 }