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 opaque node. |
| 32 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 33 | public class BgpAttrOpaqueNode implements BgpValueType { |
Thejaswi NK | 5b9278b | 2015-10-12 16:59:14 +0530 | [diff] [blame] | 34 | |
| 35 | protected static final Logger log = LoggerFactory |
| 36 | .getLogger(BgpAttrOpaqueNode.class); |
| 37 | |
| 38 | public static final int ATTRNODE_OPAQUEDATA = 1025; |
| 39 | |
| 40 | /* Opaque Node Attribute */ |
| 41 | private byte[] opaqueNodeAttribute; |
| 42 | |
| 43 | /** |
| 44 | * Constructor to initialize parameter. |
| 45 | * |
| 46 | * @param opaqueNodeAttribute opaque node attribute |
| 47 | */ |
| 48 | public BgpAttrOpaqueNode(byte[] opaqueNodeAttribute) { |
| 49 | this.opaqueNodeAttribute = Arrays.copyOf(opaqueNodeAttribute, opaqueNodeAttribute.length); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns object of this class with specified values. |
| 54 | * |
| 55 | * @param opaqueNodeAttribute Prefix Metric |
| 56 | * @return object of BgpAttrOpaqueNode |
| 57 | */ |
| 58 | public static BgpAttrOpaqueNode of(byte[] opaqueNodeAttribute) { |
| 59 | return new BgpAttrOpaqueNode(opaqueNodeAttribute); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Reads the Opaque Node Properties. |
| 64 | * |
| 65 | * @param cb ChannelBuffer |
| 66 | * @return object of BgpAttrOpaqueNode |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 67 | * @throws BgpParseException while parsing BgpAttrOpaqueNode |
Thejaswi NK | 5b9278b | 2015-10-12 16:59:14 +0530 | [diff] [blame] | 68 | */ |
| 69 | public static BgpAttrOpaqueNode 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 | |
| 72 | byte[] opaqueNodeAttribute; |
| 73 | |
| 74 | short lsAttrLength = cb.readShort(); |
| 75 | |
| 76 | if (cb.readableBytes() < lsAttrLength) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 77 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 78 | BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
Thejaswi NK | 5b9278b | 2015-10-12 16:59:14 +0530 | [diff] [blame] | 79 | lsAttrLength); |
| 80 | } |
| 81 | |
| 82 | opaqueNodeAttribute = new byte[lsAttrLength]; |
| 83 | cb.readBytes(opaqueNodeAttribute); |
| 84 | |
| 85 | return BgpAttrOpaqueNode.of(opaqueNodeAttribute); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns opaque node attribute. |
| 90 | * |
| 91 | * @return LS node attribute value |
| 92 | */ |
| 93 | public byte[] attrOpaqueNode() { |
| 94 | return opaqueNodeAttribute; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public short getType() { |
| 99 | return ATTRNODE_OPAQUEDATA; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public int hashCode() { |
| 104 | return Arrays.hashCode(opaqueNodeAttribute); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public boolean equals(Object obj) { |
| 109 | if (this == obj) { |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | if (obj instanceof BgpAttrOpaqueNode) { |
| 114 | BgpAttrOpaqueNode other = (BgpAttrOpaqueNode) obj; |
| 115 | return Arrays |
| 116 | .equals(opaqueNodeAttribute, other.opaqueNodeAttribute); |
| 117 | } |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public int write(ChannelBuffer cb) { |
| 123 | // TODO This will be implemented in the next version |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public String toString() { |
| 129 | return MoreObjects.toStringHelper(getClass()).omitNullValues() |
| 130 | .add("opaqueNodeAttribute", opaqueNodeAttribute).toString(); |
| 131 | } |
| 132 | |
| 133 | } |