fe46297f1d39ebb07a4abe6177cf4542c6174982
[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 lombok.AllArgsConstructor;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUtils;
32 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.provider.PolicyModelsProvider;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
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 ParticipantUpdate messages to participants on DMaaP.
44  */
45 @Component
46 @AllArgsConstructor
47 public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<ParticipantUpdate> {
48
49     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantUpdatePublisher.class);
50
51     private final PolicyModelsProvider modelsProvider;
52
53     /**
54      * Send ParticipantUpdate to Participant.
55      *
56      * @param participantId the participant Id
57      * @param participantType the participant Type
58      */
59     public void send(ToscaConceptIdentifier participantId, ToscaConceptIdentifier participantType,
60             boolean commissionFlag) {
61         var message = new ParticipantUpdate();
62         message.setParticipantId(participantId);
63         message.setParticipantType(participantType);
64         message.setTimestamp(Instant.now());
65
66         ToscaServiceTemplate toscaServiceTemplate = null;
67         try {
68             var list = modelsProvider.getServiceTemplateList(null, null);
69             if (!list.isEmpty()) {
70                 toscaServiceTemplate = list.get(0);
71             }
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         if (toscaServiceTemplate != null) {
79             for (var toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet()) {
80                 if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(),
81                         toscaServiceTemplate)) {
82                     var clParticipantType =
83                             ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties());
84                     prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(),
85                             toscaInputEntry.getValue(), participantDefinitionUpdates);
86                 }
87             }
88         }
89
90         if (commissionFlag) {
91             // Commission the controlloop but sending participantdefinitions to participants
92             message.setParticipantDefinitionUpdates(participantDefinitionUpdates);
93         } else {
94             // DeCommission the controlloop but deleting participantdefinitions on participants
95             message.setParticipantDefinitionUpdates(null);
96         }
97         LOGGER.debug("Participant Update sent {}", message);
98         super.send(message);
99     }
100
101     private void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey,
102             ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) {
103
104         var clDefinition = new ControlLoopElementDefinition();
105         clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion()));
106         clDefinition.setControlLoopElementToscaNodeTemplate(entryValue);
107         List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>();
108
109         if (participantDefinitionUpdates.isEmpty()) {
110             participantDefinitionUpdates
111                     .add(getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
112         } else {
113             var participantExists = false;
114             for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) {
115                 if (participantDefinitionUpdate.getParticipantType().equals(clParticipantType)) {
116                     participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition);
117                     participantExists = true;
118                 }
119             }
120             if (!participantExists) {
121                 participantDefinitionUpdates.add(
122                         getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
123             }
124         }
125     }
126
127     private ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition,
128             ToscaConceptIdentifier clParticipantType,
129             List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
130         var participantDefinition = new ParticipantDefinition();
131         participantDefinition.setParticipantType(clParticipantType);
132         controlLoopElementDefinitionList.add(clDefinition);
133         participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList);
134         return participantDefinition;
135     }
136 }