blob: e5dc89a5b95fe159d8977221b4bc0a9c46ea2aae [file] [log] [blame]
Satish Kf6d87cb2015-11-30 19:59:22 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Satish Kf6d87cb2015-11-30 19:59:22 +05303 *
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 */
16package org.onosproject.iptopology.api;
17
18import static com.google.common.base.MoreObjects.toStringHelper;
19
Shashikanth VH74e4b782015-12-10 22:06:41 +053020import java.util.Arrays;
Satish Kf6d87cb2015-11-30 19:59:22 +053021import java.util.Objects;
22
23/**
24 * Represents ISO system id of the device.
25 */
26public 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 VH74e4b782015-12-10 22:06:41 +053057 return Objects.hash(Arrays.hashCode(isoNodeId), type);
Satish Kf6d87cb2015-11-30 19:59:22 +053058 }
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 VH74e4b782015-12-10 22:06:41 +053068 return Arrays.equals(isoNodeId, other.isoNodeId) && Objects.equals(type, other.type);
Satish Kf6d87cb2015-11-30 19:59:22 +053069 }
70 return false;
71 }
72
Shashikanth VH74e4b782015-12-10 22:06:41 +053073 /*
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 Kf6d87cb2015-11-30 19:59:22 +053087 @Override
88 public String toString() {
Shashikanth VH74e4b782015-12-10 22:06:41 +053089 return toStringHelper(this).omitNullValues()
90 .add("isoNodeId", isoNodeIdString())
Satish Kf6d87cb2015-11-30 19:59:22 +053091 .add("type", type)
92 .toString();
93 }
94}