blob: 6c2c96d92a33e02544e91cdbdf28f901bdbc855a [file] [log] [blame]
Priyanka B0bee1f82015-10-13 15:39:49 +05301/*
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 */
Jonathan Hart317f4762015-11-09 16:05:36 -080016package org.onosproject.bgpio.protocol.linkstate;
Priyanka B0bee1f82015-10-13 15:39:49 +053017
18import java.util.Objects;
19
20import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053021import org.onosproject.bgpio.exceptions.BgpParseException;
22import org.onosproject.bgpio.types.BgpErrorType;
Priyanka B0bee1f82015-10-13 15:39:49 +053023import org.onosproject.bgpio.util.Constants;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import com.google.common.base.MoreObjects;
28
29/**
30 * Implementation of Node Identifier which includes local node descriptor/remote node descriptors.
31 */
Priyanka B02040732015-11-29 11:30:29 +053032public class BgpNodeLSIdentifier implements Comparable<Object> {
Priyanka B0bee1f82015-10-13 15:39:49 +053033
Priyanka B02040732015-11-29 11:30:29 +053034 private static final Logger log = LoggerFactory.getLogger(BgpNodeLSIdentifier.class);
Priyanka B0bee1f82015-10-13 15:39:49 +053035 private NodeDescriptors nodeDescriptors;
36
37 /**
38 * Resets fields.
39 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053040 public BgpNodeLSIdentifier() {
Priyanka B0bee1f82015-10-13 15:39:49 +053041 this.nodeDescriptors = null;
42 }
43
44 /**
45 * Constructor to initialize fields.
46 *
47 * @param nodeDescriptors local/remote node descriptor
48 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053049 public BgpNodeLSIdentifier(NodeDescriptors nodeDescriptors) {
Priyanka B0bee1f82015-10-13 15:39:49 +053050 this.nodeDescriptors = nodeDescriptors;
51 }
52
53 /**
54 * Parse local node descriptors.
55 *
56 * @param cb ChannelBuffer
57 * @param protocolId protocol identifier
58 * @return object of this BGPNodeLSIdentifier
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053059 * @throws BgpParseException while parsing local node descriptors
Priyanka B0bee1f82015-10-13 15:39:49 +053060 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053061 public static BgpNodeLSIdentifier parseLocalNodeDescriptors(ChannelBuffer cb, byte protocolId)
62 throws BgpParseException {
Priyanka B02040732015-11-29 11:30:29 +053063 log.debug("parse Local node descriptor");
64 ChannelBuffer tempBuf = cb.copy();
Priyanka B0bee1f82015-10-13 15:39:49 +053065 short type = cb.readShort();
66 short length = cb.readShort();
67 if (cb.readableBytes() < length) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053068 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR,
Priyanka B0bee1f82015-10-13 15:39:49 +053069 tempBuf.readBytes(cb.readableBytes() + Constants.TYPE_AND_LEN));
70 }
71 NodeDescriptors nodeDescriptors = new NodeDescriptors();
72 ChannelBuffer tempCb = cb.readBytes(length);
73
74 if (type == NodeDescriptors.LOCAL_NODE_DES_TYPE) {
75 nodeDescriptors = NodeDescriptors.read(tempCb, length, type, protocolId);
76 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053077 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.MALFORMED_ATTRIBUTE_LIST, null);
Priyanka B0bee1f82015-10-13 15:39:49 +053078 }
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053079 return new BgpNodeLSIdentifier(nodeDescriptors);
Priyanka B0bee1f82015-10-13 15:39:49 +053080 }
81
82 /**
83 * Returns node descriptors.
84 *
85 * @return node descriptors
86 */
87 public NodeDescriptors getNodedescriptors() {
88 return this.nodeDescriptors;
89 }
90
91 @Override
92 public boolean equals(Object obj) {
93 if (this == obj) {
94 return true;
95 }
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053096 if (obj instanceof BgpNodeLSIdentifier) {
97 BgpNodeLSIdentifier other = (BgpNodeLSIdentifier) obj;
Priyanka B0bee1f82015-10-13 15:39:49 +053098 return Objects.equals(nodeDescriptors, other.nodeDescriptors);
99 }
100 return false;
101 }
102
103 @Override
104 public int hashCode() {
105 return Objects.hash(nodeDescriptors);
106 }
107
108 @Override
109 public String toString() {
110 return MoreObjects.toStringHelper(getClass())
111 .add("NodeDescriptors", nodeDescriptors)
112 .toString();
113 }
Priyanka B02040732015-11-29 11:30:29 +0530114
115 @Override
116 public int compareTo(Object o) {
117 if (this.equals(o)) {
118 return 0;
119 }
120 return this.nodeDescriptors.compareTo(((BgpNodeLSIdentifier) o).nodeDescriptors);
121 }
Jonathan Hart317f4762015-11-09 16:05:36 -0800122}