Kiran Ramachandra | 959353a | 2016-02-16 22:12:07 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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.tlvtypes; |
| 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.onlab.packet.Ip4Address; |
| 22 | import org.onosproject.ospf.exceptions.OspfErrorType; |
| 23 | import org.onosproject.ospf.exceptions.OspfParseException; |
| 24 | import org.onosproject.ospf.protocol.lsa.TlvHeader; |
| 25 | import org.onosproject.ospf.protocol.lsa.types.TopLevelTlv; |
| 26 | import org.onosproject.ospf.protocol.util.OspfUtil; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
| 32 | |
| 33 | /** |
| 34 | * Representation of an OSPF Opaque router tlv. |
| 35 | */ |
| 36 | public class RouterTlv extends TlvHeader implements TopLevelTlv { |
| 37 | |
| 38 | private static final Logger log = |
| 39 | LoggerFactory.getLogger(RouterTlv.class); |
| 40 | private Ip4Address routerAddress; |
| 41 | |
| 42 | /** |
| 43 | * Creates an instance of Opaque router tlv. |
| 44 | * |
| 45 | * @param header tlv header |
| 46 | */ |
| 47 | public RouterTlv(TlvHeader header) { |
| 48 | this.setTlvType(header.tlvType()); |
| 49 | this.setTlvLength(header.tlvLength()); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Gets router address. |
| 54 | * |
| 55 | * @return router address |
| 56 | */ |
| 57 | public Ip4Address routerAddress() { |
| 58 | return routerAddress; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Sets router address. |
| 63 | * |
| 64 | * @param routerAddress router address. |
| 65 | */ |
| 66 | public void setRouterAddress(Ip4Address routerAddress) { |
| 67 | this.routerAddress = routerAddress; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Reads bytes from channel buffer . |
| 72 | * |
| 73 | * @param channelBuffer channel buffer instance |
| 74 | * @throws Exception might throws exception while parsing buffer |
| 75 | */ |
| 76 | public void readFrom(ChannelBuffer channelBuffer) throws Exception { |
| 77 | try { |
| 78 | byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; |
| 79 | channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); |
| 80 | this.setRouterAddress(Ip4Address.valueOf(tempByteArray)); |
| 81 | } catch (Exception e) { |
| 82 | log.debug("Error::RouterTLV:: {}", e.getMessage()); |
| 83 | throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, |
| 84 | OspfErrorType.BAD_MESSAGE); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Gets router tlv as byte array. |
| 90 | * |
| 91 | * @return router tlv as byte array |
| 92 | */ |
| 93 | public byte[] asBytes() { |
| 94 | byte[] lsaMessage = null; |
| 95 | |
| 96 | byte[] tlvHeader = getTlvHeaderAsByteArray(); |
| 97 | byte[] tlvBody = getTlvBodyAsByteArray(); |
| 98 | lsaMessage = Bytes.concat(tlvHeader, tlvBody); |
| 99 | |
| 100 | return lsaMessage; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Gets tlv body as byte array. |
| 105 | * |
| 106 | * @return tlv body as byte array |
| 107 | */ |
| 108 | public byte[] getTlvBodyAsByteArray() { |
| 109 | List<Byte> bodyLst = new ArrayList<>(); |
| 110 | bodyLst.addAll(Bytes.asList(this.routerAddress().toOctets())); |
| 111 | |
| 112 | return Bytes.toArray(bodyLst); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public String toString() { |
| 117 | return MoreObjects.toStringHelper(getClass()) |
| 118 | .omitNullValues() |
| 119 | .add("routerAddress", routerAddress) |
| 120 | .toString(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | |