blob: bc12809b71368a0d41dd8336ec6ee06d690e12b9 [file] [log] [blame]
bharat saraswalf7364db2015-08-11 13:39:31 +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.pcepio.types;
18
19import java.util.Objects;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onosproject.pcepio.protocol.PcepNai;
23
24import com.google.common.base.MoreObjects;
25
26public class PcepNaiIpv4Adjacency implements PcepNai {
27
28 public static final byte ST_TYPE = 0x03;
29 private final int localIpv4Addr;
30 private final int remoteIpv4Addr;
31
32 /**
33 * Constructor to initialize variables.
34 *
35 * @param localIpv4 local ipv4 address
36 * @param remoteIpv4 remote ipv4 address
37 */
38 public PcepNaiIpv4Adjacency(int localIpv4, int remoteIpv4) {
39 this.localIpv4Addr = localIpv4;
40 this.remoteIpv4Addr = remoteIpv4;
41 }
42
SureshBR592f9502015-08-25 19:16:34 +053043 /**
44 * Returns Object of Pcep nai Ipv4 Adjacency.
45 *
46 * @param localIpv4Addr local ipv4 address
47 * @param remoteIpv4Addr remote ipv4 address
48 * @return Object of Pcep nai Ipv4 Adjacency
49 */
50 public static PcepNaiIpv4Adjacency of(int localIpv4Addr, int remoteIpv4Addr) {
51 return new PcepNaiIpv4Adjacency(localIpv4Addr, remoteIpv4Addr);
52 }
53
bharat saraswalf7364db2015-08-11 13:39:31 +053054 @Override
55 public byte getType() {
56 return ST_TYPE;
57 }
58
59 @Override
60 public int write(ChannelBuffer bb) {
61 int iLenStartIndex = bb.writerIndex();
62 bb.writeInt(localIpv4Addr);
63 bb.writeInt(remoteIpv4Addr);
64 return bb.writerIndex() - iLenStartIndex;
65 }
66
67 /**
68 * Reads the channel buffer and returns object of PcepNAIIpv4AdjacencyVer1.
69 *
70 * @param cb of channel buffer
71 * @return object of PcepNAIIpv4Adjacency
72 */
73 public static PcepNaiIpv4Adjacency read(ChannelBuffer cb) {
74 int localIpv4 = cb.readInt();
75 int remoteIpv4 = cb.readInt();
76 return new PcepNaiIpv4Adjacency(localIpv4, remoteIpv4);
77 }
78
79 @Override
80 public int hashCode() {
81 return Objects.hash(localIpv4Addr, remoteIpv4Addr);
82 }
83
84 @Override
85 public boolean equals(Object obj) {
86 if (this == obj) {
87 return true;
88 }
89 if (obj instanceof PcepNaiIpv4Adjacency) {
90 PcepNaiIpv4Adjacency other = (PcepNaiIpv4Adjacency) obj;
91 return Objects.equals(this.localIpv4Addr, other.localIpv4Addr)
92 && Objects.equals(this.remoteIpv4Addr, other.remoteIpv4Addr);
93 }
94 return false;
95 }
96
97 @Override
98 public String toString() {
99 return MoreObjects.toStringHelper(getClass())
bharat saraswale1806302015-08-21 12:16:46 +0530100 .add("localIPv4Address", localIpv4Addr)
101 .add("remoteIPv4Address", remoteIpv4Addr)
bharat saraswalf7364db2015-08-11 13:39:31 +0530102 .toString();
103 }
104}