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