blob: 41d758368a615440e6b1913d353ac65b6b7f9894 [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;
Jian Li1118c122016-11-01 21:58:15 +090019import com.google.common.base.Objects;
Jian Li26069e22016-08-10 22:00:52 +090020import org.onosproject.lisp.msg.exceptions.LispParseError;
Jian Lia7b394d2016-08-21 23:11:46 +090021import org.onosproject.lisp.msg.exceptions.LispReaderException;
Jian Liedc5db12016-08-23 17:30:19 +090022import org.onosproject.lisp.msg.exceptions.LispWriterException;
Jian Li10a09062016-07-26 23:58:50 +090023import org.onosproject.lisp.msg.types.LispAfiAddress;
Jian Li5e505c62016-12-05 02:44:24 +090024import org.onosproject.lisp.msg.types.LispAfiAddress.AfiAddressReader;
25import org.onosproject.lisp.msg.types.LispAfiAddress.AfiAddressWriter;
Jian Li10a09062016-07-26 23:58:50 +090026
Jian Li1118c122016-11-01 21:58:15 +090027import static com.google.common.base.MoreObjects.toStringHelper;
Jian Lid4e63702016-08-30 18:29:20 +090028import static com.google.common.base.Preconditions.checkNotNull;
Jian Liedc5db12016-08-23 17:30:19 +090029
Jian Li10a09062016-07-26 23:58:50 +090030/**
31 * LISP EID record section which is part of LISP map request message.
32 */
Jian Li631e62a2016-08-03 22:42:00 +090033public final class LispEidRecord {
Jian Li10a09062016-07-26 23:58:50 +090034
35 private final byte maskLength;
36 private final LispAfiAddress prefix;
37
38 /**
39 * Initializes LispEidRecord with mask length and EID prefix.
40 *
41 * @param maskLength mask length
42 * @param prefix EID prefix
43 */
44 public LispEidRecord(byte maskLength, LispAfiAddress prefix) {
45 this.maskLength = maskLength;
Jian Lid4e63702016-08-30 18:29:20 +090046
47 checkNotNull(prefix, "Must specify an address prefix");
48
Jian Li10a09062016-07-26 23:58:50 +090049 this.prefix = prefix;
50 }
51
52 /**
53 * Obtains mask length of the EID Record.
54 *
55 * @return mask length of the EID Record
56 */
57 public byte getMaskLength() {
58 return maskLength;
59 }
60
61 /**
62 * Obtains EID prefix.
63 *
64 * @return EID prefix
65 */
66 public LispAfiAddress getPrefix() {
67 return prefix;
68 }
Jian Li26069e22016-08-10 22:00:52 +090069
Jian Li1118c122016-11-01 21:58:15 +090070 @Override
71 public String toString() {
72 return toStringHelper(this)
73 .add("maskLength", maskLength)
74 .add("prefix", prefix).toString();
75 }
76
77 @Override
78 public boolean equals(Object o) {
79 if (this == o) {
80 return true;
81 }
82 if (o == null || getClass() != o.getClass()) {
83 return false;
84 }
85
86 LispEidRecord that = (LispEidRecord) o;
87 return Objects.equal(maskLength, that.maskLength) &&
88 Objects.equal(prefix, that.prefix);
89 }
90
91 @Override
92 public int hashCode() {
93 return Objects.hashCode(maskLength, prefix);
94 }
95
Jian Li26069e22016-08-10 22:00:52 +090096 /**
Jian Liedc5db12016-08-23 17:30:19 +090097 * A LISP message reader for EidRecord portion.
Jian Li26069e22016-08-10 22:00:52 +090098 */
Jian Liedc5db12016-08-23 17:30:19 +090099 public static final class EidRecordReader implements LispMessageReader<LispEidRecord> {
Jian Li26069e22016-08-10 22:00:52 +0900100
Jian Li47671902016-08-11 01:18:18 +0900101 private static final int RESERVED_SKIP_LENGTH = 1;
102
Jian Li26069e22016-08-10 22:00:52 +0900103 @Override
Jian Lia7b394d2016-08-21 23:11:46 +0900104 public LispEidRecord readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
Jian Li47671902016-08-11 01:18:18 +0900105
106 // let's skip the reserved field
107 byteBuf.skipBytes(RESERVED_SKIP_LENGTH);
108
Jian Lib26d3502016-08-31 01:43:24 +0900109 // mask length -> 8 bits
110 short maskLength = byteBuf.readUnsignedByte();
Jian Li47671902016-08-11 01:18:18 +0900111
Jian Li5e505c62016-12-05 02:44:24 +0900112 LispAfiAddress prefix = new AfiAddressReader().readFrom(byteBuf);
Jian Li47671902016-08-11 01:18:18 +0900113
114 return new LispEidRecord((byte) maskLength, prefix);
Jian Li26069e22016-08-10 22:00:52 +0900115 }
116 }
Jian Liedc5db12016-08-23 17:30:19 +0900117
118 /**
119 * A LISP message writer for EidRecord portion.
120 */
121 public static final class EidRecordWriter implements LispMessageWriter<LispEidRecord> {
122
123 private static final int UNUSED_ZERO = 0;
124
125 @Override
126 public void writeTo(ByteBuf byteBuf, LispEidRecord message) throws LispWriterException {
127
128 // fill zero into reserved field
129 byteBuf.writeByte((short) UNUSED_ZERO);
130
131 // mask length
132 byteBuf.writeByte(message.getMaskLength());
133
134 // EID prefix AFI with EID prefix
135 AfiAddressWriter afiAddressWriter = new AfiAddressWriter();
136 afiAddressWriter.writeTo(byteBuf, message.getPrefix());
137 }
138 }
Jian Li10a09062016-07-26 23:58:50 +0900139}