f1d0f9b44c61fd1a434798521087953554d7d40d
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved.
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.models.acm.messages.kafka.participant;
22
23 import java.time.Instant;
24 import java.util.HashSet;
25 import java.util.Set;
26 import java.util.UUID;
27 import lombok.AccessLevel;
28 import lombok.Getter;
29 import lombok.NonNull;
30 import lombok.Setter;
31 import lombok.ToString;
32
33 /**
34  * Class to represent the base class for various messages that will be exchanged between the ACM runtime and
35  * participants.
36  */
37 @Getter
38 @Setter
39 @ToString
40 public class ParticipantMessage {
41     @Setter(AccessLevel.NONE)
42     private ParticipantMessageType messageType;
43
44     private UUID messageId = UUID.randomUUID();
45
46     /**
47      * Time-stamp, in milliseconds, when the message was created. Defaults to the current time.
48      */
49     private Instant timestamp = Instant.now();
50
51     /**
52      * Participant ID, or {@code null} for messages from participants.
53      */
54     private UUID participantId;
55
56     private UUID replicaId;
57
58     /**
59      * Automation Composition ID, or {@code null} for messages to participants.
60      */
61     private UUID automationCompositionId;
62
63     private UUID compositionId;
64
65     private UUID revisionIdComposition;
66     private UUID revisionIdInstance;
67
68     /**
69      * List of participantId that should receive the message.
70      */
71     private Set<UUID> participantIdList = new HashSet<>();
72
73     /**
74      * Constructor for instantiating a participant message class.
75      *
76      * @param messageType the message type
77      */
78     public ParticipantMessage(final ParticipantMessageType messageType) {
79         this.messageType = messageType;
80     }
81
82     /**
83      * Constructs the object, making a deep copy. Does <i>not</i> copy the request id or the time stamp.
84      *
85      * @param source source from which to copy
86      */
87     public ParticipantMessage(final ParticipantMessage source) {
88         this.messageId = source.messageId;
89         this.timestamp = source.timestamp;
90         this.messageType = source.messageType;
91         this.participantId = source.participantId;
92         this.replicaId = source.replicaId;
93         this.automationCompositionId = source.automationCompositionId;
94         this.compositionId = source.compositionId;
95         this.revisionIdComposition = source.revisionIdComposition;
96         this.revisionIdInstance = source.revisionIdInstance;
97         this.participantIdList = new HashSet<>(source.participantIdList);
98     }
99
100     /**
101      * Determines if this message applies to this participant type.
102      *
103      * @param refParticipantId id of the participant from properties file to match against
104      * @param refReplicaId id of the replica from properties file to match against
105      * @return {@code true} if this message applies to this participant, {@code false} otherwise
106      */
107     public boolean appliesTo(@NonNull final UUID refParticipantId, @NonNull final UUID refReplicaId) {
108         // Broadcast message to specific list of participants
109         // or all participants when participantIdList is empty
110         // filter backward compatible with old ACM-r
111         if (participantIdList != null && !participantIdList.isEmpty()
112                 && !participantIdList.contains(refParticipantId)) {
113             return false;
114         }
115         // Broadcast message to all participants and all replicas or specific participant and all replicas,
116         // filter backward compatible with old ACM-r
117         if ((this.participantId == null)
118                 || (refParticipantId.equals(this.participantId) && this.replicaId == null)) {
119             return true;
120         }
121         // Targeted message at a specific participant and replica
122         return refReplicaId.equals(this.replicaId);
123     }
124 }