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; |
| 19 | |
| 20 | import static org.onosproject.lisp.msg.protocols.DefaultLispMapReply.ReplyReader; |
| 21 | import static org.onosproject.lisp.msg.protocols.DefaultLispMapNotify.NotifyReader; |
| 22 | import static org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.RegisterReader; |
| 23 | import static org.onosproject.lisp.msg.protocols.DefaultLispMapRequest.RequestReader; |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame^] | 24 | import static org.onosproject.lisp.msg.protocols.DefaultLispEncapsulatedControl.EcmReader; |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * A factory class which helps to instantiate LISP reader class. |
| 28 | */ |
| 29 | public final class LispMessageReaderFactory { |
| 30 | private static final int TYPE_SHIFT_BIT = 4; |
| 31 | |
| 32 | private LispMessageReaderFactory() {} |
| 33 | |
| 34 | /** |
| 35 | * Obtains corresponding LISP message reader. |
| 36 | * |
| 37 | * @param buffer netty byte buffer |
| 38 | * @return LISP message reader |
| 39 | */ |
| 40 | public static LispMessageReader getReader(ByteBuf buffer) { |
| 41 | LispMessageReader reader; |
| 42 | |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame^] | 43 | LispType type = LispType.valueOf( |
| 44 | (short) (buffer.getByte(0) >> TYPE_SHIFT_BIT)); |
| 45 | |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 46 | switch (type) { |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame^] | 47 | case LISP_MAP_REQUEST: |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 48 | reader = new RequestReader(); |
| 49 | break; |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame^] | 50 | case LISP_MAP_REPLY: |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 51 | reader = new ReplyReader(); |
| 52 | break; |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame^] | 53 | case LISP_MAP_REGISTER: |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 54 | reader = new RegisterReader(); |
| 55 | break; |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame^] | 56 | case LISP_MAP_NOTIFY: |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 57 | reader = new NotifyReader(); |
| 58 | break; |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame^] | 59 | case LISP_ENCAPSULATED_CONTROL: |
| 60 | reader = new EcmReader(); |
| 61 | break; |
| 62 | case UNKNOWN: |
| 63 | throw new IllegalArgumentException("Unknown message type: " |
| 64 | + type); |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 65 | default: |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame^] | 66 | throw new IllegalArgumentException("Undefined message type: " |
| 67 | + type); |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 68 | } |
| 69 | return reader; |
| 70 | } |
| 71 | } |