Add support for configuration attributes based on yang leaves with custom types ...
[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.TOAttribute;
16
17 import javax.management.openmbean.ArrayType;
18 import javax.management.openmbean.CompositeType;
19 import javax.management.openmbean.OpenType;
20 import javax.management.openmbean.SimpleType;
21
22 public abstract class AttributeIfcSwitchStatement<T> {
23
24     protected AttributeIfc lastAttribute;
25
26     public T switchAttribute(AttributeIfc attributeIfc) {
27
28         this.lastAttribute = attributeIfc;
29
30         if (attributeIfc instanceof JavaAttribute) {
31             try {
32                 return caseJavaAttribute(attributeIfc.getOpenType());
33             } catch (UnknownOpenTypeException e) {
34                 throw getIllegalArgumentException(attributeIfc);
35             }
36
37         } else if (attributeIfc instanceof DependencyAttribute) {
38             return caseDependencyAttribute(((DependencyAttribute) attributeIfc).getOpenType());
39         } else if (attributeIfc instanceof ListAttribute) {
40             return caseListAttribute(((ListAttribute) attributeIfc).getOpenType());
41         } else if (attributeIfc instanceof TOAttribute) {
42             return caseTOAttribute(((TOAttribute) attributeIfc).getOpenType());
43         }
44
45         throw getIllegalArgumentException(attributeIfc);
46     }
47
48     private IllegalArgumentException getIllegalArgumentException(AttributeIfc attributeIfc) {
49         return new IllegalArgumentException("Unknown attribute type " + attributeIfc.getClass() + ", " + attributeIfc
50                 + " with open type:" + attributeIfc.getOpenType());
51     }
52
53     public final T caseJavaAttribute(OpenType<?> openType) {
54         if (openType instanceof SimpleType<?>) {
55             return caseJavaSimpleAttribute((SimpleType<?>) openType);
56         } else if (openType instanceof ArrayType<?>) {
57             return caseJavaArrayAttribute((ArrayType<?>) openType);
58         } else if (openType instanceof CompositeType) {
59             return caseJavaCompositeAttribute((CompositeType) openType);
60         }
61
62         throw new UnknownOpenTypeException("Unknown attribute open type " + openType);
63     }
64
65     protected abstract T caseJavaSimpleAttribute(SimpleType<?> openType);
66
67     protected abstract T caseJavaArrayAttribute(ArrayType<?> openType);
68
69     protected abstract T caseJavaCompositeAttribute(CompositeType openType);
70
71     protected abstract T caseDependencyAttribute(SimpleType<?> attributeIfc);
72
73     protected abstract T caseTOAttribute(CompositeType openType);
74
75     protected abstract T caseListAttribute(ArrayType<?> openType);
76
77
78     private static class UnknownOpenTypeException extends RuntimeException {
79         public UnknownOpenTypeException(String message) {
80             super(message);
81         }
82     }
83 }