d68a643d2bc5149a81da4edec983891c8ef43c9b
[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         var controlLoopUpdateMsg = new ControlLoopUpdate();
62         controlLoopUpdateMsg.setControlLoopId(controlLoop.getKey().asIdentifier());
63         controlLoopUpdateMsg.setMessageId(UUID.randomUUID());
64         controlLoopUpdateMsg.setTimestamp(Instant.now());
65         ToscaServiceTemplate toscaServiceTemplate;
66         try {
67             toscaServiceTemplate = modelsProvider.getServiceTemplateList(null, null).get(0);
68         } catch (PfModelException pfme) {
69             LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme);
70             return;
71         }
72
73         List<ParticipantUpdates> participantUpdates = new ArrayList<>();
74         for (ControlLoopElement element : controlLoop.getElements().values()) {
75             ToscaNodeTemplate toscaNodeTemplate = toscaServiceTemplate
76                 .getToscaTopologyTemplate().getNodeTemplates().get(element.getDefinition().getName());
77             // If the ControlLoopElement has policy_type_id or policy_id, identify it as a PolicyControlLoopElement
78             // and pass respective PolicyTypes or Policies as part of toscaServiceTemplateFragment
79             if ((toscaNodeTemplate.getProperties().get(POLICY_TYPE_ID) != null)
80                     || (toscaNodeTemplate.getProperties().get(POLICY_ID) != null)) {
81                 // ControlLoopElement for policy framework, send policies and policyTypes to participants
82                 if ((toscaServiceTemplate.getPolicyTypes() != null)
83                         || (toscaServiceTemplate.getToscaTopologyTemplate().getPolicies() != null)) {
84                     ToscaServiceTemplate toscaServiceTemplateFragment = new ToscaServiceTemplate();
85                     toscaServiceTemplateFragment.setPolicyTypes(toscaServiceTemplate.getPolicyTypes());
86
87                     ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate();
88                     toscaTopologyTemplate.setPolicies(toscaServiceTemplate.getToscaTopologyTemplate().getPolicies());
89                     toscaServiceTemplateFragment.setToscaTopologyTemplate(toscaTopologyTemplate);
90
91                     toscaServiceTemplateFragment.setDataTypes(toscaServiceTemplate.getDataTypes());
92
93                     element.setToscaServiceTemplateFragment(toscaServiceTemplateFragment);
94                 }
95             }
96             prepareParticipantUpdate(element, participantUpdates);
97         }
98         controlLoopUpdateMsg.setParticipantUpdatesList(participantUpdates);
99
100         LOGGER.debug("ControlLoopUpdate message sent {}", controlLoopUpdateMsg);
101         super.send(controlLoopUpdateMsg);
102     }
103
104     private void prepareParticipantUpdate(ControlLoopElement clElement,
105         List<ParticipantUpdates> participantUpdates) {
106         if (participantUpdates.isEmpty()) {
107             participantUpdates.add(getControlLoopElementList(clElement));
108         } else {
109             var participantExists = false;
110             for (ParticipantUpdates participantUpdate : participantUpdates) {
111                 if (participantUpdate.getParticipantId().equals(clElement.getParticipantId())) {
112                     participantUpdate.getControlLoopElementList().add(clElement);
113                     participantExists = true;
114                 }
115             }
116             if (!participantExists) {
117                 participantUpdates.add(getControlLoopElementList(clElement));
118             }
119         }
120     }
121
122     private ParticipantUpdates getControlLoopElementList(ControlLoopElement clElement) {
123         var participantUpdate = new ParticipantUpdates();
124         List<ControlLoopElement> controlLoopElementList = new ArrayList<>();
125         participantUpdate.setParticipantId(clElement.getParticipantId());
126         controlLoopElementList.add(clElement);
127         participantUpdate.setControlLoopElementList(controlLoopElementList);
128         return participantUpdate;
129     }
130 }