blob: e45b659e218ff2447fe5647b44355e3de6f7abb1 [file] [log] [blame]
sunish vk7bdf4d42016-06-24 12:29:43 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
sunish vk7bdf4d42016-06-24 12:29:43 +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 */
16
17package org.onosproject.isis.controller.topology;
18
19import java.net.URI;
20import java.net.URISyntaxException;
21import java.util.Objects;
22
23import static com.google.common.base.Preconditions.checkArgument;
24
25/**
26 * Represents an ISIS router id.
27 */
28public class IsisRouterId {
29
30 private static final String SCHEME = "l3";
31 private static final long UNKNOWN = 0;
32 private final String ipAddress;
33
34 /**
35 * Creates an instance of ISIS router id.
36 *
37 * @param ipAddress IP address of the router
38 */
39 public IsisRouterId(String ipAddress) {
40 this.ipAddress = ipAddress;
41 }
42
43 /**
44 * Creates an instance from ip address.
45 *
46 * @param ipAddress IP address
47 * @return ISIS router id instance
48 */
49 public static IsisRouterId isisRouterId(String ipAddress) {
50 return new IsisRouterId(ipAddress);
51 }
52
53 /**
54 * Creates ISIS router id instance from the URI.
55 *
56 * @param uri device URI
57 * @return ISIS router id instance
58 */
59 public static IsisRouterId isisRouterId(URI uri) {
60 checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme");
61 return new IsisRouterId(uri.getSchemeSpecificPart());
62 }
63
64 /**
65 * Returns device URI from the given router id.
66 *
67 * @param isisRouterId router id instance
68 * @return device URI
69 */
70 public static URI uri(IsisRouterId isisRouterId) {
71 return uri(isisRouterId.ipAddress());
72 }
73
74 /**
75 * Returns device URI from the given IP address.
76 *
77 * @param ipAddress device IP address
78 * @return device URI
79 */
80 public static URI uri(String ipAddress) {
81 try {
82 return new URI(SCHEME, ipAddress, null);
83 } catch (URISyntaxException e) {
84 return null;
85 }
86 }
87
88 /**
89 * Returns the IP address.
90 *
91 * @return IP address
92 */
93 public String ipAddress() {
94 return ipAddress;
95 }
96
97 @Override
98 public String toString() {
99 return ipAddress;
100 }
101
102 @Override
103 public boolean equals(Object other) {
104 if (!(other instanceof IsisRouterId)) {
105 return false;
106 }
107
108 IsisRouterId otherIsisRouterId = (IsisRouterId) other;
109 return Objects.equals(ipAddress, otherIsisRouterId.ipAddress);
110 }
111
112 @Override
113 public int hashCode() {
114 return Objects.hash(ipAddress);
115 }
116}