Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | */ |
| 16 | package org.onosproject.lisp.msg.protocols; |
| 17 | |
| 18 | import io.netty.buffer.ByteBuf; |
yoonseon | beb4122 | 2016-11-11 16:24:16 -0800 | [diff] [blame] | 19 | import org.onlab.util.ByteOperator; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 20 | import org.onosproject.lisp.msg.protocols.DefaultLispEncapsulatedControl.EcmReader; |
| 21 | import org.onosproject.lisp.msg.protocols.DefaultLispInfoReply.InfoReplyReader; |
| 22 | import org.onosproject.lisp.msg.protocols.DefaultLispInfoRequest.InfoRequestReader; |
| 23 | import org.onosproject.lisp.msg.protocols.DefaultLispMapNotify.NotifyReader; |
| 24 | import org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.RegisterReader; |
| 25 | import org.onosproject.lisp.msg.protocols.DefaultLispMapReply.ReplyReader; |
| 26 | import org.onosproject.lisp.msg.protocols.DefaultLispMapRequest.RequestReader; |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * A factory class which helps to instantiate LISP reader class. |
| 30 | */ |
| 31 | public final class LispMessageReaderFactory { |
| 32 | private static final int TYPE_SHIFT_BIT = 4; |
yoonseon | beb4122 | 2016-11-11 16:24:16 -0800 | [diff] [blame] | 33 | private static final int INFO_REPLY_INDEX = 3; |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 34 | |
| 35 | private LispMessageReaderFactory() {} |
| 36 | |
| 37 | /** |
| 38 | * Obtains corresponding LISP message reader. |
| 39 | * |
| 40 | * @param buffer netty byte buffer |
| 41 | * @return LISP message reader |
| 42 | */ |
| 43 | public static LispMessageReader getReader(ByteBuf buffer) { |
| 44 | LispMessageReader reader; |
| 45 | |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 46 | LispType type = LispType.valueOf( |
Jian Li | f11594a | 2016-10-31 18:16:02 +0900 | [diff] [blame] | 47 | (short) (buffer.getUnsignedByte(0) >> TYPE_SHIFT_BIT)); |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 48 | |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 49 | switch (type) { |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 50 | case LISP_MAP_REQUEST: |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 51 | reader = new RequestReader(); |
| 52 | break; |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 53 | case LISP_MAP_REPLY: |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 54 | reader = new ReplyReader(); |
| 55 | break; |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 56 | case LISP_MAP_REGISTER: |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 57 | reader = new RegisterReader(); |
| 58 | break; |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 59 | case LISP_MAP_NOTIFY: |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 60 | reader = new NotifyReader(); |
| 61 | break; |
yoonseon | beb4122 | 2016-11-11 16:24:16 -0800 | [diff] [blame] | 62 | case LISP_INFO: |
| 63 | boolean isInfoReply = ByteOperator.getBit( |
| 64 | (byte) buffer.getUnsignedByte(0), INFO_REPLY_INDEX); |
| 65 | if (isInfoReply) { |
| 66 | reader = new InfoReplyReader(); |
| 67 | } else { |
| 68 | reader = new InfoRequestReader(); |
| 69 | } |
| 70 | break; |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 71 | case LISP_ENCAPSULATED_CONTROL: |
| 72 | reader = new EcmReader(); |
| 73 | break; |
| 74 | case UNKNOWN: |
| 75 | throw new IllegalArgumentException("Unknown message type: " |
| 76 | + type); |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 77 | default: |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 78 | throw new IllegalArgumentException("Undefined message type: " |
| 79 | + type); |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 80 | } |
| 81 | return reader; |
| 82 | } |
| 83 | } |