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;
24 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
27 * The Class Message is used to pass protocol messages between Apex components.
29 * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
31 public abstract class Message implements Serializable {
32 private static final int HASH_PRIME = 31;
35 private static final long serialVersionUID = 2271443377544488309L;
37 // Default timeout on server side should be used
38 private static final int DEFAULT_REPLY_TIMEOUT = -1;
40 // The Action or message type of the message
41 private Action action = null;
43 // The artifact key of the artifact to which this message is related
44 private AxArtifactKey targetKey = null;
46 // The data of the message
47 private String messageData = null;
49 // The timeout time for replies in milliseconds
50 private int replyTimeout = DEFAULT_REPLY_TIMEOUT;
53 * Instantiates a new message.
55 * @param action the action or message type of the message
56 * @param targetKey the artifact key of the artifact to which this message relates
58 public Message(final Action action, final AxArtifactKey targetKey) {
59 this(action, targetKey, null);
63 * Instantiates a new message.
65 * @param action the action or message type of the message
66 * @param targetKey the artifact key of the artifact to which this message relates
67 * @param messageData the message data to deliver
69 public Message(final Action action, final AxArtifactKey targetKey, final String messageData) {
71 this.targetKey = targetKey;
72 this.messageData = messageData;
76 * Set the message timeout.
78 * @param replyTimeout the timeout on reply messages in milliseconds
80 public void setReplyTimeout(final int replyTimeout) {
81 this.replyTimeout = replyTimeout;
85 * Sets the message data.
87 * @param messageData the new message data
89 public void setMessageData(final String messageData) {
90 this.messageData = messageData;
94 * Append to the message data.
96 * @param newMessageData the message data
98 public void appendMessageData(final String newMessageData) {
99 if (this.messageData == null) {
100 this.messageData = newMessageData;
102 this.messageData += newMessageData;
107 * Gets the artifact key of the target of the message.
111 public final AxArtifactKey getTarget() {
116 * Gets the artifact key name of the target of the message.
118 * @return the target name
120 public final String getTargetName() {
121 return targetKey.getName();
125 * Gets the action or message type of this message.
129 public final Action getAction() {
134 * Gets the message data.
136 * @return the message data
138 public final String getMessageData() {
146 public boolean equals(final Object object) {
147 if (this == object) {
150 if (object == null || getClass() != object.getClass()) {
154 final Message message = (Message) object;
156 if (action != null ? !action.equals(message.action) : message.action != null) {
159 if (targetKey != null ? !targetKey.equals(message.targetKey) : message.targetKey != null) {
162 return !(messageData != null ? !messageData.equals(message.messageData) : message.messageData != null);
170 public int hashCode() {
171 int result = action != null ? action.hashCode() : 0;
172 result = HASH_PRIME * result + (targetKey != null ? targetKey.hashCode() : 0);
173 result = HASH_PRIME * result + (messageData != null ? messageData.hashCode() : 0);
181 public String toString() {
182 return "Message [action=" + action + ", targetKey=" + targetKey + ", data=" + messageData + "]";
186 * Get the timeout to wait for a reply.
188 * @return the timeout in milliseconds
190 public int getReplyTimeout() {