blob: e0d631f65febecd5a4d2132d76a5c4b6080c96a7 [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;
20
Jian Lid56f97e2016-07-19 15:48:15 +090021import java.util.Objects;
22
23import static com.google.common.base.MoreObjects.toStringHelper;
24
25/**
26 * Distinguished name address that is used by LISP Locator.
27 */
28public class LispDistinguishedNameAddress extends LispAfiAddress {
29
30 private final String distinguishedName;
31
32 /**
33 * Initializes LISP locator's distinguished name address with AFI enum.
34 *
35 * @param distinguishedName distinguished name address
36 */
37 public LispDistinguishedNameAddress(String distinguishedName) {
38 super(AddressFamilyIdentifierEnum.DISTINGUISHED_NAME);
39 this.distinguishedName = distinguishedName;
40 }
41
42 /**
43 * Obtains LISP locator's distinguished name address.
44 *
45 * @return distinguished name address
46 */
47 public String getDistinguishedName() {
48 return distinguishedName;
49 }
50
51 @Override
52 public int hashCode() {
53 return Objects.hash(distinguishedName);
54 }
55
56 @Override
57 public boolean equals(Object obj) {
58 if (this == obj) {
59 return true;
60 }
61
62 if (obj instanceof LispDistinguishedNameAddress) {
63 final LispDistinguishedNameAddress other = (LispDistinguishedNameAddress) obj;
64 return Objects.equals(this.distinguishedName, other.distinguishedName);
65 }
66 return false;
67 }
68
69 @Override
70 public String toString() {
71 return toStringHelper(this)
72 .add("distinguished name", distinguishedName)
73 .toString();
74 }
Jian Li115d8602016-08-15 20:21:53 +090075
76 /**
77 * Distinguished name address reader class.
78 */
79 public static class DistinguishedNameAddressReader
80 implements LispAddressReader<LispDistinguishedNameAddress> {
81
82 @Override
83 public LispDistinguishedNameAddress readFrom(ByteBuf byteBuf) throws LispParseError {
84
85 StringBuilder sb = new StringBuilder();
86 byte character = byteBuf.readByte();
87 while (character != 0) {
88 sb.append((char) character);
89 character = byteBuf.readByte();
90 }
91
92 return new LispDistinguishedNameAddress(sb.toString());
93 }
94 }
Jian Lid56f97e2016-07-19 15:48:15 +090095}