blob: 814760b7001f8e09049c29deddd4fd6ee2c86a7a [file] [log] [blame]
Jian Lie4f12162016-09-13 00:09:09 +09001/*
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 */
16package org.onosproject.lisp.msg.protocols;
17
18import io.netty.buffer.ByteBuf;
yoonseonbeb41222016-11-11 16:24:16 -080019import org.onlab.util.ByteOperator;
Jian Li5e505c62016-12-05 02:44:24 +090020import org.onosproject.lisp.msg.protocols.DefaultLispEncapsulatedControl.EcmReader;
21import org.onosproject.lisp.msg.protocols.DefaultLispInfoReply.InfoReplyReader;
22import org.onosproject.lisp.msg.protocols.DefaultLispInfoRequest.InfoRequestReader;
23import org.onosproject.lisp.msg.protocols.DefaultLispMapNotify.NotifyReader;
24import org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.RegisterReader;
25import org.onosproject.lisp.msg.protocols.DefaultLispMapReply.ReplyReader;
26import org.onosproject.lisp.msg.protocols.DefaultLispMapRequest.RequestReader;
Jian Lie4f12162016-09-13 00:09:09 +090027
28/**
29 * A factory class which helps to instantiate LISP reader class.
30 */
31public final class LispMessageReaderFactory {
32 private static final int TYPE_SHIFT_BIT = 4;
yoonseonbeb41222016-11-11 16:24:16 -080033 private static final int INFO_REPLY_INDEX = 3;
Jian Lie4f12162016-09-13 00:09:09 +090034
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 Hanca814bf2016-09-12 11:37:48 -070046 LispType type = LispType.valueOf(
Jian Lif11594a2016-10-31 18:16:02 +090047 (short) (buffer.getUnsignedByte(0) >> TYPE_SHIFT_BIT));
Yoonseon Hanca814bf2016-09-12 11:37:48 -070048
Jian Lie4f12162016-09-13 00:09:09 +090049 switch (type) {
Yoonseon Hanca814bf2016-09-12 11:37:48 -070050 case LISP_MAP_REQUEST:
Jian Lie4f12162016-09-13 00:09:09 +090051 reader = new RequestReader();
52 break;
Yoonseon Hanca814bf2016-09-12 11:37:48 -070053 case LISP_MAP_REPLY:
Jian Lie4f12162016-09-13 00:09:09 +090054 reader = new ReplyReader();
55 break;
Yoonseon Hanca814bf2016-09-12 11:37:48 -070056 case LISP_MAP_REGISTER:
Jian Lie4f12162016-09-13 00:09:09 +090057 reader = new RegisterReader();
58 break;
Yoonseon Hanca814bf2016-09-12 11:37:48 -070059 case LISP_MAP_NOTIFY:
Jian Lie4f12162016-09-13 00:09:09 +090060 reader = new NotifyReader();
61 break;
yoonseonbeb41222016-11-11 16:24:16 -080062 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 Hanca814bf2016-09-12 11:37:48 -070071 case LISP_ENCAPSULATED_CONTROL:
72 reader = new EcmReader();
73 break;
74 case UNKNOWN:
75 throw new IllegalArgumentException("Unknown message type: "
76 + type);
Jian Lie4f12162016-09-13 00:09:09 +090077 default:
Yoonseon Hanca814bf2016-09-12 11:37:48 -070078 throw new IllegalArgumentException("Undefined message type: "
79 + type);
Jian Lie4f12162016-09-13 00:09:09 +090080 }
81 return reader;
82 }
83}