sunish vk | 30637eb | 2016-02-16 15:19:32 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
sunish vk | 30637eb | 2016-02-16 15:19:32 +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.ospf.controller; |
| 18 | |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | |
| 21 | import java.net.URI; |
| 22 | import java.net.URISyntaxException; |
| 23 | import java.util.Objects; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkArgument; |
| 26 | |
| 27 | /** |
| 28 | * Represents an OSPF router id. |
| 29 | */ |
| 30 | public class OspfRouterId { |
| 31 | |
| 32 | private static final String SCHEME = "ospf"; |
| 33 | private static final long UNKNOWN = 0; |
| 34 | private final IpAddress ipAddress; |
| 35 | |
| 36 | /** |
| 37 | * Creates an instance of OSPF router id. |
| 38 | * |
| 39 | * @param ipAddress IP address of the router |
| 40 | */ |
| 41 | public OspfRouterId(IpAddress ipAddress) { |
| 42 | this.ipAddress = ipAddress; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Creates an instance from ip address. |
| 47 | * |
| 48 | * @param ipAddress IP address |
| 49 | * @return OSPF router id instance |
| 50 | */ |
| 51 | public static OspfRouterId ospfRouterId(IpAddress ipAddress) { |
| 52 | return new OspfRouterId(ipAddress); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Creates OSPF router id instance from the URI. |
| 57 | * |
| 58 | * @param uri device URI |
| 59 | * @return OSPF router id instance |
| 60 | */ |
| 61 | public static OspfRouterId ospfRouterId(URI uri) { |
| 62 | checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme"); |
| 63 | return new OspfRouterId(IpAddress.valueOf(uri.getSchemeSpecificPart())); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns device URI from the given router id. |
| 68 | * |
| 69 | * @param ospfRouterId router id instance |
| 70 | * @return device URI |
| 71 | */ |
| 72 | public static URI uri(OspfRouterId ospfRouterId) { |
| 73 | return uri(ospfRouterId.ipAddress()); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns device URI from the given IP address. |
| 78 | * |
| 79 | * @param ipAddress device IP address |
| 80 | * @return device URI |
| 81 | */ |
| 82 | public static URI uri(IpAddress ipAddress) { |
| 83 | try { |
| 84 | return new URI(SCHEME, ipAddress.toString(), null); |
| 85 | } catch (URISyntaxException e) { |
| 86 | return null; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns the IP address. |
| 92 | * |
| 93 | * @return IP address |
| 94 | */ |
| 95 | public IpAddress ipAddress() { |
| 96 | return ipAddress; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public String toString() { |
| 101 | return ipAddress.toString(); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public boolean equals(Object other) { |
| 106 | if (!(other instanceof OspfRouterId)) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | OspfRouterId otherOspfRouterId = (OspfRouterId) other; |
| 111 | return Objects.equals(ipAddress, otherOspfRouterId.ipAddress); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public int hashCode() { |
| 116 | return Objects.hash(ipAddress); |
| 117 | } |
| 118 | } |