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.apache.commons.lang3.tuple.Pair; |
| 19 | import org.onlab.packet.MacAddress; |
| 20 | import org.onlab.util.Identifier; |
| 21 | |
| 22 | /** |
| 23 | * A representation of a Mac Address and Unsigned Integer as an MD identifier. |
| 24 | */ |
| 25 | public class MdIdMacUint extends Identifier<Pair<MacAddress, Integer>> implements MdId { |
| 26 | private static final String MACUINT_PATTERN = "([a-fA-F0-9]{2}[\\-:]){6}[0-9]{1,5}"; |
| 27 | private static final int MAC_UINT_LENGTH_BYTES = 8; |
| 28 | private static final int UINT_MIN = 0; |
| 29 | private static final int UINT_MAX = 65535; |
| 30 | |
| 31 | protected MdIdMacUint(Pair<MacAddress, Integer> macAndUint) { |
| 32 | super(macAndUint); |
| 33 | } |
| 34 | |
| 35 | @Override |
| 36 | public String mdName() { |
| 37 | return identifier.getLeft().toString() + ":" + identifier.getRight(); |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public String toString() { |
| 42 | return mdName(); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public int getNameLength() { |
| 47 | return MAC_UINT_LENGTH_BYTES; |
| 48 | } |
| 49 | |
| 50 | public static MdId asMdId(String mdName) { |
| 51 | if (mdName == null || !mdName.matches(MACUINT_PATTERN)) { |
| 52 | throw new IllegalArgumentException("MD Name must follow pattern " |
| 53 | + MACUINT_PATTERN + " Rejecting: " + mdName); |
| 54 | } |
| 55 | MacAddress macAddress = MacAddress.valueOf(mdName.substring(0, 17)); |
| 56 | int uInt = Integer.parseInt(mdName.substring(18)); |
| 57 | |
| 58 | return asMdId(macAddress, uInt); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public MdNameType nameType() { |
| 63 | return MdNameType.MACANDUINT; |
| 64 | } |
| 65 | |
| 66 | public static MdId asMdId(MacAddress macAddress, int uInt) { |
| 67 | if (uInt < UINT_MIN || uInt > UINT_MAX) { |
| 68 | throw new IllegalArgumentException("uInt must be between " + |
| 69 | UINT_MIN + " and " + UINT_MAX + ". Rejecting: " + uInt); |
| 70 | } |
| 71 | return new MdIdMacUint(Pair.of(macAddress, uInt)); |
| 72 | } |
| 73 | } |