5edf528b8641999e45834ff18b06e07484dfb697
[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.ArrayList;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28 import lombok.AllArgsConstructor;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition;
32 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
33 import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider;
34 import org.onap.policy.common.utils.coder.Coder;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.models.base.PfModelException;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.stereotype.Component;
44
45 /**
46  * This class is used to send ParticipantUpdate messages to participants on DMaaP.
47  */
48 @Component
49 @AllArgsConstructor
50 public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<ParticipantUpdate> {
51
52     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantUpdatePublisher.class);
53     private static final String CONTROL_LOOP_ELEMENT = "ControlLoopElement";
54     private final CommissioningProvider commissioningProvider;
55     private static final Coder CODER = new StandardCoder();
56
57     /**
58      * Send ParticipantUpdate to Participant.
59      *
60      * @param participantId the participant Id
61      * @param participantType the participant Type
62      */
63     public void send(ToscaConceptIdentifier participantId, ToscaConceptIdentifier participantType) {
64         var message = new ParticipantUpdate();
65         message.setParticipantId(participantId);
66         message.setParticipantType(participantType);
67         message.setTimestamp(Instant.now());
68
69         ToscaServiceTemplate toscaServiceTemplate;
70         try {
71             toscaServiceTemplate = commissioningProvider.getToscaServiceTemplate(null, null);
72         } catch (PfModelException pfme) {
73             LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme);
74             return;
75         }
76
77         List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
78         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry :
79             toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet()) {
80             if (toscaInputEntry.getValue().getType().contains(CONTROL_LOOP_ELEMENT)) {
81                 ToscaConceptIdentifier clParticipantId;
82                 try {
83                     clParticipantId = CODER.decode(
84                             toscaInputEntry.getValue().getProperties().get("participant_id").toString(),
85                             ToscaConceptIdentifier.class);
86                 } catch (CoderException e) {
87                     throw new RuntimeException("cannot get ParticipantId from toscaNodeTemplate", e);
88                 }
89                 prepareParticipantDefinitionUpdate(clParticipantId, toscaInputEntry.getKey(),
90                     toscaInputEntry.getValue(), participantDefinitionUpdates);
91             }
92         }
93
94         message.setParticipantDefinitionUpdates(participantDefinitionUpdates);
95         message.setToscaServiceTemplate(toscaServiceTemplate);
96         LOGGER.debug("Participant Update sent {}", message);
97         super.send(message);
98     }
99
100     private void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantId, String entryKey,
101         ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) {
102
103         var clDefinition = new ControlLoopElementDefinition();
104         clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(
105             entryKey, entryValue.getVersion()));
106         clDefinition.setControlLoopElementToscaNodeTemplate(entryValue);
107         List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>();
108
109         if (participantDefinitionUpdates.isEmpty()) {
110             participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantId,
111                 controlLoopElementDefinitionList));
112         } else {
113             boolean participantExists = false;
114             for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) {
115                 if (participantDefinitionUpdate.getParticipantId().equals(clParticipantId)) {
116                     participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition);
117                     participantExists = true;
118                 }
119             }
120             if (!participantExists) {
121                 participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantId,
122                     controlLoopElementDefinitionList));
123             }
124         }
125     }
126
127     private ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition,
128         ToscaConceptIdentifier clParticipantId,
129         List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
130         ParticipantDefinition participantDefinition = new ParticipantDefinition();
131         participantDefinition.setParticipantId(clParticipantId);
132         controlLoopElementDefinitionList.add(clDefinition);
133         participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList);
134         return participantDefinition;
135     }
136 }