cfc0e393244fbe804fc91da405818be97c098c66
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.runtime.supervision.comm;
22
23 import java.time.Instant;
24 import java.util.LinkedHashMap;
25 import java.util.Map;
26 import java.util.UUID;
27 import lombok.AllArgsConstructor;
28 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
29 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
30 import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider;
31 import org.onap.policy.models.base.PfModelException;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.stereotype.Component;
36
37 /**
38  * This class is used to send ParticipantUpdate messages to participants on DMaaP.
39  */
40 @Component
41 @AllArgsConstructor
42 public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<ParticipantUpdate> {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantUpdatePublisher.class);
45
46     private final CommissioningProvider commissioningProvider;
47
48     /**
49      * Send ParticipantUpdate to Participant.
50      *
51      * @param participantId the participant Id
52      * @param participantType the participant Type
53      */
54     public void send(ToscaConceptIdentifier participantId, ToscaConceptIdentifier participantType) {
55         var message = new ParticipantUpdate();
56         message.setParticipantId(participantId);
57         message.setParticipantType(participantType);
58         message.setTimestamp(Instant.now());
59
60         var clDefinition = new ControlLoopElementDefinition();
61         clDefinition.setId(UUID.randomUUID());
62
63         try {
64             clDefinition.setControlLoopElementToscaServiceTemplate(
65                     commissioningProvider.getToscaServiceTemplate(null, null));
66         } catch (PfModelException pfme) {
67             LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme);
68             return;
69         }
70
71         Map<UUID, ControlLoopElementDefinition> controlLoopElementDefinitionMap = new LinkedHashMap<>();
72         controlLoopElementDefinitionMap.put(UUID.randomUUID(), clDefinition);
73
74         Map<String, Map<UUID, ControlLoopElementDefinition>> participantDefinitionUpdateMap = new LinkedHashMap<>();
75         participantDefinitionUpdateMap.put(participantId.toString(), controlLoopElementDefinitionMap);
76         message.setParticipantDefinitionUpdateMap(participantDefinitionUpdateMap);
77
78         LOGGER.debug("Participant Update sent {}", message);
79         super.send(message);
80     }
81 }