config-manager-facade-xml: final parameters
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / attributes / AttributeIfcSwitchStatement.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.config.facade.xml.mapping.attributes;
10
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;
22
23 public abstract class AttributeIfcSwitchStatement<T> {
24
25     private AttributeIfc lastAttribute;
26
27     public T switchAttribute(final AttributeIfc attributeIfc) {
28
29         this.lastAttribute = attributeIfc;
30
31         OpenType<?> openType = attributeIfc.getOpenType();
32
33         if (attributeIfc instanceof JavaAttribute) {
34             try {
35                 if(((JavaAttribute)attributeIfc).getTypeDefinition() instanceof BinaryTypeDefinition) {
36                     return caseJavaBinaryAttribute(openType);
37                 } else if(((JavaAttribute)attributeIfc).isUnion()) {
38                     return caseJavaUnionAttribute(openType);
39                 } else if(((JavaAttribute)attributeIfc).isIdentityRef()) {
40                     return caseJavaIdentityRefAttribute(openType);
41                 } else if(((JavaAttribute)attributeIfc).isEnum()) {
42                     return caseJavaEnumAttribute(openType);
43                 } else {
44                     return caseJavaAttribute(openType);
45                 }
46             } catch (final UnknownOpenTypeException e) {
47                 throw getIllegalArgumentException(attributeIfc);
48             }
49
50         } else if (attributeIfc instanceof DependencyAttribute) {
51             return caseDependencyAttribute(((DependencyAttribute) attributeIfc).getOpenType());
52         } else if (attributeIfc instanceof ListAttribute) {
53             return caseListAttribute((ArrayType<?>) openType);
54         } else if (attributeIfc instanceof ListDependenciesAttribute) {
55             return caseListDependeciesAttribute((ArrayType<?>) openType);
56         } else if (attributeIfc instanceof TOAttribute) {
57             return caseTOAttribute(((TOAttribute) attributeIfc).getOpenType());
58         }
59
60         throw getIllegalArgumentException(attributeIfc);
61     }
62
63     public AttributeIfc getLastAttribute() {
64         return lastAttribute;
65     }
66
67     protected T caseJavaIdentityRefAttribute(final OpenType<?> openType) {
68         return caseJavaAttribute(openType);
69     }
70
71     protected T caseJavaUnionAttribute(final OpenType<?> openType) {
72         return caseJavaAttribute(openType);
73     }
74
75     protected T caseJavaEnumAttribute(final OpenType<?> openType) {
76         return caseJavaAttribute(openType);
77     }
78
79     protected T caseJavaBinaryAttribute(final OpenType<?> openType) {
80         return caseJavaAttribute(openType);
81     }
82
83     private IllegalArgumentException getIllegalArgumentException(final AttributeIfc attributeIfc) {
84         return new IllegalArgumentException("Unknown attribute type " + attributeIfc.getClass() + ", " + attributeIfc
85                 + " with open type:" + attributeIfc.getOpenType());
86     }
87
88     public final T caseJavaAttribute(final OpenType<?> openType) {
89         if (openType instanceof SimpleType<?>) {
90             return caseJavaSimpleAttribute((SimpleType<?>) openType);
91         } else if (openType instanceof ArrayType<?>) {
92             return caseJavaArrayAttribute((ArrayType<?>) openType);
93         } else if (openType instanceof CompositeType) {
94             return caseJavaCompositeAttribute((CompositeType) openType);
95         }
96
97         throw new UnknownOpenTypeException("Unknown attribute open type " + openType);
98     }
99
100     protected abstract T caseJavaSimpleAttribute(SimpleType<?> openType);
101
102     protected abstract T caseJavaArrayAttribute(ArrayType<?> openType);
103
104     protected abstract T caseJavaCompositeAttribute(CompositeType openType);
105
106     protected abstract T caseDependencyAttribute(SimpleType<?> attributeIfc);
107
108     protected abstract T caseTOAttribute(CompositeType openType);
109
110     protected abstract T caseListAttribute(ArrayType<?> openType);
111
112     protected abstract T caseListDependeciesAttribute(ArrayType<?> openType);
113
114     private static class UnknownOpenTypeException extends RuntimeException {
115         private static final long serialVersionUID = 1L;
116
117         public UnknownOpenTypeException(final String message) {
118             super(message);
119         }
120     }
121 }