Reduce verbosity/criticality of inconsistent yangstore messages
[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 org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
12 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
13 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
14 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
15 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
16 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
17 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
18
19 import javax.management.openmbean.ArrayType;
20 import javax.management.openmbean.CompositeType;
21 import javax.management.openmbean.OpenType;
22 import javax.management.openmbean.SimpleType;
23
24 public abstract class AttributeIfcSwitchStatement<T> {
25
26     protected 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
41                     return caseJavaAttribute(openType);
42             } catch (UnknownOpenTypeException e) {
43                 throw getIllegalArgumentException(attributeIfc);
44             }
45
46         } else if (attributeIfc instanceof DependencyAttribute) {
47             return caseDependencyAttribute(((DependencyAttribute) attributeIfc).getOpenType());
48         } else if (attributeIfc instanceof ListAttribute) {
49             return caseListAttribute((ArrayType<?>) openType);
50         } else if (attributeIfc instanceof ListDependenciesAttribute) {
51             return caseListDependeciesAttribute((ArrayType<?>) openType);
52         } else if (attributeIfc instanceof TOAttribute) {
53             return caseTOAttribute(((TOAttribute) attributeIfc).getOpenType());
54         }
55
56         throw getIllegalArgumentException(attributeIfc);
57     }
58
59     protected T caseJavaUnionAttribute(OpenType<?> openType) {
60         return caseJavaAttribute(openType);
61     }
62
63     protected T caseJavaBinaryAttribute(OpenType<?> openType) {
64         return caseJavaAttribute(openType);
65     }
66
67     private IllegalArgumentException getIllegalArgumentException(AttributeIfc attributeIfc) {
68         return new IllegalArgumentException("Unknown attribute type " + attributeIfc.getClass() + ", " + attributeIfc
69                 + " with open type:" + attributeIfc.getOpenType());
70     }
71
72     public final T caseJavaAttribute(OpenType<?> openType) {
73         if (openType instanceof SimpleType<?>) {
74             return caseJavaSimpleAttribute((SimpleType<?>) openType);
75         } else if (openType instanceof ArrayType<?>) {
76             return caseJavaArrayAttribute((ArrayType<?>) openType);
77         } else if (openType instanceof CompositeType) {
78             return caseJavaCompositeAttribute((CompositeType) openType);
79         }
80
81         throw new UnknownOpenTypeException("Unknown attribute open type " + openType);
82     }
83
84     protected abstract T caseJavaSimpleAttribute(SimpleType<?> openType);
85
86     protected abstract T caseJavaArrayAttribute(ArrayType<?> openType);
87
88     protected abstract T caseJavaCompositeAttribute(CompositeType openType);
89
90     protected abstract T caseDependencyAttribute(SimpleType<?> attributeIfc);
91
92     protected abstract T caseTOAttribute(CompositeType openType);
93
94     protected abstract T caseListAttribute(ArrayType<?> openType);
95
96     protected abstract T caseListDependeciesAttribute(ArrayType<?> openType);
97
98     private static class UnknownOpenTypeException extends RuntimeException {
99         public UnknownOpenTypeException(String message) {
100             super(message);
101         }
102     }
103 }