2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.bgpio.protocol.linkstate;
18 import java.util.Objects;
20 import org.jboss.netty.buffer.ChannelBuffer;
21 import org.onosproject.bgpio.exceptions.BgpParseException;
22 import org.onosproject.bgpio.types.BgpErrorType;
23 import org.onosproject.bgpio.util.Constants;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import com.google.common.base.MoreObjects;
30 * Implementation of Node Identifier which includes local node descriptor/remote node descriptors.
32 public class BgpNodeLSIdentifier implements Comparable<Object> {
34 private static final Logger log = LoggerFactory.getLogger(BgpNodeLSIdentifier.class);
35 private NodeDescriptors nodeDescriptors;
40 public BgpNodeLSIdentifier() {
41 this.nodeDescriptors = null;
45 * Constructor to initialize fields.
47 * @param nodeDescriptors local/remote node descriptor
49 public BgpNodeLSIdentifier(NodeDescriptors nodeDescriptors) {
50 this.nodeDescriptors = nodeDescriptors;
54 * Parse local node descriptors.
56 * @param cb ChannelBuffer
57 * @param protocolId protocol identifier
58 * @return object of this BGPNodeLSIdentifier
59 * @throws BgpParseException while parsing local node descriptors
61 public static BgpNodeLSIdentifier parseLocalNodeDescriptors(ChannelBuffer cb, byte protocolId)
62 throws BgpParseException {
63 log.debug("parse Local node descriptor");
64 ChannelBuffer tempBuf = cb.copy();
65 short type = cb.readShort();
66 short length = cb.readShort();
67 if (cb.readableBytes() < length) {
68 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR,
69 tempBuf.readBytes(cb.readableBytes() + Constants.TYPE_AND_LEN));
71 NodeDescriptors nodeDescriptors = new NodeDescriptors();
72 ChannelBuffer tempCb = cb.readBytes(length);
74 if (type == NodeDescriptors.LOCAL_NODE_DES_TYPE) {
75 nodeDescriptors = NodeDescriptors.read(tempCb, length, type, protocolId);
77 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.MALFORMED_ATTRIBUTE_LIST, null);
79 return new BgpNodeLSIdentifier(nodeDescriptors);
83 * Returns node descriptors.
85 * @return node descriptors
87 public NodeDescriptors getNodedescriptors() {
88 return this.nodeDescriptors;
92 public boolean equals(Object obj) {
96 if (obj instanceof BgpNodeLSIdentifier) {
97 BgpNodeLSIdentifier other = (BgpNodeLSIdentifier) obj;
98 return Objects.equals(nodeDescriptors, other.nodeDescriptors);
104 public int hashCode() {
105 return Objects.hash(nodeDescriptors);
109 public String toString() {
110 return MoreObjects.toStringHelper(getClass())
111 .add("NodeDescriptors", nodeDescriptors)
116 public int compareTo(Object o) {
117 if (this.equals(o)) {
120 return this.nodeDescriptors.compareTo(((BgpNodeLSIdentifier) o).nodeDescriptors);