c08be06c9f619bfc48ff144f523f64535cb8a605
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / AttributeIfcSwitchStatement.java
1 /*
2  * Copyright (c) 2013 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.netconf.confignetconfconnector.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
16 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
17 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
18 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
19 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
20 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
21 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
22 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
23
24 public abstract class AttributeIfcSwitchStatement<T> {
25
26     private AttributeIfc lastAttribute;
27
28     public T switchAttribute(AttributeIfc attributeIfc) {
29
30         this.lastAttribute = attributeIfc;
31
32         OpenType<?> openType = attributeIfc.getOpenType();
33
34         if (attributeIfc instanceof JavaAttribute) {
35             try {
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 {
43                     return caseJavaAttribute(openType);
44                 }
45             } catch (UnknownOpenTypeException e) {
46                 throw getIllegalArgumentException(attributeIfc);
47             }
48
49         } else if (attributeIfc instanceof DependencyAttribute) {
50             return caseDependencyAttribute(((DependencyAttribute) attributeIfc).getOpenType());
51         } else if (attributeIfc instanceof ListAttribute) {
52             return caseListAttribute((ArrayType<?>) openType);
53         } else if (attributeIfc instanceof ListDependenciesAttribute) {
54             return caseListDependeciesAttribute((ArrayType<?>) openType);
55         } else if (attributeIfc instanceof TOAttribute) {
56             return caseTOAttribute(((TOAttribute) attributeIfc).getOpenType());
57         }
58
59         throw getIllegalArgumentException(attributeIfc);
60     }
61
62     public AttributeIfc getLastAttribute() {
63         return lastAttribute;
64     }
65
66     protected T caseJavaIdentityRefAttribute(OpenType<?> openType) {
67         return caseJavaAttribute(openType);
68     }
69
70     protected T caseJavaUnionAttribute(OpenType<?> openType) {
71         return caseJavaAttribute(openType);
72     }
73
74     protected T caseJavaBinaryAttribute(OpenType<?> openType) {
75         return caseJavaAttribute(openType);
76     }
77
78     private IllegalArgumentException getIllegalArgumentException(AttributeIfc attributeIfc) {
79         return new IllegalArgumentException("Unknown attribute type " + attributeIfc.getClass() + ", " + attributeIfc
80                 + " with open type:" + attributeIfc.getOpenType());
81     }
82
83     public final T caseJavaAttribute(OpenType<?> openType) {
84         if (openType instanceof SimpleType<?>) {
85             return caseJavaSimpleAttribute((SimpleType<?>) openType);
86         } else if (openType instanceof ArrayType<?>) {
87             return caseJavaArrayAttribute((ArrayType<?>) openType);
88         } else if (openType instanceof CompositeType) {
89             return caseJavaCompositeAttribute((CompositeType) openType);
90         }
91
92         throw new UnknownOpenTypeException("Unknown attribute open type " + openType);
93     }
94
95     protected abstract T caseJavaSimpleAttribute(SimpleType<?> openType);
96
97     protected abstract T caseJavaArrayAttribute(ArrayType<?> openType);
98
99     protected abstract T caseJavaCompositeAttribute(CompositeType openType);
100
101     protected abstract T caseDependencyAttribute(SimpleType<?> attributeIfc);
102
103     protected abstract T caseTOAttribute(CompositeType openType);
104
105     protected abstract T caseListAttribute(ArrayType<?> openType);
106
107     protected abstract T caseListDependeciesAttribute(ArrayType<?> openType);
108
109     private static class UnknownOpenTypeException extends RuntimeException {
110         private static final long serialVersionUID = 1L;
111
112         public UnknownOpenTypeException(String message) {
113             super(message);
114         }
115     }
116 }