Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.bgp.controller; |
| 18 | |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import java.net.URI; |
| 21 | import java.net.URISyntaxException; |
| 22 | import java.util.Objects; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | |
| 26 | /** |
| 27 | * The class representing a network peer bgp ip. |
| 28 | * This class is immutable. |
| 29 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 30 | public final class BgpId { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 31 | |
| 32 | private static final String SCHEME = "bgp"; |
| 33 | private static final long UNKNOWN = 0; |
| 34 | private final IpAddress ipAddress; |
| 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) { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 42 | this.ipAddress = ipAddress; |
| 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() { |
| 61 | return ipAddress; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Convert the BGPId value to a ':' separated hexadecimal string. |
| 66 | * |
| 67 | * @return the BGPId value as a ':' separated hexadecimal string. |
| 68 | */ |
| 69 | @Override |
| 70 | public String toString() { |
| 71 | return ipAddress.toString(); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public boolean equals(Object other) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 76 | if (!(other instanceof BgpId)) { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 77 | return false; |
| 78 | } |
| 79 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 80 | BgpId otherBGPid = (BgpId) other; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 81 | return Objects.equals(ipAddress, otherBGPid.ipAddress); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public int hashCode() { |
| 86 | return Objects.hash(ipAddress); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns BGPId created from the given device URI. |
| 91 | * |
| 92 | * @param uri device URI |
| 93 | * @return object of BGPId |
| 94 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 95 | public static BgpId bgpId(URI uri) { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 96 | checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme"); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 97 | return new BgpId(IpAddress.valueOf(uri.getSchemeSpecificPart())); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Produces device URI from the given DPID. |
| 102 | * |
| 103 | * @param bgpId device bgpId |
| 104 | * @return device URI |
| 105 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 106 | public static URI uri(BgpId bgpId) { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 107 | return uri(bgpId.ipAddress()); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Produces device URI from the given DPID long. |
| 112 | * |
| 113 | * @param ipAddress device ip address |
| 114 | * @return device URI |
| 115 | */ |
| 116 | public static URI uri(IpAddress ipAddress) { |
| 117 | try { |
| 118 | return new URI(SCHEME, ipAddress.toString(), null); |
| 119 | } catch (URISyntaxException e) { |
| 120 | return null; |
| 121 | } |
| 122 | } |
Bharat saraswal | 6c88695 | 2015-12-11 01:43:11 +0530 | [diff] [blame] | 123 | } |