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