Merge "Add get-config commit edit-config to testtool"
[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.binding.generator.util.BindingGeneratorUtil;
11 import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider;
12 import org.opendaylight.yangtools.sal.binding.model.api.Type;
13 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
18
19 public class TypeProviderWrapper {
20     private final TypeProvider typeProvider;
21
22     public TypeProviderWrapper(TypeProvider typeProvider) {
23         this.typeProvider = typeProvider;
24     }
25
26     /**
27      * For input node, find if it contains config:java-name-prefix extension. If
28      * not found, convert local name of node converted to cammel case.
29      */
30     public static String findJavaNamePrefix(SchemaNode schemaNode) {
31         return convertToJavaName(schemaNode, true);
32     }
33
34     public static String findJavaParameter(SchemaNode schemaNode) {
35         return convertToJavaName(schemaNode, false);
36     }
37
38     public static String convertToJavaName(SchemaNode schemaNode,
39                                            boolean capitalizeFirstLetter) {
40         for (UnknownSchemaNode unknownNode : schemaNode.getUnknownSchemaNodes()) {
41             if (ConfigConstants.JAVA_NAME_PREFIX_EXTENSION_QNAME
42                     .equals(unknownNode.getNodeType())) {
43                 String value = unknownNode.getNodeParameter();
44                 return convertToJavaName(value, capitalizeFirstLetter);
45             }
46         }
47         return convertToJavaName(schemaNode.getQName().getLocalName(),
48                 capitalizeFirstLetter);
49     }
50
51     public static String convertToJavaName(String localName,
52                                            boolean capitalizeFirstLetter) {
53         if (capitalizeFirstLetter) {
54             return BindingGeneratorUtil.parseToClassName(localName);
55         } else {
56             return BindingGeneratorUtil.parseToValidParamName(localName);
57         }
58     }
59
60     public Type getType(LeafSchemaNode leaf) {
61         TypeDefinition<?> type = leaf.getType();
62         return getType(leaf, type);
63     }
64
65     public String getDefault(LeafSchemaNode node) {
66         return typeProvider.getTypeDefaultConstruction(node);
67     }
68
69     public Type getType(SchemaNode leaf, TypeDefinition<?> type) {
70         Type javaType;
71         try {
72             javaType = typeProvider.javaTypeForSchemaDefinitionType(
73                     type, leaf);
74             if (javaType == null) {
75                 throw new IllegalArgumentException("Unknown type received for "
76                         + leaf.toString());
77             }
78         } catch (IllegalArgumentException e) {
79             throw new IllegalArgumentException("Error while resolving type of "
80                     + leaf, e);
81         }
82         return javaType;
83     }
84
85     // there is no getType in common interface
86     public Type getType(LeafListSchemaNode leaf) {
87         Type javaType;
88         try {
89             javaType = typeProvider.javaTypeForSchemaDefinitionType(
90                     leaf.getType(), leaf);
91             if (javaType == null) {
92                 throw new IllegalArgumentException(
93                         "Unknown type received for  " + leaf.toString());
94             }
95         } catch (IllegalArgumentException e) {
96             throw new IllegalArgumentException("Error while resolving type of "
97                     + leaf, e);
98         }
99         return javaType;
100     }
101
102     public String getJMXParamForBaseType(TypeDefinition<?> baseType) {
103         return typeProvider.getConstructorPropertyName(baseType);
104     }
105
106     public String getJMXParamForUnionInnerType(TypeDefinition<?> unionInnerType) {
107         return typeProvider.getParamNameFromType(unionInnerType);
108     }
109 }