blob: c333236f18238de19a931b5cc13c9e5604a9407b [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;
GUNiba871702016-08-22 21:06:02 +090021import org.onosproject.lisp.msg.exceptions.LispWriterException;
Jian Lia7b394d2016-08-21 23:11:46 +090022
Jian Lid56f97e2016-07-19 15:48:15 +090023import java.util.Objects;
24
Jian Li5e505c62016-12-05 02:44:24 +090025import static org.onosproject.lisp.msg.types.AddressFamilyIdentifierEnum.IP4;
26import static org.onosproject.lisp.msg.types.AddressFamilyIdentifierEnum.IP6;
27import static org.onosproject.lisp.msg.types.AddressFamilyIdentifierEnum.NO_ADDRESS;
28import static org.onosproject.lisp.msg.types.AddressFamilyIdentifierEnum.DISTINGUISHED_NAME;
29import static org.onosproject.lisp.msg.types.AddressFamilyIdentifierEnum.MAC;
30import static org.onosproject.lisp.msg.types.AddressFamilyIdentifierEnum.LCAF;
31import static org.onosproject.lisp.msg.types.AddressFamilyIdentifierEnum.AS;
Jian Lia7b394d2016-08-21 23:11:46 +090032
Jian Li6b77dc02016-07-14 16:04:25 +090033/**
34 * LISP Locator address typed by Address Family Identifier (AFI).
35 */
36public abstract class LispAfiAddress {
37
38 private final AddressFamilyIdentifierEnum afi;
39
40 /**
41 * Initializes AFI enumeration value.
42 *
43 * @param afi address family identifier
44 */
45 protected LispAfiAddress(AddressFamilyIdentifierEnum afi) {
46 this.afi = afi;
47 }
48
49 /**
50 * Obtains AFI enumeration value.
51 *
52 * @return AFI enumeration value
53 */
54 public AddressFamilyIdentifierEnum getAfi() {
55 return afi;
56 }
57
58 @Override
59 public int hashCode() {
Jian Lid56f97e2016-07-19 15:48:15 +090060 return Objects.hash(afi);
Jian Li6b77dc02016-07-14 16:04:25 +090061 }
62
63 @Override
64 public boolean equals(Object obj) {
65 if (this == obj) {
66 return true;
67 }
68
69 if (obj == null) {
70 return false;
71 }
72
73 if (getClass() != obj.getClass()) {
74 return false;
75 }
76
77 LispAfiAddress other = (LispAfiAddress) obj;
78 if (afi != other.afi) {
79 return false;
80 }
81 return true;
82 }
Jian Lia7b394d2016-08-21 23:11:46 +090083
GUNiba871702016-08-22 21:06:02 +090084 /**
85 * AFI address reader class.
86 */
Jian Lia7b394d2016-08-21 23:11:46 +090087 public static class AfiAddressReader implements LispAddressReader<LispAfiAddress> {
88
89 @Override
90 public LispAfiAddress readFrom(ByteBuf byteBuf)
91 throws LispParseError, LispReaderException {
92
93 int index = byteBuf.readerIndex();
94
95 // AFI code -> 16 bits
96 short afiCode = (short) byteBuf.getUnsignedShort(index);
97
Jian Lif11594a2016-10-31 18:16:02 +090098 // handle no address
99 if (afiCode == NO_ADDRESS.getIanaCode()) {
100 byteBuf.readUnsignedShort();
Jian Li6ef1b3f2016-11-12 18:16:06 +0900101 return new LispNoAddress.NoAddressReader().readFrom(byteBuf);
Jian Lif11594a2016-10-31 18:16:02 +0900102 }
103
Jian Lia7b394d2016-08-21 23:11:46 +0900104 // handle IPv4 and IPv6 address
Jian Lid4e63702016-08-30 18:29:20 +0900105 if (afiCode == IP4.getIanaCode() ||
Jian Lia7b394d2016-08-21 23:11:46 +0900106 afiCode == IP6.getIanaCode()) {
107 return new LispIpAddress.IpAddressReader().readFrom(byteBuf);
108 }
109
110 // handle distinguished name address
111 if (afiCode == DISTINGUISHED_NAME.getIanaCode()) {
112 return new LispDistinguishedNameAddress.DistinguishedNameAddressReader().readFrom(byteBuf);
113 }
114
115 // handle MAC address
116 if (afiCode == MAC.getIanaCode()) {
117 return new LispMacAddress.MacAddressReader().readFrom(byteBuf);
118 }
119
120 // handle LCAF address
121 if (afiCode == LCAF.getIanaCode()) {
122 return new LispLcafAddress.LcafAddressReader().readFrom(byteBuf);
123 }
124
125 // handle autonomous system address
126 if (afiCode == AS.getIanaCode()) {
127 return new LispAsAddress.AsAddressReader().readFrom(byteBuf);
128 }
129
130 return null;
131 }
132 }
GUNiba871702016-08-22 21:06:02 +0900133
134 /**
135 * AFI address writer class.
136 */
137 public static class AfiAddressWriter implements LispAddressWriter<LispAfiAddress> {
138
139 @Override
140 public void writeTo(ByteBuf byteBuf, LispAfiAddress address) throws LispWriterException {
Jian Li76ea0572016-08-29 12:41:16 +0900141
142 // AFI code
143 byteBuf.writeShort(address.getAfi().getIanaCode());
144
GUNiba871702016-08-22 21:06:02 +0900145 switch (address.getAfi()) {
Jian Li6ef1b3f2016-11-12 18:16:06 +0900146 case NO_ADDRESS:
147 new LispNoAddress.NoAddressWriter().writeTo(byteBuf, (LispNoAddress) address);
148 break;
Jian Lid4e63702016-08-30 18:29:20 +0900149 case IP4:
GUNiba871702016-08-22 21:06:02 +0900150 new LispIpAddress.IpAddressWriter().writeTo(byteBuf, (LispIpv4Address) address);
151 break;
152 case IP6:
153 new LispIpAddress.IpAddressWriter().writeTo(byteBuf, (LispIpv6Address) address);
154 break;
155 case DISTINGUISHED_NAME:
156 new LispDistinguishedNameAddress.DistinguishedNameAddressWriter().writeTo(byteBuf,
157 (LispDistinguishedNameAddress) address);
158 break;
159 case MAC:
160 new LispMacAddress.MacAddressWriter().writeTo(byteBuf, (LispMacAddress) address);
161 break;
162 case LCAF:
163 new LispLcafAddress.LcafAddressWriter().writeTo(byteBuf, (LispLcafAddress) address);
164 break;
165 case AS:
166 new LispAsAddress.AsAddressWriter().writeTo(byteBuf, (LispAsAddress) address);
167 break;
168 default: break; // TODO: need log warning message
169 }
170 }
171 }
Jian Li6b77dc02016-07-14 16:04:25 +0900172}