0f6cc7cbc2f86636c473e5ac7c002aa0c4ae53a7
[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.messages.dmaap.participant.ParticipantUpdate;
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.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 PolicyModelsProvider modelsProvider;
54
55     /**
56      * Send ParticipantUpdate to Participant.
57      *
58      */
59     public void send(Map<String, ToscaNodeType> commonPropertiesMap, boolean commissionFlag) {
60         var message = new ParticipantUpdate();
61         message.setTimestamp(Instant.now());
62
63         ToscaServiceTemplate toscaServiceTemplate = null;
64         try {
65             var list = modelsProvider.getServiceTemplateList(null, null);
66             if (!list.isEmpty()) {
67                 toscaServiceTemplate = list.get(0);
68             }
69         } catch (PfModelException pfme) {
70             LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme);
71             return;
72         }
73
74         List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
75         if (toscaServiceTemplate != null) {
76             for (var toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet()) {
77                 if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(),
78                         toscaServiceTemplate)) {
79                     var clParticipantType =
80                             ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties());
81                     prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(),
82                             toscaInputEntry.getValue(), participantDefinitionUpdates, commonPropertiesMap);
83                 }
84             }
85         }
86
87         if (commissionFlag) {
88             // Commission the controlloop but sending participantdefinitions to participants
89             message.setParticipantDefinitionUpdates(participantDefinitionUpdates);
90         } else {
91             // DeCommission the controlloop but deleting participantdefinitions on participants
92             message.setParticipantDefinitionUpdates(null);
93         }
94         LOGGER.debug("Participant Update sent {}", message);
95         super.send(message);
96     }
97
98     private void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey,
99             ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates,
100             Map<String, ToscaNodeType> commonPropertiesMap) {
101
102         var clDefinition = new ControlLoopElementDefinition();
103         clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion()));
104         clDefinition.setControlLoopElementToscaNodeTemplate(entryValue);
105         ToscaNodeType nodeType = commonPropertiesMap.get(entryValue.getType());
106         if (nodeType != null) {
107             clDefinition.setCommonPropertiesMap(nodeType.getProperties());
108         }
109
110         List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>();
111
112         if (participantDefinitionUpdates.isEmpty()) {
113             participantDefinitionUpdates
114                     .add(getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
115         } else {
116             var participantExists = false;
117             for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) {
118                 if (participantDefinitionUpdate.getParticipantType().equals(clParticipantType)) {
119                     participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition);
120                     participantExists = true;
121                 }
122             }
123             if (!participantExists) {
124                 participantDefinitionUpdates.add(
125                         getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
126             }
127         }
128     }
129
130     private ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition,
131             ToscaConceptIdentifier clParticipantType,
132             List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
133         var participantDefinition = new ParticipantDefinition();
134         participantDefinition.setParticipantType(clParticipantType);
135         controlLoopElementDefinitionList.add(clDefinition);
136         participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList);
137         return participantDefinition;
138     }
139 }