Update policy committers in policy/models
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / concepts / PdpMessage.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.pdp.concepts;
23
24 import java.util.UUID;
25 import lombok.AccessLevel;
26 import lombok.EqualsAndHashCode;
27 import lombok.Getter;
28 import lombok.NonNull;
29 import lombok.Setter;
30 import lombok.ToString;
31 import org.onap.policy.models.pdp.enums.PdpMessageType;
32
33 /**
34  * Class to represent the base class for various messages that will be exchanged between
35  * PAP and PDP.
36  *
37  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
38  */
39 @Getter
40 @Setter
41 @ToString
42 @EqualsAndHashCode
43 public class PdpMessage {
44
45     @Setter(AccessLevel.NONE)
46     private PdpMessageType messageName;
47
48     private String requestId = UUID.randomUUID().toString();
49
50     /**
51      * Time-stamp, in milliseconds, when the message was created. Defaults to the current
52      * time.
53      */
54     private long timestampMs = System.currentTimeMillis();
55
56     /**
57      * PDP name, or {@code null} for state-change broadcast messages.
58      */
59     private String name;
60
61     /**
62      * Group associated with the PDP. For state-change messages, this may be {@code null},
63      * if the {@link #name} is provided.  Also {@code null} for topic-check messages.
64      */
65     private String pdpGroup;
66
67     /**
68      * Group associated with the PDP. For state-change messages, this may be {@code null},
69      * if the {@link #name} is provided.  Also {@code null} for topic-check messages.
70      */
71     private String pdpSubgroup;
72
73
74     /**
75      * Constructor for instantiating PdpMessage class with message name.
76      *
77      * @param messageName the message name
78      */
79     public PdpMessage(final PdpMessageType messageName) {
80         this.messageName = messageName;
81     }
82
83     /**
84      * Constructs the object, making a deep copy. Does <i>not</i> copy the request id or
85      * the time stamp.
86      *
87      * @param source source from which to copy
88      */
89     public PdpMessage(final PdpMessage source) {
90         this.messageName = source.messageName;
91         this.name = source.name;
92         this.pdpGroup = source.pdpGroup;
93         this.pdpSubgroup = source.pdpSubgroup;
94     }
95
96     /**
97      * Determines if this message applies to this PDP.
98      *
99      * @param pdpName name of this PDP
100      * @param group group to which this PDP has been assigned, or {@code null} if the PDP
101      *        has not been assigned to a group yet
102      * @param subgroup group to which this PDP has been assigned, or {@code null} if the
103      *        PDP has not been assigned to a subgroup yet
104      * @return {@code true} if this message applies to this PDP, {@code false} otherwise
105      */
106     public boolean appliesTo(@NonNull String pdpName, String group, String subgroup) {
107         if (pdpName.equals(name)) {
108             return true;
109         }
110
111         if (name != null) {
112             // message included a PDP name, but it does not match
113             return false;
114         }
115
116         // message does not provide a PDP name - must be a broadcast
117
118         if (group == null || subgroup == null) {
119             // this PDP has no assignment yet, thus should ignore broadcast messages
120             return false;
121         }
122
123         if (!group.equals(pdpGroup)) {
124             return false;
125         }
126
127         if (pdpSubgroup == null) {
128             // message was broadcast to entire group
129             return true;
130         }
131
132         return subgroup.equals(pdpSubgroup);
133     }
134 }