c6788f12ccf86a7a1e6edfbe90b7f34386c00430
[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.common.utils.CommonUtils;
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.ToscaNodeType;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.stereotype.Component;
42
43 /**
44  * This class is used to send ParticipantUpdate messages to participants on DMaaP.
45  */
46 @Component
47 @AllArgsConstructor
48 public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<ParticipantUpdate> {
49
50     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantUpdatePublisher.class);
51
52     private final ServiceTemplateProvider serviceTemplateProvider;
53
54     /**
55      * Send ParticipantUpdate to all Participants.
56      *
57      * @param name the ToscaServiceTemplate name
58      * @param version the ToscaServiceTemplate version
59      */
60     public void sendComissioningBroadcast(String name, String version) {
61         sendCommissioning(name, version, null, null);
62     }
63
64     /**
65      * Send ParticipantUpdate to Participant
66      * if participantType and participantId are null then message is broadcast.
67      *
68      * @param name the ToscaServiceTemplate name
69      * @param version the ToscaServiceTemplate version
70      * @param participantType the ParticipantType
71      * @param participantId the ParticipantId
72      */
73     public boolean sendCommissioning(String name, String version, ToscaConceptIdentifier participantType,
74             ToscaConceptIdentifier participantId) {
75         var message = new ParticipantUpdate();
76         message.setParticipantType(participantType);
77         message.setParticipantId(participantId);
78         message.setTimestamp(Instant.now());
79
80         ToscaServiceTemplate toscaServiceTemplate = null;
81         Map<String, ToscaNodeType> commonPropertiesMap = null;
82         try {
83             var list = serviceTemplateProvider.getServiceTemplateList(name, version);
84             if (!list.isEmpty()) {
85                 toscaServiceTemplate = list.get(0);
86                 commonPropertiesMap =
87                         serviceTemplateProvider.getCommonOrInstancePropertiesFromNodeTypes(true, toscaServiceTemplate);
88             } else {
89                 LOGGER.warn("No tosca service template found, cannot send participantupdate {} {}", name, version);
90                 return false;
91             }
92         } catch (PfModelException pfme) {
93             LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme);
94             return false;
95         }
96
97         List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
98         for (var toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet()) {
99             if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(),
100                     toscaServiceTemplate)) {
101                 CommonUtils.prepareParticipantDefinitionUpdate(
102                     ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()),
103                     toscaInputEntry.getKey(), toscaInputEntry.getValue(),
104                     participantDefinitionUpdates, commonPropertiesMap);
105             }
106         }
107
108         // Commission the controlloop but sending participantdefinitions to participants
109         message.setParticipantDefinitionUpdates(participantDefinitionUpdates);
110         LOGGER.debug("Participant Update sent {}", message);
111         super.send(message);
112         return true;
113     }
114
115     /**
116      * Send ParticipantUpdate to Participant after that commissioning has been removed.
117      */
118     public void sendDecomisioning() {
119         var message = new ParticipantUpdate();
120         message.setTimestamp(Instant.now());
121         // DeCommission the controlloop but deleting participantdefinitions on participants
122         message.setParticipantDefinitionUpdates(null);
123
124         LOGGER.debug("Participant Update sent {}", message);
125         super.send(message);
126     }
127 }