cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -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; |
| 22 | |
| 23 | /** |
| 24 | * The class representing a network switch PCEPDid. This class is immutable. |
| 25 | */ |
| 26 | public final class PcepDpid { |
| 27 | |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 28 | private static final String SCHEME = "l3"; |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 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 | } |