Committed by
Gerrit Code Review
[Emu][onos-2603] - Implement LinkState Link attributes-OSPF Forwarding Address
Change-Id: I6001acfb681512c97f048c324ae09e32fa340805
Showing
1 changed file
with
187 additions
and
0 deletions
1 | +/* | ||
2 | + * Copyright 2015 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 | + */ | ||
16 | +package org.onosproject.bgpio.types.attr; | ||
17 | + | ||
18 | +import java.util.Objects; | ||
19 | + | ||
20 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
21 | +import org.onlab.packet.Ip4Address; | ||
22 | +import org.onlab.packet.Ip6Address; | ||
23 | +import org.onosproject.bgpio.exceptions.BGPParseException; | ||
24 | +import org.onosproject.bgpio.types.BGPErrorType; | ||
25 | +import org.onosproject.bgpio.types.BGPValueType; | ||
26 | +import org.onosproject.bgpio.util.Validation; | ||
27 | +import org.slf4j.Logger; | ||
28 | +import org.slf4j.LoggerFactory; | ||
29 | + | ||
30 | +import com.google.common.base.MoreObjects; | ||
31 | + | ||
32 | +/** | ||
33 | + * Implements BGP prefix OSPF Forwarding address attribute. | ||
34 | + */ | ||
35 | +public class BgpPrefixAttrOspfFwdAddr implements BGPValueType { | ||
36 | + | ||
37 | + protected static final Logger log = LoggerFactory | ||
38 | + .getLogger(BgpPrefixAttrOspfFwdAddr.class); | ||
39 | + | ||
40 | + public static final int ATTR_PREFIX_OSPFFWDADDR = 1156; | ||
41 | + public static final int IPV4_LEN = 4; | ||
42 | + public static final int IPV6_LEN = 16; | ||
43 | + | ||
44 | + /* OSPF Forwarding Address */ | ||
45 | + private final short lsAttrLength; | ||
46 | + private final Ip4Address ip4RouterId; | ||
47 | + private final Ip6Address ip6RouterId; | ||
48 | + | ||
49 | + /** | ||
50 | + * Constructor to initialize the value. | ||
51 | + * | ||
52 | + * @param lsAttrLength length of the IP address | ||
53 | + * @param ip4RouterId Valid IPV4 address if length is 4 else null | ||
54 | + * @param ip6RouterId Valid IPV6 address if length is 16 else null | ||
55 | + */ | ||
56 | + public BgpPrefixAttrOspfFwdAddr(short lsAttrLength, Ip4Address ip4RouterId, | ||
57 | + Ip6Address ip6RouterId) { | ||
58 | + this.lsAttrLength = lsAttrLength; | ||
59 | + this.ip4RouterId = ip4RouterId; | ||
60 | + this.ip6RouterId = ip6RouterId; | ||
61 | + } | ||
62 | + | ||
63 | + /** | ||
64 | + * Returns object of this class with specified values. | ||
65 | + * | ||
66 | + * @param lsAttrLength length of the IP address | ||
67 | + * @param ip4RouterId Valid IPV4 address if length is 4 else null | ||
68 | + * @param ip6RouterId Valid IPV6 address if length is 16 else null | ||
69 | + * @return object of BgpPrefixAttrOspfFwdAddr | ||
70 | + */ | ||
71 | + public static BgpPrefixAttrOspfFwdAddr of(final short lsAttrLength, | ||
72 | + final Ip4Address ip4RouterId, | ||
73 | + final Ip6Address ip6RouterId) { | ||
74 | + return new BgpPrefixAttrOspfFwdAddr(lsAttrLength, ip4RouterId, | ||
75 | + ip6RouterId); | ||
76 | + } | ||
77 | + | ||
78 | + /** | ||
79 | + * Reads the OSPF Forwarding Address. | ||
80 | + * | ||
81 | + * @param cb ChannelBuffer | ||
82 | + * @return object of BgpPrefixAttrOSPFFwdAddr | ||
83 | + * @throws BGPParseException while parsing BgpPrefixAttrOspfFwdAddr | ||
84 | + */ | ||
85 | + public static BgpPrefixAttrOspfFwdAddr read(ChannelBuffer cb) | ||
86 | + throws BGPParseException { | ||
87 | + short lsAttrLength; | ||
88 | + byte[] ipBytes; | ||
89 | + Ip4Address ip4RouterId = null; | ||
90 | + Ip6Address ip6RouterId = null; | ||
91 | + | ||
92 | + lsAttrLength = cb.readShort(); | ||
93 | + ipBytes = new byte[lsAttrLength]; | ||
94 | + | ||
95 | + if ((cb.readableBytes() < lsAttrLength)) { | ||
96 | + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, | ||
97 | + BGPErrorType.ATTRIBUTE_LENGTH_ERROR, | ||
98 | + lsAttrLength); | ||
99 | + } | ||
100 | + | ||
101 | + cb.readBytes(ipBytes); | ||
102 | + | ||
103 | + if (IPV4_LEN == lsAttrLength) { | ||
104 | + ip4RouterId = Ip4Address.valueOf(ipBytes); | ||
105 | + } else if (IPV6_LEN == lsAttrLength) { | ||
106 | + ip6RouterId = Ip6Address.valueOf(ipBytes); | ||
107 | + } | ||
108 | + | ||
109 | + return BgpPrefixAttrOspfFwdAddr.of(lsAttrLength, ip4RouterId, | ||
110 | + ip6RouterId); | ||
111 | + } | ||
112 | + | ||
113 | + /** | ||
114 | + * Returns IPV4 Address of OSPF forwarding address. | ||
115 | + * | ||
116 | + * @return IPV4 address | ||
117 | + */ | ||
118 | + public Ip4Address ospfv4FwdAddr() { | ||
119 | + return ip4RouterId; | ||
120 | + } | ||
121 | + | ||
122 | + /** | ||
123 | + * Returns IPV6 Address of OSPF forwarding address. | ||
124 | + * | ||
125 | + * @return IPV6 address | ||
126 | + */ | ||
127 | + public Ip6Address ospfv6FwdAddr() { | ||
128 | + return ip6RouterId; | ||
129 | + } | ||
130 | + | ||
131 | + /** | ||
132 | + * Returns OSPF forwarding address length. | ||
133 | + * | ||
134 | + * @return length of the ip address | ||
135 | + */ | ||
136 | + public short ospfFwdAddrLen() { | ||
137 | + return lsAttrLength; | ||
138 | + } | ||
139 | + | ||
140 | + @Override | ||
141 | + public short getType() { | ||
142 | + return ATTR_PREFIX_OSPFFWDADDR; | ||
143 | + } | ||
144 | + | ||
145 | + @Override | ||
146 | + public int hashCode() { | ||
147 | + if (IPV4_LEN == lsAttrLength) { | ||
148 | + return Objects.hash(ip4RouterId); | ||
149 | + } else { | ||
150 | + return Objects.hash(ip6RouterId); | ||
151 | + } | ||
152 | + } | ||
153 | + | ||
154 | + @Override | ||
155 | + public boolean equals(Object obj) { | ||
156 | + if (this == obj) { | ||
157 | + return true; | ||
158 | + } | ||
159 | + | ||
160 | + if (obj instanceof BgpPrefixAttrOspfFwdAddr) { | ||
161 | + BgpPrefixAttrOspfFwdAddr other = (BgpPrefixAttrOspfFwdAddr) obj; | ||
162 | + if (IPV4_LEN == lsAttrLength) { | ||
163 | + return Objects.equals(ip4RouterId, other.ip4RouterId); | ||
164 | + } else { | ||
165 | + return Objects.equals(ip6RouterId, other.ip6RouterId); | ||
166 | + } | ||
167 | + } | ||
168 | + return false; | ||
169 | + } | ||
170 | + | ||
171 | + @Override | ||
172 | + public int write(ChannelBuffer cb) { | ||
173 | + // TODO This will be implemented in the next version | ||
174 | + return 0; | ||
175 | + } | ||
176 | + | ||
177 | + @Override | ||
178 | + public String toString() { | ||
179 | + if (IPV4_LEN == lsAttrLength) { | ||
180 | + return MoreObjects.toStringHelper(getClass()).omitNullValues() | ||
181 | + .add("ip4RouterId", ip4RouterId).toString(); | ||
182 | + } else { | ||
183 | + return MoreObjects.toStringHelper(getClass()).omitNullValues() | ||
184 | + .add("ip6RouterId", ip6RouterId).toString(); | ||
185 | + } | ||
186 | + } | ||
187 | +} |
-
Please register or login to post a comment