Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.pcepio.types; |
| 18 | |
Saritha | 8539a2e | 2017-01-05 11:58:54 +0530 | [diff] [blame] | 19 | import java.util.Arrays; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 20 | |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| 22 | import org.onosproject.pcepio.protocol.PcepNai; |
| 23 | |
| 24 | import com.google.common.base.MoreObjects; |
| 25 | |
| 26 | /** |
| 27 | * Provides Pcep Nai Ipv6 Node Id. |
| 28 | */ |
| 29 | public class PcepNaiIpv6NodeId implements PcepNai { |
| 30 | |
| 31 | public static final byte ST_TYPE = 0x02; |
| 32 | public static final byte IPV6_LEN = 0x10; |
| 33 | |
| 34 | private final byte[] ipv6NodeId; |
| 35 | |
| 36 | /** |
| 37 | * Constructor to initialize ipv6NodeId. |
| 38 | * |
| 39 | * @param value ipv6 node id |
| 40 | */ |
| 41 | public PcepNaiIpv6NodeId(byte[] value) { |
| 42 | this.ipv6NodeId = value; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Return object of Pcep Nai Ipv6 Node ID. |
| 47 | * |
| 48 | * @param ipv6NodeId Ipv6 node ID. |
| 49 | * @return object of Pcep Nai Ipv6 Node ID. |
| 50 | */ |
| 51 | public static PcepNaiIpv6NodeId of(byte[] ipv6NodeId) { |
| 52 | return new PcepNaiIpv6NodeId(ipv6NodeId); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public byte getType() { |
| 57 | return ST_TYPE; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public int write(ChannelBuffer cb) { |
| 62 | int iLenStartIndex = cb.writerIndex(); |
| 63 | cb.writeBytes(ipv6NodeId); |
| 64 | return cb.writerIndex() - iLenStartIndex; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Reads from the channel buffer and returns object of PcepNAIIpv6NodeId. |
| 69 | * |
| 70 | * @param cb of type channel buffer. |
| 71 | * @return object of PcepNAIIpv6NodeId |
| 72 | */ |
| 73 | public static PcepNaiIpv6NodeId read(ChannelBuffer cb) { |
| 74 | byte[] ipv6NodeId = new byte[IPV6_LEN]; |
| 75 | cb.readBytes(ipv6NodeId, 0, IPV6_LEN); |
| 76 | return new PcepNaiIpv6NodeId(ipv6NodeId); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public int hashCode() { |
Saritha | 8539a2e | 2017-01-05 11:58:54 +0530 | [diff] [blame] | 81 | return Arrays.hashCode(ipv6NodeId); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public boolean equals(Object obj) { |
| 86 | if (this == obj) { |
| 87 | return true; |
| 88 | } |
| 89 | if (obj instanceof PcepNaiIpv6NodeId) { |
| 90 | PcepNaiIpv6NodeId other = (PcepNaiIpv6NodeId) obj; |
Saritha | 8539a2e | 2017-01-05 11:58:54 +0530 | [diff] [blame] | 91 | return Arrays.equals(this.ipv6NodeId, other.ipv6NodeId); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 92 | } |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public String toString() { |
Sho SHIMIZU | 7cdbcf7 | 2015-09-03 14:43:05 -0700 | [diff] [blame] | 98 | return MoreObjects.toStringHelper(getClass()) |
| 99 | .add("IPV6NodeID", ipv6NodeId) |
| 100 | .toString(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 101 | } |
| 102 | } |