blob: 75d15625fbe7d21e7ec8ae25cbb33c06fc62af0c [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 */
16package org.onosproject.bgpio.types;
17
Jonathan Hart51539b82015-10-29 09:53:04 -070018import com.google.common.base.MoreObjects;
Priyanka Bb2988fa2015-10-09 12:45:36 +053019import org.jboss.netty.buffer.ChannelBuffer;
20import org.onlab.packet.Ip4Address;
Jonathan Hart51539b82015-10-29 09:53:04 -070021import org.onosproject.bgpio.protocol.IgpRouterId;
Priyanka Bb2988fa2015-10-09 12:45:36 +053022
Jonathan Hart51539b82015-10-29 09:53:04 -070023import java.util.Objects;
Priyanka Bb2988fa2015-10-09 12:45:36 +053024
25/**
26 * Provides implementation of OSPFPseudonode Tlv.
27 */
Jonathan Hart51539b82015-10-29 09:53:04 -070028public class OspfPseudonode implements IgpRouterId, BgpValueType {
Priyanka Bb2988fa2015-10-09 12:45:36 +053029 public static final short TYPE = 515;
30 public static final short LENGTH = 8;
31
32 private final int routerID;
33 private final Ip4Address drInterface;
34
35 /**
36 * Constructor to initialize parameters.
37 *
38 * @param routerID routerID
39 * @param drInterface IPv4 address of the DR's interface
40 */
Jonathan Hart51539b82015-10-29 09:53:04 -070041 public OspfPseudonode(int routerID, Ip4Address drInterface) {
Priyanka Bb2988fa2015-10-09 12:45:36 +053042 this.routerID = routerID;
43 this.drInterface = drInterface;
44 }
45
46 /**
47 * Returns object of this class with specified values.
48 *
49 * @param routerID routerID
50 * @param drInterface IPv4 address of the DR's interface
51 * @return object of OSPFPseudonode
52 */
Jonathan Hart51539b82015-10-29 09:53:04 -070053 public static OspfPseudonode of(final int routerID, final Ip4Address drInterface) {
54 return new OspfPseudonode(routerID, drInterface);
Priyanka Bb2988fa2015-10-09 12:45:36 +053055 }
56
57 /**
58 * Returns RouterID.
59 *
60 * @return RouterID
61 */
62 public int getrouterID() {
63 return this.routerID;
64 }
65
66 @Override
67 public int hashCode() {
68 return Objects.hash(routerID, drInterface);
69 }
70
71 @Override
72 public boolean equals(Object obj) {
73 if (this == obj) {
74 return true;
75 }
Jonathan Hart51539b82015-10-29 09:53:04 -070076 if (obj instanceof OspfPseudonode) {
77 OspfPseudonode other = (OspfPseudonode) obj;
Priyanka Bb2988fa2015-10-09 12:45:36 +053078 return Objects.equals(routerID, other.routerID) && Objects.equals(drInterface, other.drInterface);
79 }
80 return false;
81 }
82
83 @Override
84 public int write(ChannelBuffer c) {
85 int iLenStartIndex = c.writerIndex();
86 c.writeShort(TYPE);
87 c.writeShort(LENGTH);
88 c.writeInt(routerID);
89 c.writeInt(drInterface.toInt());
90 return c.writerIndex() - iLenStartIndex;
91 }
92
93 /**
94 * Reads the channel buffer and returns object of OSPFPseudonode.
95 *
96 * @param cb ChannelBuffer
97 * @return object of OSPFPseudonode
98 */
Jonathan Hart51539b82015-10-29 09:53:04 -070099 public static OspfPseudonode read(ChannelBuffer cb) {
Priyanka Bb2988fa2015-10-09 12:45:36 +0530100 int routerID = cb.readInt();
101 Ip4Address drInterface = Ip4Address.valueOf(cb.readInt());
Jonathan Hart51539b82015-10-29 09:53:04 -0700102 return OspfPseudonode.of(routerID, drInterface);
Priyanka Bb2988fa2015-10-09 12:45:36 +0530103 }
104
105 @Override
106 public short getType() {
107 return TYPE;
108 }
109
110 @Override
Priyanka B02040732015-11-29 11:30:29 +0530111 public int compareTo(Object o) {
112 if (this.equals(o)) {
113 return 0;
114 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700115 int result = ((Integer) (this.routerID)).compareTo((Integer) (((OspfPseudonode) o).routerID));
Priyanka B02040732015-11-29 11:30:29 +0530116 if (result != 0) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700117 return this.drInterface.compareTo(((OspfPseudonode) o).drInterface);
Priyanka B02040732015-11-29 11:30:29 +0530118 }
119 return result;
120 }
121
122 @Override
Priyanka Bb2988fa2015-10-09 12:45:36 +0530123 public String toString() {
124 return MoreObjects.toStringHelper(getClass())
125 .add("Type", TYPE)
126 .add("Length", LENGTH)
127 .add("RouterID", routerID)
128 .add("DRInterface", drInterface)
129 .toString();
130 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700131}