blob: 3cf186d5a0d6016a2095de8e59df5be61a831f28 [file] [log] [blame]
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07003 *
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
26/**
27 * Provides Pcep Nai Ipv4 Adjacency.
28 */
29public class PcepNaiIpv4Adjacency implements PcepNai {
30
31 public static final byte ST_TYPE = 0x03;
32 private final int localIpv4Addr;
33 private final int remoteIpv4Addr;
34
35 /**
36 * Constructor to initialize variables.
37 *
38 * @param localIpv4 local ipv4 address
39 * @param remoteIpv4 remote ipv4 address
40 */
41 public PcepNaiIpv4Adjacency(int localIpv4, int remoteIpv4) {
42 this.localIpv4Addr = localIpv4;
43 this.remoteIpv4Addr = remoteIpv4;
44 }
45
46 /**
47 * Returns Object of Pcep nai Ipv4 Adjacency.
48 *
49 * @param localIpv4Addr local ipv4 address
50 * @param remoteIpv4Addr remote ipv4 address
51 * @return Object of Pcep nai Ipv4 Adjacency
52 */
53 public static PcepNaiIpv4Adjacency of(int localIpv4Addr, int remoteIpv4Addr) {
54 return new PcepNaiIpv4Adjacency(localIpv4Addr, remoteIpv4Addr);
55 }
56
57 @Override
58 public byte getType() {
59 return ST_TYPE;
60 }
61
Avantika-Huaweif849aab2016-06-21 22:29:15 +053062 public int getLocalIpv4Addr() {
63 return localIpv4Addr;
64 }
65
66 public int getRemoteIpv4Addr() {
67 return remoteIpv4Addr;
68 }
69
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070070 @Override
71 public int write(ChannelBuffer bb) {
72 int iLenStartIndex = bb.writerIndex();
73 bb.writeInt(localIpv4Addr);
74 bb.writeInt(remoteIpv4Addr);
75 return bb.writerIndex() - iLenStartIndex;
76 }
77
78 /**
79 * Reads the channel buffer and returns object of PcepNAIIpv4AdjacencyVer1.
80 *
81 * @param cb of channel buffer
82 * @return object of PcepNAIIpv4Adjacency
83 */
84 public static PcepNaiIpv4Adjacency read(ChannelBuffer cb) {
85 int localIpv4 = cb.readInt();
86 int remoteIpv4 = cb.readInt();
87 return new PcepNaiIpv4Adjacency(localIpv4, remoteIpv4);
88 }
89
90 @Override
91 public int hashCode() {
92 return Objects.hash(localIpv4Addr, remoteIpv4Addr);
93 }
94
95 @Override
96 public boolean equals(Object obj) {
97 if (this == obj) {
98 return true;
99 }
100 if (obj instanceof PcepNaiIpv4Adjacency) {
101 PcepNaiIpv4Adjacency other = (PcepNaiIpv4Adjacency) obj;
102 return Objects.equals(this.localIpv4Addr, other.localIpv4Addr)
103 && Objects.equals(this.remoteIpv4Addr, other.remoteIpv4Addr);
104 }
105 return false;
106 }
107
108 @Override
109 public String toString() {
110 return MoreObjects.toStringHelper(getClass())
111 .add("localIPv4Address", localIpv4Addr)
112 .add("remoteIPv4Address", remoteIpv4Addr)
113 .toString();
114 }
115}