blob: d281a4ef3911a530925dbf6c8c7d77cabb9b5596 [file] [log] [blame]
Priyanka Bb2988fa2015-10-09 12:45:36 +05301/*
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
17package org.onosproject.bgpio.types;
18
19import java.util.Objects;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onosproject.bgpio.protocol.IGPRouterID;
Priyanka Bb2988fa2015-10-09 12:45:36 +053023
24import com.google.common.base.MoreObjects;
25
26/**
27 * Provides implementation of OSPFNonPseudonode Tlv.
28 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053029public class OSPFNonPseudonode implements IGPRouterID, BgpValueType {
Priyanka Bb2988fa2015-10-09 12:45:36 +053030 public static final short TYPE = 515;
31 public static final short LENGTH = 4;
32
33 private final int routerID;
34
35 /**
36 * Constructor to initialize routerID.
37 *
38 * @param routerID routerID
39 */
40 public OSPFNonPseudonode(int routerID) {
41 this.routerID = routerID;
42 }
43
44 /**
45 * Returns object of this class with specified routerID.
46 *
47 * @param routerID routerID
48 * @return object of OSPFNonPseudonode
49 */
50 public static OSPFNonPseudonode of(final int routerID) {
51 return new OSPFNonPseudonode(routerID);
52 }
53
54 /**
55 * Returns RouterID.
56 *
57 * @return RouterID
58 */
59 public int getrouterID() {
60 return this.routerID;
61 }
62
63 @Override
64 public int hashCode() {
65 return Objects.hash(routerID);
66 }
67
68 @Override
69 public boolean equals(Object obj) {
70 if (this == obj) {
71 return true;
72 }
73
74 if (obj instanceof OSPFNonPseudonode) {
75 OSPFNonPseudonode other = (OSPFNonPseudonode) obj;
76 return Objects.equals(routerID, other.routerID);
77 }
78 return false;
79 }
80
81 @Override
82 public int write(ChannelBuffer c) {
83 int iLenStartIndex = c.writerIndex();
84 c.writeShort(TYPE);
85 c.writeShort(LENGTH);
86 c.writeInt(routerID);
87 return c.writerIndex() - iLenStartIndex;
88 }
89
90 /**
91 * Reads the channel buffer and returns object of OSPFNonPseudonode.
92 *
93 * @param cb ChannelBuffer
94 * @return object of OSPFNonPseudonode
95 */
96 public static OSPFNonPseudonode read(ChannelBuffer cb) {
97 return OSPFNonPseudonode.of(cb.readInt());
98 }
99
100 @Override
101 public short getType() {
102 return TYPE;
103 }
104
105 @Override
Priyanka B02040732015-11-29 11:30:29 +0530106 public int compareTo(Object o) {
107 if (this.equals(o)) {
108 return 0;
109 }
110 return ((Integer) (this.routerID)).compareTo((Integer) (((OSPFNonPseudonode) o).routerID));
111 }
112
113 @Override
Priyanka Bb2988fa2015-10-09 12:45:36 +0530114 public String toString() {
115 return MoreObjects.toStringHelper(getClass())
116 .add("Type", TYPE)
117 .add("Length", LENGTH)
118 .add("RouterID", routerID)
119 .toString();
120 }
121}