blob: 006413bc3b9cc8a2f6e7df6d92000dd483febb83 [file] [log] [blame]
Jian Li6b77dc02016-07-14 16:04:25 +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 Lia7b394d2016-08-21 23:11:46 +090018import io.netty.buffer.ByteBuf;
19import org.onosproject.lisp.msg.exceptions.LispParseError;
20import org.onosproject.lisp.msg.exceptions.LispReaderException;
21
Jian Lid56f97e2016-07-19 15:48:15 +090022import java.util.Objects;
23
Jian Lia7b394d2016-08-21 23:11:46 +090024import static org.onosproject.lisp.msg.types.AddressFamilyIdentifierEnum.*;
25
Jian Li6b77dc02016-07-14 16:04:25 +090026/**
27 * LISP Locator address typed by Address Family Identifier (AFI).
28 */
29public abstract class LispAfiAddress {
30
31 private final AddressFamilyIdentifierEnum afi;
32
33 /**
34 * Initializes AFI enumeration value.
35 *
36 * @param afi address family identifier
37 */
38 protected LispAfiAddress(AddressFamilyIdentifierEnum afi) {
39 this.afi = afi;
40 }
41
42 /**
43 * Obtains AFI enumeration value.
44 *
45 * @return AFI enumeration value
46 */
47 public AddressFamilyIdentifierEnum getAfi() {
48 return afi;
49 }
50
51 @Override
52 public int hashCode() {
Jian Lid56f97e2016-07-19 15:48:15 +090053 return Objects.hash(afi);
Jian Li6b77dc02016-07-14 16:04:25 +090054 }
55
56 @Override
57 public boolean equals(Object obj) {
58 if (this == obj) {
59 return true;
60 }
61
62 if (obj == null) {
63 return false;
64 }
65
66 if (getClass() != obj.getClass()) {
67 return false;
68 }
69
70 LispAfiAddress other = (LispAfiAddress) obj;
71 if (afi != other.afi) {
72 return false;
73 }
74 return true;
75 }
Jian Lia7b394d2016-08-21 23:11:46 +090076
77 public static class AfiAddressReader implements LispAddressReader<LispAfiAddress> {
78
79 @Override
80 public LispAfiAddress readFrom(ByteBuf byteBuf)
81 throws LispParseError, LispReaderException {
82
83 int index = byteBuf.readerIndex();
84
85 // AFI code -> 16 bits
86 short afiCode = (short) byteBuf.getUnsignedShort(index);
87
88 // handle IPv4 and IPv6 address
89 if (afiCode == IP.getIanaCode() ||
90 afiCode == IP6.getIanaCode()) {
91 return new LispIpAddress.IpAddressReader().readFrom(byteBuf);
92 }
93
94 // handle distinguished name address
95 if (afiCode == DISTINGUISHED_NAME.getIanaCode()) {
96 return new LispDistinguishedNameAddress.DistinguishedNameAddressReader().readFrom(byteBuf);
97 }
98
99 // handle MAC address
100 if (afiCode == MAC.getIanaCode()) {
101 return new LispMacAddress.MacAddressReader().readFrom(byteBuf);
102 }
103
104 // handle LCAF address
105 if (afiCode == LCAF.getIanaCode()) {
106 return new LispLcafAddress.LcafAddressReader().readFrom(byteBuf);
107 }
108
109 // handle autonomous system address
110 if (afiCode == AS.getIanaCode()) {
111 return new LispAsAddress.AsAddressReader().readFrom(byteBuf);
112 }
113
114 return null;
115 }
116 }
Jian Li6b77dc02016-07-14 16:04:25 +0900117}