cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 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 | package org.onosproject.pcep.api; |
| 17 | |
| 18 | import java.net.URI; |
| 19 | import java.net.URISyntaxException; |
| 20 | |
| 21 | import org.onosproject.pcep.tools.PcepTools; |
harikrushna-Huawei | bbb3dd5 | 2017-04-12 17:58:38 +0530 | [diff] [blame] | 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * The class representing a network switch PCEPDid. This class is immutable. |
| 27 | */ |
| 28 | public final class PcepDpid { |
harikrushna-Huawei | bbb3dd5 | 2017-04-12 17:58:38 +0530 | [diff] [blame] | 29 | private static final Logger log = LoggerFactory.getLogger(PcepDpid.class); |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 30 | private static final String SCHEME = "l3"; |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 31 | 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-Huawei | bbb3dd5 | 2017-04-12 17:58:38 +0530 | [diff] [blame] | 70 | * 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 fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 85 | * 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 | } |