2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.yangjmxgenerator;
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;
19 public class TypeProviderWrapper {
20 private final TypeProvider typeProvider;
22 public TypeProviderWrapper(TypeProvider typeProvider) {
23 this.typeProvider = typeProvider;
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.
30 public static String findJavaNamePrefix(SchemaNode schemaNode) {
31 return convertToJavaName(schemaNode, true);
34 public static String findJavaParameter(SchemaNode schemaNode) {
35 return convertToJavaName(schemaNode, false);
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);
47 return convertToJavaName(schemaNode.getQName().getLocalName(),
48 capitalizeFirstLetter);
51 public static String convertToJavaName(String localName,
52 boolean capitalizeFirstLetter) {
53 if (capitalizeFirstLetter) {
54 return BindingGeneratorUtil.parseToClassName(localName);
56 return BindingGeneratorUtil.parseToValidParamName(localName);
60 public Type getType(LeafSchemaNode leaf) {
61 TypeDefinition<?> type = leaf.getType();
62 return getType(leaf, type);
65 public String getDefault(LeafSchemaNode node) {
66 return typeProvider.getTypeDefaultConstruction(node);
69 public Type getType(SchemaNode leaf, TypeDefinition<?> type) {
72 javaType = typeProvider.javaTypeForSchemaDefinitionType(
74 if (javaType == null) {
75 throw new IllegalArgumentException("Unknown type received for "
78 } catch (IllegalArgumentException e) {
79 throw new IllegalArgumentException("Error while resolving type of "
85 // there is no getType in common interface
86 public Type getType(LeafListSchemaNode leaf) {
89 javaType = typeProvider.javaTypeForSchemaDefinitionType(
90 leaf.getType(), leaf);
91 if (javaType == null) {
92 throw new IllegalArgumentException(
93 "Unknown type received for " + leaf.toString());
95 } catch (IllegalArgumentException e) {
96 throw new IllegalArgumentException("Error while resolving type of "
102 public String getJMXParamForBaseType(TypeDefinition<?> baseType) {
103 return typeProvider.getConstructorPropertyName(baseType);
106 public String getJMXParamForUnionInnerType(TypeDefinition<?> unionInnerType) {
107 return typeProvider.getParamNameFromType(unionInnerType);