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