blob: 7a6c625d0b849fcf1b2cc03ee122c8d3186f5dab [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 /**
37 * Private constructor.
38 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053039 private BgpId(IpAddress ipAddress) {
Satish Ke107e662015-09-21 19:00:17 +053040 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 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053049 public static BgpId bgpId(IpAddress ipAddress) {
50 return new BgpId(ipAddress);
Satish Ke107e662015-09-21 19:00:17 +053051 }
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) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053074 if (!(other instanceof BgpId)) {
Satish Ke107e662015-09-21 19:00:17 +053075 return false;
76 }
77
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053078 BgpId otherBGPid = (BgpId) other;
Satish Ke107e662015-09-21 19:00:17 +053079 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 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053093 public static BgpId bgpId(URI uri) {
Satish Ke107e662015-09-21 19:00:17 +053094 checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme");
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053095 return new BgpId(IpAddress.valueOf(uri.getSchemeSpecificPart()));
Satish Ke107e662015-09-21 19:00:17 +053096 }
97
98 /**
99 * Produces device URI from the given DPID.
100 *
101 * @param bgpId device bgpId
102 * @return device URI
103 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530104 public static URI uri(BgpId bgpId) {
Satish Ke107e662015-09-21 19:00:17 +0530105 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}