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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.clamp.controlloop.runtime.supervision.comm;
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;
44 * This class is used to send ControlLoopUpdate messages to participants on DMaaP.
48 public class ControlLoopUpdatePublisher extends AbstractParticipantPublisher<ControlLoopUpdate> {
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;
56 * Send ControlLoopUpdate to Participant.
58 * @param controlLoop the ControlLoop
60 public void send(ControlLoop controlLoop) {
65 * Send ControlLoopUpdate to Participant.
67 * @param controlLoop the ControlLoop
68 * @param startPhase the Start Phase
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;
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);
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());
98 ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate();
99 toscaTopologyTemplate.setPolicies(toscaServiceTemplate.getToscaTopologyTemplate().getPolicies());
100 toscaServiceTemplateFragment.setToscaTopologyTemplate(toscaTopologyTemplate);
102 toscaServiceTemplateFragment.setDataTypes(toscaServiceTemplate.getDataTypes());
104 element.setToscaServiceTemplateFragment(toscaServiceTemplateFragment);
107 prepareParticipantUpdate(element, participantUpdates);
109 controlLoopUpdateMsg.setParticipantUpdatesList(participantUpdates);
111 LOGGER.debug("ControlLoopUpdate message sent {}", controlLoopUpdateMsg);
112 super.send(controlLoopUpdateMsg);
115 private void prepareParticipantUpdate(ControlLoopElement clElement,
116 List<ParticipantUpdates> participantUpdates) {
117 if (participantUpdates.isEmpty()) {
118 participantUpdates.add(getControlLoopElementList(clElement));
120 var participantExists = false;
121 for (ParticipantUpdates participantUpdate : participantUpdates) {
122 if (participantUpdate.getParticipantId().equals(clElement.getParticipantId())) {
123 participantUpdate.getControlLoopElementList().add(clElement);
124 participantExists = true;
127 if (!participantExists) {
128 participantUpdates.add(getControlLoopElementList(clElement));
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;