Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 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 | */ |
Jonathan Hart | 317f476 | 2015-11-09 16:05:36 -0800 | [diff] [blame] | 16 | package org.onosproject.bgpio.protocol.linkstate; |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 17 | |
| 18 | import java.util.Objects; |
| 19 | |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 21 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 22 | import org.onosproject.bgpio.types.BgpErrorType; |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 23 | import org.onosproject.bgpio.util.Constants; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import com.google.common.base.MoreObjects; |
| 28 | |
| 29 | /** |
| 30 | * Implementation of Node Identifier which includes local node descriptor/remote node descriptors. |
| 31 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 32 | public class BgpNodeLSIdentifier { |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 33 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 34 | protected static final Logger log = LoggerFactory.getLogger(BgpNodeLSIdentifier.class); |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 35 | private NodeDescriptors nodeDescriptors; |
| 36 | |
| 37 | /** |
| 38 | * Resets fields. |
| 39 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 40 | public BgpNodeLSIdentifier() { |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 41 | this.nodeDescriptors = null; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Constructor to initialize fields. |
| 46 | * |
| 47 | * @param nodeDescriptors local/remote node descriptor |
| 48 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 49 | public BgpNodeLSIdentifier(NodeDescriptors nodeDescriptors) { |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 50 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 59 | * @throws BgpParseException while parsing local node descriptors |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 60 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 61 | public static BgpNodeLSIdentifier parseLocalNodeDescriptors(ChannelBuffer cb, byte protocolId) |
| 62 | throws BgpParseException { |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 63 | ChannelBuffer tempBuf = cb; |
| 64 | short type = cb.readShort(); |
| 65 | short length = cb.readShort(); |
| 66 | if (cb.readableBytes() < length) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 67 | throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR, |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 68 | tempBuf.readBytes(cb.readableBytes() + Constants.TYPE_AND_LEN)); |
| 69 | } |
| 70 | NodeDescriptors nodeDescriptors = new NodeDescriptors(); |
| 71 | ChannelBuffer tempCb = cb.readBytes(length); |
| 72 | |
| 73 | if (type == NodeDescriptors.LOCAL_NODE_DES_TYPE) { |
| 74 | nodeDescriptors = NodeDescriptors.read(tempCb, length, type, protocolId); |
| 75 | } else { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 76 | throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.MALFORMED_ATTRIBUTE_LIST, null); |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 77 | } |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 78 | return new BgpNodeLSIdentifier(nodeDescriptors); |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns node descriptors. |
| 83 | * |
| 84 | * @return node descriptors |
| 85 | */ |
| 86 | public NodeDescriptors getNodedescriptors() { |
| 87 | return this.nodeDescriptors; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public boolean equals(Object obj) { |
| 92 | if (this == obj) { |
| 93 | return true; |
| 94 | } |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 95 | if (obj instanceof BgpNodeLSIdentifier) { |
| 96 | BgpNodeLSIdentifier other = (BgpNodeLSIdentifier) obj; |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 97 | return Objects.equals(nodeDescriptors, other.nodeDescriptors); |
| 98 | } |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public int hashCode() { |
| 104 | return Objects.hash(nodeDescriptors); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public String toString() { |
| 109 | return MoreObjects.toStringHelper(getClass()) |
| 110 | .add("NodeDescriptors", nodeDescriptors) |
| 111 | .toString(); |
| 112 | } |
Jonathan Hart | 317f476 | 2015-11-09 16:05:36 -0800 | [diff] [blame] | 113 | } |