blob: 5a8fd6cb3612392d2c580eb194e224bc94508cb8 [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
Jian Lib25fcb42017-05-12 02:12:49 +090038 // Cache the hash code for the string, default to 0
39 private final int hash;
40
Jian Li10a09062016-07-26 23:58:50 +090041 /**
42 * Initializes LispEidRecord with mask length and EID prefix.
43 *
44 * @param maskLength mask length
45 * @param prefix EID prefix
46 */
47 public LispEidRecord(byte maskLength, LispAfiAddress prefix) {
48 this.maskLength = maskLength;
Jian Lid4e63702016-08-30 18:29:20 +090049
50 checkNotNull(prefix, "Must specify an address prefix");
51
Jian Li10a09062016-07-26 23:58:50 +090052 this.prefix = prefix;
Jian Lib25fcb42017-05-12 02:12:49 +090053 this.hash = 31 * 17 + Objects.hashCode(maskLength, prefix);
Jian Li10a09062016-07-26 23:58:50 +090054 }
55
56 /**
57 * Obtains mask length of the EID Record.
58 *
59 * @return mask length of the EID Record
60 */
61 public byte getMaskLength() {
62 return maskLength;
63 }
64
65 /**
66 * Obtains EID prefix.
67 *
68 * @return EID prefix
69 */
70 public LispAfiAddress getPrefix() {
71 return prefix;
72 }
Jian Li26069e22016-08-10 22:00:52 +090073
Jian Li1118c122016-11-01 21:58:15 +090074 @Override
75 public String toString() {
76 return toStringHelper(this)
77 .add("maskLength", maskLength)
78 .add("prefix", prefix).toString();
79 }
80
81 @Override
82 public boolean equals(Object o) {
83 if (this == o) {
84 return true;
85 }
86 if (o == null || getClass() != o.getClass()) {
87 return false;
88 }
89
90 LispEidRecord that = (LispEidRecord) o;
91 return Objects.equal(maskLength, that.maskLength) &&
92 Objects.equal(prefix, that.prefix);
93 }
94
95 @Override
96 public int hashCode() {
Jian Lib25fcb42017-05-12 02:12:49 +090097 return hash;
Jian Li1118c122016-11-01 21:58:15 +090098 }
99
Jian Li26069e22016-08-10 22:00:52 +0900100 /**
Jian Liedc5db12016-08-23 17:30:19 +0900101 * A LISP message reader for EidRecord portion.
Jian Li26069e22016-08-10 22:00:52 +0900102 */
Jian Liedc5db12016-08-23 17:30:19 +0900103 public static final class EidRecordReader implements LispMessageReader<LispEidRecord> {
Jian Li26069e22016-08-10 22:00:52 +0900104
Jian Li47671902016-08-11 01:18:18 +0900105 private static final int RESERVED_SKIP_LENGTH = 1;
106
Jian Li26069e22016-08-10 22:00:52 +0900107 @Override
Jian Lia7b394d2016-08-21 23:11:46 +0900108 public LispEidRecord readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
Jian Li47671902016-08-11 01:18:18 +0900109
110 // let's skip the reserved field
111 byteBuf.skipBytes(RESERVED_SKIP_LENGTH);
112
Jian Lib26d3502016-08-31 01:43:24 +0900113 // mask length -> 8 bits
114 short maskLength = byteBuf.readUnsignedByte();
Jian Li47671902016-08-11 01:18:18 +0900115
Jian Li5e505c62016-12-05 02:44:24 +0900116 LispAfiAddress prefix = new AfiAddressReader().readFrom(byteBuf);
Jian Li47671902016-08-11 01:18:18 +0900117
118 return new LispEidRecord((byte) maskLength, prefix);
Jian Li26069e22016-08-10 22:00:52 +0900119 }
120 }
Jian Liedc5db12016-08-23 17:30:19 +0900121
122 /**
123 * A LISP message writer for EidRecord portion.
124 */
125 public static final class EidRecordWriter implements LispMessageWriter<LispEidRecord> {
126
127 private static final int UNUSED_ZERO = 0;
128
129 @Override
130 public void writeTo(ByteBuf byteBuf, LispEidRecord message) throws LispWriterException {
131
132 // fill zero into reserved field
133 byteBuf.writeByte((short) UNUSED_ZERO);
134
135 // mask length
136 byteBuf.writeByte(message.getMaskLength());
137
138 // EID prefix AFI with EID prefix
139 AfiAddressWriter afiAddressWriter = new AfiAddressWriter();
140 afiAddressWriter.writeTo(byteBuf, message.getPrefix());
141 }
142 }
Jian Li10a09062016-07-26 23:58:50 +0900143}