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.drivers.microsemi.yang.utils; |
| 17 | |
| 18 | import org.onlab.util.HexString; |
| 19 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaId2Octet; |
| 20 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr; |
| 21 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdIccY1731; |
| 22 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdPrimaryVid; |
| 23 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdRfc2685VpnId; |
| 24 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort; |
| 25 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 26 | import org.onosproject.yang.gen.v1.ietfyangtypes.rev20130715.ietfyangtypes.YangIdentifier; |
| 27 | import org.onosproject.yang.gen.v1.mseacfm.rev20160229.mseacfm.mefcfm.maintenancedomain.maintenanceassociation.MaNameAndTypeCombo; |
| 28 | import org.onosproject.yang.gen.v1.mseacfm.rev20160229.mseacfm.mefcfm.maintenancedomain.maintenanceassociation.manameandtypecombo.DefaultNameCharacterString; |
| 29 | import org.onosproject.yang.gen.v1.mseacfm.rev20160229.mseacfm.mefcfm.maintenancedomain.maintenanceassociation.manameandtypecombo.DefaultNamePrimaryVid; |
| 30 | import org.onosproject.yang.gen.v1.mseacfm.rev20160229.mseacfm.mefcfm.maintenancedomain.maintenanceassociation.manameandtypecombo.DefaultNameRfc2685VpnId; |
| 31 | import org.onosproject.yang.gen.v1.mseacfm.rev20160229.mseacfm.mefcfm.maintenancedomain.maintenanceassociation.manameandtypecombo.DefaultNameUint16; |
| 32 | import org.onosproject.yang.gen.v1.mseacfm.rev20160229.mseacfm.mefcfm.maintenancedomain.maintenanceassociation.manameandtypecombo.DefaultNameY1731Icc; |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 33 | import org.onosproject.yang.gen.v1.mseacfm.rev20160229.mseacfm.mefcfm.maintenancedomain.maintenanceassociation.manameandtypecombo.NameCharacterString; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 34 | import org.onosproject.yang.gen.v1.mseacfm.rev20160229.mseacfm.mefcfm.maintenancedomain.maintenanceassociation.manameandtypecombo.nameprimaryvid.NamePrimaryVidUnion; |
| 35 | import org.onosproject.yang.gen.v1.mseatypes.rev20160229.mseatypes.Identifier45; |
| 36 | |
| 37 | /** |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 38 | * Utility for translating between Maintenance Association names in the CFM API model and the device YANG. |
| 39 | * |
| 40 | * This has to be in a separate file as a workaround for Checkstyle issue. |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 41 | * https://github.com/checkstyle/checkstyle/issues/3850 |
| 42 | * There are two types of DefaultNameCharacterString - one for MA and another for MD |
| 43 | * Putting both together in a file means that the full path has to be given which |
| 44 | * will then fail checkstyle |
| 45 | */ |
| 46 | public final class MaNameUtil { |
| 47 | |
| 48 | private MaNameUtil() { |
| 49 | //Hidden |
| 50 | } |
| 51 | |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 52 | /** |
| 53 | * Convert CFM API MA identifier to the YANG model MA identifier. |
| 54 | * @param maId Maintenance Association ID in CFM API |
| 55 | * @return Maintenance Association ID in YANG API |
| 56 | * @throws CfmConfigException If there's a problem with the name |
| 57 | */ |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 58 | public static MaNameAndTypeCombo getYangMaNameFromApiMaId(MaIdShort maId) |
| 59 | throws CfmConfigException { |
| 60 | MaNameAndTypeCombo maName; |
| 61 | if (maId instanceof MaIdPrimaryVid) { |
| 62 | maName = new DefaultNamePrimaryVid(); |
| 63 | ((DefaultNamePrimaryVid) maName).namePrimaryVid(NamePrimaryVidUnion.fromString(maId.maName())); |
| 64 | } else if (maId instanceof MaId2Octet) { |
| 65 | maName = new DefaultNameUint16(); |
| 66 | ((DefaultNameUint16) maName).nameUint16(Integer.valueOf(maId.maName())); |
| 67 | } else if (maId instanceof MaIdRfc2685VpnId) { |
| 68 | maName = new DefaultNameRfc2685VpnId(); |
| 69 | ((DefaultNameRfc2685VpnId) maName).nameRfc2685VpnId(HexString.fromHexString(maId.maName())); |
| 70 | } else if (maId instanceof MaIdIccY1731) { |
| 71 | maName = new DefaultNameY1731Icc(); |
| 72 | ((DefaultNameY1731Icc) maName).nameY1731Icc(YangIdentifier.of(maId.maName())); |
| 73 | } else if (maId instanceof MaIdCharStr) { |
| 74 | maName = new DefaultNameCharacterString(); |
| 75 | ((DefaultNameCharacterString) maName).name(Identifier45.fromString(maId.maName())); |
| 76 | } else { |
| 77 | throw new CfmConfigException("Unexpected error creating MD " + |
| 78 | maId.getClass().getSimpleName()); |
| 79 | } |
| 80 | return maName; |
| 81 | } |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Convert YANG API MA identifier to the CFM API MA identifier. |
| 85 | * @param nameAndTypeCombo Maintenance Association ID in YANG API |
| 86 | * @return Maintenance Association ID in CFM API |
| 87 | */ |
| 88 | public static MaIdShort getApiMaIdFromYangMaName(MaNameAndTypeCombo nameAndTypeCombo) { |
| 89 | MaIdShort maId; |
| 90 | if (nameAndTypeCombo instanceof DefaultNameCharacterString) { |
| 91 | maId = MaIdCharStr.asMaId( |
| 92 | ((DefaultNameCharacterString) nameAndTypeCombo).name().string()); |
| 93 | } else if (nameAndTypeCombo instanceof DefaultNamePrimaryVid) { |
| 94 | if (((DefaultNamePrimaryVid) nameAndTypeCombo).namePrimaryVid().enumeration() != null) { |
| 95 | maId = MaIdPrimaryVid.asMaId( |
| 96 | ((DefaultNamePrimaryVid) nameAndTypeCombo).namePrimaryVid().enumeration().name()); |
| 97 | } else if (((DefaultNamePrimaryVid) nameAndTypeCombo).namePrimaryVid().vlanIdType() != null) { |
| 98 | maId = MaIdPrimaryVid.asMaId( |
| 99 | ((DefaultNamePrimaryVid) nameAndTypeCombo).namePrimaryVid().vlanIdType().uint16()); |
| 100 | } else { |
| 101 | throw new IllegalArgumentException("Unexpected primaryVid for " + |
| 102 | "MaNameAndTypeCombo: " + nameAndTypeCombo.toString()); |
| 103 | } |
| 104 | } else if (nameAndTypeCombo instanceof DefaultNameUint16) { |
| 105 | maId = MaId2Octet.asMaId(((DefaultNameUint16) nameAndTypeCombo).nameUint16()); |
| 106 | |
| 107 | } else if (nameAndTypeCombo instanceof DefaultNameRfc2685VpnId) { |
| 108 | maId = MaIdRfc2685VpnId.asMaIdHex( |
| 109 | HexString.toHexString( |
| 110 | ((DefaultNameRfc2685VpnId) nameAndTypeCombo).nameRfc2685VpnId())); |
| 111 | } else if (nameAndTypeCombo instanceof DefaultNameY1731Icc) { |
| 112 | maId = MaIdIccY1731.asMaId(((DefaultNameY1731Icc) nameAndTypeCombo).nameY1731Icc().string()); |
| 113 | |
| 114 | } else { |
| 115 | throw new IllegalArgumentException("Unexpected type for " + |
| 116 | "MaNameAndTypeCombo: " + nameAndTypeCombo.toString()); |
| 117 | } |
| 118 | |
| 119 | return maId; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Cast the YANG generic type of MaNameAndTypeCombo specifically to char string. |
| 124 | * @param maName a YANG generic MaNameAndTypeCombo |
| 125 | * @return a YANG specific MaNameAndTypeCombo for Char string |
| 126 | */ |
| 127 | public static NameCharacterString cast(MaNameAndTypeCombo maName) { |
| 128 | return (NameCharacterString) maName; |
| 129 | } |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 130 | } |