Fix checkstyle warnings for config-netconf-connector
[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 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(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 {
42                     return caseJavaAttribute(openType);
43                 }
44             } catch (UnknownOpenTypeException e) {
45                 throw getIllegalArgumentException(attributeIfc);
46             }
47
48         } else if (attributeIfc instanceof DependencyAttribute) {
49             return caseDependencyAttribute(((DependencyAttribute) attributeIfc).getOpenType());
50         } else if (attributeIfc instanceof ListAttribute) {
51             return caseListAttribute((ArrayType<?>) openType);
52         } else if (attributeIfc instanceof ListDependenciesAttribute) {
53             return caseListDependeciesAttribute((ArrayType<?>) openType);
54         } else if (attributeIfc instanceof TOAttribute) {
55             return caseTOAttribute(((TOAttribute) attributeIfc).getOpenType());
56         }
57
58         throw getIllegalArgumentException(attributeIfc);
59     }
60
61     public AttributeIfc getLastAttribute() {
62         return lastAttribute;
63     }
64
65     protected T caseJavaIdentityRefAttribute(OpenType<?> openType) {
66         return caseJavaAttribute(openType);
67     }
68
69     protected T caseJavaUnionAttribute(OpenType<?> openType) {
70         return caseJavaAttribute(openType);
71     }
72
73     protected T caseJavaBinaryAttribute(OpenType<?> openType) {
74         return caseJavaAttribute(openType);
75     }
76
77     private IllegalArgumentException getIllegalArgumentException(AttributeIfc attributeIfc) {
78         return new IllegalArgumentException("Unknown attribute type " + attributeIfc.getClass() + ", " + attributeIfc
79                 + " with open type:" + attributeIfc.getOpenType());
80     }
81
82     public final T caseJavaAttribute(OpenType<?> openType) {
83         if (openType instanceof SimpleType<?>) {
84             return caseJavaSimpleAttribute((SimpleType<?>) openType);
85         } else if (openType instanceof ArrayType<?>) {
86             return caseJavaArrayAttribute((ArrayType<?>) openType);
87         } else if (openType instanceof CompositeType) {
88             return caseJavaCompositeAttribute((CompositeType) openType);
89         }
90
91         throw new UnknownOpenTypeException("Unknown attribute open type " + openType);
92     }
93
94     protected abstract T caseJavaSimpleAttribute(SimpleType<?> openType);
95
96     protected abstract T caseJavaArrayAttribute(ArrayType<?> openType);
97
98     protected abstract T caseJavaCompositeAttribute(CompositeType openType);
99
100     protected abstract T caseDependencyAttribute(SimpleType<?> attributeIfc);
101
102     protected abstract T caseTOAttribute(CompositeType openType);
103
104     protected abstract T caseListAttribute(ArrayType<?> openType);
105
106     protected abstract T caseListDependeciesAttribute(ArrayType<?> openType);
107
108     private static class UnknownOpenTypeException extends RuntimeException {
109         private static final long serialVersionUID = 1L;
110
111         public UnknownOpenTypeException(String message) {
112             super(message);
113         }
114     }
115 }