d5dc4a6d0e4d3beca8192a8cb74f1b75e94f0396
[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.List;
26 import java.util.Map;
27 import lombok.AllArgsConstructor;
28 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition;
30 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
31 import org.onap.policy.common.utils.coder.Coder;
32 import org.onap.policy.common.utils.coder.CoderException;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34 import org.onap.policy.models.base.PfModelException;
35 import org.onap.policy.models.provider.PolicyModelsProvider;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplates;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.stereotype.Component;
43
44 /**
45  * This class is used to send ParticipantUpdate messages to participants on DMaaP.
46  */
47 @Component
48 @AllArgsConstructor
49 public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<ParticipantUpdate> {
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantUpdatePublisher.class);
52     private static final String CONTROL_LOOP_ELEMENT = "ControlLoopElement";
53     private final PolicyModelsProvider modelsProvider;
54     private static final Coder CODER = new StandardCoder();
55
56     /**
57      * Send ParticipantUpdate to Participant.
58      *
59      * @param participantId the participant Id
60      * @param participantType the participant Type
61      */
62     public void send(ToscaConceptIdentifier participantId, ToscaConceptIdentifier participantType,
63                      boolean commissionFlag) {
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 = modelsProvider.getServiceTemplateList(null, null).get(0);
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         if (commissionFlag) {
95             // Commission the controlloop but sending participantdefinitions to participants
96             message.setParticipantDefinitionUpdates(participantDefinitionUpdates);
97             message.setToscaServiceTemplate(toscaServiceTemplate);
98         } else {
99             // DeCommission the controlloop but deleting participantdefinitions on participants
100             message.setParticipantDefinitionUpdates(null);
101             message.setToscaServiceTemplate(null);
102         }
103         LOGGER.debug("Participant Update sent {}", message);
104         super.send(message);
105     }
106
107     private void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantId, String entryKey,
108         ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) {
109
110         var clDefinition = new ControlLoopElementDefinition();
111         clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(
112             entryKey, entryValue.getVersion()));
113         clDefinition.setControlLoopElementToscaNodeTemplate(entryValue);
114         List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>();
115
116         if (participantDefinitionUpdates.isEmpty()) {
117             participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantId,
118                 controlLoopElementDefinitionList));
119         } else {
120             boolean participantExists = false;
121             for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) {
122                 if (participantDefinitionUpdate.getParticipantId().equals(clParticipantId)) {
123                     participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition);
124                     participantExists = true;
125                 }
126             }
127             if (!participantExists) {
128                 participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantId,
129                     controlLoopElementDefinitionList));
130             }
131         }
132     }
133
134     private ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition,
135         ToscaConceptIdentifier clParticipantId,
136         List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
137         var participantDefinition = new ParticipantDefinition();
138         participantDefinition.setParticipantId(clParticipantId);
139         controlLoopElementDefinitionList.add(clDefinition);
140         participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList);
141         return participantDefinition;
142     }
143 }