a802d577d41f28aec6e5864b9f94b370ad472cf4
[onosfw.git] /
1 /*
2  * Copyright 2015 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 package org.onosproject.pcepio.types;
17
18 import java.util.Objects;
19
20 import org.jboss.netty.buffer.ChannelBuffer;
21 import org.onosproject.pcepio.protocol.PcepVersion;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.common.base.MoreObjects;
26
27 /**
28  * Provide Link Protection Type.
29  */
30
31 public class LinkProtectionTypeTlv implements PcepValueType {
32
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      */
42     protected static final Logger log = LoggerFactory.getLogger(LinkProtectionTypeTlv.class);
43
44     public static final short TYPE = 20; //TDB38
45     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      */
54     public LinkProtectionTypeTlv(byte protectionCap) {
55         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      */
65     public LinkProtectionTypeTlv(byte protectionCap, byte reserved) {
66         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         }
104         if (obj instanceof LinkProtectionTypeTlv) {
105             LinkProtectionTypeTlv other = (LinkProtectionTypeTlv) obj;
106             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();
131         return new LinkProtectionTypeTlv(protectionCap, reserved);
132     }
133
134     @Override
135     public String toString() {
136         return MoreObjects.toStringHelper(getClass())
137                 .add("Type", TYPE)
138                 .add("Length", LENGTH)
139                 .add("ProtectionCap", protectionCap)
140                 .toString();
141     }
142 }