blob: c7e72c5d711ea3489a3c30a5ee4968f06b46bcd5 [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 Lia7b394d2016-08-21 23:11:46 +090025import static org.onosproject.lisp.msg.types.AddressFamilyIdentifierEnum.*;
26
Jian Li6b77dc02016-07-14 16:04:25 +090027/**
28 * LISP Locator address typed by Address Family Identifier (AFI).
29 */
30public abstract class LispAfiAddress {
31
32 private final AddressFamilyIdentifierEnum afi;
33
34 /**
35 * Initializes AFI enumeration value.
36 *
37 * @param afi address family identifier
38 */
39 protected LispAfiAddress(AddressFamilyIdentifierEnum afi) {
40 this.afi = afi;
41 }
42
43 /**
44 * Obtains AFI enumeration value.
45 *
46 * @return AFI enumeration value
47 */
48 public AddressFamilyIdentifierEnum getAfi() {
49 return afi;
50 }
51
52 @Override
53 public int hashCode() {
Jian Lid56f97e2016-07-19 15:48:15 +090054 return Objects.hash(afi);
Jian Li6b77dc02016-07-14 16:04:25 +090055 }
56
57 @Override
58 public boolean equals(Object obj) {
59 if (this == obj) {
60 return true;
61 }
62
63 if (obj == null) {
64 return false;
65 }
66
67 if (getClass() != obj.getClass()) {
68 return false;
69 }
70
71 LispAfiAddress other = (LispAfiAddress) obj;
72 if (afi != other.afi) {
73 return false;
74 }
75 return true;
76 }
Jian Lia7b394d2016-08-21 23:11:46 +090077
GUNiba871702016-08-22 21:06:02 +090078 /**
79 * AFI address reader class.
80 */
Jian Lia7b394d2016-08-21 23:11:46 +090081 public static class AfiAddressReader implements LispAddressReader<LispAfiAddress> {
82
83 @Override
84 public LispAfiAddress readFrom(ByteBuf byteBuf)
85 throws LispParseError, LispReaderException {
86
87 int index = byteBuf.readerIndex();
88
89 // AFI code -> 16 bits
90 short afiCode = (short) byteBuf.getUnsignedShort(index);
91
92 // handle IPv4 and IPv6 address
93 if (afiCode == IP.getIanaCode() ||
94 afiCode == IP6.getIanaCode()) {
95 return new LispIpAddress.IpAddressReader().readFrom(byteBuf);
96 }
97
98 // handle distinguished name address
99 if (afiCode == DISTINGUISHED_NAME.getIanaCode()) {
100 return new LispDistinguishedNameAddress.DistinguishedNameAddressReader().readFrom(byteBuf);
101 }
102
103 // handle MAC address
104 if (afiCode == MAC.getIanaCode()) {
105 return new LispMacAddress.MacAddressReader().readFrom(byteBuf);
106 }
107
108 // handle LCAF address
109 if (afiCode == LCAF.getIanaCode()) {
110 return new LispLcafAddress.LcafAddressReader().readFrom(byteBuf);
111 }
112
113 // handle autonomous system address
114 if (afiCode == AS.getIanaCode()) {
115 return new LispAsAddress.AsAddressReader().readFrom(byteBuf);
116 }
117
118 return null;
119 }
120 }
GUNiba871702016-08-22 21:06:02 +0900121
122 /**
123 * AFI address writer class.
124 */
125 public static class AfiAddressWriter implements LispAddressWriter<LispAfiAddress> {
126
127 @Override
128 public void writeTo(ByteBuf byteBuf, LispAfiAddress address) throws LispWriterException {
Jian Li76ea0572016-08-29 12:41:16 +0900129
130 // AFI code
131 byteBuf.writeShort(address.getAfi().getIanaCode());
132
GUNiba871702016-08-22 21:06:02 +0900133 switch (address.getAfi()) {
134 case IP:
135 new LispIpAddress.IpAddressWriter().writeTo(byteBuf, (LispIpv4Address) address);
136 break;
137 case IP6:
138 new LispIpAddress.IpAddressWriter().writeTo(byteBuf, (LispIpv6Address) address);
139 break;
140 case DISTINGUISHED_NAME:
141 new LispDistinguishedNameAddress.DistinguishedNameAddressWriter().writeTo(byteBuf,
142 (LispDistinguishedNameAddress) address);
143 break;
144 case MAC:
145 new LispMacAddress.MacAddressWriter().writeTo(byteBuf, (LispMacAddress) address);
146 break;
147 case LCAF:
148 new LispLcafAddress.LcafAddressWriter().writeTo(byteBuf, (LispLcafAddress) address);
149 break;
150 case AS:
151 new LispAsAddress.AsAddressWriter().writeTo(byteBuf, (LispAsAddress) address);
152 break;
153 default: break; // TODO: need log warning message
154 }
155 }
156 }
Jian Li6b77dc02016-07-14 16:04:25 +0900157}