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