blob: fa91d793681addc4a5c481812800f9c5b7341084 [file] [log] [blame]
sunishvka1dfc3e2016-04-16 12:24:47 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
sunishvka1dfc3e2016-04-16 12:24:47 +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.isis.io.isispacket.tlv.subtlv;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.primitives.Bytes;
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onosproject.isis.io.isispacket.tlv.TlvHeader;
22import org.onosproject.isis.io.util.IsisUtil;
23
24/**
25 * Representation of traffic engineering metric TE value.
26 */
27public class TrafficEngineeringMetric extends TlvHeader implements TrafficEngineeringSubTlv {
28 private long trafficEngineeringMetric;
29
30 /**
31 * Creates an instance of traffic engineering metric .
32 *
33 * @param header tlv header instance
34 */
35 public TrafficEngineeringMetric(TlvHeader header) {
36 this.setTlvType(header.tlvType());
37 this.setTlvLength(header.tlvLength());
38 }
39
40 /**
41 * Sets TE metric value.
42 *
43 * @param trafficEngineeringMetric value of trafficEngineeringMetric
44 */
45 public void setTrafficEngineeringMetric(long trafficEngineeringMetric) {
46 this.trafficEngineeringMetric = trafficEngineeringMetric;
47 }
48
49 /**
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053050 * Returns TE metric value.
sunishvka1dfc3e2016-04-16 12:24:47 +053051 *
52 * @return value of traffic engineering metric
53 */
54 public long getTrafficEngineeringMetricValue() {
55 return this.trafficEngineeringMetric;
56 }
57
58 /**
59 * Reads bytes from channel buffer .
60 *
61 * @param channelBuffer channel buffer instance
62 */
63 public void readFrom(ChannelBuffer channelBuffer) {
64 byte[] tempByteArray = new byte[tlvLength()];
65 channelBuffer.readBytes(tempByteArray, 0, tlvLength());
66 this.setTrafficEngineeringMetric(IsisUtil.byteToLong(tempByteArray));
67 }
68
69 /**
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053070 * Returns instance as byte array.
sunishvka1dfc3e2016-04-16 12:24:47 +053071 *
72 * @return instance as byte array
73 */
74 public byte[] asBytes() {
75 byte[] linkSubType = null;
76
77 byte[] linkSubTlvHeader = tlvHeaderAsByteArray();
78 byte[] linkSubTlvBody = tlvBodyAsBytes();
79 linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody);
80
81 return linkSubType;
82 }
83
84 /**
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053085 * Returns trafficEngineeringMetric as byte array .
sunishvka1dfc3e2016-04-16 12:24:47 +053086 *
87 * @return byte array of trafficEngineeringMetric
88 */
89 public byte[] tlvBodyAsBytes() {
90
91 byte[] linkSubTypeBody;
92 linkSubTypeBody = IsisUtil.convertToFourBytes(this.trafficEngineeringMetric);
93
94 return linkSubTypeBody;
95 }
96
97 @Override
98 public String toString() {
99 return MoreObjects.toStringHelper(getClass())
100 .omitNullValues()
101 .add("trafficEngineeringMetric", trafficEngineeringMetric)
102 .toString();
103 }
104}