blob: e6a90d8b64871389fc2d341082e386add8913317 [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;
24
25/**
26 * A factory class which helps to instantiate LISP reader class.
27 */
28public 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}