2 * Copyright (c) 2015, 2017 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.config.facade.xml.mapping.attributes;
11 import javax.management.openmbean.ArrayType;
12 import javax.management.openmbean.CompositeType;
13 import javax.management.openmbean.OpenType;
14 import javax.management.openmbean.SimpleType;
15 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
16 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
17 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
18 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
19 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
20 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
21 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
23 public abstract class AttributeIfcSwitchStatement<T> {
25 private AttributeIfc lastAttribute;
27 @SuppressWarnings("checkstyle:avoidHidingCauseException")
28 public T switchAttribute(final AttributeIfc attributeIfc) {
30 this.lastAttribute = attributeIfc;
32 OpenType<?> openType = attributeIfc.getOpenType();
34 if (attributeIfc instanceof JavaAttribute) {
36 if (((JavaAttribute) attributeIfc).getTypeDefinition() instanceof BinaryTypeDefinition) {
37 return caseJavaBinaryAttribute(openType);
38 } else if (((JavaAttribute) attributeIfc).isUnion()) {
39 return caseJavaUnionAttribute(openType);
40 } else if (((JavaAttribute) attributeIfc).isIdentityRef()) {
41 return caseJavaIdentityRefAttribute(openType);
42 } else if (((JavaAttribute) attributeIfc).isEnum()) {
43 return caseJavaEnumAttribute(openType);
45 return caseJavaAttribute(openType);
47 } catch (final UnknownOpenTypeException e) {
48 throw getIllegalArgumentException(attributeIfc);
51 } else if (attributeIfc instanceof DependencyAttribute) {
52 return caseDependencyAttribute(((DependencyAttribute) attributeIfc).getOpenType());
53 } else if (attributeIfc instanceof ListAttribute) {
54 return caseListAttribute((ArrayType<?>) openType);
55 } else if (attributeIfc instanceof ListDependenciesAttribute) {
56 return caseListDependeciesAttribute((ArrayType<?>) openType);
57 } else if (attributeIfc instanceof TOAttribute) {
58 return caseTOAttribute(((TOAttribute) attributeIfc).getOpenType());
61 throw getIllegalArgumentException(attributeIfc);
64 public AttributeIfc getLastAttribute() {
68 protected T caseJavaIdentityRefAttribute(final OpenType<?> openType) {
69 return caseJavaAttribute(openType);
72 protected T caseJavaUnionAttribute(final OpenType<?> openType) {
73 return caseJavaAttribute(openType);
76 protected T caseJavaEnumAttribute(final OpenType<?> openType) {
77 return caseJavaAttribute(openType);
80 protected T caseJavaBinaryAttribute(final OpenType<?> openType) {
81 return caseJavaAttribute(openType);
84 private IllegalArgumentException getIllegalArgumentException(final AttributeIfc attributeIfc) {
85 return new IllegalArgumentException("Unknown attribute type " + attributeIfc.getClass() + ", " + attributeIfc
86 + " with open type:" + attributeIfc.getOpenType());
89 public final T caseJavaAttribute(final OpenType<?> openType) {
90 if (openType instanceof SimpleType<?>) {
91 return caseJavaSimpleAttribute((SimpleType<?>) openType);
92 } else if (openType instanceof ArrayType<?>) {
93 return caseJavaArrayAttribute((ArrayType<?>) openType);
94 } else if (openType instanceof CompositeType) {
95 return caseJavaCompositeAttribute((CompositeType) openType);
98 throw new UnknownOpenTypeException("Unknown attribute open type " + openType);
101 protected abstract T caseJavaSimpleAttribute(SimpleType<?> openType);
103 protected abstract T caseJavaArrayAttribute(ArrayType<?> openType);
105 protected abstract T caseJavaCompositeAttribute(CompositeType openType);
107 protected abstract T caseDependencyAttribute(SimpleType<?> attributeIfc);
109 protected abstract T caseTOAttribute(CompositeType openType);
111 protected abstract T caseListAttribute(ArrayType<?> openType);
113 protected abstract T caseListDependeciesAttribute(ArrayType<?> openType);
115 private static class UnknownOpenTypeException extends RuntimeException {
116 private static final long serialVersionUID = 1L;
118 UnknownOpenTypeException(final String message) {