blob: 9d324e77050f7cae1d5bda73218fea9cbef1499f [file] [log] [blame]
cheng fan48e832c2015-05-29 01:54:47 +08001/*
2 * Copyright 2014 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.api;
17
18import java.net.URI;
19import java.net.URISyntaxException;
20
21import org.onosproject.pcep.tools.PcepTools;
22
23/**
24 * The class representing a network switch PCEPDid. This class is immutable.
25 */
26public final class PcepDpid {
27
28 private static final String SCHEME = "pcep";
29 private static final long UNKNOWN = 0;
30 private long nodeId;
31
32 /**
33 * Default constructor.
34 */
35 public PcepDpid() {
36 this.nodeId = PcepDpid.UNKNOWN;
37 }
38
39 /**
40 * Constructor from a long value.
41 *
42 * @param value long value for construct
43 */
44 public PcepDpid(long value) {
45 this.nodeId = value;
46 }
47
48 /**
49 * Constructor from a String.
50 *
51 * @param value string value for construct
52 */
53 public PcepDpid(String value) {
54 this.nodeId = Long.parseLong(value, 16);
55 }
56
57 /**
58 * Produces device URI from the given DPID.
59 *
60 * @param dpid device dpid
61 * @return device URI
62 */
63 public static URI uri(PcepDpid dpid) {
64 return uri(dpid.nodeId);
65 }
66
67 /**
68 * Produces device long from the given string which comes from the uri
69 * method.
70 *
71 * @param value string value which produced by uri method.
72 * @return a long value.
73 */
74 public static long toLong(String value) {
75 return PcepTools.ipToLong(value.replace(SCHEME, ""));
76 }
77
78 /**
79 * Produces device URI from the given DPID long.
80 *
81 * @param value device dpid as long
82 * @return device URI
83 */
84 public static URI uri(long value) {
85 try {
86 return new URI(SCHEME, PcepTools.longToIp(value), null);
87 } catch (URISyntaxException e) {
88 return null;
89 }
90 }
91
92 /**
93 * Return a device id with the form of long.
94 *
95 * @return long value
96 */
97 public long value() {
98 return this.nodeId;
99 }
100
101}