blob: 77168737f4cde3203db60cc3944433ec7c9b0f16 [file] [log] [blame]
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
62 @Override
63 public int write(ChannelBuffer bb) {
64 int iLenStartIndex = bb.writerIndex();
65 bb.writeInt(localIpv4Addr);
66 bb.writeInt(remoteIpv4Addr);
67 return bb.writerIndex() - iLenStartIndex;
68 }
69
70 /**
71 * Reads the channel buffer and returns object of PcepNAIIpv4AdjacencyVer1.
72 *
73 * @param cb of channel buffer
74 * @return object of PcepNAIIpv4Adjacency
75 */
76 public static PcepNaiIpv4Adjacency read(ChannelBuffer cb) {
77 int localIpv4 = cb.readInt();
78 int remoteIpv4 = cb.readInt();
79 return new PcepNaiIpv4Adjacency(localIpv4, remoteIpv4);
80 }
81
82 @Override
83 public int hashCode() {
84 return Objects.hash(localIpv4Addr, remoteIpv4Addr);
85 }
86
87 @Override
88 public boolean equals(Object obj) {
89 if (this == obj) {
90 return true;
91 }
92 if (obj instanceof PcepNaiIpv4Adjacency) {
93 PcepNaiIpv4Adjacency other = (PcepNaiIpv4Adjacency) obj;
94 return Objects.equals(this.localIpv4Addr, other.localIpv4Addr)
95 && Objects.equals(this.remoteIpv4Addr, other.remoteIpv4Addr);
96 }
97 return false;
98 }
99
100 @Override
101 public String toString() {
102 return MoreObjects.toStringHelper(getClass())
103 .add("localIPv4Address", localIpv4Addr)
104 .add("remoteIPv4Address", remoteIpv4Addr)
105 .toString();
106 }
107}