blob: 5c8550f5f9b87fedd64c2f8b8ae4a81b3c7fd84e [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
tom1d416c52014-09-29 20:55:24 -070016package org.onlab.onos.store.cluster.messaging;
17
Madan Jampani8a895092014-10-17 16:55:50 -070018import java.io.IOException;
19
Madan Jampani890bc352014-10-01 22:35:29 -070020import org.onlab.onos.cluster.NodeId;
tom1d416c52014-09-29 20:55:24 -070021
Yuta HIGUCHI971addc2014-10-07 23:23:17 -070022// TODO: Should payload type be ByteBuffer?
tom1d416c52014-09-29 20:55:24 -070023/**
24 * Base message for cluster-wide communications.
25 */
Madan Jampani890bc352014-10-01 22:35:29 -070026public class ClusterMessage {
tom1d416c52014-09-29 20:55:24 -070027
Madan Jampani890bc352014-10-01 22:35:29 -070028 private final NodeId sender;
tom1d416c52014-09-29 20:55:24 -070029 private final MessageSubject subject;
Madan Jampani53e44e62014-10-07 12:39:51 -070030 private final byte[] payload;
tom1d416c52014-09-29 20:55:24 -070031
32 /**
33 * Creates a cluster message.
34 *
35 * @param subject message subject
36 */
Madan Jampani53e44e62014-10-07 12:39:51 -070037 public ClusterMessage(NodeId sender, MessageSubject subject, byte[] payload) {
Madan Jampani890bc352014-10-01 22:35:29 -070038 this.sender = sender;
tom1d416c52014-09-29 20:55:24 -070039 this.subject = subject;
Madan Jampani890bc352014-10-01 22:35:29 -070040 this.payload = payload;
tom1d416c52014-09-29 20:55:24 -070041 }
42
43 /**
Madan Jampani890bc352014-10-01 22:35:29 -070044 * Returns the id of the controller sending this message.
45 *
46 * @return message sender id.
47 */
48 public NodeId sender() {
49 return sender;
50 }
51
52 /**
tom1d416c52014-09-29 20:55:24 -070053 * Returns the message subject indicator.
54 *
55 * @return message subject
56 */
57 public MessageSubject subject() {
58 return subject;
59 }
60
Madan Jampani890bc352014-10-01 22:35:29 -070061 /**
62 * Returns the message payload.
63 *
64 * @return message payload.
65 */
Madan Jampani53e44e62014-10-07 12:39:51 -070066 public byte[] payload() {
Madan Jampani890bc352014-10-01 22:35:29 -070067 return payload;
tom1d416c52014-09-29 20:55:24 -070068 }
Madan Jampani8a895092014-10-17 16:55:50 -070069
70 /**
71 * Sends a response to the sender.
72 *
73 * @param data payload response.
74 * @throws IOException
75 */
76 public void respond(byte[] data) throws IOException {
77 throw new IllegalStateException("One can only repond to message recived from others.");
78 }
tom1d416c52014-09-29 20:55:24 -070079}