988925f52f31113dced6e175829b0b0ceefa0440
[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.bgpio.types;
17
18 import java.util.Objects;
19
20 import org.jboss.netty.buffer.ChannelBuffer;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.google.common.base.MoreObjects;
25
26 /**
27  * Provides Implementation of Link Local/Remote IdentifiersTlv.
28  */
29 public class LinkLocalRemoteIdentifiersTlv implements BGPValueType {
30     private static final Logger log = LoggerFactory.getLogger(LinkLocalRemoteIdentifiersTlv.class);
31     public static final short TYPE = 258;
32     private static final int LENGTH = 8;
33
34     private final int linkLocalIdentifer;
35     private final int linkRemoteIdentifer;
36
37     /**
38      * Constructor to initialize parameters.
39      *
40      * @param linkLocalIdentifer link local Identifer
41      * @param linkRemoteIdentifer link remote Identifer
42      */
43     public LinkLocalRemoteIdentifiersTlv(int linkLocalIdentifer, int linkRemoteIdentifer) {
44         this.linkLocalIdentifer = linkLocalIdentifer;
45         this.linkRemoteIdentifer = linkRemoteIdentifer;
46     }
47
48     /**
49      * Returns link remote Identifer.
50      *
51      * @return link remote Identifer
52      */
53     public int getLinkRemoteIdentifier() {
54         return linkRemoteIdentifer;
55     }
56
57     /**
58      * Returns link local Identifer.
59      *
60      * @return link local Identifer
61      */
62     public int getLinkLocalIdentifier() {
63         return linkLocalIdentifer;
64     }
65
66     @Override
67     public short getType() {
68         return TYPE;
69     }
70
71     @Override
72     public int hashCode() {
73         return Objects.hash(linkLocalIdentifer, linkRemoteIdentifer);
74     }
75
76     @Override
77     public boolean equals(Object obj) {
78         if (this == obj) {
79             return true;
80         }
81         if (obj instanceof LinkLocalRemoteIdentifiersTlv) {
82             LinkLocalRemoteIdentifiersTlv other = (LinkLocalRemoteIdentifiersTlv) obj;
83             return Objects.equals(this.linkLocalIdentifer, other.linkLocalIdentifer)
84                     && Objects.equals(this.linkRemoteIdentifer, other.linkRemoteIdentifer);
85         }
86         return false;
87     }
88
89     @Override
90     public int write(ChannelBuffer cb) {
91         int iLenStartIndex = cb.writerIndex();
92         cb.writeShort(TYPE);
93         cb.writeShort(LENGTH);
94         cb.writeInt(linkLocalIdentifer);
95         cb.writeInt(linkRemoteIdentifer);
96         return cb.writerIndex() - iLenStartIndex;
97     }
98
99     /**
100      * Reads the channel buffer and returns object of LinkLocalRemoteIdentifiersTlv.
101      *
102      * @param cb channelBuffer
103      * @return object of LinkLocalRemoteIdentifiersTlv
104      */
105     public static LinkLocalRemoteIdentifiersTlv read(ChannelBuffer cb) {
106         int linkLocalIdentifer = cb.readInt();
107         int linkRemoteIdentifer = cb.readInt();
108         return LinkLocalRemoteIdentifiersTlv.of(linkLocalIdentifer, linkRemoteIdentifer);
109     }
110
111     /**
112      * Returns object of this class with specified link local identifer and link remote identifer.
113      *
114      * @param linkLocalIdentifer link local identifier
115      * @param linkRemoteIdentifer link remote identifier
116      * @return object of LinkLocalRemoteIdentifiersTlv
117      */
118     public static LinkLocalRemoteIdentifiersTlv of(final int linkLocalIdentifer, final int linkRemoteIdentifer) {
119         return new LinkLocalRemoteIdentifiersTlv(linkLocalIdentifer, linkRemoteIdentifer);
120     }
121
122     @Override
123     public String toString() {
124         return MoreObjects.toStringHelper(getClass())
125                 .add("TYPE", TYPE)
126                 .add("LENGTH", LENGTH)
127                 .add("linkLocalIdentifer", linkLocalIdentifer)
128                 .add("linkRemoteIdentifer", linkRemoteIdentifer)
129                 .toString();
130     }
131 }