74d987240095ea348b80cac90ed50f9d0b4d5945
[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.UUID;
29 import lombok.AllArgsConstructor;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUpdates;
33 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
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.ToscaNodeTemplate;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
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 ControlLoopUpdate messages to participants on DMaaP.
45  */
46 @Component
47 @AllArgsConstructor
48 public class ControlLoopUpdatePublisher extends AbstractParticipantPublisher<ControlLoopUpdate> {
49
50     private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopUpdatePublisher.class);
51     private static final String POLICY_TYPE_ID = "policy_type_id";
52     private static final String POLICY_ID = "policy_id";
53     private final PolicyModelsProvider modelsProvider;
54
55     /**
56      * Send ControlLoopUpdate to Participant.
57      *
58      * @param controlLoop the ControlLoop
59      */
60     public void send(ControlLoop controlLoop) {
61         send(controlLoop, 0);
62     }
63
64     /**
65      * Send ControlLoopUpdate to Participant.
66      *
67      * @param controlLoop the ControlLoop
68      * @param startPhase the Start Phase
69      */
70     public void send(ControlLoop controlLoop, int startPhase) {
71         var controlLoopUpdateMsg = new ControlLoopUpdate();
72         controlLoopUpdateMsg.setStartPhase(startPhase);
73         controlLoopUpdateMsg.setControlLoopId(controlLoop.getKey().asIdentifier());
74         controlLoopUpdateMsg.setMessageId(UUID.randomUUID());
75         controlLoopUpdateMsg.setTimestamp(Instant.now());
76         ToscaServiceTemplate toscaServiceTemplate;
77         try {
78             toscaServiceTemplate = modelsProvider.getServiceTemplateList(null, null).get(0);
79         } catch (PfModelException pfme) {
80             LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme);
81             return;
82         }
83
84         List<ParticipantUpdates> participantUpdates = new ArrayList<>();
85         for (ControlLoopElement element : controlLoop.getElements().values()) {
86             ToscaNodeTemplate toscaNodeTemplate = toscaServiceTemplate
87                 .getToscaTopologyTemplate().getNodeTemplates().get(element.getDefinition().getName());
88             // If the ControlLoopElement has policy_type_id or policy_id, identify it as a PolicyControlLoopElement
89             // and pass respective PolicyTypes or Policies as part of toscaServiceTemplateFragment
90             if ((toscaNodeTemplate.getProperties().get(POLICY_TYPE_ID) != null)
91                     || (toscaNodeTemplate.getProperties().get(POLICY_ID) != null)) {
92                 // ControlLoopElement for policy framework, send policies and policyTypes to participants
93                 if ((toscaServiceTemplate.getPolicyTypes() != null)
94                         || (toscaServiceTemplate.getToscaTopologyTemplate().getPolicies() != null)) {
95                     ToscaServiceTemplate toscaServiceTemplateFragment = new ToscaServiceTemplate();
96                     toscaServiceTemplateFragment.setPolicyTypes(toscaServiceTemplate.getPolicyTypes());
97
98                     ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate();
99                     toscaTopologyTemplate.setPolicies(toscaServiceTemplate.getToscaTopologyTemplate().getPolicies());
100                     toscaServiceTemplateFragment.setToscaTopologyTemplate(toscaTopologyTemplate);
101
102                     toscaServiceTemplateFragment.setDataTypes(toscaServiceTemplate.getDataTypes());
103
104                     element.setToscaServiceTemplateFragment(toscaServiceTemplateFragment);
105                 }
106             }
107             prepareParticipantUpdate(element, participantUpdates);
108         }
109         controlLoopUpdateMsg.setParticipantUpdatesList(participantUpdates);
110
111         LOGGER.debug("ControlLoopUpdate message sent {}", controlLoopUpdateMsg);
112         super.send(controlLoopUpdateMsg);
113     }
114
115     private void prepareParticipantUpdate(ControlLoopElement clElement,
116         List<ParticipantUpdates> participantUpdates) {
117         if (participantUpdates.isEmpty()) {
118             participantUpdates.add(getControlLoopElementList(clElement));
119         } else {
120             var participantExists = false;
121             for (ParticipantUpdates participantUpdate : participantUpdates) {
122                 if (participantUpdate.getParticipantId().equals(clElement.getParticipantId())) {
123                     participantUpdate.getControlLoopElementList().add(clElement);
124                     participantExists = true;
125                 }
126             }
127             if (!participantExists) {
128                 participantUpdates.add(getControlLoopElementList(clElement));
129             }
130         }
131     }
132
133     private ParticipantUpdates getControlLoopElementList(ControlLoopElement clElement) {
134         var participantUpdate = new ParticipantUpdates();
135         List<ControlLoopElement> controlLoopElementList = new ArrayList<>();
136         participantUpdate.setParticipantId(clElement.getParticipantId());
137         controlLoopElementList.add(clElement);
138         participantUpdate.setControlLoopElementList(controlLoopElementList);
139         return participantUpdate;
140     }
141 }