5d879dc56d5c8b9bfea25da1fd612310dc126009
[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.Map;
29 import lombok.AllArgsConstructor;
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.controlloop.concepts.ParticipantUtils;
33 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ServiceTemplateProvider;
34 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
35 import org.onap.policy.models.base.PfModelException;
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.ToscaNodeType;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
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
53     private final ServiceTemplateProvider serviceTemplateProvider;
54
55     /**
56      * Send ParticipantUpdate to all Participants.
57      *
58      * @param name the ToscaServiceTemplate name
59      * @param version the ToscaServiceTemplate version
60      */
61     public void sendComissioningBroadcast(String name, String version) {
62         sendCommissioning(name, version, null, null);
63     }
64
65     /**
66      * Send ParticipantUpdate to Participant
67      * if participantType and participantId are null then message is broadcast.
68      *
69      * @param name the ToscaServiceTemplate name
70      * @param version the ToscaServiceTemplate version
71      * @param participantType the ParticipantType
72      * @param participantId the ParticipantId
73      */
74     public boolean sendCommissioning(String name, String version, ToscaConceptIdentifier participantType,
75             ToscaConceptIdentifier participantId) {
76         var message = new ParticipantUpdate();
77         message.setParticipantType(participantType);
78         message.setParticipantId(participantId);
79         message.setTimestamp(Instant.now());
80
81         ToscaServiceTemplate toscaServiceTemplate = null;
82         Map<String, ToscaNodeType> commonPropertiesMap = null;
83         try {
84             var list = serviceTemplateProvider.getServiceTemplateList(name, version);
85             if (!list.isEmpty()) {
86                 toscaServiceTemplate = list.get(0);
87                 commonPropertiesMap =
88                         serviceTemplateProvider.getCommonOrInstancePropertiesFromNodeTypes(true, toscaServiceTemplate);
89             } else {
90                 LOGGER.warn("No tosca service template found, cannot send participantupdate {} {}", name, version);
91                 return false;
92             }
93         } catch (PfModelException pfme) {
94             LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme);
95             return false;
96         }
97
98         List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
99         for (var toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet()) {
100             if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(),
101                     toscaServiceTemplate)) {
102                 var clParticipantType =
103                         ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties());
104                 prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(),
105                         toscaInputEntry.getValue(), participantDefinitionUpdates, commonPropertiesMap);
106             }
107         }
108
109         // Commission the controlloop but sending participantdefinitions to participants
110         message.setParticipantDefinitionUpdates(participantDefinitionUpdates);
111         LOGGER.debug("Participant Update sent {}", message);
112         super.send(message);
113         return true;
114     }
115
116     private void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey,
117             ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates,
118             Map<String, ToscaNodeType> commonPropertiesMap) {
119
120         var clDefinition = new ControlLoopElementDefinition();
121         clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion()));
122         clDefinition.setControlLoopElementToscaNodeTemplate(entryValue);
123         ToscaNodeType nodeType = commonPropertiesMap.get(entryValue.getType());
124         if (nodeType != null) {
125             clDefinition.setCommonPropertiesMap(nodeType.getProperties());
126         }
127
128         List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>();
129
130         if (participantDefinitionUpdates.isEmpty()) {
131             participantDefinitionUpdates
132                     .add(getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
133         } else {
134             var participantExists = false;
135             for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) {
136                 if (participantDefinitionUpdate.getParticipantType().equals(clParticipantType)) {
137                     participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition);
138                     participantExists = true;
139                 }
140             }
141             if (!participantExists) {
142                 participantDefinitionUpdates.add(
143                         getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
144             }
145         }
146     }
147
148     private ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition,
149             ToscaConceptIdentifier clParticipantType,
150             List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
151         var participantDefinition = new ParticipantDefinition();
152         participantDefinition.setParticipantType(clParticipantType);
153         controlLoopElementDefinitionList.add(clDefinition);
154         participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList);
155         return participantDefinition;
156     }
157
158     /**
159      * Send ParticipantUpdate to Participant after that commissioning has been removed.
160      */
161     public void sendDecomisioning() {
162         var message = new ParticipantUpdate();
163         message.setTimestamp(Instant.now());
164         // DeCommission the controlloop but deleting participantdefinitions on participants
165         message.setParticipantDefinitionUpdates(null);
166
167         LOGGER.debug("Participant Update sent {}", message);
168         super.send(message);
169     }
170 }