Thejaswi NK | 5b9278b | 2015-10-12 16:59:14 +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 | */ |
| 16 | package org.onosproject.bgpio.types.attr; |
| 17 | |
| 18 | import java.util.Arrays; |
| 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; |
| 23 | import org.onosproject.bgpio.types.BgpValueType; |
Thejaswi NK | 5b9278b | 2015-10-12 16:59:14 +0530 | [diff] [blame] | 24 | import org.onosproject.bgpio.util.Validation; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import com.google.common.base.MoreObjects; |
| 29 | |
| 30 | /** |
| 31 | * Implements BGP attribute node name. |
| 32 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 33 | public class BgpAttrNodeName implements BgpValueType { |
Thejaswi NK | 5b9278b | 2015-10-12 16:59:14 +0530 | [diff] [blame] | 34 | |
| 35 | protected static final Logger log = LoggerFactory |
| 36 | .getLogger(BgpAttrNodeName.class); |
| 37 | |
| 38 | public static final int ATTRNODE_NAME = 1026; |
| 39 | |
| 40 | /* Node Name */ |
| 41 | private byte[] nodeName; |
| 42 | |
| 43 | /** |
| 44 | * Constructor to initialize value. |
| 45 | * |
| 46 | * @param nodeName node name |
| 47 | */ |
| 48 | public BgpAttrNodeName(byte[] nodeName) { |
| 49 | this.nodeName = Arrays.copyOf(nodeName, nodeName.length); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns object of this class with specified values. |
| 54 | * |
| 55 | * @param nodeName node name |
| 56 | * @return object of BgpAttrNodeName |
| 57 | */ |
| 58 | public static BgpAttrNodeName of(final byte[] nodeName) { |
| 59 | return new BgpAttrNodeName(nodeName); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Reads the LS attribute node name. |
| 64 | * |
| 65 | * @param cb ChannelBuffer |
| 66 | * @return object of BgpAttrNodeName |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 67 | * @throws BgpParseException while parsing BgpAttrNodeName |
Thejaswi NK | 5b9278b | 2015-10-12 16:59:14 +0530 | [diff] [blame] | 68 | */ |
| 69 | public static BgpAttrNodeName read(ChannelBuffer cb) |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 70 | throws BgpParseException { |
Thejaswi NK | 5b9278b | 2015-10-12 16:59:14 +0530 | [diff] [blame] | 71 | byte[] nodeName; |
| 72 | |
| 73 | short lsAttrLength = cb.readShort(); |
| 74 | |
| 75 | if (cb.readableBytes() < lsAttrLength) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 76 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 77 | BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
Thejaswi NK | 5b9278b | 2015-10-12 16:59:14 +0530 | [diff] [blame] | 78 | lsAttrLength); |
| 79 | } |
| 80 | |
| 81 | nodeName = new byte[lsAttrLength]; |
| 82 | cb.readBytes(nodeName); |
| 83 | return BgpAttrNodeName.of(nodeName); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns LS attribute node name. |
| 88 | * |
| 89 | * @return node name |
| 90 | */ |
| 91 | public byte[] attrNodeName() { |
| 92 | return nodeName; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public short getType() { |
| 97 | return ATTRNODE_NAME; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public int hashCode() { |
| 102 | return Arrays.hashCode(nodeName); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public boolean equals(Object obj) { |
| 107 | if (this == obj) { |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | if (obj instanceof BgpAttrNodeName) { |
| 112 | BgpAttrNodeName other = (BgpAttrNodeName) obj; |
| 113 | return Arrays.equals(nodeName, other.nodeName); |
| 114 | } |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public int write(ChannelBuffer cb) { |
| 120 | // TODO This will be implemented in the next version |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public String toString() { |
| 126 | return MoreObjects.toStringHelper(getClass()).omitNullValues() |
| 127 | .add("nodeName", nodeName).toString(); |
| 128 | } |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 129 | |
| 130 | @Override |
| 131 | public int compareTo(Object o) { |
| 132 | // TODO Auto-generated method stub |
| 133 | return 0; |
| 134 | } |
Thejaswi NK | 5b9278b | 2015-10-12 16:59:14 +0530 | [diff] [blame] | 135 | } |