Kiran Ramachandra | 959353a | 2016-02-16 22:12:07 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Kiran Ramachandra | 959353a | 2016-02-16 22:12:07 +0530 | [diff] [blame] | 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.exceptions.OspfErrorType; |
| 22 | import org.onosproject.ospf.exceptions.OspfParseException; |
| 23 | import org.onosproject.ospf.protocol.lsa.TlvHeader; |
| 24 | import org.onosproject.ospf.protocol.util.OspfUtil; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | /** |
| 29 | * Representation of link type TE value. |
| 30 | */ |
| 31 | public class LinkType extends TlvHeader implements LinkSubType { |
| 32 | private static final Logger log = |
| 33 | LoggerFactory.getLogger(LinkType.class); |
| 34 | private int linkType; |
| 35 | |
| 36 | /** |
| 37 | * Creates link type instance . |
| 38 | */ |
| 39 | public LinkType() { |
| 40 | |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Creates link type instance. |
| 45 | * |
| 46 | * @param header tlv header instance |
| 47 | */ |
| 48 | public LinkType(TlvHeader header) { |
| 49 | this.setTlvType(header.tlvType()); |
| 50 | this.setTlvLength(header.tlvLength()); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Sets link type. |
| 55 | * |
| 56 | * @param linkType value of link type |
| 57 | */ |
| 58 | public void setLinkType(int linkType) { |
| 59 | this.linkType = linkType; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Reads from channel buffer. |
| 64 | * |
| 65 | * @param channelBuffer channel buffer instance |
| 66 | * @throws Exception might throws exception while parsing buffer |
| 67 | */ |
| 68 | public void readFrom(ChannelBuffer channelBuffer) throws Exception { |
| 69 | try { |
| 70 | int len = channelBuffer.readableBytes(); |
| 71 | byte[] tempByteArray = new byte[len]; |
| 72 | channelBuffer.readBytes(tempByteArray, 0, len); |
| 73 | this.setLinkType(OspfUtil.byteToInteger(tempByteArray)); |
| 74 | } catch (Exception e) { |
| 75 | log.debug("Error::LinkType:: {}", e.getMessage()); |
| 76 | throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, |
| 77 | OspfErrorType.BAD_MESSAGE); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Gets link subtype as byte array. |
| 83 | * |
| 84 | * @return byte array of link subtype |
| 85 | */ |
| 86 | public byte[] asBytes() { |
| 87 | byte[] linkSubType = null; |
| 88 | |
| 89 | byte[] linkSubTlvHeader = getTlvHeaderAsByteArray(); |
| 90 | byte[] linkSubTlvBody = getLinkSubTypeTlvBodyAsByteArray(); |
| 91 | linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody); |
| 92 | return linkSubType; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Gets link subtype as bytes. |
| 97 | * |
| 98 | * @return byte array of link subtype |
| 99 | */ |
| 100 | public byte[] getLinkSubTypeTlvBodyAsByteArray() { |
| 101 | byte[] linkSubTypeBody = new byte[4]; |
| 102 | linkSubTypeBody[0] = (byte) this.linkType; |
| 103 | linkSubTypeBody[1] = 0; |
| 104 | linkSubTypeBody[2] = 0; |
| 105 | linkSubTypeBody[3] = 0; |
| 106 | return linkSubTypeBody; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public String toString() { |
| 111 | return MoreObjects.toStringHelper(getClass()) |
| 112 | .add("linkType", linkType) |
| 113 | .toString(); |
| 114 | } |
| 115 | } |