blob: f87b95ab9a5736112a35859c15a5570c6ea5d386 [file] [log] [blame]
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07003 *
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.pcepio.types;
17
18import java.util.Objects;
19
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onosproject.pcepio.protocol.PcepVersion;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import com.google.common.base.MoreObjects;
26
27/**
28 * Provide Link Protection Type.
29 */
30
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053031public class LinkProtectionTypeSubTlv implements PcepValueType {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070032
33 /* Reference :[RFC5307]/1.2
34 * 0 1 2 3
35 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
36 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 | Type=[TDB38] | Length=2 |
38 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 |Protection Cap | Reserved |
40 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053042 protected static final Logger log = LoggerFactory.getLogger(LinkProtectionTypeSubTlv.class);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070043
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053044 public static final short TYPE = 27;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070045 public static final short LENGTH = 2;
46 private final byte protectionCap;
47 private final byte reserved;
48
49 /**
50 * Constructor to initialize protectionCap.
51 *
52 * @param protectionCap Protection Cap
53 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053054 public LinkProtectionTypeSubTlv(byte protectionCap) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070055 this.protectionCap = protectionCap;
56 this.reserved = 0;
57 }
58
59 /**
60 * Constructor to initialize protectionCap, reserved.
61 *
62 * @param protectionCap Protection Cap
63 * @param reserved Reserved value
64 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053065 public LinkProtectionTypeSubTlv(byte protectionCap, byte reserved) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070066 this.protectionCap = protectionCap;
67 this.reserved = reserved;
68 }
69
70 /**
71 * Returns Protection Cap.
72 *
73 * @return protectionCap Protection Cap
74 */
75 public byte getProtectionCap() {
76 return protectionCap;
77 }
78
79 @Override
80 public PcepVersion getVersion() {
81 return PcepVersion.PCEP_1;
82 }
83
84 @Override
85 public short getType() {
86 return TYPE;
87 }
88
89 @Override
90 public short getLength() {
91 return LENGTH;
92 }
93
94 @Override
95 public int hashCode() {
96 return Objects.hash(protectionCap, reserved);
97 }
98
99 @Override
100 public boolean equals(Object obj) {
101 if (this == obj) {
102 return true;
103 }
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530104 if (obj instanceof LinkProtectionTypeSubTlv) {
105 LinkProtectionTypeSubTlv other = (LinkProtectionTypeSubTlv) obj;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700106 return Objects.equals(protectionCap, other.protectionCap) && Objects.equals(reserved, other.reserved);
107 }
108
109 return false;
110 }
111
112 @Override
113 public int write(ChannelBuffer c) {
114 int iLenStartIndex = c.writerIndex();
115 c.writeShort(TYPE);
116 c.writeShort(LENGTH);
117 c.writeByte(protectionCap);
118 c.writeByte(reserved);
119 return c.writerIndex() - iLenStartIndex;
120 }
121
122 /**
123 * Reads the channel buffer and returns object of LinkProtectionTypeTlv.
124 *
125 * @param c input channel buffer
126 * @return object of LinkProtectionTypeTlv
127 */
128 public static PcepValueType read(ChannelBuffer c) {
129 byte protectionCap = c.readByte();
130 byte reserved = c.readByte();
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530131 return new LinkProtectionTypeSubTlv(protectionCap, reserved);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700132 }
133
134 @Override
135 public String toString() {
Sho SHIMIZU7cdbcf72015-09-03 14:43:05 -0700136 return MoreObjects.toStringHelper(getClass())
137 .add("Type", TYPE)
138 .add("Length", LENGTH)
139 .add("ProtectionCap", protectionCap)
140 .toString();
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700141 }
Sho SHIMIZU7cdbcf72015-09-03 14:43:05 -0700142}