Fix sonar issue in PdpGroups
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / concepts / PdpMessage.java
index 6160027..5d0359c 100644 (file)
@@ -24,12 +24,14 @@ package org.onap.policy.models.pdp.concepts;
 import java.util.UUID;
 import lombok.AccessLevel;
 import lombok.Getter;
+import lombok.NonNull;
 import lombok.Setter;
 import lombok.ToString;
 import org.onap.policy.models.pdp.enums.PdpMessageType;
 
 /**
- * Class to represent the base class for various messages that will ve exchanged between PAP and PDP.
+ * Class to represent the base class for various messages that will be exchanged between
+ * PAP and PDP.
  *
  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
  */
@@ -49,6 +51,24 @@ public class PdpMessage {
      */
     private long timestampMs = System.currentTimeMillis();
 
+    /**
+     * PDP name, or {@code null} for state-change broadcast messages.
+     */
+    private String name;
+
+    /**
+     * Group associated with the PDP. For state-change messages, this may be {@code null},
+     * if the {@link #name} is provided.
+     */
+    private String pdpGroup;
+
+    /**
+     * Group associated with the PDP. For state-change messages, this may be {@code null},
+     * if the {@link #name} is provided.
+     */
+    private String pdpSubgroup;
+
+
     /**
      * Constructor for instantiating PdpMessage class with message name.
      *
@@ -57,4 +77,56 @@ public class PdpMessage {
     public PdpMessage(final PdpMessageType messageName) {
         this.messageName = messageName;
     }
+
+    /**
+     * Constructs the object, making a deep copy. Does <i>not</i> copy the request id or
+     * the time stamp.
+     *
+     * @param source source from which to copy
+     */
+    public PdpMessage(final PdpMessage source) {
+        this.messageName = source.messageName;
+        this.name = source.name;
+        this.pdpGroup = source.pdpGroup;
+        this.pdpSubgroup = source.pdpSubgroup;
+    }
+
+    /**
+     * Determines if this message applies to this PDP.
+     *
+     * @param pdpName name of this PDP
+     * @param group group to which this PDP has been assigned, or {@code null} if the PDP
+     *        has not been assigned to a group yet
+     * @param subgroup group to which this PDP has been assigned, or {@code null} if the
+     *        PDP has not been assigned to a subgroup yet
+     * @return {@code true} if this message applies to this PDP, {@code false} otherwise
+     */
+    public boolean appliesTo(@NonNull String pdpName, String group, String subgroup) {
+        if (pdpName.equals(name)) {
+            return true;
+        }
+
+        if (name != null) {
+            // message included a PDP name, but it does not match
+            return false;
+        }
+
+        // message does not provide a PDP name - must be a broadcast
+
+        if (group == null || subgroup == null) {
+            // this PDP has no assignment yet, thus should ignore broadcast messages
+            return false;
+        }
+
+        if (!group.equals(pdpGroup)) {
+            return false;
+        }
+
+        if (pdpSubgroup == null) {
+            // message was broadcast to entire group
+            return true;
+        }
+
+        return subgroup.equals(pdpSubgroup);
+    }
 }