blob: 703d0b224be098947b0f41aaac72507466f4d63f [file] [log] [blame]
Priyanka B21f4b732016-03-21 20:54:12 +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 */
16
17package org.onosproject.pcepio.types;
18
19import java.util.Objects;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onosproject.pcepio.protocol.PcepVersion;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import com.google.common.base.MoreObjects;
27
28/**
29 * Provides SR PCE Capability Tlv.
30 */
31public class SrPceCapabilityTlv implements PcepValueType {
32
33 /*
34 *
35 reference : draft-ietf-pce-segment-routing-06, section 5.1.1
36
37 0 1 2 3
38 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
39 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 | Type=TBD | Length=4 |
41 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 | Reserved | Flags | MSD |
43 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 fig: SR-PCE-CAPABILITY TLV format
45 */
46 protected static final Logger log = LoggerFactory.getLogger(SrPceCapabilityTlv.class);
47
48 public static final short TYPE = 26;
49 public static final short LENGTH = 4;
50
51 private final byte msd;
52
53 /**
54 * Constructor to initialize its parameter.
55 *
56 * @param msd maximum SID depth
57 */
58 public SrPceCapabilityTlv(byte msd) {
59 this.msd = msd;
60 }
61
62 /**
63 * Obtains newly created SrPceCapabilityTlv object.
64 *
65 * @param msd maximum SID depth
66 * @return object of SrPceCapabilityTlv
67 */
68 public static SrPceCapabilityTlv of(final byte msd) {
69 return new SrPceCapabilityTlv(msd);
70 }
71
72 /**
73 * Obtains msd.
74 *
75 * @return msd
76 */
77 public byte msd() {
78 return msd;
79 }
80
81 @Override
82 public PcepVersion getVersion() {
83 return PcepVersion.PCEP_1;
84 }
85
86 @Override
87 public short getType() {
88 return TYPE;
89 }
90
91 @Override
92 public short getLength() {
93 return LENGTH;
94 }
95
96 @Override
97 public int hashCode() {
98 return Objects.hash(msd);
99 }
100
101 @Override
102 public boolean equals(Object obj) {
103 if (this == obj) {
104 return true;
105 }
106 if (obj instanceof SrPceCapabilityTlv) {
107 SrPceCapabilityTlv other = (SrPceCapabilityTlv) obj;
108 return Objects.equals(msd, other.msd);
109 }
110 return false;
111 }
112
113 @Override
114 public int write(ChannelBuffer c) {
115 int iLenStartIndex = c.writerIndex();
116 c.writeShort(TYPE);
117 c.writeShort(LENGTH);
118 c.writeInt(msd);
119 return c.writerIndex() - iLenStartIndex;
120 }
121
122 /**
123 * Reads the channel buffer and returns object of SrPceCapabilityTlv.
124 *
125 * @param cb channel buffer
126 * @return object of Gmpls-Capability-Tlv
127 */
128 public static SrPceCapabilityTlv read(ChannelBuffer cb) {
129 //read reserved bits
130 cb.readShort();
131 //read flags
132 cb.readByte();
133 return SrPceCapabilityTlv.of(cb.readByte());
134 }
135
136 @Override
137 public String toString() {
138 return MoreObjects.toStringHelper(getClass())
139 .add("Type", TYPE)
140 .add("Length", LENGTH)
141 .add("msd", msd)
142 .toString();
143 }
144}