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