Jian Li | 10a0906 | 2016-07-26 23:58:50 +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 | |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 18 | import io.netty.buffer.ByteBuf; |
| 19 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 20 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 21 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 22 | import org.onosproject.lisp.msg.types.LispAfiAddress; |
| 23 | |
Jian Li | d4e6370 | 2016-08-30 18:29:20 +0900 | [diff] [blame] | 24 | import static com.google.common.base.Preconditions.checkNotNull; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 25 | import static org.onosproject.lisp.msg.types.LispAfiAddress.AfiAddressWriter; |
| 26 | |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 27 | /** |
| 28 | * LISP EID record section which is part of LISP map request message. |
| 29 | */ |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 30 | public final class LispEidRecord { |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 31 | |
| 32 | private final byte maskLength; |
| 33 | private final LispAfiAddress prefix; |
| 34 | |
| 35 | /** |
| 36 | * Initializes LispEidRecord with mask length and EID prefix. |
| 37 | * |
| 38 | * @param maskLength mask length |
| 39 | * @param prefix EID prefix |
| 40 | */ |
| 41 | public LispEidRecord(byte maskLength, LispAfiAddress prefix) { |
| 42 | this.maskLength = maskLength; |
Jian Li | d4e6370 | 2016-08-30 18:29:20 +0900 | [diff] [blame] | 43 | |
| 44 | checkNotNull(prefix, "Must specify an address prefix"); |
| 45 | |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 46 | this.prefix = prefix; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Obtains mask length of the EID Record. |
| 51 | * |
| 52 | * @return mask length of the EID Record |
| 53 | */ |
| 54 | public byte getMaskLength() { |
| 55 | return maskLength; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Obtains EID prefix. |
| 60 | * |
| 61 | * @return EID prefix |
| 62 | */ |
| 63 | public LispAfiAddress getPrefix() { |
| 64 | return prefix; |
| 65 | } |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 66 | |
| 67 | /** |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 68 | * A LISP message reader for EidRecord portion. |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 69 | */ |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 70 | public static final class EidRecordReader implements LispMessageReader<LispEidRecord> { |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 71 | |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 72 | private static final int RESERVED_SKIP_LENGTH = 1; |
| 73 | |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 74 | @Override |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 75 | public LispEidRecord readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException { |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 76 | |
| 77 | // let's skip the reserved field |
| 78 | byteBuf.skipBytes(RESERVED_SKIP_LENGTH); |
| 79 | |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 80 | // mask length -> 8 bits |
| 81 | short maskLength = byteBuf.readUnsignedByte(); |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 82 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 83 | LispAfiAddress prefix = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf); |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 84 | |
| 85 | return new LispEidRecord((byte) maskLength, prefix); |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 86 | } |
| 87 | } |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * A LISP message writer for EidRecord portion. |
| 91 | */ |
| 92 | public static final class EidRecordWriter implements LispMessageWriter<LispEidRecord> { |
| 93 | |
| 94 | private static final int UNUSED_ZERO = 0; |
| 95 | |
| 96 | @Override |
| 97 | public void writeTo(ByteBuf byteBuf, LispEidRecord message) throws LispWriterException { |
| 98 | |
| 99 | // fill zero into reserved field |
| 100 | byteBuf.writeByte((short) UNUSED_ZERO); |
| 101 | |
| 102 | // mask length |
| 103 | byteBuf.writeByte(message.getMaskLength()); |
| 104 | |
| 105 | // EID prefix AFI with EID prefix |
| 106 | AfiAddressWriter afiAddressWriter = new AfiAddressWriter(); |
| 107 | afiAddressWriter.writeTo(byteBuf, message.getPrefix()); |
| 108 | } |
| 109 | } |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 110 | } |