Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.incubator.net.l2monitoring.cfm.identifier; |
| 17 | |
| 18 | import org.onlab.util.Identifier; |
| 19 | |
| 20 | /** |
| 21 | * A representation of a Rfc2685VpnId identifier in Hexadecimal as an MA identifier. |
| 22 | */ |
| 23 | public final class MaIdRfc2685VpnId extends Identifier<String> implements MaIdShort { |
| 24 | private static final String VPNID_HEX_PATTERN = "([a-f|A-F|0-9]{2}:){6}[a-f|A-F|0-9]{2}"; |
| 25 | |
| 26 | protected MaIdRfc2685VpnId(String maNameHex) { |
| 27 | super(maNameHex); |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | public String maName() { |
| 32 | return identifier; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Identifier will be in the format aa:bb:cc:dd:ee:ff:11. |
| 37 | * Each pair of hex chars (and one colon) is one byte |
| 38 | * To get the length in bytes add 1 (extra colon) and divide by 3 |
| 39 | * @return name length in bytes |
| 40 | */ |
| 41 | @Override |
| 42 | public int getNameLength() { |
| 43 | return (identifier.length() + 1) / 3; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public MaIdType nameType() { |
| 48 | return MaIdType.RFC2685VPNID; |
| 49 | } |
| 50 | |
| 51 | public static MaIdShort asMaIdHex(String hexString) { |
| 52 | if (hexString == null || !hexString.matches(VPNID_HEX_PATTERN)) { |
| 53 | throw new IllegalArgumentException("MA Name must follow pattern " + |
| 54 | VPNID_HEX_PATTERN + " Rejecting: " + hexString); |
| 55 | } |
| 56 | return new MaIdRfc2685VpnId(hexString.toLowerCase()); |
| 57 | } |
| 58 | } |