70c15ee6148c6e6fbda8ecfd22316985c6045d58
[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 import com.google.common.base.MoreObjects.ToStringHelper;
27
28 /**
29  * Provides SharedRiskLinkGroupTlv.
30  */
31 public class SharedRiskLinkGroupTlv implements PcepValueType {
32
33     /*
34      * Reference :[I-D.ietf-idr- Group ls-distribution] /3.3.2.5
35      *
36      *  0                   1                   2                   3
37       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
38      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39      |              Type =TDB41      |             Length            |
40      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41      |                  Shared Risk Link Group Value                 |
42      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43      //                         ............                        //
44      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45      |                  Shared Risk Link Group Value                 |
46      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47      */
48
49     protected static final Logger log = LoggerFactory.getLogger(SharedRiskLinkGroupTlv.class);
50
51     public static final short TYPE = 1096; //TODO:NEED TO HANDLE TDB41
52
53     private final short hLength;
54
55     private final int[] srlgValue;
56
57     /**
58      * Constructor to initialize SRLG value.
59      *
60      * @param srlgValue Shared Risk Link Group Value
61      * @param hLength length
62      */
63     public SharedRiskLinkGroupTlv(int[] srlgValue, short hLength) {
64         this.srlgValue = srlgValue;
65         if (0 == hLength) {
66             this.hLength = (short) ((srlgValue.length) * 4);
67         } else {
68             this.hLength = hLength;
69         }
70     }
71
72     /**
73      * Returns object of SharedRiskLinkGroupTlv.
74      *
75      * @param raw value
76      * @param hLength length
77      * @return object of SharedRiskLinkGroupTlv
78      */
79     public static SharedRiskLinkGroupTlv of(final int[] raw, short hLength) {
80         return new SharedRiskLinkGroupTlv(raw, hLength);
81     }
82
83     /**
84      * Returns SRLG Value.
85      *
86      * @return srlgValue
87      */
88     public int[] getValue() {
89         return srlgValue;
90     }
91
92     @Override
93     public PcepVersion getVersion() {
94         return PcepVersion.PCEP_1;
95     }
96
97     @Override
98     public short getType() {
99         return TYPE;
100     }
101
102     @Override
103     public short getLength() {
104         return hLength;
105     }
106
107     @Override
108     public int hashCode() {
109         return Objects.hash(srlgValue);
110     }
111
112     @Override
113     public boolean equals(Object obj) {
114         if (this == obj) {
115             return true;
116         }
117         if (obj instanceof SharedRiskLinkGroupTlv) {
118             SharedRiskLinkGroupTlv other = (SharedRiskLinkGroupTlv) obj;
119             return Objects.equals(this.srlgValue, other.srlgValue);
120         }
121         return false;
122     }
123
124     @Override
125     public int write(ChannelBuffer c) {
126         int iLenStartIndex = c.writerIndex();
127         c.writeShort(TYPE);
128         c.writeShort(hLength);
129         for (int b : srlgValue) {
130             c.writeInt(b);
131         }
132         return c.writerIndex() - iLenStartIndex;
133     }
134
135     /**
136      * Reads from channel buffer and returns object of SharedRiskLinkGroupTlv.
137      *
138      * @param c input channel buffer
139      * @param hLength length
140      * @return object of SharedRiskLinkGroupTlv
141      */
142     public static PcepValueType read(ChannelBuffer c, short hLength) {
143         int iLength = hLength / 4;
144         int[] iSharedRiskLinkGroup = new int[iLength];
145         for (int i = 0; i < iLength; i++) {
146             iSharedRiskLinkGroup[i] = c.readInt();
147         }
148         return new SharedRiskLinkGroupTlv(iSharedRiskLinkGroup, hLength);
149     }
150
151
152     @Override
153     public String toString() {
154         ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
155
156         toStrHelper.add("Type", TYPE);
157         toStrHelper.add("Length", hLength);
158
159         StringBuffer result = new StringBuffer();
160         for (int b : srlgValue) {
161             result.append(String.format("%02X ", b));
162         }
163         toStrHelper.add("Value", result);
164
165         return toStrHelper.toString();
166     }
167 }