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