sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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.isis.io.isispacket.tlv.subtlv; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.primitives.Bytes; |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.onosproject.isis.io.isispacket.tlv.TlvHeader; |
| 22 | import org.onosproject.isis.io.util.IsisUtil; |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * Representation of an administrative group. |
| 27 | */ |
| 28 | public class AdministrativeGroup extends TlvHeader implements TrafficEngineeringSubTlv { |
| 29 | |
| 30 | private int administrativeGroup; |
| 31 | |
| 32 | /** |
| 33 | * Creates an administrative group instance. |
| 34 | * |
| 35 | * @param header Tlv Header instance |
| 36 | */ |
| 37 | public AdministrativeGroup(TlvHeader header) { |
| 38 | this.setTlvType(header.tlvType()); |
| 39 | this.setTlvLength(header.tlvLength()); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Gets administrative group value. |
| 44 | * |
| 45 | * @return administrative group value |
| 46 | */ |
| 47 | public int administrativeGroup() { |
| 48 | return administrativeGroup; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Sets administrative group value. |
| 53 | * |
| 54 | * @param administrativeGroup value |
| 55 | */ |
| 56 | public void setAdministrativeGroup(int administrativeGroup) { |
| 57 | this.administrativeGroup = administrativeGroup; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Gets administrative group value. |
| 62 | * |
| 63 | * @return administrativeGroup value |
| 64 | */ |
| 65 | public int getAdministrativeGroupValue() { |
| 66 | return this.administrativeGroup; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Reads bytes from channel buffer. |
| 71 | * |
| 72 | * @param channelBuffer Channel buffer instance |
| 73 | */ |
| 74 | public void readFrom(ChannelBuffer channelBuffer) { |
| 75 | byte[] tempByteArray = new byte[tlvLength()]; |
| 76 | channelBuffer.readBytes(tempByteArray, 0, tlvLength()); |
| 77 | this.setAdministrativeGroup(IsisUtil.byteToInteger(tempByteArray)); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns administrative group as byte array. |
| 82 | * |
| 83 | * @return administrative group instance as byte array |
| 84 | */ |
| 85 | public byte[] asBytes() { |
| 86 | byte[] linkSubType = null; |
| 87 | |
| 88 | byte[] linkSubTlvHeader = tlvHeaderAsByteArray(); |
| 89 | byte[] linkSubTlvBody = tlvBodyAsBytes(); |
| 90 | linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody); |
| 91 | |
| 92 | return linkSubType; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Gets administrative group body as byte array. |
| 97 | * |
| 98 | * @return byte array of sub tlv administrative group |
| 99 | */ |
| 100 | public byte[] tlvBodyAsBytes() { |
| 101 | |
| 102 | byte[] linkSubTypeBody; |
| 103 | linkSubTypeBody = IsisUtil.convertToFourBytes(this.administrativeGroup); |
| 104 | |
| 105 | return linkSubTypeBody; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public String toString() { |
| 110 | return MoreObjects.toStringHelper(getClass()) |
| 111 | .add("administrativeGroup", administrativeGroup) |
| 112 | .toString(); |
| 113 | } |
| 114 | } |