56da7e5a48adf8ea36851b32a52222ce44b0d2f5
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021,2022 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.acm.runtime.supervision.comm;
24
25 import io.micrometer.core.annotation.Timed;
26 import java.time.Instant;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.UUID;
30 import lombok.AllArgsConstructor;
31 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
32 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
33 import org.onap.policy.clamp.models.acm.concepts.ParticipantUpdates;
34 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionUpdate;
35 import org.onap.policy.clamp.models.acm.persistence.provider.ServiceTemplateProvider;
36 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
37 import org.onap.policy.models.base.PfModelException;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
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 AutomationCompositionUpdate messages to participants on DMaaP.
45  */
46 @Component
47 @AllArgsConstructor
48 public class AutomationCompositionUpdatePublisher extends AbstractParticipantPublisher<AutomationCompositionUpdate> {
49
50     private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionUpdatePublisher.class);
51     private final ServiceTemplateProvider serviceTemplateProvider;
52
53     /**
54      * Send AutomationCompositionUpdate to Participant.
55      *
56      * @param automationComposition the AutomationComposition
57      */
58     @Timed(value = "publisher.automation_composition_update",
59             description = "AUTOMATION_COMPOSITION_UPDATE messages published")
60     public void send(AutomationComposition automationComposition) {
61         send(automationComposition, 0);
62     }
63
64     /**
65      * Send AutomationCompositionUpdate to Participant.
66      *
67      * @param automationComposition the AutomationComposition
68      * @param startPhase the Start Phase
69      */
70     @Timed(value = "publisher.automation_composition_update",
71             description = "AUTOMATION_COMPOSITION_UPDATE messages published")
72     public void send(AutomationComposition automationComposition, int startPhase) {
73         var automationCompositionUpdateMsg = new AutomationCompositionUpdate();
74         automationCompositionUpdateMsg.setStartPhase(startPhase);
75         automationCompositionUpdateMsg.setAutomationCompositionId(automationComposition.getKey().asIdentifier());
76         automationCompositionUpdateMsg.setMessageId(UUID.randomUUID());
77         automationCompositionUpdateMsg.setTimestamp(Instant.now());
78         ToscaServiceTemplate toscaServiceTemplate;
79         try {
80             toscaServiceTemplate = serviceTemplateProvider.getAllServiceTemplates().get(0);
81         } catch (PfModelException pfme) {
82             LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme);
83             return;
84         }
85
86         List<ParticipantUpdates> participantUpdates = new ArrayList<>();
87         for (AutomationCompositionElement element : automationComposition.getElements().values()) {
88             AcmUtils.setServiceTemplatePolicyInfo(element, toscaServiceTemplate);
89             AcmUtils.prepareParticipantUpdate(element, participantUpdates);
90         }
91         automationCompositionUpdateMsg.setParticipantUpdatesList(participantUpdates);
92
93         LOGGER.debug("AutomationCompositionUpdate message sent {}", automationCompositionUpdateMsg);
94         super.send(automationCompositionUpdateMsg);
95     }
96 }