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.sal.binding.yang.types;
10 import org.opendaylight.controller.binding.generator.util.Types;
11 import org.opendaylight.controller.sal.binding.generator.spi.TypeProvider;
12 import org.opendaylight.controller.sal.binding.model.api.Type;
13 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
14 import org.opendaylight.controller.yang.model.api.LeafListSchemaNode;
15 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
16 import org.opendaylight.controller.yang.model.api.Module;
17 import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;
18 import org.opendaylight.controller.yang.model.api.SchemaContext;
19 import org.opendaylight.controller.yang.model.api.TypeDefinition;
20 import org.opendaylight.controller.yang.model.api.type.IdentityrefTypeDefinition;
21 import org.opendaylight.controller.yang.model.api.type.LeafrefTypeDefinition;
22 import org.opendaylight.controller.yang.model.util.ExtendedType;
23 import org.opendaylight.controller.yang.model.util.Leafref;
24 import org.opendaylight.controller.yang.model.util.SchemaContextUtil;
26 public class TypeProviderImpl implements TypeProvider {
28 private SchemaContextUtil schemaContextUtil;
30 public TypeProviderImpl(SchemaContext schemaContext) {
31 schemaContextUtil = new SchemaContextUtil(schemaContext);
37 * @see org.opendaylight.controller.yang.model.type.provider.TypeProvider#
38 * javaTypeForYangType(java.lang.String)
41 public Type javaTypeForYangType(String type) {
42 Type t = BaseYangTypes.BASE_YANG_TYPES_PROVIDER
43 .javaTypeForYangType(type);
48 public Type javaTypeForSchemaDefinitionType(
49 final TypeDefinition<?> typeDefinition) {
50 Type returnType = null;
51 if (typeDefinition != null) {
52 if (typeDefinition instanceof Leafref) {
53 final LeafrefTypeDefinition leafref = (LeafrefTypeDefinition) typeDefinition;
54 returnType = provideTypeForLeafref(leafref);
55 } else if (typeDefinition instanceof IdentityrefTypeDefinition) {
57 } else if (typeDefinition instanceof ExtendedType) {
58 final TypeDefinition<?> baseType = typeDefinition.getBaseType();
59 return javaTypeForSchemaDefinitionType(baseType);
61 returnType = baseTypeForExtendedType(typeDefinition);
67 public Type baseTypeForExtendedType(final TypeDefinition<?> typeDefinition) {
68 Type returnType = null;
69 if (typeDefinition != null) {
70 if (typeDefinition instanceof ExtendedType) {
71 final TypeDefinition<?> extType = typeDefinition.getBaseType();
72 return baseTypeForExtendedType(extType);
74 returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER
75 .javaTypeForSchemaDefinitionType(typeDefinition);
81 public Type provideTypeForLeafref(final LeafrefTypeDefinition leafrefType) {
82 Type returnType = null;
83 if ((leafrefType != null) && (leafrefType.getPathStatement() != null)
84 && (leafrefType.getPath() != null)) {
86 final RevisionAwareXPath xpath = leafrefType.getPathStatement();
87 final String strXPath = xpath.toString();
89 if (strXPath != null) {
90 if (strXPath.matches(".*//[.* | .*//].*")) {
91 returnType = Types.typeForClass(Object.class);
93 final Module module = schemaContextUtil
94 .resolveModuleFromSchemaPath(leafrefType.getPath());
96 final DataSchemaNode dataNode;
97 if (xpath.isAbsolute()) {
98 dataNode = schemaContextUtil.findDataSchemaNode(
101 dataNode = schemaContextUtil
102 .findDataSchemaNodeForRelativeXPath(module,
105 returnType = resolveTypeFromDataSchemaNode(dataNode);
113 private Type resolveTypeFromDataSchemaNode(final DataSchemaNode dataNode) {
114 Type returnType = null;
115 if (dataNode != null) {
116 if (dataNode instanceof LeafSchemaNode) {
117 final LeafSchemaNode leaf = (LeafSchemaNode) dataNode;
118 returnType = javaTypeForSchemaDefinitionType(leaf.getType());
119 } else if (dataNode instanceof LeafListSchemaNode) {
120 final LeafListSchemaNode leafList = (LeafListSchemaNode) dataNode;
121 returnType = javaTypeForSchemaDefinitionType(leafList.getType());