Satish K | f6d87cb | 2015-11-30 19:59:22 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Satish K | f6d87cb | 2015-11-30 19:59:22 +0530 | [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 | package org.onosproject.iptopology.api; |
| 17 | |
| 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 19 | |
Shashikanth VH | 74e4b78 | 2015-12-10 22:06:41 +0530 | [diff] [blame] | 20 | import java.util.Arrays; |
Satish K | f6d87cb | 2015-11-30 19:59:22 +0530 | [diff] [blame] | 21 | import java.util.Objects; |
| 22 | |
| 23 | /** |
| 24 | * Represents ISO system id of the device. |
| 25 | */ |
| 26 | public class IsoNodeId implements RouteIdentifier { |
| 27 | private final byte[] isoNodeId; |
| 28 | private final ProtocolType type; |
| 29 | |
| 30 | /** |
| 31 | * Constructor to initialize the values. |
| 32 | * |
| 33 | * @param isoNodeId ISO system-ID |
| 34 | * @param type Protocol type |
| 35 | */ |
| 36 | public IsoNodeId(byte[] isoNodeId, ProtocolType type) { |
| 37 | this.isoNodeId = isoNodeId; |
| 38 | this.type = type; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Obtains ISO system id of the device. |
| 43 | * |
| 44 | * @return ISO system id |
| 45 | */ |
| 46 | public byte[] isoNodeId() { |
| 47 | return isoNodeId; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public ProtocolType type() { |
| 52 | return type; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public int hashCode() { |
Shashikanth VH | 74e4b78 | 2015-12-10 22:06:41 +0530 | [diff] [blame] | 57 | return Objects.hash(Arrays.hashCode(isoNodeId), type); |
Satish K | f6d87cb | 2015-11-30 19:59:22 +0530 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public boolean equals(Object obj) { |
| 62 | if (this == obj) { |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | if (obj instanceof IsoNodeId) { |
| 67 | IsoNodeId other = (IsoNodeId) obj; |
Shashikanth VH | 74e4b78 | 2015-12-10 22:06:41 +0530 | [diff] [blame] | 68 | return Arrays.equals(isoNodeId, other.isoNodeId) && Objects.equals(type, other.type); |
Satish K | f6d87cb | 2015-11-30 19:59:22 +0530 | [diff] [blame] | 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
Shashikanth VH | 74e4b78 | 2015-12-10 22:06:41 +0530 | [diff] [blame] | 73 | /* |
| 74 | * Get iso node ID in specified string format. |
| 75 | */ |
| 76 | private String isoNodeIdString() { |
| 77 | if (isoNodeId != null) { |
| 78 | int p1 = (int) isoNodeId[0] << 8 | (int) isoNodeId[1]; |
| 79 | int p2 = (int) isoNodeId[2] << 8 | (int) isoNodeId[3]; |
| 80 | int p3 = (int) isoNodeId[4] << 8 | (int) isoNodeId[5]; |
| 81 | |
| 82 | return String.format("%1$d.%2$d.%3$d", p1, p2, p3); |
| 83 | } |
| 84 | return null; |
| 85 | } |
| 86 | |
Satish K | f6d87cb | 2015-11-30 19:59:22 +0530 | [diff] [blame] | 87 | @Override |
| 88 | public String toString() { |
Shashikanth VH | 74e4b78 | 2015-12-10 22:06:41 +0530 | [diff] [blame] | 89 | return toStringHelper(this).omitNullValues() |
| 90 | .add("isoNodeId", isoNodeIdString()) |
Satish K | f6d87cb | 2015-11-30 19:59:22 +0530 | [diff] [blame] | 91 | .add("type", type) |
| 92 | .toString(); |
| 93 | } |
| 94 | } |