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 | import com.google.common.net.InternetDomainName; |
| 21 | |
| 22 | /** |
| 23 | * A representation of a domain name as an MD identifier. |
| 24 | */ |
| 25 | public class MdIdDomainName extends Identifier<InternetDomainName> implements MdId { |
| 26 | |
| 27 | protected MdIdDomainName(InternetDomainName mdDomainName) { |
| 28 | super(mdDomainName); |
| 29 | } |
| 30 | |
| 31 | @Override |
| 32 | public String mdName() { |
| 33 | return identifier.toString(); |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public int getNameLength() { |
| 38 | return identifier.toString().length(); |
| 39 | } |
| 40 | |
| 41 | public static MdId asMdId(String mdName) { |
| 42 | if (mdName == null || !InternetDomainName.isValid(mdName)) { |
| 43 | throw new IllegalArgumentException("MD Name must follow internet domain name pattern " |
| 44 | + " Rejecting: " + mdName); |
| 45 | } |
| 46 | return asMdId(InternetDomainName.from(mdName)); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public MdNameType nameType() { |
| 51 | return MdNameType.DOMAINNAME; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | public static MdId asMdId(InternetDomainName mdName) { |
| 56 | if (mdName == null || mdName.toString().length() < MD_NAME_MIN_LEN || |
| 57 | mdName.toString().length() > MD_NAME_MAX_LEN) { |
| 58 | throw new IllegalArgumentException("MD Domain Name must be between " + |
| 59 | MD_NAME_MIN_LEN + " and " + MD_NAME_MAX_LEN + " chars long" |
| 60 | + " Rejecting: " + mdName); |
| 61 | } |
| 62 | return new MdIdDomainName(mdName); |
| 63 | } |
| 64 | } |