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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.core.protocols;
23 import java.io.Serializable;
25 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
28 * The Class Message is used to pass protocol messages between Apex components.
30 * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
32 public abstract class Message implements Serializable {
33 private static final int HASH_PRIME = 31;
36 private static final long serialVersionUID = 2271443377544488309L;
38 // Default timeout on server side should be used
39 private static final int DEFAULT_REPLY_TIMEOUT = -1;
41 // The Action or message type of the message
42 private Action action = null;
44 // The artifact key of the artifact to which this message is related
45 private AxArtifactKey targetKey = null;
47 // The data of the message
48 private String messageData = null;
50 // The timeout time for replies in milliseconds
51 private int replyTimeout = DEFAULT_REPLY_TIMEOUT;
54 * Instantiates a new message.
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
59 public Message(final Action action, final AxArtifactKey targetKey) {
60 this(action, targetKey, null);
64 * Instantiates a new message.
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
70 public Message(final Action action, final AxArtifactKey targetKey, final String messageData) {
72 this.targetKey = targetKey;
73 this.messageData = messageData;
77 * Set the message timeout.
79 * @param replyTimeout the timeout on reply messages in milliseconds
81 public void setReplyTimeout(final int replyTimeout) {
82 this.replyTimeout = replyTimeout;
86 * Sets the message data.
88 * @param messageData the new message data
90 public void setMessageData(final String messageData) {
91 this.messageData = messageData;
95 * Append to the message data.
97 * @param newMessageData the message data
99 public void appendMessageData(final String newMessageData) {
100 if (this.messageData == null) {
101 this.messageData = newMessageData;
103 this.messageData += newMessageData;
108 * Gets the artifact key of the target of the message.
112 public final AxArtifactKey getTarget() {
117 * Gets the artifact key name of the target of the message.
119 * @return the target name
121 public final String getTargetName() {
122 return targetKey.getName();
126 * Gets the action or message type of this message.
130 public final Action getAction() {
135 * Gets the message data.
137 * @return the message data
139 public final String getMessageData() {
147 public boolean equals(final Object object) {
148 if (this == object) {
151 if (object == null || getClass() != object.getClass()) {
155 final Message message = (Message) object;
157 if (action != null ? !action.equals(message.action) : message.action != null) {
160 if (targetKey != null ? !targetKey.equals(message.targetKey) : message.targetKey != null) {
163 return !(messageData != null ? !messageData.equals(message.messageData) : message.messageData != null);
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);
182 public String toString() {
183 return "Message [action=" + action + ", targetKey=" + targetKey + ", data=" + messageData + "]";
187 * Get the timeout to wait for a reply.
189 * @return the timeout in milliseconds
191 public int getReplyTimeout() {