ad025555b01397421e5e279cef14fb0e30b68f8d
[policy/apex-pdp.git] / core / core-protocols / src / main / java / org / onap / policy / apex / core / protocols / Message.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. 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.apex.core.protocols;
22
23 import java.io.Serializable;
24
25 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
26
27 /**
28  * The Class Message is used to pass protocol messages between Apex components.
29  *
30  * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
31  */
32 public abstract class Message implements Serializable {
33     private static final int HASH_PRIME = 31;
34
35     // Serialization ID
36     private static final long serialVersionUID = 2271443377544488309L;
37
38     // Default timeout on server side should be used
39     private static final int DEFAULT_REPLY_TIMEOUT = -1;
40
41     // The Action or message type of the message
42     private Action action = null;
43
44     // The artifact key of the artifact to which this message is related
45     private AxArtifactKey targetKey = null;
46
47     // The data of the message
48     private String messageData = null;
49
50     // The timeout time for replies in milliseconds
51     private int replyTimeout = DEFAULT_REPLY_TIMEOUT;
52
53     /**
54      * Instantiates a new message.
55      *
56      * @param action the action or message type of the message
57      * @param targetKey the artifact key of the artifact to which this message relates
58      */
59     public Message(final Action action, final AxArtifactKey targetKey) {
60         this(action, targetKey, null);
61     }
62
63     /**
64      * Instantiates a new message.
65      *
66      * @param action the action or message type of the message
67      * @param targetKey the artifact key of the artifact to which this message relates
68      * @param messageData the message data to deliver
69      */
70     public Message(final Action action, final AxArtifactKey targetKey, final String messageData) {
71         this.action = action;
72         this.targetKey = targetKey;
73         this.messageData = messageData;
74     }
75
76     /**
77      * Set the message timeout.
78      *
79      * @param replyTimeout the timeout on reply messages in milliseconds
80      */
81     public void setReplyTimeout(final int replyTimeout) {
82         this.replyTimeout = replyTimeout;
83     }
84
85     /**
86      * Sets the message data.
87      *
88      * @param messageData the new message data
89      */
90     public void setMessageData(final String messageData) {
91         this.messageData = messageData;
92     }
93
94     /**
95      * Append to the message data.
96      *
97      * @param newMessageData the message data
98      */
99     public void appendMessageData(final String newMessageData) {
100         if (this.messageData == null) {
101             this.messageData = newMessageData;
102         } else {
103             this.messageData += newMessageData;
104         }
105     }
106
107     /**
108      * Gets the artifact key of the target of the message.
109      *
110      * @return the target
111      */
112     public final AxArtifactKey getTarget() {
113         return targetKey;
114     }
115
116     /**
117      * Gets the artifact key name of the target of the message.
118      *
119      * @return the target name
120      */
121     public final String getTargetName() {
122         return targetKey.getName();
123     }
124
125     /**
126      * Gets the action or message type of this message.
127      *
128      * @return the action
129      */
130     public final Action getAction() {
131         return action;
132     }
133
134     /**
135      * Gets the message data.
136      *
137      * @return the message data
138      */
139     public final String getMessageData() {
140         return messageData;
141     }
142
143     /**
144      * {@inheritDoc}.
145      */
146     @Override
147     public boolean equals(final Object object) {
148         if (this == object) {
149             return true;
150         }
151         if (object == null || getClass() != object.getClass()) {
152             return false;
153         }
154
155         final Message message = (Message) object;
156
157         if (action != null ? !action.equals(message.action) : message.action != null) {
158             return false;
159         }
160         if (targetKey != null ? !targetKey.equals(message.targetKey) : message.targetKey != null) {
161             return false;
162         }
163         return !(messageData != null ? !messageData.equals(message.messageData) : message.messageData != null);
164
165     }
166
167     /**
168      * {@inheritDoc}.
169      */
170     @Override
171     public int hashCode() {
172         int result = action != null ? action.hashCode() : 0;
173         result = HASH_PRIME * result + (targetKey != null ? targetKey.hashCode() : 0);
174         result = HASH_PRIME * result + (messageData != null ? messageData.hashCode() : 0);
175         return result;
176     }
177
178     /**
179      * {@inheritDoc}.
180      */
181     @Override
182     public String toString() {
183         return "Message [action=" + action + ", targetKey=" + targetKey + ", data=" + messageData + "]";
184     }
185
186     /**
187      * Get the timeout to wait for a reply.
188      *
189      * @return the timeout in milliseconds
190      */
191     public int getReplyTimeout() {
192         return replyTimeout;
193     }
194 }