blob: 2b1a7757fdd7ee603bb9a7e8b69a3a72aba580fd [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 PcepNaiIpv6Adjacency implements PcepNai {
27
28 public static final byte ST_TYPE = 0x04;
29 public static final byte IPV6_LEN = 0x10;
30
31 private final byte[] localIpv6Addr;
32 private final byte[] remoteIpv6Addr;
33
34 /**
35 * Constructor to initialize local ipv6 and remote ipv6.
36 *
37 * @param localIpv6 local ipv6 address
38 * @param remoteIpv6 remote ipv6 address
39 */
40 public PcepNaiIpv6Adjacency(byte[] localIpv6, byte[] remoteIpv6) {
41 this.localIpv6Addr = localIpv6;
42 this.remoteIpv6Addr = remoteIpv6;
43 }
44
45 @Override
46 public byte getType() {
47 return ST_TYPE;
48 }
49
50 @Override
51 public int write(ChannelBuffer bb) {
52 int iLenStartIndex = bb.writerIndex();
53 bb.writeBytes(localIpv6Addr);
54 bb.writeBytes(remoteIpv6Addr);
55 return bb.writerIndex() - iLenStartIndex;
56 }
57
58 /**
59 * Reads from channel buffer and returns object of PcepNAIIpv6AdjacencyVer1.
60 *
61 * @param bb of type channel buffer
62 * @return object of PcepNAIIpv6AdjacencyVer1
63 */
64 public static PcepNaiIpv6Adjacency read(ChannelBuffer bb) {
65 byte[] localIpv6 = new byte[IPV6_LEN];
66 bb.readBytes(localIpv6, 0, IPV6_LEN);
67 byte[] remoteIpv6 = new byte[IPV6_LEN];
68 bb.readBytes(remoteIpv6, 0, IPV6_LEN);
69 return new PcepNaiIpv6Adjacency(localIpv6, remoteIpv6);
70 }
71
72 @Override
73 public int hashCode() {
74 return Objects.hash(localIpv6Addr, remoteIpv6Addr);
75 }
76
77 @Override
78 public boolean equals(Object obj) {
79 if (this == obj) {
80 return true;
81 }
82 if (obj instanceof PcepNaiIpv6Adjacency) {
83 PcepNaiIpv6Adjacency other = (PcepNaiIpv6Adjacency) obj;
84 return Objects.equals(this.localIpv6Addr, other.localIpv6Addr)
85 && Objects.equals(this.remoteIpv6Addr, other.remoteIpv6Addr);
86 }
87 return false;
88 }
89
90 /**
91 * Creates object of PcepNaiIpv6Adjacency with local ipv6 address and remote ipv6 address.
92 *
93 * @param localIpv6Addr local ipv6 address
94 * @param remoteIpv6Addr remote ipv6 address
95 * @return object of PcepNaiIpv6Adjacency
96 */
97
98 public static PcepNaiIpv6Adjacency of(final byte[] localIpv6Addr, final byte[] remoteIpv6Addr) {
99 return new PcepNaiIpv6Adjacency(localIpv6Addr, remoteIpv6Addr);
100 }
101
102 @Override
103 public String toString() {
104 return MoreObjects.toStringHelper(getClass())
bharat saraswale1806302015-08-21 12:16:46 +0530105 .add("localIPV6Address", localIpv6Addr)
106 .add("remoteIPV6Address", remoteIpv6Addr)
bharat saraswalf7364db2015-08-11 13:39:31 +0530107 .toString();
108 }
109
110}