blob: 45d15bd3e9de93b72bf16c4ef55ea49109f06bc7 [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;
20import java.net.URI;
21import java.net.URISyntaxException;
22import java.util.Objects;
23
24import 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 VH5dd8dbe2015-11-26 13:22:18 +053030public final class BgpId {
Satish Ke107e662015-09-21 19:00:17 +053031
32 private static final String SCHEME = "bgp";
33 private static final long UNKNOWN = 0;
34 private final IpAddress ipAddress;
35
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) {
Satish Ke107e662015-09-21 19:00:17 +053042 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 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() {
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 VH5dd8dbe2015-11-26 13:22:18 +053076 if (!(other instanceof BgpId)) {
Satish Ke107e662015-09-21 19:00:17 +053077 return false;
78 }
79
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053080 BgpId otherBGPid = (BgpId) other;
Satish Ke107e662015-09-21 19:00:17 +053081 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 VH5dd8dbe2015-11-26 13:22:18 +053095 public static BgpId bgpId(URI uri) {
Satish Ke107e662015-09-21 19:00:17 +053096 checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme");
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053097 return new BgpId(IpAddress.valueOf(uri.getSchemeSpecificPart()));
Satish Ke107e662015-09-21 19:00:17 +053098 }
99
100 /**
101 * Produces device URI from the given DPID.
102 *
103 * @param bgpId device bgpId
104 * @return device URI
105 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530106 public static URI uri(BgpId bgpId) {
Satish Ke107e662015-09-21 19:00:17 +0530107 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 saraswal6c886952015-12-11 01:43:11 +0530123}