blob: ca469cfd974825f8e38e96514aaed6016916e8d4 [file] [log] [blame]
Satish Ke107e662015-09-21 19:00:17 +05301/*
2 * Copyright 2015 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 */
16
17package org.onosproject.bgp.controller;
18
19import org.onlab.packet.IpAddress;
Jian Li597d7b22016-02-29 14:06:55 -080020import org.onlab.util.Identifier;
21
Satish Ke107e662015-09-21 19:00:17 +053022import java.net.URI;
23import java.net.URISyntaxException;
Satish Ke107e662015-09-21 19:00:17 +053024
25import static com.google.common.base.Preconditions.checkArgument;
26
27/**
28 * The class representing a network peer bgp ip.
29 * This class is immutable.
30 */
Jian Li597d7b22016-02-29 14:06:55 -080031public final class BgpId extends Identifier<IpAddress> {
Satish Ke107e662015-09-21 19:00:17 +053032
33 private static final String SCHEME = "bgp";
34 private static final long UNKNOWN = 0;
Satish Ke107e662015-09-21 19:00:17 +053035
36 /**
Priyanka B0ab34b92015-12-03 21:13:52 +053037 * Constructor to initialize ipAddress.
Bharat saraswal6c886952015-12-11 01:43:11 +053038 *
39 * @param ipAddress Ip address
Satish Ke107e662015-09-21 19:00:17 +053040 */
Priyanka B0ab34b92015-12-03 21:13:52 +053041 public BgpId(IpAddress ipAddress) {
Jian Li597d7b22016-02-29 14:06:55 -080042 super(ipAddress);
Satish Ke107e662015-09-21 19:00:17 +053043 }
44
45 /**
46 * Create a BGPId from ip address.
47 *
48 * @param ipAddress IP address
49 * @return object of BGPId
50 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053051 public static BgpId bgpId(IpAddress ipAddress) {
52 return new BgpId(ipAddress);
Satish Ke107e662015-09-21 19:00:17 +053053 }
54
55 /**
56 * Returns the ip address.
57 *
58 * @return ipAddress
59 */
60 public IpAddress ipAddress() {
Jian Li597d7b22016-02-29 14:06:55 -080061 return identifier;
Satish Ke107e662015-09-21 19:00:17 +053062 }
63
64 /**
65 * Returns BGPId created from the given device URI.
66 *
67 * @param uri device URI
68 * @return object of BGPId
69 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053070 public static BgpId bgpId(URI uri) {
Satish Ke107e662015-09-21 19:00:17 +053071 checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme");
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053072 return new BgpId(IpAddress.valueOf(uri.getSchemeSpecificPart()));
Satish Ke107e662015-09-21 19:00:17 +053073 }
74
75 /**
76 * Produces device URI from the given DPID.
77 *
78 * @param bgpId device bgpId
79 * @return device URI
80 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053081 public static URI uri(BgpId bgpId) {
Satish Ke107e662015-09-21 19:00:17 +053082 return uri(bgpId.ipAddress());
83 }
84
85 /**
86 * Produces device URI from the given DPID long.
87 *
88 * @param ipAddress device ip address
89 * @return device URI
90 */
91 public static URI uri(IpAddress ipAddress) {
92 try {
93 return new URI(SCHEME, ipAddress.toString(), null);
94 } catch (URISyntaxException e) {
95 return null;
96 }
97 }
Bharat saraswal6c886952015-12-11 01:43:11 +053098}