blob: 176604f5332064277523422ee8eaae71ce2fe355 [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.protocol.lsa.TlvHeader;
22import org.onosproject.ospf.protocol.util.OspfUtil;
23
24/**
25 * Representation of traffic engineering metric TE value.
26 */
27public class TrafficEngineeringMetric extends TlvHeader implements LinkSubType {
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 /**
50 * Gets TE metric value.
51 *
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(OspfUtil.byteToLong(tempByteArray));
67 }
68
69 /**
70 * Gets instance as byte array.
71 *
72 * @return instance as byte array
73 */
74 public byte[] asBytes() {
75 byte[] linkSubType = null;
76
77 byte[] linkSubTlvHeader = getTlvHeaderAsByteArray();
78 byte[] linkSubTlvBody = getLinkSubTypeTlvBodyAsByteArray();
79 linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody);
80
81 return linkSubType;
82 }
83
84 /**
85 * Gets trafficEngineeringMetric as byte array .
86 *
87 * @return byte array of trafficEngineeringMetric
88 */
89 public byte[] getLinkSubTypeTlvBodyAsByteArray() {
90
91 byte[] linkSubTypeBody;
92 linkSubTypeBody = OspfUtil.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}