blob: b944652ff37284382e971e43846bae573c2fd593 [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 Li89c9ca92016-11-11 04:09:02 +090035 * Traffic Engineering type is defined in draft-ietf-lisp-lcaf-20
36 * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-20#page-16
37 *
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
104 public static final class TeAddressBuilder extends LcafAddressBuilder<TeAddressBuilder> {
105 private List<LispTeRecord> records;
Jian Li99f83ef2016-11-03 19:14:25 +0100106
107 /**
108 * Sets a collection of TE records.
109 *
110 * @param records a collection of TE records
111 * @return TeAddressBuilder object
112 */
113 public TeAddressBuilder withTeRecords(List<LispTeRecord> records) {
114 this.records = records;
Jian Li99f83ef2016-11-03 19:14:25 +0100115 return this;
116 }
117
118 /**
119 * Builds LispTeLcafAddress instance.
120 *
121 * @return LispTeLcafAddress instance
122 */
123 public LispTeLcafAddress build() {
Jian Lief0f7232016-11-15 19:55:46 +0900124 return new LispTeLcafAddress(records);
Jian Li99f83ef2016-11-03 19:14:25 +0100125 }
Jian Lief0f7232016-11-15 19:55:46 +0900126 }
127 /**
128 * TE LCAF address reader class.
129 */
130 public static class TeLcafAddressReader implements LispAddressReader<LispTeLcafAddress> {
Jian Li99f83ef2016-11-03 19:14:25 +0100131
Jian Lief0f7232016-11-15 19:55:46 +0900132 @Override
133 public LispTeLcafAddress readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
Jian Li99f83ef2016-11-03 19:14:25 +0100134
Jian Lief0f7232016-11-15 19:55:46 +0900135 LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf);
Jian Li99f83ef2016-11-03 19:14:25 +0100136
Jian Lief0f7232016-11-15 19:55:46 +0900137 List<LispTeRecord> teRecords = Lists.newArrayList();
Jian Li5e505c62016-12-05 02:44:24 +0900138 while (byteBuf.readerIndex() - COMMON_HEADER_SIZE < lcafAddress.getLength()) {
Jian Lief0f7232016-11-15 19:55:46 +0900139 teRecords.add(new LispTeRecord.TeRecordReader().readFrom(byteBuf));
Jian Li99f83ef2016-11-03 19:14:25 +0100140 }
Jian Lief0f7232016-11-15 19:55:46 +0900141
142 return new TeAddressBuilder()
143 .withTeRecords(teRecords)
144 .build();
Jian Li99f83ef2016-11-03 19:14:25 +0100145 }
Jian Lief0f7232016-11-15 19:55:46 +0900146 }
Jian Li99f83ef2016-11-03 19:14:25 +0100147
Jian Lief0f7232016-11-15 19:55:46 +0900148 /**
149 * TE LCAF address writer class.
150 */
151 public static class TeLcafAddressWriter implements LispAddressWriter<LispTeLcafAddress> {
Jian Li99f83ef2016-11-03 19:14:25 +0100152
Jian Lief0f7232016-11-15 19:55:46 +0900153 @Override
154 public void writeTo(ByteBuf byteBuf, LispTeLcafAddress address) throws LispWriterException {
Jian Li99f83ef2016-11-03 19:14:25 +0100155
Jian Lief0f7232016-11-15 19:55:46 +0900156 int lcafIndex = byteBuf.writerIndex();
157 LispLcafAddress.serializeCommon(byteBuf, address);
Jian Li99f83ef2016-11-03 19:14:25 +0100158
Jian Lief0f7232016-11-15 19:55:46 +0900159 TeRecordWriter writer = new TeRecordWriter();
160
161 List<LispTeRecord> teRecords = address.getTeRecords();
162 for (int i = 0; i < teRecords.size(); i++) {
163 writer.writeTo(byteBuf, teRecords.get(i));
Jian Li99f83ef2016-11-03 19:14:25 +0100164 }
Jian Lief0f7232016-11-15 19:55:46 +0900165
166 LispLcafAddress.updateLength(lcafIndex, byteBuf);
Jian Li99f83ef2016-11-03 19:14:25 +0100167 }
168 }
169}