blob: e5382e1f7bfcc450df11b2bf4ee3668279ef1f70 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
Madan Jampaniab6d3112014-10-02 16:30:14 -070016package org.onlab.netty;
17
Madan Jampani86ed0552014-10-03 16:45:42 -070018import io.netty.buffer.ByteBuf;
19import io.netty.channel.ChannelHandlerContext;
20import io.netty.handler.codec.ReplayingDecoder;
21
Madan Jampaniab6d3112014-10-02 16:30:14 -070022import java.util.List;
23
Madan Jampani2e5f87b2015-02-22 10:37:15 -080024import org.onlab.packet.IpAddress;
25import org.onlab.packet.IpAddress.Version;
Madan Jampanic26eede2015-04-16 11:42:16 -070026import org.onosproject.store.cluster.messaging.Endpoint;
Madan Jampani29e5dfd2014-10-07 17:26:25 -070027import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
Madan Jampani49115e92015-03-14 10:43:33 -070030import com.google.common.base.Charsets;
31
Ray Milkey9f87e512016-01-05 10:00:22 -080032import static com.google.common.base.Preconditions.checkState;
33import static org.onlab.util.SonarSuppressionConstants.SONAR_SWITCH_FALLTHROUGH;
34
Madan Jampani938aa432014-10-04 17:37:23 -070035/**
36 * Decoder for inbound messages.
37 */
38public class MessageDecoder extends ReplayingDecoder<DecoderState> {
Madan Jampaniab6d3112014-10-02 16:30:14 -070039
Madan Jampani29e5dfd2014-10-07 17:26:25 -070040 private final Logger log = LoggerFactory.getLogger(getClass());
41
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -080042 private final int correctPreamble;
Madan Jampani2e5f87b2015-02-22 10:37:15 -080043 private long messageId;
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -080044 private int preamble;
Madan Jampani2e5f87b2015-02-22 10:37:15 -080045 private Version ipVersion;
46 private IpAddress senderIp;
47 private int senderPort;
Madan Jampani49115e92015-03-14 10:43:33 -070048 private int messageTypeLength;
49 private String messageType;
Madan Jampani938aa432014-10-04 17:37:23 -070050 private int contentLength;
51
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -080052 public MessageDecoder(int correctPreamble) {
53 super(DecoderState.READ_MESSAGE_PREAMBLE);
54 this.correctPreamble = correctPreamble;
Madan Jampaniab6d3112014-10-02 16:30:14 -070055 }
56
57 @Override
Ray Milkey9f87e512016-01-05 10:00:22 -080058 @java.lang.SuppressWarnings(SONAR_SWITCH_FALLTHROUGH) // suppress switch fall through warning
Madan Jampani86ed0552014-10-03 16:45:42 -070059 protected void decode(
60 ChannelHandlerContext context,
61 ByteBuf buffer,
62 List<Object> out) throws Exception {
Madan Jampaniab6d3112014-10-02 16:30:14 -070063
Yuta HIGUCHI5e8ceb42014-11-04 17:22:26 -080064 switch (state()) {
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -080065 case READ_MESSAGE_PREAMBLE:
66 preamble = buffer.readInt();
67 if (preamble != correctPreamble) {
68 throw new IllegalStateException("This message had an incorrect preamble.");
69 }
70 checkpoint(DecoderState.READ_MESSAGE_ID);
Madan Jampani2e5f87b2015-02-22 10:37:15 -080071 case READ_MESSAGE_ID:
72 messageId = buffer.readLong();
73 checkpoint(DecoderState.READ_SENDER_IP_VERSION);
74 case READ_SENDER_IP_VERSION:
75 ipVersion = buffer.readByte() == 0x0 ? Version.INET : Version.INET6;
76 checkpoint(DecoderState.READ_SENDER_IP);
77 case READ_SENDER_IP:
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -080078 byte[] octets = new byte[IpAddress.byteLength(ipVersion)];
79 buffer.readBytes(octets);
80 senderIp = IpAddress.valueOf(ipVersion, octets);
Madan Jampani2e5f87b2015-02-22 10:37:15 -080081 checkpoint(DecoderState.READ_SENDER_PORT);
82 case READ_SENDER_PORT:
83 senderPort = buffer.readInt();
Madan Jampani49115e92015-03-14 10:43:33 -070084 checkpoint(DecoderState.READ_MESSAGE_TYPE_LENGTH);
85 case READ_MESSAGE_TYPE_LENGTH:
86 messageTypeLength = buffer.readInt();
Madan Jampani2e5f87b2015-02-22 10:37:15 -080087 checkpoint(DecoderState.READ_MESSAGE_TYPE);
88 case READ_MESSAGE_TYPE:
Madan Jampani49115e92015-03-14 10:43:33 -070089 byte[] messageTypeBytes = new byte[messageTypeLength];
90 buffer.readBytes(messageTypeBytes);
91 messageType = new String(messageTypeBytes, Charsets.UTF_8);
Madan Jampani938aa432014-10-04 17:37:23 -070092 checkpoint(DecoderState.READ_CONTENT_LENGTH);
93 case READ_CONTENT_LENGTH:
94 contentLength = buffer.readInt();
Madan Jampani938aa432014-10-04 17:37:23 -070095 checkpoint(DecoderState.READ_CONTENT);
96 case READ_CONTENT:
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -080097 //TODO Perform a sanity check on the size before allocating
Madan Jampani2e5f87b2015-02-22 10:37:15 -080098 byte[] payload = new byte[contentLength];
99 buffer.readBytes(payload);
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -0800100 InternalMessage message = new InternalMessage(messageId,
Madan Jampani2e5f87b2015-02-22 10:37:15 -0800101 new Endpoint(senderIp, senderPort),
102 messageType,
103 payload);
Madan Jampani938aa432014-10-04 17:37:23 -0700104 out.add(message);
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -0800105 checkpoint(DecoderState.READ_MESSAGE_PREAMBLE);
Madan Jampani938aa432014-10-04 17:37:23 -0700106 break;
107 default:
108 checkState(false, "Must not be here");
109 }
Madan Jampaniab6d3112014-10-02 16:30:14 -0700110 }
Madan Jampani29e5dfd2014-10-07 17:26:25 -0700111
112 @Override
113 public void exceptionCaught(ChannelHandlerContext context, Throwable cause) {
114 log.error("Exception inside channel handling pipeline.", cause);
115 context.close();
116 }
Madan Jampaniab6d3112014-10-02 16:30:14 -0700117}