c820bf3f1443cfa1a5078e401c379148671ac1ec
[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.messages.dmaap.participant.ParticipantUpdate;
33 import org.onap.policy.common.utils.coder.Coder;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.common.utils.coder.StandardCoder;
36 import org.onap.policy.models.base.PfModelException;
37 import org.onap.policy.models.provider.PolicyModelsProvider;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.stereotype.Component;
44
45 /**
46  * This class is used to send ParticipantUpdate messages to participants on DMaaP.
47  */
48 @Component
49 @AllArgsConstructor
50 public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<ParticipantUpdate> {
51
52     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantUpdatePublisher.class);
53     private static final String CONTROL_LOOP_ELEMENT = "org.onap.policy.clamp.controlloop.ControlLoopElement";
54     private static final Coder CODER = new StandardCoder();
55     private final PolicyModelsProvider modelsProvider;
56
57     /**
58      * Send ParticipantUpdate to Participant.
59      *
60      * @param participantId the participant Id
61      * @param participantType the participant Type
62      */
63     public void send(ToscaConceptIdentifier participantId, ToscaConceptIdentifier participantType,
64             boolean commissionFlag) {
65         var message = new ParticipantUpdate();
66         message.setParticipantId(participantId);
67         message.setParticipantType(participantType);
68         message.setTimestamp(Instant.now());
69
70         ToscaServiceTemplate toscaServiceTemplate;
71         try {
72             toscaServiceTemplate = modelsProvider.getServiceTemplateList(null, null).get(0);
73         } catch (PfModelException pfme) {
74             LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme);
75             return;
76         }
77
78         List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
79         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate()
80                 .getNodeTemplates().entrySet()) {
81             if (toscaInputEntry.getValue().getType().contains(CONTROL_LOOP_ELEMENT)) {
82                 ToscaConceptIdentifier clParticipantType;
83                 try {
84                     clParticipantType =
85                             CODER.decode(toscaInputEntry.getValue().getProperties().get("participantType").toString(),
86                                     ToscaConceptIdentifier.class);
87                 } catch (CoderException e) {
88                     throw new RuntimeException("cannot get ParticipantType from toscaNodeTemplate", e);
89                 }
90                 prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(),
91                         toscaInputEntry.getValue(), participantDefinitionUpdates);
92             }
93         }
94
95         if (commissionFlag) {
96             // Commission the controlloop but sending participantdefinitions to participants
97             message.setParticipantDefinitionUpdates(participantDefinitionUpdates);
98         } else {
99             // DeCommission the controlloop but deleting participantdefinitions on participants
100             message.setParticipantDefinitionUpdates(null);
101         }
102         LOGGER.debug("Participant Update sent {}", message);
103         super.send(message);
104     }
105
106     private void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey,
107             ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) {
108
109         var clDefinition = new ControlLoopElementDefinition();
110         clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion()));
111         clDefinition.setControlLoopElementToscaNodeTemplate(entryValue);
112         List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>();
113
114         if (participantDefinitionUpdates.isEmpty()) {
115             participantDefinitionUpdates
116                     .add(getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
117         } else {
118             var participantExists = false;
119             for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) {
120                 if (participantDefinitionUpdate.getParticipantType().equals(clParticipantType)) {
121                     participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition);
122                     participantExists = true;
123                 }
124             }
125             if (!participantExists) {
126                 participantDefinitionUpdates.add(
127                         getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
128             }
129         }
130     }
131
132     private ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition,
133             ToscaConceptIdentifier clParticipantType,
134             List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
135         var participantDefinition = new ParticipantDefinition();
136         participantDefinition.setParticipantType(clParticipantType);
137         controlLoopElementDefinitionList.add(clDefinition);
138         participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList);
139         return participantDefinition;
140     }
141 }