5fa0a4c6860e671853f159a83ff0017c603bcf60
[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  * Provides Local and remote Link Identifiers.
29  */
30 public class LinkLocalRemoteIdentifiersTlv implements PcepValueType {
31
32     /* Reference :RFC5307
33      * 0                   1                   2                   3
34       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
35      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36      |              Type=4      |             Length=8               |
37      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38      |               Link Local Identifier                           |
39      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40      |               Link Remote Identifier                          |
41      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42
43      */
44     protected static final Logger log = LoggerFactory.getLogger(LinkLocalRemoteIdentifiersTlv.class);
45
46     public static final short TYPE = 4;
47     public static final short LENGTH = 8;
48     private final int iLinkLocalIdentifier;
49     private final int iLinkRemoteIdentifier;
50
51     /**
52      * Constructor to initialize iLinkLocalIdentifier , iLinkRemoteIdentifier.
53      *
54      * @param iLinkLocalIdentifier Link Local identifier
55      * @param iLinkRemoteIdentifier Link Remote identifier
56      */
57     public LinkLocalRemoteIdentifiersTlv(int iLinkLocalIdentifier, int iLinkRemoteIdentifier) {
58         this.iLinkLocalIdentifier = iLinkLocalIdentifier;
59         this.iLinkRemoteIdentifier = iLinkRemoteIdentifier;
60     }
61
62     /**
63      * Retruns an object of Link Local Remote Identifiers Tlv.
64      *
65      * @param iLinkLocalIdentifier Link Local identifier
66      * @param iLinkRemoteIdentifier Link Remote identifier
67      * @return object of LinkLocalRemoteIdentifiersTlv
68      */
69     public static LinkLocalRemoteIdentifiersTlv of(int iLinkLocalIdentifier, int iLinkRemoteIdentifier) {
70         return new LinkLocalRemoteIdentifiersTlv(iLinkLocalIdentifier, iLinkRemoteIdentifier);
71     }
72
73     /**
74      * Returns Link-Local-Identifier.
75      *
76      * @return iLinkLocalIdentifier Link Local Identifier
77      */
78     public int getLinkLocalIdentifier() {
79         return iLinkLocalIdentifier;
80     }
81
82     /**
83      * Returns Link-Remote-Identifier.
84      *
85      * @return iLinkRemoteIdentifier Link Remote Identifier.
86      */
87     public int getLinkRemoteIdentifier() {
88         return iLinkRemoteIdentifier;
89     }
90
91     @Override
92     public PcepVersion getVersion() {
93         return PcepVersion.PCEP_1;
94     }
95
96     @Override
97     public short getLength() {
98         return LENGTH;
99     }
100
101     @Override
102     public short getType() {
103         return TYPE;
104     }
105
106     @Override
107     public int hashCode() {
108         return Objects.hash(iLinkLocalIdentifier, iLinkRemoteIdentifier);
109     }
110
111     @Override
112     public boolean equals(Object obj) {
113         if (this == obj) {
114             return true;
115         }
116         if (obj instanceof LinkLocalRemoteIdentifiersTlv) {
117             LinkLocalRemoteIdentifiersTlv other = (LinkLocalRemoteIdentifiersTlv) obj;
118             return Objects.equals(iLinkLocalIdentifier, other.iLinkLocalIdentifier)
119                     && Objects.equals(iLinkRemoteIdentifier, other.iLinkRemoteIdentifier);
120         }
121         return false;
122     }
123
124     @Override
125     public int write(ChannelBuffer c) {
126         int iStartIndex = c.writerIndex();
127         c.writeShort(TYPE);
128         c.writeShort(LENGTH);
129         c.writeInt(iLinkLocalIdentifier);
130         c.writeInt(iLinkRemoteIdentifier);
131         return c.writerIndex() - iStartIndex;
132     }
133
134     /**
135      * Reads the channel buffer and returns object of LinkLocalRemoteIdentifiersTlv.
136      *
137      * @param c input channel buffer
138      * @return object of LinkLocalRemoteIdentifiersTlv
139      */
140     public static PcepValueType read(ChannelBuffer c) {
141         int iLinkLocalIdentifier = c.readInt();
142         int iLinkRemoteIdentifier = c.readInt();
143         return new LinkLocalRemoteIdentifiersTlv(iLinkLocalIdentifier, iLinkRemoteIdentifier);
144     }
145
146     @Override
147     public String toString() {
148         return MoreObjects.toStringHelper(getClass())
149                 .add("Type", TYPE)
150                 .add("Length", LENGTH)
151                 .add("LinkLocalIdentifier", iLinkLocalIdentifier)
152                 .add("LinkRemoteIdentifier", iLinkRemoteIdentifier)
153                 .toString();
154     }
155 }