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; |
| 24 | |
| 25 | /** |
| 26 | * A factory class which helps to instantiate LISP reader class. |
| 27 | */ |
| 28 | public final class LispMessageReaderFactory { |
| 29 | private static final int TYPE_SHIFT_BIT = 4; |
| 30 | |
| 31 | private LispMessageReaderFactory() {} |
| 32 | |
| 33 | /** |
| 34 | * Obtains corresponding LISP message reader. |
| 35 | * |
| 36 | * @param buffer netty byte buffer |
| 37 | * @return LISP message reader |
| 38 | */ |
| 39 | public static LispMessageReader getReader(ByteBuf buffer) { |
| 40 | LispMessageReader reader; |
| 41 | |
| 42 | int type = buffer.getByte(0) >> TYPE_SHIFT_BIT; |
| 43 | switch (type) { |
| 44 | case 1: |
| 45 | reader = new RequestReader(); |
| 46 | break; |
| 47 | case 2: |
| 48 | reader = new ReplyReader(); |
| 49 | break; |
| 50 | case 3: |
| 51 | reader = new RegisterReader(); |
| 52 | break; |
| 53 | case 4: |
| 54 | reader = new NotifyReader(); |
| 55 | break; |
| 56 | default: |
| 57 | throw new IllegalArgumentException("Unknown LISP message type: " + type); |
| 58 | } |
| 59 | return reader; |
| 60 | } |
| 61 | } |