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 | * ICC-based MEG ID Format, thirteen octet field. |
| 22 | * |
| 23 | * It consists of two subfields: the ITU Carrier Code (ICC) followed by a unique |
| 24 | * MEG ID code (UMC). The ITU Carrier Code consists of 1-6 |
| 25 | * left-justified characters, alphabetic, or leading alphabetic |
| 26 | * with trailing numeric. The UMC code immediately follows the ICC |
| 27 | * and shall consist of 7-12 characters, with trailing NULLs, |
| 28 | * completing the 13-character MEG ID Value. |
| 29 | * reference |
| 30 | * [Y.1731] Annex A; |
| 31 | */ |
| 32 | public final class MaIdIccY1731 extends Identifier<String> implements MaIdShort { |
| 33 | private static final String ICC_PATTERN = "[a-z|A-Z|0-9]{1,6}"; |
| 34 | private static final String UMC_PATTERN = "[a-z|A-Z|0-9]{7,12}"; |
| 35 | private int iccLength = 0; |
| 36 | |
| 37 | protected MaIdIccY1731(String icc, String umc) { |
| 38 | super(icc + umc); |
| 39 | iccLength = icc.length(); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public String toString() { |
| 44 | return identifier.substring(0, iccLength) + ":" + identifier.substring(iccLength); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public String maName() { |
| 49 | return identifier; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public int getNameLength() { |
| 54 | return identifier.length(); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public MaIdType nameType() { |
| 59 | return MaIdType.ICCY1731; |
| 60 | } |
| 61 | |
| 62 | public static MaIdShort asMaId(String icc, String umc) { |
| 63 | if (icc == null || !icc.matches(ICC_PATTERN)) { |
| 64 | throw new IllegalArgumentException("ICC part must follow pattern " |
| 65 | + ICC_PATTERN + " Rejecting: " + icc); |
| 66 | } else if (umc == null || !umc.matches(UMC_PATTERN)) { |
| 67 | throw new IllegalArgumentException("UMC part must follow pattern " |
| 68 | + UMC_PATTERN + " Rejecting: " + umc); |
| 69 | } |
| 70 | return new MaIdIccY1731(icc, umc); |
| 71 | } |
| 72 | |
| 73 | public static MaIdShort asMaId(String iccAndUmc) { |
| 74 | String[] nameParts = iccAndUmc.split(":"); |
| 75 | if (nameParts.length != 2) { |
| 76 | throw new IllegalArgumentException("Expecting format like ICC:UMC"); |
| 77 | } |
| 78 | return asMaId(nameParts[0], nameParts[1]); |
| 79 | } |
| 80 | } |