c5f33ad97bc06cf3d6b80d8c585824013ac9880a
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2023 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.runtime.supervision.comm;
22
23 import io.micrometer.core.annotation.Timed;
24 import java.time.Instant;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.UUID;
30 import java.util.function.UnaryOperator;
31 import lombok.AllArgsConstructor;
32 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
33 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
34 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
35 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.PropertiesUpdate;
36 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
37 import org.onap.policy.models.base.PfUtils;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
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 PropertiesUpdate messages to participants.
45  */
46 @Component
47 @AllArgsConstructor
48 public class AcElementPropertiesPublisher extends AbstractParticipantPublisher<PropertiesUpdate> {
49
50     private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionDeployPublisher.class);
51
52     /**
53      * Send ACElementPropertiesUpdate to Participant.
54      *
55      * @param automationComposition the AutomationComposition
56      */
57     @Timed(value = "publisher.properties_update",
58             description = "AC Element Properties Update published")
59     public void send(AutomationComposition automationComposition) {
60         Map<UUID, List<AcElementDeploy>> map = new HashMap<>();
61         for (var element : automationComposition.getElements().values()) {
62             var acElementDeploy = new AcElementDeploy();
63             acElementDeploy.setId(element.getId());
64             acElementDeploy.setDefinition(new ToscaConceptIdentifier(element.getDefinition()));
65             acElementDeploy.setOrderedState(DeployOrder.UPDATE);
66             acElementDeploy.setProperties(PfUtils.mapMap(element.getProperties(), UnaryOperator.identity()));
67
68             map.putIfAbsent(element.getParticipantId(), new ArrayList<>());
69             map.get(element.getParticipantId()).add(acElementDeploy);
70         }
71         List<ParticipantDeploy> participantDeploys = new ArrayList<>();
72         for (var entry : map.entrySet()) {
73             var participantDeploy = new ParticipantDeploy();
74             participantDeploy.setParticipantId(entry.getKey());
75             participantDeploy.setAcElementList(entry.getValue());
76             participantDeploys.add(participantDeploy);
77         }
78
79         var propertiesUpdate = new PropertiesUpdate();
80         propertiesUpdate.setCompositionId(automationComposition.getCompositionId());
81         propertiesUpdate.setAutomationCompositionId(automationComposition.getInstanceId());
82         propertiesUpdate.setMessageId(UUID.randomUUID());
83         propertiesUpdate.setTimestamp(Instant.now());
84         propertiesUpdate.setParticipantUpdatesList(participantDeploys);
85
86         LOGGER.debug("AC Element properties update sent {}", propertiesUpdate);
87         super.send(propertiesUpdate);
88     }
89 }