blob: 0ae22b714024a02d61fc73ea5ecda3e0bcdd8386 [file] [log] [blame]
Kiran Ramachandra959353a2016-02-16 22:12:07 +05301/*
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 */
16package org.onosproject.ospf.protocol.lsa.linksubtype;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.primitives.Bytes;
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onosproject.ospf.exceptions.OspfErrorType;
22import org.onosproject.ospf.exceptions.OspfParseException;
23import org.onosproject.ospf.protocol.lsa.TlvHeader;
24import org.onosproject.ospf.protocol.util.OspfUtil;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import java.net.InetAddress;
29
30/**
31 * Representation of link id value of link tlv of Traffic Engineering.
32 */
33public class LinkId extends TlvHeader implements LinkSubType {
34 private static final Logger log =
35 LoggerFactory.getLogger(LinkId.class);
36 private String linkId;
37
38 /**
39 * Creates an instance of link id.
40 *
41 * @param header tlv header instance
42 */
43 public LinkId(TlvHeader header) {
44 this.setTlvType(header.tlvType());
45 this.setTlvLength(header.tlvLength());
46 }
47
48 /**
49 * Sets link type.
50 *
51 * @param linkType link type value
52 */
53 public void setLinkId(String linkType) {
54 this.linkId = linkType;
55 }
56
57 /**
58 * Reads bytes from channel buffer.
59 *
60 * @param channelBuffer channel buffer instance
61 * @throws Exception might throws exception while parsing packet
62 */
63 public void readFrom(ChannelBuffer channelBuffer) throws Exception {
64 try {
65 byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
66 channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
67 this.setLinkId(InetAddress.getByAddress(tempByteArray).getHostName());
68 } catch (Exception e) {
69 log.debug("Error::LinkId:: {}", e.getMessage());
70 throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
71 OspfErrorType.BAD_MESSAGE);
72 }
73 }
74
75 /**
76 * Returns instance as byte array.
77 *
78 * @return instance as bytes
79 * @throws Exception might throws exception while parsing packet
80 */
81 public byte[] asBytes() throws Exception {
82 byte[] linkSubType = null;
83
84 byte[] linkSubTlvHeader = getTlvHeaderAsByteArray();
85 byte[] linkSubTlvBody = getLinkSubTypeTlvBodyAsByteArray();
86 linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody);
87
88 return linkSubType;
89 }
90
91 /**
92 * Gets byte array of link id sub tlv body.
93 *
94 * @return gets the body as byte array
95 * @throws Exception might throws exception while parsing packet
96 */
97 public byte[] getLinkSubTypeTlvBodyAsByteArray() throws Exception {
98 byte[] linkSubTypeBody = null;
99 try {
100 linkSubTypeBody = InetAddress.getByName(this.linkId).getAddress();
101 } catch (Exception e) {
102 log.debug("Error::LinkId:: {}", e.getMessage());
103 throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
104 OspfErrorType.BAD_MESSAGE);
105 }
106 return linkSubTypeBody;
107 }
108
109 /**
110 * Returns this instance as string.
111 *
112 * @return this instance as string
113 */
114 @Override
115 public String toString() {
116 return MoreObjects.toStringHelper(getClass())
117 .add("linkId", linkId)
118 .toString();
119 }
120}