Priyanka B | b2988fa | 2015-10-09 12:45:36 +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 | |
| 17 | package org.onosproject.bgpio.types.attr; |
| 18 | |
| 19 | import java.util.Objects; |
| 20 | |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| 22 | import org.onosproject.bgpio.exceptions.BGPParseException; |
| 23 | import org.onosproject.bgpio.types.BGPErrorType; |
| 24 | import org.onosproject.bgpio.types.BGPValueType; |
| 25 | import org.onosproject.bgpio.util.Validation; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import com.google.common.base.MoreObjects; |
| 30 | |
| 31 | /** |
| 32 | * BGP Multi-Topology ID of the LS attribute. |
| 33 | */ |
| 34 | public class BgpAttrNodeMultiTopologyId implements BGPValueType { |
| 35 | |
| 36 | private static final Logger log = LoggerFactory |
| 37 | .getLogger(BgpAttrNodeMultiTopologyId.class); |
| 38 | |
| 39 | public static final int ATTRNODE_MULTITOPOLOGY = 263; |
| 40 | |
| 41 | /* Opaque Node Attribute */ |
| 42 | private short[] multiTopologyId; |
| 43 | |
| 44 | /** |
| 45 | * Constructor to initialize the Node attribute multi-topology ID. |
| 46 | * |
| 47 | * @param multiTopologyId multi-topology ID |
| 48 | */ |
| 49 | BgpAttrNodeMultiTopologyId(short[] multiTopologyId) { |
| 50 | this.multiTopologyId = multiTopologyId; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Reads the Multi-topology ID of Node attribute. |
| 55 | * |
| 56 | * @param cb ChannelBuffer |
| 57 | * @return Constructor of BgpAttrNodeMultiTopologyId |
| 58 | * @throws BGPParseException while parsing BgpAttrNodeMultiTopologyId |
| 59 | */ |
| 60 | public static BgpAttrNodeMultiTopologyId read(ChannelBuffer cb) |
| 61 | throws BGPParseException { |
| 62 | |
| 63 | log.debug("BgpAttrNodeMultiTopologyId"); |
| 64 | short lsAttrLength = cb.readShort(); |
| 65 | int len = lsAttrLength / 2; // Length is 2*n and n is the number of MT-IDs |
| 66 | |
| 67 | if (cb.readableBytes() < lsAttrLength) { |
| 68 | Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, |
| 69 | BGPErrorType.ATTRIBUTE_LENGTH_ERROR, |
| 70 | cb.readableBytes()); |
| 71 | } |
| 72 | |
| 73 | short[] multiTopologyId; |
| 74 | multiTopologyId = new short[len]; |
| 75 | for (int i = 0; i < len; i++) { |
| 76 | multiTopologyId[i] = cb.readShort(); |
| 77 | } |
| 78 | |
| 79 | return new BgpAttrNodeMultiTopologyId(multiTopologyId); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * to get the multi-topology ID. |
| 84 | * |
| 85 | * @return multitopology ID |
| 86 | */ |
| 87 | short[] getAttrMultiTopologyId() { |
| 88 | return multiTopologyId; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public short getType() { |
| 93 | return ATTRNODE_MULTITOPOLOGY; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public int hashCode() { |
| 98 | return Objects.hash(multiTopologyId); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public boolean equals(Object obj) { |
| 103 | if (this == obj) { |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | if (obj instanceof BgpAttrNodeMultiTopologyId) { |
| 108 | BgpAttrNodeMultiTopologyId other = (BgpAttrNodeMultiTopologyId) obj; |
| 109 | return Objects.equals(multiTopologyId, other.multiTopologyId); |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public int write(ChannelBuffer cb) { |
| 116 | // TODO Auto-generated method stub |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public String toString() { |
| 122 | return MoreObjects.toStringHelper(getClass()) |
| 123 | .omitNullValues() |
| 124 | .add("multiTopologyId", multiTopologyId) |
| 125 | .toString(); |
| 126 | } |
| 127 | } |