blob: 2798cc96305f630537540210d2d902426ed6c655 [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;
19
20import static org.onosproject.lisp.msg.protocols.DefaultLispMapReply.ReplyReader;
21import static org.onosproject.lisp.msg.protocols.DefaultLispMapNotify.NotifyReader;
22import static org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.RegisterReader;
23import static org.onosproject.lisp.msg.protocols.DefaultLispMapRequest.RequestReader;
Yoonseon Hanca814bf2016-09-12 11:37:48 -070024import static org.onosproject.lisp.msg.protocols.DefaultLispEncapsulatedControl.EcmReader;
Jian Lie4f12162016-09-13 00:09:09 +090025
26/**
27 * A factory class which helps to instantiate LISP reader class.
28 */
29public 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 Hanca814bf2016-09-12 11:37:48 -070043 LispType type = LispType.valueOf(
44 (short) (buffer.getByte(0) >> TYPE_SHIFT_BIT));
45
Jian Lie4f12162016-09-13 00:09:09 +090046 switch (type) {
Yoonseon Hanca814bf2016-09-12 11:37:48 -070047 case LISP_MAP_REQUEST:
Jian Lie4f12162016-09-13 00:09:09 +090048 reader = new RequestReader();
49 break;
Yoonseon Hanca814bf2016-09-12 11:37:48 -070050 case LISP_MAP_REPLY:
Jian Lie4f12162016-09-13 00:09:09 +090051 reader = new ReplyReader();
52 break;
Yoonseon Hanca814bf2016-09-12 11:37:48 -070053 case LISP_MAP_REGISTER:
Jian Lie4f12162016-09-13 00:09:09 +090054 reader = new RegisterReader();
55 break;
Yoonseon Hanca814bf2016-09-12 11:37:48 -070056 case LISP_MAP_NOTIFY:
Jian Lie4f12162016-09-13 00:09:09 +090057 reader = new NotifyReader();
58 break;
Yoonseon Hanca814bf2016-09-12 11:37:48 -070059 case LISP_ENCAPSULATED_CONTROL:
60 reader = new EcmReader();
61 break;
62 case UNKNOWN:
63 throw new IllegalArgumentException("Unknown message type: "
64 + type);
Jian Lie4f12162016-09-13 00:09:09 +090065 default:
Yoonseon Hanca814bf2016-09-12 11:37:48 -070066 throw new IllegalArgumentException("Undefined message type: "
67 + type);
Jian Lie4f12162016-09-13 00:09:09 +090068 }
69 return reader;
70 }
71}