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 | */ |
| 30 | public final class BGPId { |
| 31 | |
| 32 | private static final String SCHEME = "bgp"; |
| 33 | private static final long UNKNOWN = 0; |
| 34 | private final IpAddress ipAddress; |
| 35 | |
| 36 | /** |
| 37 | * Private constructor. |
| 38 | */ |
| 39 | private BGPId(IpAddress ipAddress) { |
| 40 | this.ipAddress = ipAddress; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Create a BGPId from ip address. |
| 45 | * |
| 46 | * @param ipAddress IP address |
| 47 | * @return object of BGPId |
| 48 | */ |
| 49 | public static BGPId bgpId(IpAddress ipAddress) { |
| 50 | return new BGPId(ipAddress); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns the ip address. |
| 55 | * |
| 56 | * @return ipAddress |
| 57 | */ |
| 58 | public IpAddress ipAddress() { |
| 59 | return ipAddress; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Convert the BGPId value to a ':' separated hexadecimal string. |
| 64 | * |
| 65 | * @return the BGPId value as a ':' separated hexadecimal string. |
| 66 | */ |
| 67 | @Override |
| 68 | public String toString() { |
| 69 | return ipAddress.toString(); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public boolean equals(Object other) { |
| 74 | if (!(other instanceof BGPId)) { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | BGPId otherBGPid = (BGPId) other; |
| 79 | return Objects.equals(ipAddress, otherBGPid.ipAddress); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public int hashCode() { |
| 84 | return Objects.hash(ipAddress); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns BGPId created from the given device URI. |
| 89 | * |
| 90 | * @param uri device URI |
| 91 | * @return object of BGPId |
| 92 | */ |
| 93 | public static BGPId bgpId(URI uri) { |
| 94 | checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme"); |
| 95 | return new BGPId(IpAddress.valueOf(uri.getSchemeSpecificPart())); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Produces device URI from the given DPID. |
| 100 | * |
| 101 | * @param bgpId device bgpId |
| 102 | * @return device URI |
| 103 | */ |
| 104 | public static URI uri(BGPId bgpId) { |
| 105 | return uri(bgpId.ipAddress()); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Produces device URI from the given DPID long. |
| 110 | * |
| 111 | * @param ipAddress device ip address |
| 112 | * @return device URI |
| 113 | */ |
| 114 | public static URI uri(IpAddress ipAddress) { |
| 115 | try { |
| 116 | return new URI(SCHEME, ipAddress.toString(), null); |
| 117 | } catch (URISyntaxException e) { |
| 118 | return null; |
| 119 | } |
| 120 | } |
| 121 | } |