blob: 3ac52695027a30db33d80030b0889bfa3e77f441 [file] [log] [blame]
cheng fan48e832c2015-05-29 01:54:47 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
cheng fan48e832c2015-05-29 01:54:47 +08003 *
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;
harikrushna-Huaweibbb3dd52017-04-12 17:58:38 +053022import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
cheng fan48e832c2015-05-29 01:54:47 +080024
25/**
26 * The class representing a network switch PCEPDid. This class is immutable.
27 */
28public final class PcepDpid {
harikrushna-Huaweibbb3dd52017-04-12 17:58:38 +053029 private static final Logger log = LoggerFactory.getLogger(PcepDpid.class);
Priyanka B9bee0802016-04-27 22:06:02 +053030 private static final String SCHEME = "l3";
cheng fan48e832c2015-05-29 01:54:47 +080031 private static final long UNKNOWN = 0;
32 private long nodeId;
33
34 /**
35 * Default constructor.
36 */
37 public PcepDpid() {
38 this.nodeId = PcepDpid.UNKNOWN;
39 }
40
41 /**
42 * Constructor from a long value.
43 *
44 * @param value long value for construct
45 */
46 public PcepDpid(long value) {
47 this.nodeId = value;
48 }
49
50 /**
51 * Constructor from a String.
52 *
53 * @param value string value for construct
54 */
55 public PcepDpid(String value) {
56 this.nodeId = Long.parseLong(value, 16);
57 }
58
59 /**
60 * Produces device URI from the given DPID.
61 *
62 * @param dpid device dpid
63 * @return device URI
64 */
65 public static URI uri(PcepDpid dpid) {
66 return uri(dpid.nodeId);
67 }
68
69 /**
harikrushna-Huaweibbb3dd52017-04-12 17:58:38 +053070 * Produces pcep URI.
71 *
72 * @param value string to get URI
73 * @return pcep URI, otherwise null
74 */
75 public static URI uri(String value) {
76 try {
77 return new URI(SCHEME, value, null);
78 } catch (URISyntaxException e) {
79 log.debug("Exception PcepId URI: " + e.toString());
80 }
81 return null;
82 }
83
84 /**
cheng fan48e832c2015-05-29 01:54:47 +080085 * Produces device long from the given string which comes from the uri
86 * method.
87 *
88 * @param value string value which produced by uri method.
89 * @return a long value.
90 */
91 public static long toLong(String value) {
92 return PcepTools.ipToLong(value.replace(SCHEME, ""));
93 }
94
95 /**
96 * Produces device URI from the given DPID long.
97 *
98 * @param value device dpid as long
99 * @return device URI
100 */
101 public static URI uri(long value) {
102 try {
103 return new URI(SCHEME, PcepTools.longToIp(value), null);
104 } catch (URISyntaxException e) {
105 return null;
106 }
107 }
108
109 /**
110 * Return a device id with the form of long.
111 *
112 * @return long value
113 */
114 public long value() {
115 return this.nodeId;
116 }
117
118}