Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 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.onlab.packet.Ip4Address; |
| 22 | import org.onosproject.bgpio.protocol.IGPRouterID; |
| 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
| 26 | import com.google.common.base.MoreObjects; |
| 27 | |
| 28 | /** |
| 29 | * Provides implementation of OSPFPseudonode Tlv. |
| 30 | */ |
| 31 | public class OSPFPseudonode implements IGPRouterID, BGPValueType { |
| 32 | |
| 33 | protected static final Logger log = LoggerFactory.getLogger(OSPFPseudonode.class); |
| 34 | |
| 35 | public static final short TYPE = 515; |
| 36 | public static final short LENGTH = 8; |
| 37 | |
| 38 | private final int routerID; |
| 39 | private final Ip4Address drInterface; |
| 40 | |
| 41 | /** |
| 42 | * Constructor to initialize parameters. |
| 43 | * |
| 44 | * @param routerID routerID |
| 45 | * @param drInterface IPv4 address of the DR's interface |
| 46 | */ |
| 47 | public OSPFPseudonode(int routerID, Ip4Address drInterface) { |
| 48 | this.routerID = routerID; |
| 49 | this.drInterface = drInterface; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns object of this class with specified values. |
| 54 | * |
| 55 | * @param routerID routerID |
| 56 | * @param drInterface IPv4 address of the DR's interface |
| 57 | * @return object of OSPFPseudonode |
| 58 | */ |
| 59 | public static OSPFPseudonode of(final int routerID, final Ip4Address drInterface) { |
| 60 | return new OSPFPseudonode(routerID, drInterface); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns RouterID. |
| 65 | * |
| 66 | * @return RouterID |
| 67 | */ |
| 68 | public int getrouterID() { |
| 69 | return this.routerID; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public int hashCode() { |
| 74 | return Objects.hash(routerID, drInterface); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public boolean equals(Object obj) { |
| 79 | if (this == obj) { |
| 80 | return true; |
| 81 | } |
| 82 | if (obj instanceof OSPFPseudonode) { |
| 83 | OSPFPseudonode other = (OSPFPseudonode) obj; |
| 84 | return Objects.equals(routerID, other.routerID) && Objects.equals(drInterface, other.drInterface); |
| 85 | } |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public int write(ChannelBuffer c) { |
| 91 | int iLenStartIndex = c.writerIndex(); |
| 92 | c.writeShort(TYPE); |
| 93 | c.writeShort(LENGTH); |
| 94 | c.writeInt(routerID); |
| 95 | c.writeInt(drInterface.toInt()); |
| 96 | return c.writerIndex() - iLenStartIndex; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Reads the channel buffer and returns object of OSPFPseudonode. |
| 101 | * |
| 102 | * @param cb ChannelBuffer |
| 103 | * @return object of OSPFPseudonode |
| 104 | */ |
| 105 | public static OSPFPseudonode read(ChannelBuffer cb) { |
| 106 | int routerID = cb.readInt(); |
| 107 | Ip4Address drInterface = Ip4Address.valueOf(cb.readInt()); |
| 108 | return OSPFPseudonode.of(routerID, drInterface); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public short getType() { |
| 113 | return TYPE; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public String toString() { |
| 118 | return MoreObjects.toStringHelper(getClass()) |
| 119 | .add("Type", TYPE) |
| 120 | .add("Length", LENGTH) |
| 121 | .add("RouterID", routerID) |
| 122 | .add("DRInterface", drInterface) |
| 123 | .toString(); |
| 124 | } |
| 125 | } |