Priyanka B
Committed by Gerrit Code Review

[Emu] [ONOS-2603] Implement BGP Update Protocol Message and parse all Link State…

… atrributes of Node and Prefix
Change-Id: I51f3cdbf273e7b82d1cccb21c9b2de83aba3a95d
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 +
17 +package org.onosproject.bgpio.protocol;
18 +
19 +import org.jboss.netty.buffer.ChannelBuffer;
20 +import org.onosproject.bgpio.exceptions.BGPParseException;
21 +
22 +/**
23 + * Abstraction of an entity providing BGP Message Writer.
24 + */
25 +public interface BGPMessageWriter<T> {
26 +
27 + /**
28 + * Writes the Objects of the BGP Message into Channel Buffer.
29 + *
30 + * @param cb Channel Buffer
31 + * @param message BGP Message
32 + * @throws BGPParseException
33 + * While writing message
34 + */
35 + void write(ChannelBuffer cb, T message) throws BGPParseException;
36 +}
...\ No newline at end of file ...\ No newline at end of file
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 +
17 +package org.onosproject.bgpio.protocol;
18 +
19 +/**
20 + * Provides Abstraction of IGP RouterID TLV.
21 + */
22 +public interface IGPRouterID {
23 +}
...\ No newline at end of file ...\ No newline at end of file
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 +
17 +package org.onosproject.bgpio.protocol.link_state;
18 +
19 +import java.util.Iterator;
20 +import java.util.LinkedList;
21 +import java.util.Objects;
22 +
23 +import org.jboss.netty.buffer.ChannelBuffer;
24 +import org.onosproject.bgpio.exceptions.BGPParseException;
25 +import org.onosproject.bgpio.types.BGPErrorType;
26 +import org.onosproject.bgpio.types.BGPValueType;
27 +import org.onosproject.bgpio.types.IPReachabilityInformationTlv;
28 +import org.onosproject.bgpio.types.OSPFRouteTypeTlv;
29 +import org.onosproject.bgpio.types.attr.BgpAttrNodeMultiTopologyId;
30 +import org.onosproject.bgpio.util.UnSupportedAttribute;
31 +import org.slf4j.Logger;
32 +import org.slf4j.LoggerFactory;
33 +
34 +import com.google.common.base.MoreObjects;
35 +
36 +/**
37 + * Provides Implementation of Local node descriptors and prefix descriptors.
38 + */
39 +public class BGPPrefixLSIdentifier {
40 +
41 + protected static final Logger log = LoggerFactory.getLogger(BGPPrefixLSIdentifier.class);
42 + public static final int TYPE_AND_LEN = 4;
43 + private NodeDescriptors localNodeDescriptors;
44 + private LinkedList<BGPValueType> prefixDescriptor;
45 +
46 + /**
47 + * Resets parameters.
48 + */
49 + public BGPPrefixLSIdentifier() {
50 + this.localNodeDescriptors = null;
51 + this.prefixDescriptor = null;
52 + }
53 +
54 + /**
55 + * Constructor to initialize parameters.
56 + *
57 + * @param localNodeDescriptors Local node descriptors
58 + * @param prefixDescriptor Prefix Descriptors
59 + */
60 + public BGPPrefixLSIdentifier(NodeDescriptors localNodeDescriptors, LinkedList<BGPValueType> prefixDescriptor) {
61 + this.localNodeDescriptors = localNodeDescriptors;
62 + this.prefixDescriptor = prefixDescriptor;
63 + }
64 +
65 + /**
66 + * Reads the channel buffer and parses Prefix Identifier.
67 + *
68 + * @param cb ChannelBuffer
69 + * @param protocolId protocol ID
70 + * @return object of this class
71 + * @throws BGPParseException while parsing Prefix Identifier
72 + */
73 + public static BGPPrefixLSIdentifier parsePrefixIdendifier(ChannelBuffer cb, byte protocolId)
74 + throws BGPParseException {
75 + //Parse Local Node descriptor
76 + NodeDescriptors localNodeDescriptors = new NodeDescriptors();
77 + localNodeDescriptors = parseLocalNodeDescriptors(cb, protocolId);
78 +
79 + //Parse Prefix descriptor
80 + LinkedList<BGPValueType> prefixDescriptor = new LinkedList<>();
81 + prefixDescriptor = parsePrefixDescriptors(cb);
82 + return new BGPPrefixLSIdentifier(localNodeDescriptors, prefixDescriptor);
83 + }
84 +
85 + /**
86 + * Parse local node descriptors.
87 + *
88 + * @param cb ChannelBuffer
89 + * @param protocolId protocol identifier
90 + * @return LocalNodeDescriptors
91 + * @throws BGPParseException while parsing local node descriptors
92 + */
93 + public static NodeDescriptors parseLocalNodeDescriptors(ChannelBuffer cb, byte protocolId)
94 + throws BGPParseException {
95 + ChannelBuffer tempBuf = cb;
96 + short type = cb.readShort();
97 + short length = cb.readShort();
98 + if (cb.readableBytes() < length) {
99 + //length + 4 implies data contains type, length and value
100 + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.OPTIONAL_ATTRIBUTE_ERROR,
101 + tempBuf.readBytes(cb.readableBytes() + TYPE_AND_LEN));
102 + }
103 + NodeDescriptors localNodeDescriptors = new NodeDescriptors();
104 + ChannelBuffer tempCb = cb.readBytes(length);
105 +
106 + if (type == NodeDescriptors.LOCAL_NODE_DES_TYPE) {
107 + localNodeDescriptors = NodeDescriptors.read(tempCb, length, type, protocolId);
108 + } else {
109 + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR,
110 + BGPErrorType.MALFORMED_ATTRIBUTE_LIST, null);
111 + }
112 + return localNodeDescriptors;
113 + }
114 +
115 + /**
116 + * Parse list of prefix descriptors.
117 + *
118 + * @param cb ChannelBuffer
119 + * @return list of prefix descriptors
120 + * @throws BGPParseException while parsing list of prefix descriptors
121 + */
122 + public static LinkedList<BGPValueType> parsePrefixDescriptors(ChannelBuffer cb) throws BGPParseException {
123 + LinkedList<BGPValueType> prefixDescriptor = new LinkedList<>();
124 + BGPValueType tlv = null;
125 + boolean isIpReachInfo = false;
126 + ChannelBuffer tempCb;
127 + int count = 0;
128 +
129 + while (cb.readableBytes() > 0) {
130 + ChannelBuffer tempBuf = cb;
131 + short type = cb.readShort();
132 + short length = cb.readShort();
133 + if (cb.readableBytes() < length) {
134 + //length + 4 implies data contains type, length and value
135 + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.OPTIONAL_ATTRIBUTE_ERROR,
136 + tempBuf.readBytes(cb.readableBytes() + TYPE_AND_LEN));
137 + }
138 + tempCb = cb.readBytes(length);
139 + switch (type) {
140 + case OSPFRouteTypeTlv.TYPE:
141 + tlv = OSPFRouteTypeTlv.read(tempCb);
142 + break;
143 + case IPReachabilityInformationTlv.TYPE:
144 + tlv = IPReachabilityInformationTlv.read(tempCb, length);
145 + isIpReachInfo = true;
146 + break;
147 + case BgpAttrNodeMultiTopologyId.ATTRNODE_MULTITOPOLOGY:
148 + tlv = BgpAttrNodeMultiTopologyId.read(tempCb);
149 + count = count + 1;
150 + if (count > 1) {
151 + //length + 4 implies data contains type, length and value
152 + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR,
153 + BGPErrorType.OPTIONAL_ATTRIBUTE_ERROR, tempBuf.readBytes(length + TYPE_AND_LEN));
154 + }
155 + break;
156 + default:
157 + UnSupportedAttribute.skipBytes(tempCb, length);
158 + }
159 + prefixDescriptor.add(tlv);
160 + }
161 +
162 + if (!isIpReachInfo) {
163 + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.OPTIONAL_ATTRIBUTE_ERROR,
164 + null);
165 + }
166 + return prefixDescriptor;
167 + }
168 +
169 + /**
170 + * Returns local node descriptors.
171 + *
172 + * @return local node descriptors
173 + */
174 + public NodeDescriptors getLocalNodeDescriptors() {
175 + return this.localNodeDescriptors;
176 + }
177 +
178 + /**
179 + * Returns Prefix descriptors.
180 + *
181 + * @return Prefix descriptors
182 + */
183 + public LinkedList<BGPValueType> getPrefixdescriptor() {
184 + return this.prefixDescriptor;
185 + }
186 +
187 + @Override
188 + public int hashCode() {
189 + return Objects.hash(prefixDescriptor.hashCode(), localNodeDescriptors);
190 + }
191 +
192 + @Override
193 + public boolean equals(Object obj) {
194 + if (this == obj) {
195 + return true;
196 + }
197 +
198 + if (obj instanceof BGPPrefixLSIdentifier) {
199 + int countObjSubTlv = 0;
200 + int countOtherSubTlv = 0;
201 + boolean isCommonSubTlv = true;
202 + BGPPrefixLSIdentifier other = (BGPPrefixLSIdentifier) obj;
203 +
204 + Iterator<BGPValueType> objListIterator = other.prefixDescriptor.iterator();
205 + countOtherSubTlv = other.prefixDescriptor.size();
206 + countObjSubTlv = prefixDescriptor.size();
207 + if (countObjSubTlv != countOtherSubTlv) {
208 + return false;
209 + } else {
210 + while (objListIterator.hasNext() && isCommonSubTlv) {
211 + BGPValueType subTlv = objListIterator.next();
212 + isCommonSubTlv = Objects.equals(prefixDescriptor.contains(subTlv),
213 + other.prefixDescriptor.contains(subTlv));
214 + }
215 + return isCommonSubTlv && Objects.equals(this.localNodeDescriptors, other.localNodeDescriptors);
216 + }
217 + }
218 + return false;
219 + }
220 +
221 + @Override
222 + public String toString() {
223 + return MoreObjects.toStringHelper(getClass())
224 + .add("localNodeDescriptors", localNodeDescriptors)
225 + .add("prefixDescriptor", prefixDescriptor)
226 + .toString();
227 + }
228 +}
...\ No newline at end of file ...\ No newline at end of file
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 +
17 +package org.onosproject.bgpio.protocol.link_state;
18 +
19 +import java.util.Iterator;
20 +import java.util.LinkedList;
21 +import java.util.Objects;
22 +
23 +import org.jboss.netty.buffer.ChannelBuffer;
24 +import org.onosproject.bgpio.exceptions.BGPParseException;
25 +import org.onosproject.bgpio.types.AreaIDTlv;
26 +import org.onosproject.bgpio.types.AutonomousSystemTlv;
27 +import org.onosproject.bgpio.types.BGPErrorType;
28 +import org.onosproject.bgpio.types.BGPLSIdentifierTlv;
29 +import org.onosproject.bgpio.types.BGPValueType;
30 +import org.onosproject.bgpio.types.IsIsNonPseudonode;
31 +import org.onosproject.bgpio.types.IsIsPseudonode;
32 +import org.onosproject.bgpio.types.OSPFNonPseudonode;
33 +import org.onosproject.bgpio.types.OSPFPseudonode;
34 +import org.onosproject.bgpio.util.UnSupportedAttribute;
35 +import org.slf4j.Logger;
36 +import org.slf4j.LoggerFactory;
37 +
38 +import com.google.common.base.MoreObjects;
39 +
40 +/**
41 + * Provides Local and Remote NodeDescriptors which contains Node Descriptor Sub-TLVs.
42 + */
43 +public class NodeDescriptors {
44 +
45 + /*
46 + *Reference :draft-ietf-idr-ls-distribution-11
47 + 0 1 2 3
48 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
49 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 + | Type | Length |
51 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 + | |
53 + // Node Descriptor Sub-TLVs (variable) //
54 + | |
55 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 +
57 + Figure : Local or Remote Node Descriptors TLV format
58 + */
59 +
60 + protected static final Logger log = LoggerFactory.getLogger(NodeDescriptors.class);
61 +
62 + public static final short LOCAL_NODE_DES_TYPE = 256;
63 + public static final short REMOTE_NODE_DES_TYPE = 257;
64 + public static final short IGP_ROUTERID_TYPE = 515;
65 + public static final short IS_IS_LEVEL_1_PROTOCOL_ID = 1;
66 + public static final short IS_IS_LEVEL_2_PROTOCOL_ID = 2;
67 + public static final short OSPF_V2_PROTOCOL_ID = 3;
68 + public static final short OSPF_V3_PROTOCOL_ID = 6;
69 + public static final int TYPE_AND_LEN = 4;
70 + public static final int ISISNONPSEUDONODE_LEN = 6;
71 + public static final int ISISPSEUDONODE_LEN = 7;
72 + public static final int OSPFNONPSEUDONODE_LEN = 4;
73 + public static final int OSPFPSEUDONODE_LEN = 8;
74 + private LinkedList<BGPValueType> subTlvs;
75 + private short deslength;
76 + private short desType;
77 +
78 + /**
79 + * Resets parameters.
80 + */
81 + public NodeDescriptors() {
82 + this.subTlvs = null;
83 + this.deslength = 0;
84 + this.desType = 0;
85 + }
86 +
87 + /**
88 + * Constructor to initialize parameters.
89 + *
90 + * @param subTlvs list of subTlvs
91 + * @param deslength Descriptors length
92 + * @param desType local node descriptor or remote node descriptor type
93 + */
94 + public NodeDescriptors(LinkedList<BGPValueType> subTlvs, short deslength, short desType) {
95 + this.subTlvs = subTlvs;
96 + this.deslength = deslength;
97 + this.desType = desType;
98 + }
99 +
100 + /**
101 + * Returns list of subTlvs.
102 + *
103 + * @return subTlvs list of subTlvs
104 + */
105 + public LinkedList<BGPValueType> getSubTlvs() {
106 + return subTlvs;
107 + }
108 +
109 + @Override
110 + public int hashCode() {
111 + return Objects.hash(subTlvs.hashCode());
112 + }
113 +
114 + @Override
115 + public boolean equals(Object obj) {
116 + if (this == obj) {
117 + return true;
118 + }
119 +
120 + if (obj instanceof NodeDescriptors) {
121 + int countObjSubTlv = 0;
122 + int countOtherSubTlv = 0;
123 + boolean isCommonSubTlv = true;
124 + NodeDescriptors other = (NodeDescriptors) obj;
125 + Iterator<BGPValueType> objListIterator = other.subTlvs.iterator();
126 + countOtherSubTlv = other.subTlvs.size();
127 + countObjSubTlv = subTlvs.size();
128 + if (countObjSubTlv != countOtherSubTlv) {
129 + return false;
130 + } else {
131 + while (objListIterator.hasNext() && isCommonSubTlv) {
132 + BGPValueType subTlv = objListIterator.next();
133 + isCommonSubTlv = Objects.equals(subTlvs.contains(subTlv), other.subTlvs.contains(subTlv));
134 + }
135 + return isCommonSubTlv;
136 + }
137 + }
138 + return false;
139 + }
140 +
141 + /**
142 + * Reads node descriptors Sub-TLVs.
143 + *
144 + * @param cb ChannelBuffer
145 + * @param desLength node descriptor length
146 + * @param desType local node descriptor or remote node descriptor type
147 + * @param protocolId protocol ID
148 + * @return object of NodeDescriptors
149 + * @throws BGPParseException while parsing node descriptors
150 + */
151 + public static NodeDescriptors read(ChannelBuffer cb, short desLength, short desType, byte protocolId)
152 + throws BGPParseException {
153 + LinkedList<BGPValueType> subTlvs;
154 + subTlvs = new LinkedList<>();
155 + BGPValueType tlv = null;
156 +
157 + while (cb.readableBytes() > 0) {
158 + ChannelBuffer tempBuf = cb;
159 + short type = cb.readShort();
160 + short length = cb.readShort();
161 + if (cb.readableBytes() < length) {
162 + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.OPTIONAL_ATTRIBUTE_ERROR,
163 + tempBuf.readBytes(cb.readableBytes() + TYPE_AND_LEN));
164 + }
165 + ChannelBuffer tempCb = cb.readBytes(length);
166 + switch (type) {
167 + case AutonomousSystemTlv.TYPE:
168 + tlv = AutonomousSystemTlv.read(tempCb);
169 + break;
170 + case BGPLSIdentifierTlv.TYPE:
171 + tlv = BGPLSIdentifierTlv.read(tempCb);
172 + break;
173 + case AreaIDTlv.TYPE:
174 + tlv = AreaIDTlv.read(tempCb);
175 + break;
176 + case IGP_ROUTERID_TYPE:
177 + if (protocolId == IS_IS_LEVEL_1_PROTOCOL_ID || protocolId == IS_IS_LEVEL_2_PROTOCOL_ID) {
178 + if (length == ISISNONPSEUDONODE_LEN) {
179 + tlv = IsIsNonPseudonode.read(tempCb);
180 + } else if (length == ISISPSEUDONODE_LEN) {
181 + tlv = IsIsPseudonode.read(tempCb);
182 + }
183 + } else if (protocolId == OSPF_V2_PROTOCOL_ID || protocolId == OSPF_V3_PROTOCOL_ID) {
184 + if (length == OSPFNONPSEUDONODE_LEN) {
185 + tlv = OSPFNonPseudonode.read(tempCb);
186 + } else if (length == OSPFPSEUDONODE_LEN) {
187 + tlv = OSPFPseudonode.read(tempCb);
188 + }
189 + }
190 + break;
191 + default:
192 + UnSupportedAttribute.skipBytes(tempCb, length);
193 + }
194 + subTlvs.add(tlv);
195 + }
196 + return new NodeDescriptors(subTlvs, desLength, desType);
197 + }
198 +
199 + /**
200 + * Returns node descriptors length.
201 + *
202 + * @return node descriptors length
203 + */
204 + public short getLength() {
205 + return this.deslength;
206 + }
207 +
208 + /**
209 + * Returns node descriptors type.
210 + *
211 + * @return node descriptors type
212 + */
213 + public short getType() {
214 + return this.desType;
215 + }
216 +
217 + @Override
218 + public String toString() {
219 + return MoreObjects.toStringHelper(getClass())
220 + .add("desType", desType)
221 + .add("deslength", deslength)
222 + .add("subTlvs", subTlvs)
223 + .toString();
224 + }
225 +}
...\ No newline at end of file ...\ No newline at end of file
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;
17 +
18 +import java.util.Objects;
19 +
20 +import org.jboss.netty.buffer.ChannelBuffer;
21 +import org.slf4j.Logger;
22 +import org.slf4j.LoggerFactory;
23 +
24 +import com.google.common.base.MoreObjects;
25 +
26 +/**
27 + * Provides AreaID Tlv which contains opaque value (32 Bit Area-ID).
28 + */
29 +public class AreaIDTlv implements BGPValueType {
30 +
31 + /* Reference :draft-ietf-idr-ls-distribution-11
32 + * 0 1 2 3
33 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
34 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 + | Type= 514 | Length=4 |
36 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 + | opaque value (32 Bit Area-ID) |
38 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 + */
40 +
41 + protected static final Logger log = LoggerFactory.getLogger(AreaIDTlv.class);
42 +
43 + public static final short TYPE = 514;
44 + public static final short LENGTH = 4;
45 +
46 + private final int areaID;
47 +
48 + /**
49 + * Constructor to initialize areaID.
50 + *
51 + * @param areaID of BGP AreaID Tlv
52 + */
53 + public AreaIDTlv(int areaID) {
54 + this.areaID = areaID;
55 + }
56 +
57 + /**
58 + * Returns object of this class with specified areaID.
59 + *
60 + * @param areaID opaque value of area id
61 + * @return object of AreaIDTlv
62 + */
63 + public static AreaIDTlv of(final int areaID) {
64 + return new AreaIDTlv(areaID);
65 + }
66 +
67 + /**
68 + * Returns opaque value of area id.
69 + *
70 + * @return opaque value of area id
71 + */
72 + public int getAreaID() {
73 + return areaID;
74 + }
75 +
76 + @Override
77 + public int hashCode() {
78 + return Objects.hash(areaID);
79 + }
80 +
81 + @Override
82 + public boolean equals(Object obj) {
83 + if (this == obj) {
84 + return true;
85 + }
86 +
87 + if (obj instanceof AreaIDTlv) {
88 + AreaIDTlv other = (AreaIDTlv) obj;
89 + return Objects.equals(areaID, other.areaID);
90 + }
91 + return false;
92 + }
93 +
94 + @Override
95 + public int write(ChannelBuffer c) {
96 + int iLenStartIndex = c.writerIndex();
97 + c.writeShort(TYPE);
98 + c.writeShort(LENGTH);
99 + c.writeInt(areaID);
100 + return c.writerIndex() - iLenStartIndex;
101 + }
102 +
103 + /**
104 + * Reads the channel buffer and returns object of AreaIDTlv.
105 + *
106 + * @param cb ChannelBuffer
107 + * @return object of AreaIDTlv
108 + */
109 + public static AreaIDTlv read(ChannelBuffer cb) {
110 + return AreaIDTlv.of(cb.readInt());
111 + }
112 +
113 + @Override
114 + public short getType() {
115 + return TYPE;
116 + }
117 +
118 + @Override
119 + public String toString() {
120 + return MoreObjects.toStringHelper(getClass())
121 + .add("Type", TYPE)
122 + .add("Length", LENGTH)
123 + .add("Value", areaID)
124 + .toString();
125 + }
126 +}
...\ No newline at end of file ...\ No newline at end of file
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;
17 +
18 +import java.util.Objects;
19 +
20 +import org.jboss.netty.buffer.ChannelBuffer;
21 +import org.slf4j.Logger;
22 +import org.slf4j.LoggerFactory;
23 +
24 +import com.google.common.base.MoreObjects;
25 +
26 +/**
27 + * Provides Autonomous System Tlv which contains opaque value (32 Bit AS Number).
28 + */
29 +public class AutonomousSystemTlv implements BGPValueType {
30 +
31 + /* Reference :draft-ietf-idr-ls-distribution-11
32 + * 0 1 2 3
33 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
34 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 + | Type= 512 | Length=4 |
36 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 + | opaque value (32 Bit AS Number) |
38 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 + */
40 +
41 + protected static final Logger log = LoggerFactory.getLogger(AutonomousSystemTlv.class);
42 +
43 + public static final short TYPE = 512;
44 + public static final short LENGTH = 4;
45 +
46 + private final int asNum;
47 +
48 + /**
49 + * Constructor to initialize asNum.
50 + *
51 + * @param asNum 32 Bit AS Number
52 + */
53 + public AutonomousSystemTlv(int asNum) {
54 + this.asNum = asNum;
55 + }
56 +
57 + /**
58 + * Returns object of this class with specified asNum.
59 + *
60 + * @param asNum 32 Bit AS Number
61 + * @return object of AutonomousSystemTlv
62 + */
63 + public static AutonomousSystemTlv of(final int asNum) {
64 + return new AutonomousSystemTlv(asNum);
65 + }
66 +
67 + /**
68 + * Returns opaque value of AS Number.
69 + *
70 + * @return opaque value of AS Number
71 + */
72 + public int getAsNum() {
73 + return asNum;
74 + }
75 +
76 + @Override
77 + public int hashCode() {
78 + return Objects.hash(asNum);
79 + }
80 +
81 + @Override
82 + public boolean equals(Object obj) {
83 + if (this == obj) {
84 + return true;
85 + }
86 +
87 + if (obj instanceof AutonomousSystemTlv) {
88 + AutonomousSystemTlv other = (AutonomousSystemTlv) obj;
89 + return Objects.equals(asNum, other.asNum);
90 + }
91 + return false;
92 + }
93 +
94 + @Override
95 + public int write(ChannelBuffer c) {
96 + int iLenStartIndex = c.writerIndex();
97 + c.writeShort(TYPE);
98 + c.writeShort(LENGTH);
99 + c.writeInt(asNum);
100 + return c.writerIndex() - iLenStartIndex;
101 + }
102 +
103 + /**
104 + * Reads the channel buffer and returns object of AutonomousSystemTlv.
105 + *
106 + * @param c ChannelBuffer
107 + * @return object of AutonomousSystemTlv
108 + */
109 + public static AutonomousSystemTlv read(ChannelBuffer c) {
110 + return AutonomousSystemTlv.of(c.readInt());
111 + }
112 +
113 + @Override
114 + public short getType() {
115 + return TYPE;
116 + }
117 +
118 + @Override
119 + public String toString() {
120 + return MoreObjects.toStringHelper(getClass())
121 + .add("Type", TYPE)
122 + .add("Length", LENGTH)
123 + .add("asNum", asNum)
124 + .toString();
125 + }
126 +}
...\ No newline at end of file ...\ No newline at end of file
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 +
17 +package org.onosproject.bgpio.types;
18 +
19 +/**
20 + * BgpErrorType class defines all errorCodes and error Subcodes required for Notification message.
21 + */
22 +public final class BGPErrorType {
23 + private BGPErrorType() {
24 + }
25 +
26 + //Error Codes
27 + public static final byte MESSAGE_HEADER_ERROR = 1;
28 + public static final byte OPEN_MESSAGE_ERROR = 2;
29 + public static final byte UPDATE_MESSAGE_ERROR = 3;
30 + public static final byte HOLD_TIMER_EXPIRED = 4;
31 + public static final byte FINITE_STATE_MACHINE_ERROR = 4;
32 + public static final byte CEASE = 5;
33 +
34 + //Message Header Error subcodes
35 + public static final byte CONNECTION_NOT_SYNCHRONIZED = 1;
36 + public static final byte BAD_MESSAGE_LENGTH = 2;
37 + public static final byte BAD_MESSAGE_TYPE = 3;
38 +
39 + //OPEN Message Error subcodes
40 + public static final byte UNSUPPORTED_VERSION_NUMBER = 1;
41 + public static final byte BAD_PEER_AS = 2;
42 + public static final byte BAD_BGP_IDENTIFIER = 3;
43 + public static final byte UNSUPPORTED_OPTIONAL_PARAMETER = 4;
44 + public static final byte UNACCEPTABLE_HOLD_TIME = 5;
45 +
46 + //UPDATE Message Error subcodes
47 + public static final byte MALFORMED_ATTRIBUTE_LIST = 1;
48 + public static final byte UNRECOGNIZED_WELLKNOWN_ATTRIBUTE = 2;
49 + public static final byte MISSING_WELLKNOWN_ATTRIBUTE = 3;
50 + public static final byte ATTRIBUTE_FLAGS_ERROR = 4;
51 + public static final byte ATTRIBUTE_LENGTH_ERROR = 5;
52 + public static final byte INVALID_ORIGIN_ATTRIBUTE = 6;
53 + public static final byte INVALID_NEXTHOP_ATTRIBUTE = 8;
54 + public static final byte OPTIONAL_ATTRIBUTE_ERROR = 9;
55 + public static final byte INVALID_NETWORK_FIELD = 10;
56 + public static final byte MALFORMED_ASPATH = 11;
57 +}
...\ No newline at end of file ...\ No newline at end of file
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 +
17 +package org.onosproject.bgpio.types;
18 +
19 +import java.util.Objects;
20 +
21 +import org.jboss.netty.buffer.ChannelBuffer;
22 +import org.slf4j.Logger;
23 +import org.slf4j.LoggerFactory;
24 +
25 +import com.google.common.base.MoreObjects;
26 +
27 +/**
28 + * Provides BGPLSIdentifier Tlv which contains opaque value (32 Bit BGPLS-Identifier).
29 + */
30 +public class BGPLSIdentifierTlv implements BGPValueType {
31 +
32 + /* Reference :draft-ietf-idr-ls-distribution-11
33 + * 0 1 2 3
34 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
35 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 + | Type= 513 | Length=4 |
37 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 + | opaque value (32 Bit BGPLS-Identifier) |
39 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 + */
41 +
42 + protected static final Logger log = LoggerFactory.getLogger(BGPLSIdentifierTlv.class);
43 +
44 + public static final short TYPE = 513;
45 + public static final short LENGTH = 4;
46 +
47 + private final int bgpLSIdentifier;
48 +
49 + /**
50 + * Constructor to initialize bgpLSIdentifier.
51 + *
52 + * @param bgpLSIdentifier BgpLS-Identifier
53 + */
54 + public BGPLSIdentifierTlv(int bgpLSIdentifier) {
55 + this.bgpLSIdentifier = bgpLSIdentifier;
56 + }
57 +
58 + /**
59 + * Returns object of this class with specified rbgpLSIdentifier.
60 + *
61 + * @param bgpLSIdentifier BgpLS-Identifier
62 + * @return BgpLS-Identifier
63 + */
64 + public static BGPLSIdentifierTlv of(final int bgpLSIdentifier) {
65 + return new BGPLSIdentifierTlv(bgpLSIdentifier);
66 + }
67 +
68 + /**
69 + * Returns opaque value of BgpLS-Identifier.
70 + *
71 + * @return opaque value of BgpLS-Identifier
72 + */
73 + public int getBgpLSIdentifier() {
74 + return bgpLSIdentifier;
75 + }
76 +
77 + @Override
78 + public int hashCode() {
79 + return Objects.hash(bgpLSIdentifier);
80 + }
81 +
82 + @Override
83 + public boolean equals(Object obj) {
84 + if (this == obj) {
85 + return true;
86 + }
87 +
88 + if (obj instanceof BGPLSIdentifierTlv) {
89 + BGPLSIdentifierTlv other = (BGPLSIdentifierTlv) obj;
90 + return Objects.equals(bgpLSIdentifier, other.bgpLSIdentifier);
91 + }
92 + return false;
93 + }
94 +
95 + @Override
96 + public int write(ChannelBuffer c) {
97 + int iLenStartIndex = c.writerIndex();
98 + c.writeShort(TYPE);
99 + c.writeShort(LENGTH);
100 + c.writeInt(bgpLSIdentifier);
101 + return c.writerIndex() - iLenStartIndex;
102 + }
103 +
104 + /**
105 + * Reads the channel buffer and parses BGPLS Identifier TLV.
106 + *
107 + * @param cb ChannelBuffer
108 + * @return object of BGPLSIdentifierTlv
109 + */
110 + public static BGPLSIdentifierTlv read(ChannelBuffer cb) {
111 + return BGPLSIdentifierTlv.of(cb.readInt());
112 + }
113 +
114 + @Override
115 + public short getType() {
116 + return TYPE;
117 + }
118 +
119 + @Override
120 + public String toString() {
121 + return MoreObjects.toStringHelper(getClass())
122 + .add("Type", TYPE)
123 + .add("Length", LENGTH)
124 + .add("Value", bgpLSIdentifier)
125 + .toString();
126 + }
127 +}
...\ No newline at end of file ...\ No newline at end of file
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;
17 +
18 +import java.util.Objects;
19 +
20 +import org.jboss.netty.buffer.ChannelBuffer;
21 +import org.onlab.packet.IpPrefix;
22 +import org.onosproject.bgpio.util.Validation;
23 +import org.slf4j.Logger;
24 +import org.slf4j.LoggerFactory;
25 +
26 +import com.google.common.base.MoreObjects;
27 +
28 +/**
29 + * Provides IP Reachability InformationTlv Tlv which contains IP Prefix.
30 + */
31 +public class IPReachabilityInformationTlv implements BGPValueType {
32 +
33 + /*
34 + * Reference :draft-ietf-idr-ls-distribution-11
35 +
36 + 0 1 2 3
37 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
38 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 + | Type | Length |
40 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 + | Prefix Length | IP Prefix (variable) //
42 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 +
44 + Figure 14: IP Reachability Information TLV Format
45 + */
46 +
47 + protected static final Logger log = LoggerFactory.getLogger(IPReachabilityInformationTlv.class);
48 +
49 + public static final short TYPE = 265;
50 + public static final int ONE_BYTE_LEN = 8;
51 + private byte prefixLen;
52 + private byte[] ipPrefix;
53 + public short length;
54 +
55 + /**
56 + * Constructor to initialize parameters.
57 + *
58 + * @param prefixLen length of IP Prefix
59 + * @param ipPrefix IP Prefix
60 + * @param length length of value field
61 + */
62 + public IPReachabilityInformationTlv(byte prefixLen, byte[] ipPrefix, short length) {
63 + this.ipPrefix = ipPrefix;
64 + this.prefixLen = prefixLen;
65 + this.length = length;
66 + }
67 +
68 + /**
69 + * Returns IP Prefix.
70 + *
71 + * @return IP Prefix
72 + */
73 + public IpPrefix getPrefixValue() {
74 + IpPrefix prefix = Validation.bytesToPrefix(ipPrefix, prefixLen);
75 + return prefix;
76 + }
77 +
78 + /**
79 + * Returns IP Prefix length.
80 + *
81 + * @return IP Prefix length
82 + */
83 + public byte getPrefixLen() {
84 + return this.prefixLen;
85 + }
86 +
87 + @Override
88 + public int hashCode() {
89 + return Objects.hash(ipPrefix, prefixLen);
90 + }
91 +
92 + @Override
93 + public boolean equals(Object obj) {
94 + if (this == obj) {
95 + return true;
96 + }
97 +
98 + if (obj instanceof IPReachabilityInformationTlv) {
99 + IPReachabilityInformationTlv other = (IPReachabilityInformationTlv) obj;
100 + return Objects.equals(prefixLen, other.prefixLen) && Objects.equals(ipPrefix, other.ipPrefix);
101 + }
102 + return false;
103 + }
104 +
105 + @Override
106 + public int write(ChannelBuffer cb) {
107 + int iLenStartIndex = cb.writerIndex();
108 + cb.writeShort(TYPE);
109 + cb.writeShort(length);
110 + cb.writeByte(prefixLen);
111 + cb.writeBytes(ipPrefix);
112 + return cb.writerIndex() - iLenStartIndex;
113 + }
114 +
115 + /**
116 + * Reads the channel buffer and returns object of IPReachabilityInformationTlv.
117 + *
118 + * @param cb ChannelBuffer
119 + * @param length of value field
120 + * @return object of IPReachabilityInformationTlv
121 + */
122 + public static IPReachabilityInformationTlv read(ChannelBuffer cb, short length) {
123 + byte preficLen = cb.readByte();
124 + byte[] prefix;
125 + if (preficLen == 0) {
126 + prefix = new byte[] {0};
127 + } else {
128 + int len = preficLen / ONE_BYTE_LEN;
129 + int reminder = preficLen % ONE_BYTE_LEN;
130 + if (reminder > 0) {
131 + len = len + 1;
132 + }
133 + prefix = new byte[len];
134 + cb.readBytes(prefix, 0, len);
135 + }
136 + return IPReachabilityInformationTlv.of(preficLen, prefix, length);
137 + }
138 +
139 + public static IPReachabilityInformationTlv of(final byte preficLen, final byte[] prefix, final short length) {
140 + return new IPReachabilityInformationTlv(preficLen, prefix, length);
141 + }
142 + @Override
143 + public short getType() {
144 + return TYPE;
145 + }
146 +
147 + @Override
148 + public String toString() {
149 + return MoreObjects.toStringHelper(getClass())
150 + .add("Type", TYPE)
151 + .add("Length", length)
152 + .add("Prefixlength", getPrefixLen())
153 + .add("Prefixvalue", getPrefixValue())
154 + .toString();
155 + }
156 +}
...\ No newline at end of file ...\ No newline at end of file
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;
17 +
18 +import java.util.Objects;
19 +
20 +import org.jboss.netty.buffer.ChannelBuffer;
21 +import org.onosproject.bgpio.protocol.IGPRouterID;
22 +import org.slf4j.Logger;
23 +import org.slf4j.LoggerFactory;
24 +
25 +import com.google.common.base.MoreObjects;
26 +
27 +/**
28 + * Provides Implementation of IsIsNonPseudonode Tlv.
29 + */
30 +public class IsIsNonPseudonode implements IGPRouterID, BGPValueType {
31 + protected static final Logger log = LoggerFactory.getLogger(IsIsNonPseudonode.class);
32 +
33 + public static final short TYPE = 515;
34 + public static final short LENGTH = 6;
35 +
36 + private final byte[] isoNodeID;
37 +
38 + /**
39 + * Constructor to initialize isoNodeID.
40 + *
41 + * @param isoNodeID ISO system-ID
42 + */
43 + public IsIsNonPseudonode(byte[] isoNodeID) {
44 + this.isoNodeID = isoNodeID;
45 + }
46 +
47 + /**
48 + * Returns object of this class with specified isoNodeID.
49 + *
50 + * @param isoNodeID ISO system-ID
51 + * @return object of IsIsNonPseudonode
52 + */
53 + public static IsIsNonPseudonode of(final byte[] isoNodeID) {
54 + return new IsIsNonPseudonode(isoNodeID);
55 + }
56 +
57 + /**
58 + * Returns ISO NodeID.
59 + *
60 + * @return ISO NodeID
61 + */
62 + public byte[] getISONodeID() {
63 + return isoNodeID;
64 + }
65 +
66 + @Override
67 + public int hashCode() {
68 + return Objects.hash(isoNodeID);
69 + }
70 +
71 + @Override
72 + public boolean equals(Object obj) {
73 + if (this == obj) {
74 + return true;
75 + }
76 + if (obj instanceof IsIsNonPseudonode) {
77 + IsIsNonPseudonode other = (IsIsNonPseudonode) obj;
78 + return Objects.equals(isoNodeID, other.isoNodeID);
79 + }
80 + return false;
81 + }
82 +
83 + @Override
84 + public int write(ChannelBuffer c) {
85 + int iLenStartIndex = c.writerIndex();
86 + c.writeShort(TYPE);
87 + c.writeShort(LENGTH);
88 + c.writeBytes(isoNodeID);
89 + return c.writerIndex() - iLenStartIndex;
90 + }
91 +
92 + /**
93 + * Reads the channel buffer and returns object of IsIsNonPseudonode.
94 + *
95 + * @param cb ChannelBuffer
96 + * @return object of IsIsNonPseudonode
97 + */
98 + public static IsIsNonPseudonode read(ChannelBuffer cb) {
99 + byte[] isoNodeID = new byte[LENGTH];
100 + cb.readBytes(isoNodeID, 0, LENGTH);
101 + return IsIsNonPseudonode.of(isoNodeID);
102 + }
103 +
104 + @Override
105 + public short getType() {
106 + return TYPE;
107 + }
108 +
109 + @Override
110 + public String toString() {
111 + return MoreObjects.toStringHelper(getClass())
112 + .add("Type", TYPE)
113 + .add("Length", LENGTH)
114 + .add("ISONodeID", isoNodeID)
115 + .toString();
116 + }
117 +}
...\ No newline at end of file ...\ No newline at end of file
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;
17 +
18 +import java.util.Objects;
19 +
20 +import org.jboss.netty.buffer.ChannelBuffer;
21 +import org.onosproject.bgpio.protocol.IGPRouterID;
22 +import org.slf4j.Logger;
23 +import org.slf4j.LoggerFactory;
24 +
25 +import com.google.common.base.MoreObjects;
26 +
27 +/**
28 + * Provides implementation of IsIsPseudonode Tlv.
29 + */
30 +public class IsIsPseudonode implements IGPRouterID, BGPValueType {
31 + protected static final Logger log = LoggerFactory.getLogger(IsIsPseudonode.class);
32 +
33 + public static final short TYPE = 515;
34 + public static final short LENGTH = 7;
35 +
36 + private final byte[] isoNodeID;
37 + private byte psnIdentifier;
38 +
39 + /**
40 + * Constructor to initialize isoNodeID.
41 + *
42 + * @param isoNodeID ISO system-ID
43 + * @param psnIdentifier PSN identifier
44 + */
45 + public IsIsPseudonode(byte[] isoNodeID, byte psnIdentifier) {
46 + this.isoNodeID = isoNodeID;
47 + this.psnIdentifier = psnIdentifier;
48 + }
49 +
50 + /**
51 + * Returns object of this class with specified values.
52 + *
53 + * @param isoNodeID ISO system-ID
54 + * @param psnIdentifier PSN identifier
55 + * @return object of IsIsPseudonode
56 + */
57 + public static IsIsPseudonode of(final byte[] isoNodeID, final byte psnIdentifier) {
58 + return new IsIsPseudonode(isoNodeID, psnIdentifier);
59 + }
60 +
61 + /**
62 + * Returns ISO NodeID.
63 + *
64 + * @return ISO NodeID
65 + */
66 + public byte[] getISONodeID() {
67 + return isoNodeID;
68 + }
69 +
70 + /**
71 + * Returns PSN Identifier.
72 + *
73 + * @return PSN Identifier
74 + */
75 + public byte getPSNIdentifier() {
76 + return this.psnIdentifier;
77 + }
78 +
79 + @Override
80 + public int hashCode() {
81 + return Objects.hash(isoNodeID, psnIdentifier);
82 + }
83 +
84 + @Override
85 + public boolean equals(Object obj) {
86 + if (this == obj) {
87 + return true;
88 + }
89 + if (obj instanceof IsIsPseudonode) {
90 + IsIsPseudonode other = (IsIsPseudonode) obj;
91 + return Objects.equals(isoNodeID, other.isoNodeID) && Objects.equals(psnIdentifier, other.psnIdentifier);
92 + }
93 + return false;
94 + }
95 +
96 + @Override
97 + public int write(ChannelBuffer c) {
98 + int iLenStartIndex = c.writerIndex();
99 + c.writeShort(TYPE);
100 + c.writeShort(LENGTH);
101 + c.writeBytes(isoNodeID);
102 + c.writeByte(psnIdentifier);
103 + return c.writerIndex() - iLenStartIndex;
104 + }
105 +
106 + /**
107 + * Reads the channel buffer and returns object of IsIsPseudonode.
108 + *
109 + * @param cb ChannelBuffer
110 + * @return object of IsIsPseudonode
111 + */
112 + public static IsIsPseudonode read(ChannelBuffer cb) {
113 + byte[] isoNodeID = new byte[LENGTH - 1];
114 + cb.readBytes(isoNodeID, 0, LENGTH - 1);
115 + byte psnIdentifier = cb.readByte();
116 + return IsIsPseudonode.of(isoNodeID, psnIdentifier);
117 + }
118 +
119 + @Override
120 + public short getType() {
121 + return TYPE;
122 + }
123 +
124 + @Override
125 + public String toString() {
126 + return MoreObjects.toStringHelper(getClass())
127 + .add("Type", TYPE)
128 + .add("Length", LENGTH)
129 + .add("isoNodeID", isoNodeID)
130 + .add("psnIdentifier", psnIdentifier)
131 + .toString();
132 + }
133 +}
...\ No newline at end of file ...\ No newline at end of file
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 +
17 +package org.onosproject.bgpio.types;
18 +
19 +import java.util.Objects;
20 +
21 +import org.jboss.netty.buffer.ChannelBuffer;
22 +import org.onosproject.bgpio.protocol.IGPRouterID;
23 +import org.slf4j.Logger;
24 +import org.slf4j.LoggerFactory;
25 +
26 +import com.google.common.base.MoreObjects;
27 +
28 +/**
29 + * Provides implementation of OSPFNonPseudonode Tlv.
30 + */
31 +public class OSPFNonPseudonode implements IGPRouterID, BGPValueType {
32 +
33 + protected static final Logger log = LoggerFactory.getLogger(OSPFNonPseudonode.class);
34 +
35 + public static final short TYPE = 515;
36 + public static final short LENGTH = 4;
37 +
38 + private final int routerID;
39 +
40 + /**
41 + * Constructor to initialize routerID.
42 + *
43 + * @param routerID routerID
44 + */
45 + public OSPFNonPseudonode(int routerID) {
46 + this.routerID = routerID;
47 + }
48 +
49 + /**
50 + * Returns object of this class with specified routerID.
51 + *
52 + * @param routerID routerID
53 + * @return object of OSPFNonPseudonode
54 + */
55 + public static OSPFNonPseudonode of(final int routerID) {
56 + return new OSPFNonPseudonode(routerID);
57 + }
58 +
59 + /**
60 + * Returns RouterID.
61 + *
62 + * @return RouterID
63 + */
64 + public int getrouterID() {
65 + return this.routerID;
66 + }
67 +
68 + @Override
69 + public int hashCode() {
70 + return Objects.hash(routerID);
71 + }
72 +
73 + @Override
74 + public boolean equals(Object obj) {
75 + if (this == obj) {
76 + return true;
77 + }
78 +
79 + if (obj instanceof OSPFNonPseudonode) {
80 + OSPFNonPseudonode other = (OSPFNonPseudonode) obj;
81 + return Objects.equals(routerID, other.routerID);
82 + }
83 + return false;
84 + }
85 +
86 + @Override
87 + public int write(ChannelBuffer c) {
88 + int iLenStartIndex = c.writerIndex();
89 + c.writeShort(TYPE);
90 + c.writeShort(LENGTH);
91 + c.writeInt(routerID);
92 + return c.writerIndex() - iLenStartIndex;
93 + }
94 +
95 + /**
96 + * Reads the channel buffer and returns object of OSPFNonPseudonode.
97 + *
98 + * @param cb ChannelBuffer
99 + * @return object of OSPFNonPseudonode
100 + */
101 + public static OSPFNonPseudonode read(ChannelBuffer cb) {
102 + return OSPFNonPseudonode.of(cb.readInt());
103 + }
104 +
105 + @Override
106 + public short getType() {
107 + return TYPE;
108 + }
109 +
110 + @Override
111 + public String toString() {
112 + return MoreObjects.toStringHelper(getClass())
113 + .add("Type", TYPE)
114 + .add("Length", LENGTH)
115 + .add("RouterID", routerID)
116 + .toString();
117 + }
118 +}
...\ No newline at end of file ...\ No newline at end of file
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;
17 +
18 +import java.util.Objects;
19 +
20 +import org.jboss.netty.buffer.ChannelBuffer;
21 +import org.onlab.packet.Ip4Address;
22 +import org.onosproject.bgpio.protocol.IGPRouterID;
23 +import org.slf4j.Logger;
24 +import org.slf4j.LoggerFactory;
25 +
26 +import com.google.common.base.MoreObjects;
27 +
28 +/**
29 + * Provides implementation of OSPFPseudonode Tlv.
30 + */
31 +public class OSPFPseudonode implements IGPRouterID, BGPValueType {
32 +
33 + protected static final Logger log = LoggerFactory.getLogger(OSPFPseudonode.class);
34 +
35 + public static final short TYPE = 515;
36 + public static final short LENGTH = 8;
37 +
38 + private final int routerID;
39 + private final Ip4Address drInterface;
40 +
41 + /**
42 + * Constructor to initialize parameters.
43 + *
44 + * @param routerID routerID
45 + * @param drInterface IPv4 address of the DR's interface
46 + */
47 + public OSPFPseudonode(int routerID, Ip4Address drInterface) {
48 + this.routerID = routerID;
49 + this.drInterface = drInterface;
50 + }
51 +
52 + /**
53 + * Returns object of this class with specified values.
54 + *
55 + * @param routerID routerID
56 + * @param drInterface IPv4 address of the DR's interface
57 + * @return object of OSPFPseudonode
58 + */
59 + public static OSPFPseudonode of(final int routerID, final Ip4Address drInterface) {
60 + return new OSPFPseudonode(routerID, drInterface);
61 + }
62 +
63 + /**
64 + * Returns RouterID.
65 + *
66 + * @return RouterID
67 + */
68 + public int getrouterID() {
69 + return this.routerID;
70 + }
71 +
72 + @Override
73 + public int hashCode() {
74 + return Objects.hash(routerID, drInterface);
75 + }
76 +
77 + @Override
78 + public boolean equals(Object obj) {
79 + if (this == obj) {
80 + return true;
81 + }
82 + if (obj instanceof OSPFPseudonode) {
83 + OSPFPseudonode other = (OSPFPseudonode) obj;
84 + return Objects.equals(routerID, other.routerID) && Objects.equals(drInterface, other.drInterface);
85 + }
86 + return false;
87 + }
88 +
89 + @Override
90 + public int write(ChannelBuffer c) {
91 + int iLenStartIndex = c.writerIndex();
92 + c.writeShort(TYPE);
93 + c.writeShort(LENGTH);
94 + c.writeInt(routerID);
95 + c.writeInt(drInterface.toInt());
96 + return c.writerIndex() - iLenStartIndex;
97 + }
98 +
99 + /**
100 + * Reads the channel buffer and returns object of OSPFPseudonode.
101 + *
102 + * @param cb ChannelBuffer
103 + * @return object of OSPFPseudonode
104 + */
105 + public static OSPFPseudonode read(ChannelBuffer cb) {
106 + int routerID = cb.readInt();
107 + Ip4Address drInterface = Ip4Address.valueOf(cb.readInt());
108 + return OSPFPseudonode.of(routerID, drInterface);
109 + }
110 +
111 + @Override
112 + public short getType() {
113 + return TYPE;
114 + }
115 +
116 + @Override
117 + public String toString() {
118 + return MoreObjects.toStringHelper(getClass())
119 + .add("Type", TYPE)
120 + .add("Length", LENGTH)
121 + .add("RouterID", routerID)
122 + .add("DRInterface", drInterface)
123 + .toString();
124 + }
125 +}
...\ No newline at end of file ...\ No newline at end of file
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;
17 +
18 +import java.util.Objects;
19 +
20 +import org.jboss.netty.buffer.ChannelBuffer;
21 +import org.onosproject.bgpio.exceptions.BGPParseException;
22 +import org.slf4j.Logger;
23 +import org.slf4j.LoggerFactory;
24 +
25 +import com.google.common.base.MoreObjects;
26 +
27 +/**
28 + * Provides OSPF Route Type Tlv which contains route type.
29 + */
30 +public class OSPFRouteTypeTlv implements BGPValueType {
31 +
32 + /* Reference :draft-ietf-idr-ls-distribution-11
33 + 0 1 2 3
34 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
35 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 + | Type | Length |
37 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 + | Route Type |
39 + +-+-+-+-+-+-+-+-+
40 +
41 + Figure : OSPF Route Type TLV Format
42 + */
43 +
44 + protected static final Logger log = LoggerFactory.getLogger(OSPFRouteTypeTlv.class);
45 +
46 + public static final short TYPE = 264;
47 + public static final short LENGTH = 1;
48 + public static final int INTRA_AREA_TYPE = 1;
49 + public static final short INTER_AREA_TYPE = 2;
50 + public static final short EXTERNAL_TYPE_1 = 3;
51 + public static final short EXTERNAL_TYPE_2 = 4;
52 + public static final short NSSA_TYPE_1 = 5;
53 + public static final short NSSA_TYPE_2 = 6;
54 + private final byte routeType;
55 +
56 + /**
57 + * Enum for Route Type.
58 + */
59 + public enum ROUTETYPE {
60 + Intra_Area(1), Inter_Area(2), External_1(3), External_2(4), NSSA_1(5), NSSA_2(6);
61 + int value;
62 + ROUTETYPE(int val) {
63 + value = val;
64 + }
65 + public byte getType() {
66 + return (byte) value;
67 + }
68 + }
69 +
70 + /**
71 + * Constructor to initialize routeType.
72 + *
73 + * @param routeType Route type
74 + */
75 + public OSPFRouteTypeTlv(byte routeType) {
76 + this.routeType = routeType;
77 + }
78 +
79 + /**
80 + * Returns object of this class with specified routeType.
81 + *
82 + * @param routeType Route type
83 + * @return object of OSPFRouteTypeTlv
84 + */
85 + public static OSPFRouteTypeTlv of(final byte routeType) {
86 + return new OSPFRouteTypeTlv(routeType);
87 + }
88 +
89 + /**
90 + * Returns RouteType.
91 + *
92 + * @return RouteType
93 + * @throws BGPParseException if routeType is not matched
94 + */
95 + public ROUTETYPE getValue() throws BGPParseException {
96 + switch (routeType) {
97 + case INTRA_AREA_TYPE:
98 + return ROUTETYPE.Intra_Area;
99 + case INTER_AREA_TYPE:
100 + return ROUTETYPE.Inter_Area;
101 + case EXTERNAL_TYPE_1:
102 + return ROUTETYPE.External_1;
103 + case EXTERNAL_TYPE_2:
104 + return ROUTETYPE.External_2;
105 + case NSSA_TYPE_1:
106 + return ROUTETYPE.NSSA_1;
107 + case NSSA_TYPE_2:
108 + return ROUTETYPE.NSSA_2;
109 + default:
110 + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null);
111 + }
112 + }
113 +
114 + @Override
115 + public int hashCode() {
116 + return Objects.hash(routeType);
117 + }
118 +
119 + @Override
120 + public boolean equals(Object obj) {
121 + if (this == obj) {
122 + return true;
123 + }
124 + if (obj instanceof OSPFRouteTypeTlv) {
125 + OSPFRouteTypeTlv other = (OSPFRouteTypeTlv) obj;
126 + return Objects.equals(routeType, other.routeType);
127 + }
128 + return false;
129 + }
130 +
131 + @Override
132 + public int write(ChannelBuffer c) {
133 + int iLenStartIndex = c.writerIndex();
134 + c.writeShort(TYPE);
135 + c.writeShort(LENGTH);
136 + c.writeByte(routeType);
137 + return c.writerIndex() - iLenStartIndex;
138 + }
139 +
140 + /**
141 + * Reads from ChannelBuffer and parses OSPFRouteTypeTlv.
142 + *
143 + * @param cb channelBuffer
144 + * @return object of OSPFRouteTypeTlv
145 + */
146 + public static OSPFRouteTypeTlv read(ChannelBuffer cb) {
147 + return OSPFRouteTypeTlv.of(cb.readByte());
148 + }
149 +
150 + @Override
151 + public short getType() {
152 + return TYPE;
153 + }
154 +
155 + @Override
156 + public String toString() {
157 + return MoreObjects.toStringHelper(getClass())
158 + .add("Type", TYPE)
159 + .add("Length", LENGTH)
160 + .add("Value", routeType)
161 + .toString();
162 + }
163 +}
...\ No newline at end of file ...\ No newline at end of file
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 +
17 +package org.onosproject.bgpio.types.attr;
18 +
19 +import java.util.Objects;
20 +
21 +import org.jboss.netty.buffer.ChannelBuffer;
22 +import org.onosproject.bgpio.exceptions.BGPParseException;
23 +import org.onosproject.bgpio.types.BGPErrorType;
24 +import org.onosproject.bgpio.types.BGPValueType;
25 +import org.onosproject.bgpio.util.Validation;
26 +import org.slf4j.Logger;
27 +import org.slf4j.LoggerFactory;
28 +
29 +import com.google.common.base.MoreObjects;
30 +
31 +/**
32 + * BGP Multi-Topology ID of the LS attribute.
33 + */
34 +public class BgpAttrNodeMultiTopologyId implements BGPValueType {
35 +
36 + private static final Logger log = LoggerFactory
37 + .getLogger(BgpAttrNodeMultiTopologyId.class);
38 +
39 + public static final int ATTRNODE_MULTITOPOLOGY = 263;
40 +
41 + /* Opaque Node Attribute */
42 + private short[] multiTopologyId;
43 +
44 + /**
45 + * Constructor to initialize the Node attribute multi-topology ID.
46 + *
47 + * @param multiTopologyId multi-topology ID
48 + */
49 + BgpAttrNodeMultiTopologyId(short[] multiTopologyId) {
50 + this.multiTopologyId = multiTopologyId;
51 + }
52 +
53 + /**
54 + * Reads the Multi-topology ID of Node attribute.
55 + *
56 + * @param cb ChannelBuffer
57 + * @return Constructor of BgpAttrNodeMultiTopologyId
58 + * @throws BGPParseException while parsing BgpAttrNodeMultiTopologyId
59 + */
60 + public static BgpAttrNodeMultiTopologyId read(ChannelBuffer cb)
61 + throws BGPParseException {
62 +
63 + log.debug("BgpAttrNodeMultiTopologyId");
64 + short lsAttrLength = cb.readShort();
65 + int len = lsAttrLength / 2; // Length is 2*n and n is the number of MT-IDs
66 +
67 + if (cb.readableBytes() < lsAttrLength) {
68 + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
69 + BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
70 + cb.readableBytes());
71 + }
72 +
73 + short[] multiTopologyId;
74 + multiTopologyId = new short[len];
75 + for (int i = 0; i < len; i++) {
76 + multiTopologyId[i] = cb.readShort();
77 + }
78 +
79 + return new BgpAttrNodeMultiTopologyId(multiTopologyId);
80 + }
81 +
82 + /**
83 + * to get the multi-topology ID.
84 + *
85 + * @return multitopology ID
86 + */
87 + short[] getAttrMultiTopologyId() {
88 + return multiTopologyId;
89 + }
90 +
91 + @Override
92 + public short getType() {
93 + return ATTRNODE_MULTITOPOLOGY;
94 + }
95 +
96 + @Override
97 + public int hashCode() {
98 + return Objects.hash(multiTopologyId);
99 + }
100 +
101 + @Override
102 + public boolean equals(Object obj) {
103 + if (this == obj) {
104 + return true;
105 + }
106 +
107 + if (obj instanceof BgpAttrNodeMultiTopologyId) {
108 + BgpAttrNodeMultiTopologyId other = (BgpAttrNodeMultiTopologyId) obj;
109 + return Objects.equals(multiTopologyId, other.multiTopologyId);
110 + }
111 + return false;
112 + }
113 +
114 + @Override
115 + public int write(ChannelBuffer cb) {
116 + // TODO Auto-generated method stub
117 + return 0;
118 + }
119 +
120 + @Override
121 + public String toString() {
122 + return MoreObjects.toStringHelper(getClass())
123 + .omitNullValues()
124 + .add("multiTopologyId", multiTopologyId)
125 + .toString();
126 + }
127 +}
...\ No newline at end of file ...\ No newline at end of file
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 +
17 +package org.onosproject.bgpio.util;
18 +
19 +import org.jboss.netty.buffer.ChannelBuffer;
20 +import org.slf4j.Logger;
21 +import org.slf4j.LoggerFactory;
22 +
23 +/**
24 + * Provides methods to handle UnSupportedAttribute.
25 + */
26 +public final class UnSupportedAttribute {
27 + protected static final Logger log = LoggerFactory.getLogger(UnSupportedAttribute.class);
28 +
29 + private UnSupportedAttribute() {
30 + }
31 +
32 + /**
33 + * Reads channel buffer parses attribute header and skips specified length.
34 + *
35 + * @param cb channelBuffer
36 + */
37 + public static void read(ChannelBuffer cb) {
38 + Validation parseFlags = Validation.parseAttributeHeader(cb);
39 + cb.skipBytes(parseFlags.getLength());
40 + }
41 +
42 + /**
43 + * Skip specified bytes in channel buffer.
44 + *
45 + * @param cb channelBuffer
46 + * @param length to be skipped
47 + */
48 + public static void skipBytes(ChannelBuffer cb, short length) {
49 + cb.skipBytes(length);
50 + }
51 +}
...\ No newline at end of file ...\ No newline at end of file
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 +
17 +package org.onosproject.bgpio.util;
18 +
19 +import java.util.Arrays;
20 +
21 +import org.jboss.netty.buffer.ChannelBuffer;
22 +import org.jboss.netty.buffer.ChannelBuffers;
23 +import org.onlab.packet.IpAddress;
24 +import org.onlab.packet.IpPrefix;
25 +import org.onosproject.bgpio.exceptions.BGPParseException;
26 +
27 +import com.google.common.primitives.Ints;
28 +
29 +/**
30 + * Provides methods to parse attribute header, validate length and type.
31 + */
32 +public class Validation {
33 + public static final byte FIRST_BIT = (byte) 0x80;
34 + public static final byte SECOND_BIT = 0x40;
35 + public static final byte THIRD_BIT = 0x20;
36 + public static final byte FOURTH_BIT = 0x01;
37 + public static final byte IPV4_SIZE = 4;
38 + private boolean firstBit;
39 + private boolean secondBit;
40 + private boolean thirdBit;
41 + private boolean fourthBit;
42 + private int len;
43 + private boolean isShort;
44 +
45 + Validation(boolean firstBit, boolean secondBit, boolean thirdBit, boolean fourthBit, int len, boolean isShort) {
46 + this.firstBit = firstBit;
47 + this.secondBit = secondBit;
48 + this.thirdBit = thirdBit;
49 + this.fourthBit = fourthBit;
50 + this.len = len;
51 + this.isShort = isShort;
52 + }
53 +
54 + /**
55 + * Parses attribute Header.
56 + *
57 + * @param cb ChannelBuffer
58 + * @return object of Validation
59 + */
60 + public static Validation parseAttributeHeader(ChannelBuffer cb) {
61 +
62 + boolean firstBit;
63 + boolean secondBit;
64 + boolean thirdBit;
65 + boolean fourthBit;
66 + boolean isShort;
67 + byte flags = cb.readByte();
68 + byte typeCode = cb.readByte();
69 + byte temp = flags;
70 + //first Bit : Optional (1) or well-known (0)
71 + firstBit = ((temp & FIRST_BIT) == FIRST_BIT);
72 + //second Bit : Transitive (1) or non-Transitive (0)
73 + secondBit = ((temp & SECOND_BIT) == SECOND_BIT);
74 + //third Bit : partial (1) or complete (0)
75 + thirdBit = ((temp & THIRD_BIT) == THIRD_BIT);
76 + //forth Bit(Extended Length bit) : Attribute Length is 1 octects (0) or 2 octects (1)
77 + fourthBit = ((temp & FOURTH_BIT) == FOURTH_BIT);
78 + int len;
79 + if (fourthBit) {
80 + isShort = true;
81 + short length = cb.readShort();
82 + len = length;
83 + } else {
84 + isShort = false;
85 + byte length = cb.readByte();
86 + len = length;
87 + }
88 + return new Validation(firstBit, secondBit, thirdBit, fourthBit, len, isShort);
89 + }
90 +
91 + /**
92 + * Throws exception if length is not correct.
93 + *
94 + * @param errorCode Error code
95 + * @param subErrCode Sub Error Code
96 + * @param length erroneous length
97 + * @throws BGPParseException for erroneous length
98 + */
99 + public static void validateLen(byte errorCode, byte subErrCode, int length) throws BGPParseException {
100 + byte[] errLen = Ints.toByteArray(length);
101 + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
102 + buffer.writeBytes(errLen);
103 + throw new BGPParseException(errorCode, subErrCode, buffer);
104 + }
105 +
106 + /**
107 + * Throws exception if type is not correct.
108 + *
109 + * @param errorCode Error code
110 + * @param subErrCode Sub Error Code
111 + * @param type erroneous type
112 + * @throws BGPParseException for erroneous type
113 + */
114 + public static void validateType(byte errorCode, byte subErrCode, int type) throws BGPParseException {
115 + byte[] errType = Ints.toByteArray(type);
116 + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
117 + buffer.writeBytes(errType);
118 + throw new BGPParseException(errorCode, subErrCode, buffer);
119 + }
120 +
121 + /**
122 + * Returns first bit in type flags.
123 + *
124 + * @return first bit in type flags
125 + */
126 + public boolean getFirstBit() {
127 + return this.firstBit;
128 + }
129 +
130 + /**
131 + * Returns second bit in type flags.
132 + *
133 + * @return second bit in type flags
134 + */
135 + public boolean getSecondBit() {
136 + return this.secondBit;
137 + }
138 +
139 + /**
140 + * Returns third bit in type flags.
141 + *
142 + * @return third bit in type flags
143 + */
144 + public boolean getThirdBit() {
145 + return this.thirdBit;
146 + }
147 +
148 + /**
149 + * Returns fourth bit in type flags.
150 + *
151 + * @return fourth bit in type flags
152 + */
153 + public boolean getFourthBit() {
154 + return this.fourthBit;
155 + }
156 +
157 + /**
158 + * Returns attribute length.
159 + *
160 + * @return attribute length
161 + */
162 + public int getLength() {
163 + return this.len;
164 + }
165 +
166 + /**
167 + * Returns whether attribute length read in short or byte.
168 + *
169 + * @return whether attribute length read in short or byte
170 + */
171 + public boolean isShort() {
172 + return this.isShort;
173 + }
174 +
175 + /**
176 + * Converts byte array of prefix value to IpPrefix object.
177 + *
178 + * @param value byte array of prefix value
179 + * @param length prefix length in bits
180 + * @return object of IpPrefix
181 + */
182 + public static IpPrefix bytesToPrefix(byte[] value, int length) {
183 + if (value.length != IPV4_SIZE) {
184 + value = Arrays.copyOf(value, IPV4_SIZE);
185 + }
186 + IpPrefix ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, length);
187 + return ipPrefix;
188 + }
189 +}
...\ No newline at end of file ...\ No newline at end of file