24f60eafc261771555038c0ab0c637268ac6b115
[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.common.utils.CommonUtils;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUpdates;
34 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ServiceTemplateProvider;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
36 import org.onap.policy.models.base.PfModelException;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.stereotype.Component;
41
42 /**
43  * This class is used to send ControlLoopUpdate messages to participants on DMaaP.
44  */
45 @Component
46 @AllArgsConstructor
47 public class ControlLoopUpdatePublisher extends AbstractParticipantPublisher<ControlLoopUpdate> {
48
49     private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopUpdatePublisher.class);
50     private final ServiceTemplateProvider serviceTemplateProvider;
51
52     /**
53      * Send ControlLoopUpdate to Participant.
54      *
55      * @param controlLoop the ControlLoop
56      */
57     public void send(ControlLoop controlLoop) {
58         send(controlLoop, 0);
59     }
60
61     /**
62      * Send ControlLoopUpdate to Participant.
63      *
64      * @param controlLoop the ControlLoop
65      * @param startPhase the Start Phase
66      */
67     public void send(ControlLoop controlLoop, int startPhase) {
68         var controlLoopUpdateMsg = new ControlLoopUpdate();
69         controlLoopUpdateMsg.setStartPhase(startPhase);
70         controlLoopUpdateMsg.setControlLoopId(controlLoop.getKey().asIdentifier());
71         controlLoopUpdateMsg.setMessageId(UUID.randomUUID());
72         controlLoopUpdateMsg.setTimestamp(Instant.now());
73         ToscaServiceTemplate toscaServiceTemplate;
74         try {
75             toscaServiceTemplate = serviceTemplateProvider.getAllServiceTemplates().get(0);
76         } catch (PfModelException pfme) {
77             LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme);
78             return;
79         }
80
81         List<ParticipantUpdates> participantUpdates = new ArrayList<>();
82         for (ControlLoopElement element : controlLoop.getElements().values()) {
83             CommonUtils.setServiceTemplatePolicyInfo(element, toscaServiceTemplate);
84             CommonUtils.prepareParticipantUpdate(element, participantUpdates);
85         }
86         controlLoopUpdateMsg.setParticipantUpdatesList(participantUpdates);
87
88         LOGGER.debug("ControlLoopUpdate message sent {}", controlLoopUpdateMsg);
89         super.send(controlLoopUpdateMsg);
90     }
91 }