blob: 3ff622bf7d5a9761986c21bc13ffd6c57a9d9768 [file] [log] [blame]
Phaneendra Manda1c0061d2015-08-06 12:29:38 +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 */
16package org.onosproject.pcep.controller;
17
18import static com.google.common.base.Preconditions.checkArgument;
19import java.net.URI;
20import java.net.URISyntaxException;
21import java.util.Objects;
22
23import org.onlab.packet.IpAddress;
24
25/**
26 * The class representing a network client pc ip.
27 * This class is immutable.
28 */
29public final class PccId {
30
31 private static final String SCHEME = "pcep";
32 private static final long UNKNOWN = 0;
33 private final IpAddress ipAddress;
34
35 /**
36 * Private constructor.
37 */
38 private PccId(IpAddress ipAddress) {
39 this.ipAddress = ipAddress;
40 }
41
42 /**
43 * Create a PccId from ip address.
44 *
45 * @param ipAddress IP address
46 * @return ipAddress
47 */
48 public static PccId pccId(IpAddress ipAddress) {
49 return new PccId(ipAddress);
50 }
51
52 /**
53 * Returns the ip address.
54 *
55 * @return ipAddress
56 */
57 public IpAddress ipAddress() {
58 return ipAddress;
59 }
60
61 /**
62 * Convert the PccId value to a ':' separated hexadecimal string.
63 *
64 * @return the PccId value as a ':' separated hexadecimal string.
65 */
66 @Override
67 public String toString() {
68 return ipAddress.toString();
69 }
70
71 @Override
72 public boolean equals(Object other) {
73 if (!(other instanceof PccId)) {
74 return false;
75 }
76
77 PccId otherPccid = (PccId) other;
78 return Objects.equals(ipAddress, otherPccid.ipAddress);
79 }
80
81 @Override
82 public int hashCode() {
83 return Objects.hash(ipAddress);
84 }
85
86 /**
87 * Returns PccId created from the given client URI.
88 *
89 * @param uri device URI
90 * @return pccid
91 */
92 public static PccId pccid(URI uri) {
93 checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme");
94 return new PccId(IpAddress.valueOf(uri.getSchemeSpecificPart()));
95 }
96
97 /**
98 * Produces client URI from the given DPID.
99 *
100 * @param pccid client pccid
101 * @return client URI
102 */
103 public static URI uri(PccId pccid) {
104 return uri(pccid.ipAddress());
105 }
106
107 /**
108 * Produces client URI from the given ip address.
109 *
110 * @param ipAddress ip of client
111 * @return client URI
112 */
113 public static URI uri(IpAddress ipAddress) {
114 try {
115 return new URI(SCHEME, ipAddress.toString(), null);
116 } catch (URISyntaxException e) {
117 return null;
118 }
119 }
120}