Jian Li | d56f97e | 2016-07-19 15:48:15 +0900 | [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 | |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 18 | import io.netty.buffer.ByteBuf; |
| 19 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 20 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 21 | |
Jian Li | d56f97e | 2016-07-19 15:48:15 +0900 | [diff] [blame] | 22 | import java.util.Objects; |
| 23 | |
| 24 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 25 | |
| 26 | /** |
| 27 | * Distinguished name address that is used by LISP Locator. |
| 28 | */ |
| 29 | public class LispDistinguishedNameAddress extends LispAfiAddress { |
| 30 | |
| 31 | private final String distinguishedName; |
| 32 | |
| 33 | /** |
| 34 | * Initializes LISP locator's distinguished name address with AFI enum. |
| 35 | * |
| 36 | * @param distinguishedName distinguished name address |
| 37 | */ |
| 38 | public LispDistinguishedNameAddress(String distinguishedName) { |
| 39 | super(AddressFamilyIdentifierEnum.DISTINGUISHED_NAME); |
| 40 | this.distinguishedName = distinguishedName; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Obtains LISP locator's distinguished name address. |
| 45 | * |
| 46 | * @return distinguished name address |
| 47 | */ |
| 48 | public String getDistinguishedName() { |
| 49 | return distinguishedName; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public int hashCode() { |
| 54 | return Objects.hash(distinguishedName); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public boolean equals(Object obj) { |
| 59 | if (this == obj) { |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | if (obj instanceof LispDistinguishedNameAddress) { |
| 64 | final LispDistinguishedNameAddress other = (LispDistinguishedNameAddress) obj; |
| 65 | return Objects.equals(this.distinguishedName, other.distinguishedName); |
| 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public String toString() { |
| 72 | return toStringHelper(this) |
| 73 | .add("distinguished name", distinguishedName) |
| 74 | .toString(); |
| 75 | } |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Distinguished name address reader class. |
| 79 | */ |
| 80 | public static class DistinguishedNameAddressReader |
| 81 | implements LispAddressReader<LispDistinguishedNameAddress> { |
| 82 | |
| 83 | @Override |
Jian Li | 3a99e71 | 2016-12-16 21:23:16 +0900 | [diff] [blame] | 84 | public LispDistinguishedNameAddress readFrom(ByteBuf byteBuf) |
| 85 | throws LispParseError { |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 86 | |
| 87 | StringBuilder sb = new StringBuilder(); |
Jian Li | 76ea057 | 2016-08-29 12:41:16 +0900 | [diff] [blame] | 88 | byte character; |
| 89 | while (byteBuf.readerIndex() < byteBuf.writerIndex()) { |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 90 | character = byteBuf.readByte(); |
Jian Li | 76ea057 | 2016-08-29 12:41:16 +0900 | [diff] [blame] | 91 | sb.append((char) character); |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | return new LispDistinguishedNameAddress(sb.toString()); |
| 95 | } |
| 96 | } |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Distinguished name address writer class. |
| 100 | */ |
| 101 | public static class DistinguishedNameAddressWriter |
| 102 | implements LispAddressWriter<LispDistinguishedNameAddress> { |
| 103 | |
| 104 | @Override |
Jian Li | 3a99e71 | 2016-12-16 21:23:16 +0900 | [diff] [blame] | 105 | public void writeTo(ByteBuf byteBuf, LispDistinguishedNameAddress address) |
| 106 | throws LispWriterException { |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 107 | String distinguishedName = address.getDistinguishedName(); |
| 108 | byte[] nameBytes = distinguishedName.getBytes(); |
Jian Li | 3282a34 | 2017-05-23 14:30:15 +0900 | [diff] [blame^] | 109 | for (byte nameByte : nameBytes) { |
| 110 | byteBuf.writeByte(nameByte); |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | } |
Jian Li | d56f97e | 2016-07-19 15:48:15 +0900 | [diff] [blame] | 114 | } |