blob: ba0cc96a3909ae70991b52808ab5ffd4e1fec8ea [file] [log] [blame]
Sean Condon96b896d2017-12-11 12:44:29 -08001/*
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 */
16package org.onosproject.drivers.microsemi.yang.utils;
17
18import org.onlab.packet.IpAddress;
19import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
20import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr;
21import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdDomainName;
22import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdMacUint;
23import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdNone;
24import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
25import org.onosproject.yang.gen.v1.ietfinettypes.rev20130715.ietfinettypes.DomainName;
26import org.onosproject.yang.gen.v1.mseacfm.rev20160229.mseacfm.mefcfm.maintenancedomain.MdNameAndTypeCombo;
27import org.onosproject.yang.gen.v1.mseacfm.rev20160229.mseacfm.mefcfm.maintenancedomain.mdnameandtypecombo.DefaultMacAddressAndUint;
28import org.onosproject.yang.gen.v1.mseacfm.rev20160229.mseacfm.mefcfm.maintenancedomain.mdnameandtypecombo.DefaultNameCharacterString;
29import org.onosproject.yang.gen.v1.mseacfm.rev20160229.mseacfm.mefcfm.maintenancedomain.mdnameandtypecombo.DefaultNameDomainName;
30import org.onosproject.yang.gen.v1.mseacfm.rev20160229.mseacfm.mefcfm.maintenancedomain.mdnameandtypecombo.DefaultNameNone;
31import org.onosproject.yang.gen.v1.mseacfm.rev20160229.mseacfm.mefcfm.maintenancedomain.mdnameandtypecombo.MacAddressAndUint;
32import org.onosproject.yang.gen.v1.mseacfm.rev20160229.mseacfm.mefcfm.maintenancedomain.mdnameandtypecombo.NameCharacterString;
33import org.onosproject.yang.gen.v1.mseacfm.rev20160229.mseacfm.mefcfm.maintenancedomain.mdnameandtypecombo.namedomainname.NameDomainNameUnion;
34import org.onosproject.yang.gen.v1.mseatypes.rev20160229.mseatypes.Identifier45;
35import org.onosproject.yang.gen.v1.mseatypes.rev20160229.mseatypes.MacAddressAndUintStr;
36
37/**
38 * Utility for translating between Maintenance Domain 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.
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 */
46public final class MdNameUtil {
47
48 private MdNameUtil() {
49 //Hidden
50 }
51
52 /**
53 * Convert CFM API MD identifier to the YANG model MD identifier.
54 * @param mdId Maintenance Domain ID in CFM API
55 * @return Maintenance Domain ID in YANG API
56 * @throws CfmConfigException If there's a problem with the name
57 */
58 public static MdNameAndTypeCombo getYangMdNameFromApiMdId(MdId mdId)
59 throws CfmConfigException {
60 MdNameAndTypeCombo mdName;
61 if (mdId instanceof MdIdDomainName) {
62 boolean isIpAddr = false;
63 try {
64 if (IpAddress.valueOf(mdId.mdName()) != null) {
65 isIpAddr = true;
66 }
67 } catch (IllegalArgumentException e) {
68 //continue
69 }
70 if (isIpAddr) {
71 mdName = new DefaultNameDomainName();
72 ((DefaultNameDomainName) mdName).nameDomainName(NameDomainNameUnion.of(
73 org.onosproject.yang.gen.v1.ietfinettypes.rev20130715.ietfinettypes.
74 IpAddress.fromString(mdId.mdName())));
75 } else {
76 mdName = new DefaultNameDomainName();
77 ((DefaultNameDomainName) mdName).nameDomainName(NameDomainNameUnion
78 .of(DomainName.fromString(mdId.mdName())));
79 }
80 } else if (mdId instanceof MdIdMacUint) {
81 mdName = new DefaultMacAddressAndUint();
82 ((DefaultMacAddressAndUint) mdName).nameMacAddressAndUint(MacAddressAndUintStr.fromString(mdId.mdName()));
83 } else if (mdId instanceof MdIdNone) {
84 mdName = new DefaultNameNone();
85 } else if (mdId instanceof MdIdCharStr) {
86 mdName = new DefaultNameCharacterString();
87 ((DefaultNameCharacterString) mdName).name(Identifier45.fromString(mdId.mdName()));
88 } else {
89 throw new CfmConfigException("Unexpected error creating MD " +
90 mdId.getClass().getSimpleName());
91 }
92 return mdName;
93 }
94
95 /**
96 * Convert YANG API MD identifier to the CFM API MD identifier.
97 * @param nameAndTypeCombo Maintenance Domain ID in YANG API
98 * @return Maintenance Domain ID in CFM API
99 */
100 public static MdId getApiMdIdFromYangMdName(MdNameAndTypeCombo nameAndTypeCombo) {
101 MdId mdId;
102 if (nameAndTypeCombo instanceof DefaultNameDomainName) {
103 NameDomainNameUnion domainName =
104 ((DefaultNameDomainName) nameAndTypeCombo).nameDomainName();
105 if (domainName.ipAddress() != null) {
106 mdId = MdIdDomainName.asMdId(domainName.ipAddress().toString());
107 } else if (domainName.domainName() != null) {
108 mdId = MdIdDomainName.asMdId(domainName.domainName().string());
109 } else {
110 throw new IllegalArgumentException("Unexpected domainName for " +
111 "MdNameAndTypeCombo: " + nameAndTypeCombo.toString());
112 }
113 } else if (nameAndTypeCombo instanceof DefaultNameCharacterString) {
114 mdId = MdIdCharStr.asMdId(
115 ((NameCharacterString) nameAndTypeCombo).name().string());
116
117 } else if (nameAndTypeCombo instanceof DefaultMacAddressAndUint) {
118 mdId = MdIdMacUint.asMdId(
119 ((MacAddressAndUint) nameAndTypeCombo).nameMacAddressAndUint().string());
120
121 } else if (nameAndTypeCombo instanceof DefaultNameNone) {
122 mdId = MdIdNone.asMdId();
123 } else {
124 throw new IllegalArgumentException("Unexpected type for " +
125 "MdNameAndTypeCombo: " + nameAndTypeCombo.toString());
126 }
127
128 return mdId;
129 }
130
131 /**
132 * Cast the YANG generic type of MdNameAndTypeCombo specifically to char string.
133 * @param maName a YANG generic MdNameAndTypeCombo
134 * @return a YANG specific MdNameAndTypeCombo for Char string
135 */
136 public static NameCharacterString cast(MdNameAndTypeCombo maName) {
137 return (NameCharacterString) maName;
138 }
139}