076edd07a486a850acb5cae9bfd7567ec4eb0621
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-impl / src / main / java / org / opendaylight / controller / sal / binding / yang / types / TypeProviderImpl.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.sal.binding.yang.types;
9
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;
25
26 public class TypeProviderImpl implements TypeProvider {
27
28     private SchemaContextUtil schemaContextUtil;
29
30     public TypeProviderImpl(SchemaContext schemaContext) {
31         schemaContextUtil = new SchemaContextUtil(schemaContext);
32     }
33
34     /*
35      * (non-Javadoc)
36      * 
37      * @see org.opendaylight.controller.yang.model.type.provider.TypeProvider#
38      * javaTypeForYangType(java.lang.String)
39      */
40     @Override
41     public Type javaTypeForYangType(String type) {
42         Type t = BaseYangTypes.BASE_YANG_TYPES_PROVIDER
43                 .javaTypeForYangType(type);
44         return t;
45     }
46
47     @Override
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) {
56
57             } else if (typeDefinition instanceof ExtendedType) {
58                 final TypeDefinition<?> baseType = typeDefinition.getBaseType();
59                 return javaTypeForSchemaDefinitionType(baseType);
60             } else {
61                 returnType = baseTypeForExtendedType(typeDefinition);
62             }
63         }
64         return returnType;
65     }
66
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);
73             } else {
74                 returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER
75                         .javaTypeForSchemaDefinitionType(typeDefinition);
76             }
77         }
78         return returnType;
79     }
80
81     public Type provideTypeForLeafref(final LeafrefTypeDefinition leafrefType) {
82         Type returnType = null;
83         if ((leafrefType != null) && (leafrefType.getPathStatement() != null)
84                 && (leafrefType.getPath() != null)) {
85
86             final RevisionAwareXPath xpath = leafrefType.getPathStatement();
87             final String strXPath = xpath.toString();
88
89             if (strXPath != null) {
90                 if (strXPath.matches(".*//[.* | .*//].*")) {
91                     returnType = Types.typeForClass(Object.class);
92                 } else {
93                     final Module module = schemaContextUtil
94                             .resolveModuleFromSchemaPath(leafrefType.getPath());
95                     if (module != null) {
96                         final DataSchemaNode dataNode;
97                         if (xpath.isAbsolute()) {
98                             dataNode = schemaContextUtil.findDataSchemaNode(
99                                     module, xpath);
100                         } else {
101                             dataNode = schemaContextUtil
102                                     .findDataSchemaNodeForRelativeXPath(module,
103                                             leafrefType, xpath);
104                         }
105                         returnType = resolveTypeFromDataSchemaNode(dataNode);
106                     }
107                 }
108             }
109         }
110         return returnType;
111     }
112
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());
122             }
123         }
124         return returnType;
125     }
126 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.