blob: 35fe975f819c78d53c909f897ae89fcbadd4f30e [file] [log] [blame]
Jian Li10a09062016-07-26 23:58:50 +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
Jian Li26069e22016-08-10 22:00:52 +090018import io.netty.buffer.ByteBuf;
19import org.onosproject.lisp.msg.exceptions.LispParseError;
Jian Lia7b394d2016-08-21 23:11:46 +090020import org.onosproject.lisp.msg.exceptions.LispReaderException;
Jian Liedc5db12016-08-23 17:30:19 +090021import org.onosproject.lisp.msg.exceptions.LispWriterException;
Jian Li10a09062016-07-26 23:58:50 +090022import org.onosproject.lisp.msg.types.LispAfiAddress;
23
Jian Liedc5db12016-08-23 17:30:19 +090024import static org.onosproject.lisp.msg.types.LispAfiAddress.AfiAddressWriter;
25
Jian Li10a09062016-07-26 23:58:50 +090026/**
27 * LISP EID record section which is part of LISP map request message.
28 */
Jian Li631e62a2016-08-03 22:42:00 +090029public final class LispEidRecord {
Jian Li10a09062016-07-26 23:58:50 +090030
31 private final byte maskLength;
32 private final LispAfiAddress prefix;
33
34 /**
35 * Initializes LispEidRecord with mask length and EID prefix.
36 *
37 * @param maskLength mask length
38 * @param prefix EID prefix
39 */
40 public LispEidRecord(byte maskLength, LispAfiAddress prefix) {
41 this.maskLength = maskLength;
42 this.prefix = prefix;
43 }
44
45 /**
46 * Obtains mask length of the EID Record.
47 *
48 * @return mask length of the EID Record
49 */
50 public byte getMaskLength() {
51 return maskLength;
52 }
53
54 /**
55 * Obtains EID prefix.
56 *
57 * @return EID prefix
58 */
59 public LispAfiAddress getPrefix() {
60 return prefix;
61 }
Jian Li26069e22016-08-10 22:00:52 +090062
63 /**
Jian Liedc5db12016-08-23 17:30:19 +090064 * A LISP message reader for EidRecord portion.
Jian Li26069e22016-08-10 22:00:52 +090065 */
Jian Liedc5db12016-08-23 17:30:19 +090066 public static final class EidRecordReader implements LispMessageReader<LispEidRecord> {
Jian Li26069e22016-08-10 22:00:52 +090067
Jian Li47671902016-08-11 01:18:18 +090068 private static final int RESERVED_SKIP_LENGTH = 1;
69
Jian Li26069e22016-08-10 22:00:52 +090070 @Override
Jian Lia7b394d2016-08-21 23:11:46 +090071 public LispEidRecord readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
Jian Li47671902016-08-11 01:18:18 +090072
73 // let's skip the reserved field
74 byteBuf.skipBytes(RESERVED_SKIP_LENGTH);
75
76 short maskLength = (short) byteBuf.readUnsignedShort();
77
Jian Lia7b394d2016-08-21 23:11:46 +090078 LispAfiAddress prefix = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);
Jian Li47671902016-08-11 01:18:18 +090079
80 return new LispEidRecord((byte) maskLength, prefix);
Jian Li26069e22016-08-10 22:00:52 +090081 }
82 }
Jian Liedc5db12016-08-23 17:30:19 +090083
84 /**
85 * A LISP message writer for EidRecord portion.
86 */
87 public static final class EidRecordWriter implements LispMessageWriter<LispEidRecord> {
88
89 private static final int UNUSED_ZERO = 0;
90
91 @Override
92 public void writeTo(ByteBuf byteBuf, LispEidRecord message) throws LispWriterException {
93
94 // fill zero into reserved field
95 byteBuf.writeByte((short) UNUSED_ZERO);
96
97 // mask length
98 byteBuf.writeByte(message.getMaskLength());
99
100 // EID prefix AFI with EID prefix
101 AfiAddressWriter afiAddressWriter = new AfiAddressWriter();
102 afiAddressWriter.writeTo(byteBuf, message.getPrefix());
103 }
104 }
Jian Li10a09062016-07-26 23:58:50 +0900105}