blob: e530097047181602f1e6447866a8e8ca4ddcdbbb [file] [log] [blame]
Kiran Ramachandra959353a2016-02-16 22:12:07 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Kiran Ramachandra959353a2016-02-16 22:12:07 +05303 *
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;
Ray Milkey019fba42018-01-31 14:07:47 -080029import java.net.UnknownHostException;
Kiran Ramachandra959353a2016-02-16 22:12:07 +053030
31/**
32 * Representation of link id value of link tlv of Traffic Engineering.
33 */
34public class LinkId extends TlvHeader implements LinkSubType {
35 private static final Logger log =
36 LoggerFactory.getLogger(LinkId.class);
37 private String linkId;
38
39 /**
40 * Creates an instance of link id.
41 *
42 * @param header tlv header instance
43 */
44 public LinkId(TlvHeader header) {
45 this.setTlvType(header.tlvType());
46 this.setTlvLength(header.tlvLength());
47 }
48
49 /**
50 * Sets link type.
51 *
52 * @param linkType link type value
53 */
54 public void setLinkId(String linkType) {
55 this.linkId = linkType;
56 }
57
58 /**
59 * Reads bytes from channel buffer.
60 *
61 * @param channelBuffer channel buffer instance
Ray Milkey019fba42018-01-31 14:07:47 -080062 * @throws OspfParseException might throws exception while parsing packet
Kiran Ramachandra959353a2016-02-16 22:12:07 +053063 */
Ray Milkey019fba42018-01-31 14:07:47 -080064 public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
Kiran Ramachandra959353a2016-02-16 22:12:07 +053065 try {
66 byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
67 channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
68 this.setLinkId(InetAddress.getByAddress(tempByteArray).getHostName());
Ray Milkey019fba42018-01-31 14:07:47 -080069 } catch (UnknownHostException e) {
Kiran Ramachandra959353a2016-02-16 22:12:07 +053070 log.debug("Error::LinkId:: {}", e.getMessage());
71 throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
72 OspfErrorType.BAD_MESSAGE);
73 }
74 }
75
76 /**
77 * Returns instance as byte array.
78 *
79 * @return instance as bytes
Ray Milkey019fba42018-01-31 14:07:47 -080080 * @throws OspfParseException might throws exception while parsing packet
Kiran Ramachandra959353a2016-02-16 22:12:07 +053081 */
Ray Milkey019fba42018-01-31 14:07:47 -080082 public byte[] asBytes() throws OspfParseException {
Kiran Ramachandra959353a2016-02-16 22:12:07 +053083 byte[] linkSubType = null;
84
85 byte[] linkSubTlvHeader = getTlvHeaderAsByteArray();
86 byte[] linkSubTlvBody = getLinkSubTypeTlvBodyAsByteArray();
87 linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody);
88
89 return linkSubType;
90 }
91
92 /**
93 * Gets byte array of link id sub tlv body.
94 *
95 * @return gets the body as byte array
Ray Milkey019fba42018-01-31 14:07:47 -080096 * @throws OspfParseException might throws exception while parsing packet
Kiran Ramachandra959353a2016-02-16 22:12:07 +053097 */
Ray Milkey019fba42018-01-31 14:07:47 -080098 public byte[] getLinkSubTypeTlvBodyAsByteArray() throws OspfParseException {
Kiran Ramachandra959353a2016-02-16 22:12:07 +053099 byte[] linkSubTypeBody = null;
100 try {
101 linkSubTypeBody = InetAddress.getByName(this.linkId).getAddress();
102 } catch (Exception e) {
103 log.debug("Error::LinkId:: {}", e.getMessage());
104 throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
105 OspfErrorType.BAD_MESSAGE);
106 }
107 return linkSubTypeBody;
108 }
109
110 /**
111 * Returns this instance as string.
112 *
113 * @return this instance as string
114 */
115 @Override
116 public String toString() {
117 return MoreObjects.toStringHelper(getClass())
118 .add("linkId", linkId)
119 .toString();
120 }
121}