blob: 5892653d3a8d60e99305a3a73ba085142602fb41 [file] [log] [blame]
Jian Li99f83ef2016-11-03 19:14:25 +01001/*
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.types;
17
18import com.google.common.collect.ImmutableList;
19import com.google.common.collect.Lists;
20import io.netty.buffer.ByteBuf;
21import org.onosproject.lisp.msg.exceptions.LispParseError;
22import org.onosproject.lisp.msg.exceptions.LispReaderException;
23import org.onosproject.lisp.msg.exceptions.LispWriterException;
24import org.onosproject.lisp.msg.types.LispTeRecord.TeRecordWriter;
25
26import java.util.List;
27import java.util.Objects;
28
29import static com.google.common.base.MoreObjects.toStringHelper;
Jian Li5e505c62016-12-05 02:44:24 +090030import static org.onosproject.lisp.msg.types.LispLcafAddress.COMMON_HEADER_SIZE;
Jian Li99f83ef2016-11-03 19:14:25 +010031
32/**
33 * Traffic Engineering (TE) type LCAF address class.
34 * <p>
Jian Lid6483cc2016-12-12 02:26:13 +090035 * Traffic Engineering type is defined in draft-ietf-lisp-lcaf-22
36 * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-22#page-16
Jian Li89c9ca92016-11-11 04:09:02 +090037 *
Jian Li99f83ef2016-11-03 19:14:25 +010038 * <pre>
39 * {@literal
40 * 0 1 2 3
41 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * | AFI = 16387 | Rsvd1 | Flags |
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jian Li89c9ca92016-11-11 04:09:02 +090045 * | Type = 10 | Rsvd2 | Length |
Jian Li99f83ef2016-11-03 19:14:25 +010046 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 * | Rsvd3 |L|P|S| AFI = x |
48 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 * | Reencap Hop 1 ... |
50 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 * | Rsvd3 |L|P|S| AFI = x |
52 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 * | Reencap Hop k ... |
54 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 * }</pre>
56 */
57public final class LispTeLcafAddress extends LispLcafAddress {
58
59 private final List<LispTeRecord> records;
60
61 /**
62 * Initializes Traffic Engineering type LCAF address.
63 *
64 * @param records a collection of Re-encapsulated RLOC addresses
65 */
Jian Lief0f7232016-11-15 19:55:46 +090066 private LispTeLcafAddress(List<LispTeRecord> records) {
67 super(LispCanonicalAddressFormatEnum.TRAFFIC_ENGINEERING);
Jian Li99f83ef2016-11-03 19:14:25 +010068 this.records = records;
69 }
70
71 /**
72 * Obtains a collection of TE records.
73 *
74 * @return a collection of TE records
75 */
76 public List<LispTeRecord> getTeRecords() {
77 return ImmutableList.copyOf(records);
78 }
79
80 @Override
81 public int hashCode() {
82 return Objects.hash(records);
83 }
84
85 @Override
86 public boolean equals(Object obj) {
87 if (this == obj) {
88 return true;
89 }
90
91 if (obj instanceof LispTeLcafAddress) {
92 final LispTeLcafAddress other = (LispTeLcafAddress) obj;
93 return Objects.equals(records, other.records);
94 }
95 return false;
96 }
97
98 @Override
99 public String toString() {
100 return toStringHelper(this)
101 .add("TE records", records).toString();
102 }
103
Jian Li3a99e712016-12-16 21:23:16 +0900104 public static final class TeAddressBuilder
105 extends LcafAddressBuilder<TeAddressBuilder> {
Jian Li99f83ef2016-11-03 19:14:25 +0100106 private List<LispTeRecord> records;
Jian Li99f83ef2016-11-03 19:14:25 +0100107
108 /**
109 * Sets a collection of TE records.
110 *
111 * @param records a collection of TE records
112 * @return TeAddressBuilder object
113 */
114 public TeAddressBuilder withTeRecords(List<LispTeRecord> records) {
115 this.records = records;
Jian Li99f83ef2016-11-03 19:14:25 +0100116 return this;
117 }
118
119 /**
120 * Builds LispTeLcafAddress instance.
121 *
122 * @return LispTeLcafAddress instance
123 */
124 public LispTeLcafAddress build() {
Jian Lief0f7232016-11-15 19:55:46 +0900125 return new LispTeLcafAddress(records);
Jian Li99f83ef2016-11-03 19:14:25 +0100126 }
Jian Lief0f7232016-11-15 19:55:46 +0900127 }
128 /**
129 * TE LCAF address reader class.
130 */
Jian Li3a99e712016-12-16 21:23:16 +0900131 public static class TeLcafAddressReader
132 implements LispAddressReader<LispTeLcafAddress> {
Jian Li99f83ef2016-11-03 19:14:25 +0100133
Jian Lief0f7232016-11-15 19:55:46 +0900134 @Override
Jian Li3a99e712016-12-16 21:23:16 +0900135 public LispTeLcafAddress readFrom(ByteBuf byteBuf)
136 throws LispParseError, LispReaderException {
Jian Li99f83ef2016-11-03 19:14:25 +0100137
Jian Lief0f7232016-11-15 19:55:46 +0900138 LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf);
Jian Li99f83ef2016-11-03 19:14:25 +0100139
Jian Lief0f7232016-11-15 19:55:46 +0900140 List<LispTeRecord> teRecords = Lists.newArrayList();
Jian Li5e505c62016-12-05 02:44:24 +0900141 while (byteBuf.readerIndex() - COMMON_HEADER_SIZE < lcafAddress.getLength()) {
Jian Lief0f7232016-11-15 19:55:46 +0900142 teRecords.add(new LispTeRecord.TeRecordReader().readFrom(byteBuf));
Jian Li99f83ef2016-11-03 19:14:25 +0100143 }
Jian Lief0f7232016-11-15 19:55:46 +0900144
145 return new TeAddressBuilder()
146 .withTeRecords(teRecords)
147 .build();
Jian Li99f83ef2016-11-03 19:14:25 +0100148 }
Jian Lief0f7232016-11-15 19:55:46 +0900149 }
Jian Li99f83ef2016-11-03 19:14:25 +0100150
Jian Lief0f7232016-11-15 19:55:46 +0900151 /**
152 * TE LCAF address writer class.
153 */
Jian Li3a99e712016-12-16 21:23:16 +0900154 public static class TeLcafAddressWriter
155 implements LispAddressWriter<LispTeLcafAddress> {
Jian Li99f83ef2016-11-03 19:14:25 +0100156
Jian Lief0f7232016-11-15 19:55:46 +0900157 @Override
Jian Li3a99e712016-12-16 21:23:16 +0900158 public void writeTo(ByteBuf byteBuf, LispTeLcafAddress address)
159 throws LispWriterException {
Jian Li99f83ef2016-11-03 19:14:25 +0100160
Jian Lief0f7232016-11-15 19:55:46 +0900161 int lcafIndex = byteBuf.writerIndex();
162 LispLcafAddress.serializeCommon(byteBuf, address);
Jian Li99f83ef2016-11-03 19:14:25 +0100163
Jian Lief0f7232016-11-15 19:55:46 +0900164 TeRecordWriter writer = new TeRecordWriter();
165
166 List<LispTeRecord> teRecords = address.getTeRecords();
167 for (int i = 0; i < teRecords.size(); i++) {
168 writer.writeTo(byteBuf, teRecords.get(i));
Jian Li99f83ef2016-11-03 19:14:25 +0100169 }
Jian Lief0f7232016-11-15 19:55:46 +0900170
171 LispLcafAddress.updateLength(lcafIndex, byteBuf);
Jian Li99f83ef2016-11-03 19:14:25 +0100172 }
173 }
174}