Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame^] | 2 | * Copyright 2016 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame^] | 16 | package org.onosproject.store.cluster.messaging.impl; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 17 | |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame^] | 18 | import com.google.common.base.Charsets; |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 19 | import io.netty.buffer.ByteBuf; |
| 20 | import io.netty.channel.ChannelHandlerContext; |
| 21 | import io.netty.handler.codec.ReplayingDecoder; |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.IpAddress.Version; |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 24 | import org.onosproject.store.cluster.messaging.Endpoint; |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame^] | 28 | import java.util.List; |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame] | 29 | |
Ray Milkey | 9f87e51 | 2016-01-05 10:00:22 -0800 | [diff] [blame] | 30 | import static com.google.common.base.Preconditions.checkState; |
Ray Milkey | 9f87e51 | 2016-01-05 10:00:22 -0800 | [diff] [blame] | 31 | |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Decoder for inbound messages. |
| 34 | */ |
| 35 | public class MessageDecoder extends ReplayingDecoder<DecoderState> { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 36 | |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 37 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 38 | |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 39 | private final int correctPreamble; |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 40 | private long messageId; |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 41 | private int preamble; |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 42 | private Version ipVersion; |
| 43 | private IpAddress senderIp; |
| 44 | private int senderPort; |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame] | 45 | private int messageTypeLength; |
| 46 | private String messageType; |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 47 | private int contentLength; |
| 48 | |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 49 | public MessageDecoder(int correctPreamble) { |
| 50 | super(DecoderState.READ_MESSAGE_PREAMBLE); |
| 51 | this.correctPreamble = correctPreamble; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame^] | 55 | @SuppressWarnings("squid:S128") // suppress switch fall through warning |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 56 | protected void decode( |
| 57 | ChannelHandlerContext context, |
| 58 | ByteBuf buffer, |
| 59 | List<Object> out) throws Exception { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 60 | |
Yuta HIGUCHI | 5e8ceb4 | 2014-11-04 17:22:26 -0800 | [diff] [blame] | 61 | switch (state()) { |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 62 | case READ_MESSAGE_PREAMBLE: |
| 63 | preamble = buffer.readInt(); |
| 64 | if (preamble != correctPreamble) { |
| 65 | throw new IllegalStateException("This message had an incorrect preamble."); |
| 66 | } |
| 67 | checkpoint(DecoderState.READ_MESSAGE_ID); |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 68 | case READ_MESSAGE_ID: |
| 69 | messageId = buffer.readLong(); |
| 70 | checkpoint(DecoderState.READ_SENDER_IP_VERSION); |
| 71 | case READ_SENDER_IP_VERSION: |
| 72 | ipVersion = buffer.readByte() == 0x0 ? Version.INET : Version.INET6; |
| 73 | checkpoint(DecoderState.READ_SENDER_IP); |
| 74 | case READ_SENDER_IP: |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 75 | byte[] octets = new byte[IpAddress.byteLength(ipVersion)]; |
| 76 | buffer.readBytes(octets); |
| 77 | senderIp = IpAddress.valueOf(ipVersion, octets); |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 78 | checkpoint(DecoderState.READ_SENDER_PORT); |
| 79 | case READ_SENDER_PORT: |
| 80 | senderPort = buffer.readInt(); |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame] | 81 | checkpoint(DecoderState.READ_MESSAGE_TYPE_LENGTH); |
| 82 | case READ_MESSAGE_TYPE_LENGTH: |
| 83 | messageTypeLength = buffer.readInt(); |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 84 | checkpoint(DecoderState.READ_MESSAGE_TYPE); |
| 85 | case READ_MESSAGE_TYPE: |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame] | 86 | byte[] messageTypeBytes = new byte[messageTypeLength]; |
| 87 | buffer.readBytes(messageTypeBytes); |
| 88 | messageType = new String(messageTypeBytes, Charsets.UTF_8); |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 89 | checkpoint(DecoderState.READ_CONTENT_LENGTH); |
| 90 | case READ_CONTENT_LENGTH: |
| 91 | contentLength = buffer.readInt(); |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 92 | checkpoint(DecoderState.READ_CONTENT); |
| 93 | case READ_CONTENT: |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 94 | //TODO Perform a sanity check on the size before allocating |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 95 | byte[] payload = new byte[contentLength]; |
| 96 | buffer.readBytes(payload); |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 97 | InternalMessage message = new InternalMessage(messageId, |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame^] | 98 | new Endpoint(senderIp, senderPort), |
| 99 | messageType, |
| 100 | payload); |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 101 | out.add(message); |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 102 | checkpoint(DecoderState.READ_MESSAGE_PREAMBLE); |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 103 | break; |
| 104 | default: |
| 105 | checkState(false, "Must not be here"); |
| 106 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 107 | } |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 108 | |
| 109 | @Override |
| 110 | public void exceptionCaught(ChannelHandlerContext context, Throwable cause) { |
| 111 | log.error("Exception inside channel handling pipeline.", cause); |
| 112 | context.close(); |
| 113 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 114 | } |