Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Satish K | e107e66 | 2015-09-21 19:00:17 +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 | |
| 17 | package org.onosproject.bgp.controller; |
| 18 | |
| 19 | import org.onlab.packet.IpAddress; |
Jian Li | 597d7b2 | 2016-02-29 14:06:55 -0800 | [diff] [blame] | 20 | import org.onlab.util.Identifier; |
| 21 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 22 | import java.net.URI; |
| 23 | import java.net.URISyntaxException; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 24 | |
| 25 | import 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 Li | 597d7b2 | 2016-02-29 14:06:55 -0800 | [diff] [blame] | 31 | public final class BgpId extends Identifier<IpAddress> { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 32 | |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 33 | private static final String SCHEME = "l3"; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 34 | private static final long UNKNOWN = 0; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 35 | |
| 36 | /** |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 37 | * Constructor to initialize ipAddress. |
Bharat saraswal | 6c88695 | 2015-12-11 01:43:11 +0530 | [diff] [blame] | 38 | * |
| 39 | * @param ipAddress Ip address |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 40 | */ |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 41 | public BgpId(IpAddress ipAddress) { |
Jian Li | 597d7b2 | 2016-02-29 14:06:55 -0800 | [diff] [blame] | 42 | super(ipAddress); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Create a BGPId from ip address. |
| 47 | * |
| 48 | * @param ipAddress IP address |
| 49 | * @return object of BGPId |
| 50 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 51 | public static BgpId bgpId(IpAddress ipAddress) { |
| 52 | return new BgpId(ipAddress); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns the ip address. |
| 57 | * |
| 58 | * @return ipAddress |
| 59 | */ |
| 60 | public IpAddress ipAddress() { |
Jian Li | 597d7b2 | 2016-02-29 14:06:55 -0800 | [diff] [blame] | 61 | return identifier; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns BGPId created from the given device URI. |
| 66 | * |
| 67 | * @param uri device URI |
| 68 | * @return object of BGPId |
| 69 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 70 | public static BgpId bgpId(URI uri) { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 71 | checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme"); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 72 | return new BgpId(IpAddress.valueOf(uri.getSchemeSpecificPart())); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Produces device URI from the given DPID. |
| 77 | * |
| 78 | * @param bgpId device bgpId |
| 79 | * @return device URI |
| 80 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 81 | public static URI uri(BgpId bgpId) { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 82 | 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 saraswal | 6c88695 | 2015-12-11 01:43:11 +0530 | [diff] [blame] | 98 | } |