blob: 5dd8e3882f560313a4479211cc28ffb4238f4948 [file] [log] [blame]
Jian Lid56f97e2016-07-19 15:48:15 +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.types;
17
Jian Li115d8602016-08-15 20:21:53 +090018import io.netty.buffer.ByteBuf;
19import org.onosproject.lisp.msg.exceptions.LispParseError;
GUNiba871702016-08-22 21:06:02 +090020import org.onosproject.lisp.msg.exceptions.LispWriterException;
Jian Li115d8602016-08-15 20:21:53 +090021
Jian Lid56f97e2016-07-19 15:48:15 +090022import java.util.Objects;
23
24import static com.google.common.base.MoreObjects.toStringHelper;
25
26/**
27 * Distinguished name address that is used by LISP Locator.
28 */
29public 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 Li115d8602016-08-15 20:21:53 +090076
77 /**
78 * Distinguished name address reader class.
79 */
80 public static class DistinguishedNameAddressReader
81 implements LispAddressReader<LispDistinguishedNameAddress> {
82
83 @Override
Jian Li3a99e712016-12-16 21:23:16 +090084 public LispDistinguishedNameAddress readFrom(ByteBuf byteBuf)
85 throws LispParseError {
Jian Li115d8602016-08-15 20:21:53 +090086
87 StringBuilder sb = new StringBuilder();
Jian Li76ea0572016-08-29 12:41:16 +090088 byte character;
89 while (byteBuf.readerIndex() < byteBuf.writerIndex()) {
Jian Li115d8602016-08-15 20:21:53 +090090 character = byteBuf.readByte();
Jian Li76ea0572016-08-29 12:41:16 +090091 sb.append((char) character);
Jian Li115d8602016-08-15 20:21:53 +090092 }
93
94 return new LispDistinguishedNameAddress(sb.toString());
95 }
96 }
GUNiba871702016-08-22 21:06:02 +090097
98 /**
99 * Distinguished name address writer class.
100 */
101 public static class DistinguishedNameAddressWriter
102 implements LispAddressWriter<LispDistinguishedNameAddress> {
103
104 @Override
Jian Li3a99e712016-12-16 21:23:16 +0900105 public void writeTo(ByteBuf byteBuf, LispDistinguishedNameAddress address)
106 throws LispWriterException {
GUNiba871702016-08-22 21:06:02 +0900107 String distinguishedName = address.getDistinguishedName();
108 byte[] nameBytes = distinguishedName.getBytes();
Jian Li3282a342017-05-23 14:30:15 +0900109 for (byte nameByte : nameBytes) {
110 byteBuf.writeByte(nameByte);
GUNiba871702016-08-22 21:06:02 +0900111 }
112 }
113 }
Jian Lid56f97e2016-07-19 15:48:15 +0900114}