blob: d6b3c9c48297552c2d65c56056805a3276f5c7e9 [file] [log] [blame]
Jian Li7ccc3a82016-12-09 01:30:56 +09001/*
2 * Copyright 2016-present Open Networking Laboratory
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 */
16package org.onosproject.lisp.ctl;
17
18import org.onlab.packet.IpAddress;
19import org.onlab.util.Identifier;
20
21import java.net.URI;
22import java.net.URISyntaxException;
23
24import static com.google.common.base.Preconditions.checkArgument;
25
26/**
27 * The class representing a network router identifier.
28 * This class is immutable.
29 */
30public final class LispRouterId extends Identifier<IpAddress> {
31
32 private static final String SCHEME = "lisp";
33 private static final IpAddress UNKNOWN = IpAddress.valueOf("0.0.0.0");
34
35 /**
36 * A default constructor.
37 */
38 public LispRouterId() {
39 super(UNKNOWN);
40 }
41
42 /**
43 * A constructor with an IpAddress value specified.
44 *
45 * @param value the value to use
46 */
47 public LispRouterId(IpAddress value) {
48 super(value);
49 }
50
51 /**
52 * A constructor with a String value specified.
53 *
54 * @param value the value to use
55 */
56 public LispRouterId(String value) {
57 super(IpAddress.valueOf(value));
58 }
59
60 /**
61 * Returns LispRouterId created from a given device URI.
62 *
63 * @param uri device URI
64 * @return object of LispRouterId
65 */
66 public static LispRouterId routerId(URI uri) {
67 checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme");
68 return new LispRouterId(IpAddress.valueOf(uri.getSchemeSpecificPart()));
69 }
70
71 /**
72 * Produces a device URI from the given LispRouterId.
73 *
74 * @param routerId device identifier
75 * @return device URI
76 */
77 public static URI uri(LispRouterId routerId) {
78 return uri(routerId.id());
79 }
80
81 /**
82 * Produces device URI from the given device IpAddress.
83 *
84 * @param ipAddress device ip address
85 * @return device URI
86 */
87 public static URI uri(IpAddress ipAddress) {
88 try {
89 return new URI(SCHEME, ipAddress.toString(), null);
90 } catch (URISyntaxException e) {
91 return null;
92 }
93 }
94}