Jian Li | 99f83ef | 2016-11-03 19:14:25 +0100 | [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.types; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import io.netty.buffer.ByteBuf; |
| 21 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 22 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
| 23 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
| 24 | import org.onosproject.lisp.msg.types.LispTeRecord.TeRecordWriter; |
| 25 | |
| 26 | import java.util.List; |
| 27 | import java.util.Objects; |
| 28 | |
| 29 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame^] | 30 | import static org.onosproject.lisp.msg.types.LispLcafAddress.COMMON_HEADER_SIZE; |
Jian Li | 99f83ef | 2016-11-03 19:14:25 +0100 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Traffic Engineering (TE) type LCAF address class. |
| 34 | * <p> |
Jian Li | 89c9ca9 | 2016-11-11 04:09:02 +0900 | [diff] [blame] | 35 | * 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 Li | 99f83ef | 2016-11-03 19:14:25 +0100 | [diff] [blame] | 38 | * <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 Li | 89c9ca9 | 2016-11-11 04:09:02 +0900 | [diff] [blame] | 45 | * | Type = 10 | Rsvd2 | Length | |
Jian Li | 99f83ef | 2016-11-03 19:14:25 +0100 | [diff] [blame] | 46 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 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 | */ |
| 57 | public 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 Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 66 | private LispTeLcafAddress(List<LispTeRecord> records) { |
| 67 | super(LispCanonicalAddressFormatEnum.TRAFFIC_ENGINEERING); |
Jian Li | 99f83ef | 2016-11-03 19:14:25 +0100 | [diff] [blame] | 68 | 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 Li | 99f83ef | 2016-11-03 19:14:25 +0100 | [diff] [blame] | 106 | |
| 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 Li | 99f83ef | 2016-11-03 19:14:25 +0100 | [diff] [blame] | 115 | return this; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Builds LispTeLcafAddress instance. |
| 120 | * |
| 121 | * @return LispTeLcafAddress instance |
| 122 | */ |
| 123 | public LispTeLcafAddress build() { |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 124 | return new LispTeLcafAddress(records); |
Jian Li | 99f83ef | 2016-11-03 19:14:25 +0100 | [diff] [blame] | 125 | } |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 126 | } |
| 127 | /** |
| 128 | * TE LCAF address reader class. |
| 129 | */ |
| 130 | public static class TeLcafAddressReader implements LispAddressReader<LispTeLcafAddress> { |
Jian Li | 99f83ef | 2016-11-03 19:14:25 +0100 | [diff] [blame] | 131 | |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 132 | @Override |
| 133 | public LispTeLcafAddress readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException { |
Jian Li | 99f83ef | 2016-11-03 19:14:25 +0100 | [diff] [blame] | 134 | |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 135 | LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf); |
Jian Li | 99f83ef | 2016-11-03 19:14:25 +0100 | [diff] [blame] | 136 | |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 137 | List<LispTeRecord> teRecords = Lists.newArrayList(); |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame^] | 138 | while (byteBuf.readerIndex() - COMMON_HEADER_SIZE < lcafAddress.getLength()) { |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 139 | teRecords.add(new LispTeRecord.TeRecordReader().readFrom(byteBuf)); |
Jian Li | 99f83ef | 2016-11-03 19:14:25 +0100 | [diff] [blame] | 140 | } |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 141 | |
| 142 | return new TeAddressBuilder() |
| 143 | .withTeRecords(teRecords) |
| 144 | .build(); |
Jian Li | 99f83ef | 2016-11-03 19:14:25 +0100 | [diff] [blame] | 145 | } |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 146 | } |
Jian Li | 99f83ef | 2016-11-03 19:14:25 +0100 | [diff] [blame] | 147 | |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 148 | /** |
| 149 | * TE LCAF address writer class. |
| 150 | */ |
| 151 | public static class TeLcafAddressWriter implements LispAddressWriter<LispTeLcafAddress> { |
Jian Li | 99f83ef | 2016-11-03 19:14:25 +0100 | [diff] [blame] | 152 | |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 153 | @Override |
| 154 | public void writeTo(ByteBuf byteBuf, LispTeLcafAddress address) throws LispWriterException { |
Jian Li | 99f83ef | 2016-11-03 19:14:25 +0100 | [diff] [blame] | 155 | |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 156 | int lcafIndex = byteBuf.writerIndex(); |
| 157 | LispLcafAddress.serializeCommon(byteBuf, address); |
Jian Li | 99f83ef | 2016-11-03 19:14:25 +0100 | [diff] [blame] | 158 | |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 159 | 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 Li | 99f83ef | 2016-11-03 19:14:25 +0100 | [diff] [blame] | 164 | } |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 165 | |
| 166 | LispLcafAddress.updateLength(lcafIndex, byteBuf); |
Jian Li | 99f83ef | 2016-11-03 19:14:25 +0100 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | } |