Merge "Fixed deserialization of IdentityRefs in Restconf URI."
[controller.git] / opendaylight / config / yang-jmx-generator / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / TypeProviderWrapper.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 package org.opendaylight.controller.config.yangjmxgenerator;
9
10 import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider;
11 import org.opendaylight.yangtools.sal.binding.model.api.Type;
12 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
13 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
16
17 public class TypeProviderWrapper {
18     private final TypeProvider typeProvider;
19
20     public TypeProviderWrapper(TypeProvider typeProvider) {
21         this.typeProvider = typeProvider;
22     }
23
24     public Type getType(LeafSchemaNode leaf) {
25         TypeDefinition<?> type = leaf.getType();
26         return getType(leaf, type);
27     }
28
29     public String getDefault(LeafSchemaNode node) {
30         return typeProvider.getTypeDefaultConstruction(node);
31     }
32
33     public Type getType(SchemaNode leaf, TypeDefinition<?> type) {
34         Type javaType;
35         try {
36             javaType = typeProvider.javaTypeForSchemaDefinitionType(
37                     type, leaf);
38             if (javaType == null)
39                 throw new IllegalArgumentException("Unknown type received for "
40                         + leaf.toString());
41         } catch (IllegalArgumentException e) {
42             throw new IllegalArgumentException("Error while resolving type of "
43                     + leaf, e);
44         }
45         return javaType;
46     }
47
48     // there is no getType in common interface
49     public Type getType(LeafListSchemaNode leaf) {
50         Type javaType;
51         try {
52             javaType = typeProvider.javaTypeForSchemaDefinitionType(
53                     leaf.getType(), leaf);
54             if (javaType == null)
55                 throw new IllegalArgumentException(
56                         "Unknown type received for  " + leaf.toString());
57         } catch (IllegalArgumentException e) {
58             throw new IllegalArgumentException("Error while resolving type of "
59                     + leaf, e);
60         }
61         return javaType;
62     }
63
64     public String getJMXParamForBaseType(TypeDefinition<?> baseType) {
65         return typeProvider.getConstructorPropertyName(baseType);
66     }
67 }