ac5a998b4803bd829670b51da31bcd221ecbac57
[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.acm.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.models.acm.concepts.AutomationComposition;
31 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
32 import org.onap.policy.clamp.models.acm.concepts.ParticipantUpdates;
33 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionUpdate;
34 import org.onap.policy.clamp.models.acm.persistence.provider.ServiceTemplateProvider;
35 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
36 import org.onap.policy.models.base.PfModelException;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.stereotype.Component;
41
42 /**
43  * This class is used to send AutomationCompositionUpdate messages to participants on DMaaP.
44  */
45 @Component
46 @AllArgsConstructor
47 public class AutomationCompositionUpdatePublisher extends AbstractParticipantPublisher<AutomationCompositionUpdate> {
48
49     private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionUpdatePublisher.class);
50     private final ServiceTemplateProvider serviceTemplateProvider;
51
52     /**
53      * Send AutomationCompositionUpdate to Participant.
54      *
55      * @param automationComposition the AutomationComposition
56      */
57     public void send(AutomationComposition automationComposition) {
58         send(automationComposition, 0);
59     }
60
61     /**
62      * Send AutomationCompositionUpdate to Participant.
63      *
64      * @param automationComposition the AutomationComposition
65      * @param startPhase the Start Phase
66      */
67     public void send(AutomationComposition automationComposition, int startPhase) {
68         var automationCompositionUpdateMsg = new AutomationCompositionUpdate();
69         automationCompositionUpdateMsg.setStartPhase(startPhase);
70         automationCompositionUpdateMsg.setAutomationCompositionId(automationComposition.getKey().asIdentifier());
71         automationCompositionUpdateMsg.setMessageId(UUID.randomUUID());
72         automationCompositionUpdateMsg.setTimestamp(Instant.now());
73         ToscaServiceTemplate toscaServiceTemplate;
74         try {
75             toscaServiceTemplate = serviceTemplateProvider.getAllServiceTemplates().get(0);
76         } catch (PfModelException pfme) {
77             LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme);
78             return;
79         }
80
81         List<ParticipantUpdates> participantUpdates = new ArrayList<>();
82         for (AutomationCompositionElement element : automationComposition.getElements().values()) {
83             AcmUtils.setServiceTemplatePolicyInfo(element, toscaServiceTemplate);
84             AcmUtils.prepareParticipantUpdate(element, participantUpdates);
85         }
86         automationCompositionUpdateMsg.setParticipantUpdatesList(participantUpdates);
87
88         LOGGER.debug("AutomationCompositionUpdate message sent {}", automationCompositionUpdateMsg);
89         super.send(automationCompositionUpdateMsg);
90     }
91 }