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.yangtools.sal.binding.generator.impl;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkNotNull;
12 import static com.google.common.base.Preconditions.checkState;
13 import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.computeDefaultSUID;
14 import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.moduleNamespaceToPackageName;
15 import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.packageNameForGeneratedType;
16 import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.parseToValidParamName;
17 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.DATA_OBJECT;
18 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.DATA_ROOT;
19 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.IDENTIFIABLE;
20 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.IDENTIFIER;
21 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.NOTIFICATION;
22 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.augmentable;
23 import static org.opendaylight.yangtools.binding.generator.util.Types.BOOLEAN;
24 import static org.opendaylight.yangtools.binding.generator.util.Types.FUTURE;
25 import static org.opendaylight.yangtools.binding.generator.util.Types.VOID;
26 import static org.opendaylight.yangtools.binding.generator.util.Types.typeForClass;
27 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode;
28 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findNodeInSchemaContext;
29 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findOriginal;
30 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.List;
41 import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil;
42 import org.opendaylight.yangtools.binding.generator.util.BindingTypes;
43 import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl;
44 import org.opendaylight.yangtools.binding.generator.util.Types;
45 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedPropertyBuilderImpl;
46 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
47 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
48 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
49 import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider;
50 import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
51 import org.opendaylight.yangtools.sal.binding.model.api.Constant;
52 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
53 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
54 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
55 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
56 import org.opendaylight.yangtools.sal.binding.model.api.Type;
57 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
58 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.EnumBuilder;
59 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;
60 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;
61 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
62 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
63 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.MethodSignatureBuilder;
64 import org.opendaylight.yangtools.sal.binding.yang.types.GroupingDefinitionDependencySort;
65 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
66 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
67 import org.opendaylight.yangtools.yang.binding.BindingMapping;
68 import org.opendaylight.yangtools.yang.binding.DataContainer;
69 import org.opendaylight.yangtools.yang.binding.RpcService;
70 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
71 import org.opendaylight.yangtools.yang.common.QName;
72 import org.opendaylight.yangtools.yang.common.RpcResult;
73 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
74 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
75 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
76 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
77 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
78 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
79 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
80 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
81 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
82 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
83 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
84 import org.opendaylight.yangtools.yang.model.api.Module;
85 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
86 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
87 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
88 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
89 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
90 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
91 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
92 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
93 import org.opendaylight.yangtools.yang.model.api.UsesNode;
94 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
95 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
96 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
97 import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
98 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
99 import org.opendaylight.yangtools.yang.model.util.UnionType;
100 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
101 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
102 import org.slf4j.Logger;
103 import org.slf4j.LoggerFactory;
105 import com.google.common.base.Splitter;
106 import com.google.common.collect.Iterables;
107 import com.google.common.collect.Sets;
109 public class BindingGeneratorImpl implements BindingGenerator {
110 private static final Logger LOG = LoggerFactory.getLogger(BindingGeneratorImpl.class);
112 private final Map<Module, ModuleContext> genCtx = new HashMap<>();
115 * Outer key represents the package name. Outer value represents map of all
116 * builders in the same package. Inner key represents the schema node name
117 * (in JAVA class/interface name format). Inner value represents instance of
118 * builder for schema node specified in key part.
120 private Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders;
123 * Provide methods for converting YANG types to JAVA types.
125 private TypeProvider typeProvider;
128 * Holds reference to schema context to resolve data of augmented element
129 * when creating augmentation builder
131 private SchemaContext schemaContext;
134 * Constant with the concrete name of namespace.
136 private final static String YANG_EXT_NAMESPACE = "urn:opendaylight:yang:extension:yang-ext";
139 * Constant with the concrete name of identifier.
141 private final static String AUGMENT_IDENTIFIER_NAME = "augment-identifier";
144 * Resolves generated types from <code>context</code> schema nodes of all
147 * Generated types are created for modules, groupings, types, containers,
148 * lists, choices, augments, rpcs, notification, identities.
151 * schema context which contains data about all schema nodes
153 * @return list of types (usually <code>GeneratedType</code>
154 * <code>GeneratedTransferObject</code>which are generated from
155 * <code>context</code> data.
156 * @throws IllegalArgumentException
157 * if arg <code>context</code> is null
158 * @throws IllegalStateException
159 * if <code>context</code> contain no modules
162 public List<Type> generateTypes(final SchemaContext context) {
163 checkArgument(context != null, "Schema Context reference cannot be NULL.");
164 checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
165 schemaContext = context;
166 typeProvider = new TypeProviderImpl(context);
167 final Set<Module> modules = context.getModules();
168 return generateTypes(context, modules);
172 * Resolves generated types from <code>context</code> schema nodes only for
173 * modules specified in <code>modules</code>
175 * Generated types are created for modules, groupings, types, containers,
176 * lists, choices, augments, rpcs, notification, identities.
179 * schema context which contains data about all schema nodes
182 * set of modules for which schema nodes should be generated
184 * @return list of types (usually <code>GeneratedType</code> or
185 * <code>GeneratedTransferObject</code>) which:
187 * <li>are generated from <code>context</code> schema nodes and</li>
188 * <li>are also part of some of the module in <code>modules</code>
191 * @throws IllegalArgumentException
193 * <li>if arg <code>context</code> is null or</li>
194 * <li>if arg <code>modules</code> is null</li>
196 * @throws IllegalStateException
197 * if <code>context</code> contain no modules
200 public List<Type> generateTypes(final SchemaContext context, final Set<Module> modules) {
201 checkArgument(context != null, "Schema Context reference cannot be NULL.");
202 checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
203 checkArgument(modules != null, "Set of Modules cannot be NULL.");
205 schemaContext = context;
206 typeProvider = new TypeProviderImpl(context);
207 Module[] modulesArray = new Module[context.getModules().size()];
208 context.getModules().toArray(modulesArray);
209 final List<Module> contextModules = ModuleDependencySort.sort(modulesArray);
210 genTypeBuilders = new HashMap<>();
212 for (Module contextModule : contextModules) {
213 moduleToGenTypes(contextModule, context);
215 for (Module contextModule : contextModules) {
216 allAugmentsToGenTypes(contextModule);
219 final List<Type> filteredGenTypes = new ArrayList<>();
220 for (Module m : modules) {
221 final ModuleContext ctx = checkNotNull(genCtx.get(m), "Module context not found for module %s", m);
222 filteredGenTypes.addAll(ctx.getGeneratedTypes());
223 final Set<Type> additionalTypes = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(m);
224 if (additionalTypes != null) {
225 filteredGenTypes.addAll(additionalTypes);
229 return filteredGenTypes;
232 private void moduleToGenTypes(final Module m, final SchemaContext context) {
233 genCtx.put(m, new ModuleContext());
234 allTypeDefinitionsToGenTypes(m);
235 groupingsToGenTypes(m, m.getGroupings());
236 rpcMethodsToGenType(m);
237 allIdentitiesToGenTypes(m, context);
238 notificationsToGenType(m);
240 if (!m.getChildNodes().isEmpty()) {
241 final GeneratedTypeBuilder moduleType = moduleToDataType(m);
242 genCtx.get(m).addModuleNode(moduleType);
243 final String basePackageName = moduleNamespaceToPackageName(m);
244 resolveDataSchemaNodes(m, basePackageName, moduleType, moduleType, m.getChildNodes());
249 * Converts all extended type definitions of module to the list of
250 * <code>Type</code> objects.
253 * module from which is obtained set of type definitions
254 * @throws IllegalArgumentException
256 * <li>if module is null</li>
257 * <li>if name of module is null</li>
259 * @throws IllegalStateException
260 * if set of type definitions from module is null
262 private void allTypeDefinitionsToGenTypes(final Module module) {
263 checkArgument(module != null, "Module reference cannot be NULL.");
264 checkArgument(module.getName() != null, "Module name cannot be NULL.");
265 final DataNodeIterator it = new DataNodeIterator(module);
266 final List<TypeDefinition<?>> typeDefinitions = it.allTypedefs();
267 checkState(typeDefinitions != null, "Type Definitions for module «module.name» cannot be NULL.");
269 for (TypeDefinition<?> typedef : typeDefinitions) {
270 if (typedef != null) {
271 final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef,
274 genCtx.get(module).addTypedefType(typedef.getPath(), type);
280 private GeneratedTypeBuilder processDataSchemaNode(final Module module, final String basePackageName,
281 final GeneratedTypeBuilder childOf, final DataSchemaNode node) {
282 if (node.isAugmenting() || node.isAddedByUses()) {
285 final String packageName = packageNameForGeneratedType(basePackageName, node.getPath());
286 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, node, childOf);
287 genType.addComment(node.getDescription());
288 if (node instanceof DataNodeContainer) {
289 genCtx.get(module).addChildNodeType(node.getPath(), genType);
290 groupingsToGenTypes(module, ((DataNodeContainer) node).getGroupings());
291 processUsesAugments((DataNodeContainer) node, module);
296 private void containerToGenType(final Module module, final String basePackageName, final GeneratedTypeBuilder parent,
297 final GeneratedTypeBuilder childOf, final ContainerSchemaNode node) {
298 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node);
299 if (genType != null) {
300 constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), genType);
301 resolveDataSchemaNodes(module, basePackageName, genType, genType, node.getChildNodes());
305 private void listToGenType(final Module module, final String basePackageName, final GeneratedTypeBuilder parent,
306 final GeneratedTypeBuilder childOf, final ListSchemaNode node) {
307 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node);
308 if (genType != null) {
309 constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), Types.listTypeFor(genType));
311 final List<String> listKeys = listKeys(node);
312 final String packageName = packageNameForGeneratedType(basePackageName, node.getPath());
313 final GeneratedTOBuilder genTOBuilder = resolveListKeyTOBuilder(packageName, node);
314 if (genTOBuilder != null) {
315 final Type identifierMarker = Types.parameterizedTypeFor(IDENTIFIER, genType);
316 final Type identifiableMarker = Types.parameterizedTypeFor(IDENTIFIABLE, genTOBuilder);
317 genTOBuilder.addImplementsType(identifierMarker);
318 genType.addImplementsType(identifiableMarker);
321 for (DataSchemaNode schemaNode : node.getChildNodes()) {
322 if (!schemaNode.isAugmenting()) {
323 addSchemaNodeToListBuilders(basePackageName, schemaNode, genType, genTOBuilder, listKeys, module);
328 if (genTOBuilder != null) {
329 final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
330 prop.setValue(Long.toString(computeDefaultSUID(genTOBuilder)));
331 genTOBuilder.setSUID(prop);
334 typeBuildersToGenTypes(module, genType, genTOBuilder);
338 private void processUsesAugments(final DataNodeContainer node, final Module module) {
339 final String basePackageName = moduleNamespaceToPackageName(module);
340 for (UsesNode usesNode : node.getUses()) {
341 for (AugmentationSchema augment : usesNode.getAugmentations()) {
342 usesAugmentationToGenTypes(basePackageName, augment, module, usesNode, node);
343 processUsesAugments(augment, module);
349 * Converts all <b>augmentation</b> of the module to the list
350 * <code>Type</code> objects.
353 * module from which is obtained list of all augmentation objects
354 * to iterate over them
355 * @throws IllegalArgumentException
357 * <li>if the module is null</li>
358 * <li>if the name of module is null</li>
360 * @throws IllegalStateException
361 * if set of augmentations from module is null
363 private void allAugmentsToGenTypes(final Module module) {
364 checkArgument(module != null, "Module reference cannot be NULL.");
365 checkArgument(module.getName() != null, "Module name cannot be NULL.");
366 checkState(module.getAugmentations() != null, "Augmentations Set cannot be NULL.");
368 final String basePackageName = moduleNamespaceToPackageName(module);
369 final List<AugmentationSchema> augmentations = resolveAugmentations(module);
370 for (AugmentationSchema augment : augmentations) {
371 augmentationToGenTypes(basePackageName, augment, module);
376 * Returns list of <code>AugmentationSchema</code> objects. The objects are
377 * sorted according to the length of their target path from the shortest to
381 * module from which is obtained list of all augmentation objects
382 * @return list of sorted <code>AugmentationSchema</code> objects obtained
383 * from <code>module</code>
384 * @throws IllegalArgumentException
386 * @throws IllegalStateException
387 * if set of module augmentations is null
389 private List<AugmentationSchema> resolveAugmentations(final Module module) {
390 checkArgument(module != null, "Module reference cannot be NULL.");
391 checkState(module.getAugmentations() != null, "Augmentations Set cannot be NULL.");
393 final Set<AugmentationSchema> augmentations = module.getAugmentations();
394 List<AugmentationSchema> sortedAugmentations = new ArrayList<>(augmentations);
395 Collections.sort(sortedAugmentations, Comparators.AUGMENT_COMP);
397 return sortedAugmentations;
401 * Create GeneratedTypeBuilder object from module argument.
404 * Module object from which builder will be created
405 * @return <code>GeneratedTypeBuilder</code> which is internal
406 * representation of the module
407 * @throws IllegalArgumentException
410 private GeneratedTypeBuilder moduleToDataType(final Module module) {
411 checkArgument(module != null, "Module reference cannot be NULL.");
413 final GeneratedTypeBuilder moduleDataTypeBuilder = moduleTypeBuilder(module, "Data");
414 addImplementedInterfaceFromUses(module, moduleDataTypeBuilder);
415 moduleDataTypeBuilder.addImplementsType(DATA_ROOT);
416 moduleDataTypeBuilder.addComment(module.getDescription());
417 return moduleDataTypeBuilder;
421 * Converts all <b>rpcs</b> inputs and outputs substatements of the module
422 * to the list of <code>Type</code> objects. In addition are to containers
423 * and lists which belong to input or output also part of returning list.
426 * module from which is obtained set of all rpc objects to
428 * @throws IllegalArgumentException
430 * <li>if the module is null</li>
431 * <li>if the name of module is null</li>
433 * @throws IllegalStateException
434 * if set of rpcs from module is null
436 private void rpcMethodsToGenType(final Module module) {
437 checkArgument(module != null, "Module reference cannot be NULL.");
438 checkArgument(module.getName() != null, "Module name cannot be NULL.");
439 final Set<RpcDefinition> rpcDefinitions = module.getRpcs();
440 checkState(rpcDefinitions != null, "Set of rpcs from module " + module.getName() + " cannot be NULL.");
441 if (rpcDefinitions.isEmpty()) {
445 final String basePackageName = moduleNamespaceToPackageName(module);
446 final GeneratedTypeBuilder interfaceBuilder = moduleTypeBuilder(module, "Service");
447 interfaceBuilder.addImplementsType(Types.typeForClass(RpcService.class));
448 for (RpcDefinition rpc : rpcDefinitions) {
450 final String rpcName = BindingMapping.getClassName(rpc.getQName());
451 final String rpcMethodName = parseToValidParamName(rpcName);
452 final String rpcComment = rpc.getDescription();
453 final MethodSignatureBuilder method = interfaceBuilder.addMethod(rpcMethodName);
454 final ContainerSchemaNode input = rpc.getInput();
455 final ContainerSchemaNode output = rpc.getOutput();
458 final GeneratedTypeBuilder inType = addRawInterfaceDefinition(basePackageName, input, rpcName);
459 addImplementedInterfaceFromUses(input, inType);
460 inType.addImplementsType(DATA_OBJECT);
461 inType.addImplementsType(augmentable(inType));
462 resolveDataSchemaNodes(module, basePackageName, inType, inType, input.getChildNodes());
463 genCtx.get(module).addChildNodeType(input.getPath(), inType);
464 final GeneratedType inTypeInstance = inType.toInstance();
465 method.addParameter(inTypeInstance, "input");
468 Type outTypeInstance = VOID;
469 if (output != null) {
470 final GeneratedTypeBuilder outType = addRawInterfaceDefinition(basePackageName, output, rpcName);
471 addImplementedInterfaceFromUses(output, outType);
472 outType.addImplementsType(DATA_OBJECT);
473 outType.addImplementsType(augmentable(outType));
474 resolveDataSchemaNodes(module, basePackageName, outType, outType, output.getChildNodes());
475 genCtx.get(module).addChildNodeType(output.getPath(), outType);
476 outTypeInstance = outType.toInstance();
479 final Type rpcRes = Types.parameterizedTypeFor(Types.typeForClass(RpcResult.class), outTypeInstance);
480 method.setComment(rpcComment);
481 method.setReturnType(Types.parameterizedTypeFor(FUTURE, rpcRes));
485 genCtx.get(module).addTopLevelNodeType(interfaceBuilder);
489 * Converts all <b>notifications</b> of the module to the list of
490 * <code>Type</code> objects. In addition are to this list added containers
491 * and lists which are part of this notification.
494 * module from which is obtained set of all notification objects
495 * to iterate over them
496 * @throws IllegalArgumentException
498 * <li>if the module equals null</li>
499 * <li>if the name of module equals null</li>
501 * @throws IllegalStateException
502 * if set of notifications from module is null
504 private void notificationsToGenType(final Module module) {
505 checkArgument(module != null, "Module reference cannot be NULL.");
506 checkArgument(module.getName() != null, "Module name cannot be NULL.");
507 final Set<NotificationDefinition> notifications = module.getNotifications();
508 checkState(notifications != null, "Set of notification from module " + module.getName() + " cannot be NULL.");
509 if (notifications.isEmpty()) {
513 final GeneratedTypeBuilder listenerInterface = moduleTypeBuilder(module, "Listener");
514 listenerInterface.addImplementsType(BindingTypes.NOTIFICATION_LISTENER);
515 final String basePackageName = moduleNamespaceToPackageName(module);
517 for (NotificationDefinition notification : notifications) {
518 if (notification != null) {
519 processUsesAugments(notification, module);
521 final GeneratedTypeBuilder notificationInterface = addDefaultInterfaceDefinition(basePackageName,
522 notification, BindingTypes.DATA_OBJECT);
523 notificationInterface.addImplementsType(NOTIFICATION);
524 genCtx.get(module).addChildNodeType(notification.getPath(), notificationInterface);
526 // Notification object
527 resolveDataSchemaNodes(module, basePackageName, notificationInterface, notificationInterface,
528 notification.getChildNodes());
530 listenerInterface.addMethod("on" + notificationInterface.getName())
531 .setAccessModifier(AccessModifier.PUBLIC).addParameter(notificationInterface, "notification")
532 .setComment(notification.getDescription()).setReturnType(Types.VOID);
536 genCtx.get(module).addTopLevelNodeType(listenerInterface);
540 * Converts all <b>identities</b> of the module to the list of
541 * <code>Type</code> objects.
544 * module from which is obtained set of all identity objects to
547 * schema context only used as input parameter for method
548 * {@link identityToGenType}
551 private void allIdentitiesToGenTypes(final Module module, final SchemaContext context) {
552 final Set<IdentitySchemaNode> schemaIdentities = module.getIdentities();
553 final String basePackageName = moduleNamespaceToPackageName(module);
555 if (schemaIdentities != null && !schemaIdentities.isEmpty()) {
556 for (IdentitySchemaNode identity : schemaIdentities) {
557 identityToGenType(module, basePackageName, identity, context);
563 * Converts the <b>identity</b> object to GeneratedType. Firstly it is
564 * created transport object builder. If identity contains base identity then
565 * reference to base identity is added to superior identity as its extend.
566 * If identity doesn't contain base identity then only reference to abstract
567 * class {@link org.opendaylight.yangtools.yang.model.api.BaseIdentity
568 * BaseIdentity} is added
572 * @param basePackageName
573 * string contains the module package name
575 * IdentitySchemaNode which contains data about identity
577 * SchemaContext which is used to get package and name
578 * information about base of identity
581 private void identityToGenType(final Module module, final String basePackageName, final IdentitySchemaNode identity,
582 final SchemaContext context) {
583 if (identity == null) {
586 final String packageName = packageNameForGeneratedType(basePackageName, identity.getPath());
587 final String genTypeName = BindingMapping.getClassName(identity.getQName());
588 final GeneratedTOBuilder newType = new GeneratedTOBuilderImpl(packageName, genTypeName);
589 final IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
590 if (baseIdentity == null) {
591 final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(BaseIdentity.class.getPackage().getName(),
592 BaseIdentity.class.getSimpleName());
593 newType.setExtendsType(gto.toInstance());
595 final Module baseIdentityParentModule = SchemaContextUtil.findParentModule(context, baseIdentity);
596 final String returnTypePkgName = moduleNamespaceToPackageName(baseIdentityParentModule);
597 final String returnTypeName = BindingMapping.getClassName(baseIdentity.getQName());
598 final GeneratedTransferObject gto = new GeneratedTOBuilderImpl(returnTypePkgName, returnTypeName)
600 newType.setExtendsType(gto);
602 newType.setAbstract(true);
603 newType.addComment(identity.getDescription());
604 final QName qname = identity.getQName();
605 qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, qname);
607 genCtx.get(module).addIdentityType(identity.getQName(), newType);
610 private static Constant qnameConstant(final GeneratedTypeBuilderBase<?> toBuilder, final String constantName, final QName name) {
611 StringBuilder sb = new StringBuilder("org.opendaylight.yangtools.yang.common.QName");
612 sb.append(".create(");
614 sb.append(name.getNamespace());
616 sb.append(name.getFormattedRevision());
618 sb.append(name.getLocalName());
621 return toBuilder.addConstant(typeForClass(QName.class), constantName, sb.toString());
625 * Converts all <b>groupings</b> of the module to the list of
626 * <code>Type</code> objects. Firstly are groupings sorted according mutual
627 * dependencies. At least dependent (independent) groupings are in the list
628 * saved at first positions. For every grouping the record is added to map
629 * {@link BindingGeneratorImpl#allGroupings allGroupings}
634 * of groupings from which types will be generated
637 private void groupingsToGenTypes(final Module module, final Collection<GroupingDefinition> groupings) {
638 final String basePackageName = moduleNamespaceToPackageName(module);
639 final List<GroupingDefinition> groupingsSortedByDependencies = new GroupingDefinitionDependencySort()
641 for (GroupingDefinition grouping : groupingsSortedByDependencies) {
642 groupingToGenType(basePackageName, grouping, module);
647 * Converts individual grouping to GeneratedType. Firstly generated type
648 * builder is created and every child node of grouping is resolved to the
651 * @param basePackageName
652 * string contains the module package name
654 * GroupingDefinition which contains data about grouping
657 * @return GeneratedType which is generated from grouping (object of type
658 * <code>GroupingDefinition</code>)
660 private void groupingToGenType(final String basePackageName, final GroupingDefinition grouping, final Module module) {
661 final String packageName = packageNameForGeneratedType(basePackageName, grouping.getPath());
662 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, grouping);
663 genCtx.get(module).addGroupingType(grouping.getPath(), genType);
664 resolveDataSchemaNodes(module, basePackageName, genType, genType, grouping.getChildNodes());
665 groupingsToGenTypes(module, grouping.getGroupings());
666 processUsesAugments(grouping, module);
670 * Adds enumeration builder created from <code>enumTypeDef</code> to
671 * <code>typeBuilder</code>.
673 * Each <code>enumTypeDef</code> item is added to builder with its name and
677 * EnumTypeDefinition contains enum data
679 * string contains name which will be assigned to enumeration
682 * GeneratedTypeBuilder to which will be enum builder assigned
683 * @return enumeration builder which contains data from
684 * <code>enumTypeDef</code>
686 private EnumBuilder resolveInnerEnumFromTypeDefinition(final EnumTypeDefinition enumTypeDef, final QName enumName,
687 final GeneratedTypeBuilder typeBuilder) {
688 if ((enumTypeDef != null) && (typeBuilder != null) && (enumTypeDef.getQName() != null)
689 && (enumTypeDef.getQName().getLocalName() != null)) {
690 final String enumerationName = BindingMapping.getClassName(enumName);
691 final EnumBuilder enumBuilder = typeBuilder.addEnumeration(enumerationName);
692 enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
699 * Generates type builder for <code>module</code>.
702 * Module which is source of package name for generated type
705 * string which is added to the module class name representation
707 * @return instance of GeneratedTypeBuilder which represents
708 * <code>module</code>.
709 * @throws IllegalArgumentException
710 * if <code>module</code> is null
712 private GeneratedTypeBuilder moduleTypeBuilder(final Module module, final String postfix) {
713 checkArgument(module != null, "Module reference cannot be NULL.");
714 final String packageName = moduleNamespaceToPackageName(module);
715 final String moduleName = BindingMapping.getClassName(module.getName()) + postfix;
716 return new GeneratedTypeBuilderImpl(packageName, moduleName);
720 * Converts <code>augSchema</code> to list of <code>Type</code> which
721 * contains generated type for augmentation. In addition there are also
722 * generated types for all containers, list and choices which are child of
723 * <code>augSchema</code> node or a generated types for cases are added if
724 * augmented node is choice.
726 * @param augmentPackageName
727 * string with the name of the package to which the augmentation
730 * AugmentationSchema which is contains data about augmentation
731 * (target path, childs...)
734 * @param parentUsesNode
735 * @throws IllegalArgumentException
737 * <li>if <code>augmentPackageName</code> equals null</li>
738 * <li>if <code>augSchema</code> equals null</li>
740 * @throws IllegalStateException
741 * if augment target path is null
743 private void augmentationToGenTypes(final String augmentPackageName, final AugmentationSchema augSchema, final Module module) {
744 checkArgument(augmentPackageName != null, "Package Name cannot be NULL.");
745 checkArgument(augSchema != null, "Augmentation Schema cannot be NULL.");
746 checkState(augSchema.getTargetPath() != null,
747 "Augmentation Schema does not contain Target Path (Target Path is NULL).");
749 processUsesAugments(augSchema, module);
750 final SchemaPath targetPath = augSchema.getTargetPath();
751 SchemaNode targetSchemaNode = null;
753 targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
754 if (targetSchemaNode instanceof DataSchemaNode && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
755 targetSchemaNode = findOriginal((DataSchemaNode) targetSchemaNode, schemaContext);
756 if (targetSchemaNode == null) {
757 throw new NullPointerException("Failed to find target node from grouping in augmentation " + augSchema
758 + " in module " + module.getName());
761 if (targetSchemaNode == null) {
762 throw new IllegalArgumentException("augment target not found: " + targetPath);
765 GeneratedTypeBuilder targetTypeBuilder = findChildNodeByPath(targetSchemaNode.getPath());
766 if (targetTypeBuilder == null) {
767 targetTypeBuilder = findCaseByPath(targetSchemaNode.getPath());
769 if (targetTypeBuilder == null) {
770 throw new NullPointerException("Target type not yet generated: " + targetSchemaNode);
773 if (!(targetSchemaNode instanceof ChoiceNode)) {
774 String packageName = augmentPackageName;
775 final Type targetType = new ReferencedTypeImpl(targetTypeBuilder.getPackageName(),
776 targetTypeBuilder.getName());
777 addRawAugmentGenTypeDefinition(module, packageName, augmentPackageName, targetType, augSchema);
780 generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance(),
781 (ChoiceNode) targetSchemaNode, augSchema.getChildNodes());
785 private void usesAugmentationToGenTypes(final String augmentPackageName, final AugmentationSchema augSchema, final Module module,
786 final UsesNode usesNode, final DataNodeContainer usesNodeParent) {
787 checkArgument(augmentPackageName != null, "Package Name cannot be NULL.");
788 checkArgument(augSchema != null, "Augmentation Schema cannot be NULL.");
789 checkState(augSchema.getTargetPath() != null,
790 "Augmentation Schema does not contain Target Path (Target Path is NULL).");
792 processUsesAugments(augSchema, module);
793 final SchemaPath targetPath = augSchema.getTargetPath();
794 SchemaNode targetSchemaNode = findOriginalTargetFromGrouping(targetPath, usesNode);
795 if (targetSchemaNode == null) {
796 throw new IllegalArgumentException("augment target not found: " + targetPath);
799 GeneratedTypeBuilder targetTypeBuilder = findChildNodeByPath(targetSchemaNode.getPath());
800 if (targetTypeBuilder == null) {
801 targetTypeBuilder = findCaseByPath(targetSchemaNode.getPath());
803 if (targetTypeBuilder == null) {
804 throw new NullPointerException("Target type not yet generated: " + targetSchemaNode);
807 if (!(targetSchemaNode instanceof ChoiceNode)) {
808 String packageName = augmentPackageName;
809 if (usesNodeParent instanceof SchemaNode) {
810 packageName = packageNameForGeneratedType(augmentPackageName, ((SchemaNode) usesNodeParent).getPath(),
813 addRawAugmentGenTypeDefinition(module, packageName, augmentPackageName, targetTypeBuilder.toInstance(),
816 generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance(),
817 (ChoiceNode) targetSchemaNode, augSchema.getChildNodes());
822 * Convenient method to find node added by uses statement.
826 * @param parentUsesNode
827 * parent of uses node
828 * @return node from its original location in grouping
830 private DataSchemaNode findOriginalTargetFromGrouping(final SchemaPath targetPath, final UsesNode parentUsesNode) {
831 SchemaNode targetGrouping = findNodeInSchemaContext(schemaContext, parentUsesNode.getGroupingPath()
833 if (!(targetGrouping instanceof GroupingDefinition)) {
834 throw new IllegalArgumentException("Failed to generate code for augment in " + parentUsesNode);
837 GroupingDefinition grouping = (GroupingDefinition) targetGrouping;
838 SchemaNode result = grouping;
839 for (QName node : targetPath.getPathFromRoot()) {
840 // finding by local name is valid, grouping cannot contain nodes
841 // with same name and different namespace
842 if (result instanceof DataNodeContainer) {
843 result = ((DataNodeContainer) result).getDataChildByName(node.getLocalName());
844 } else if (result instanceof ChoiceNode) {
845 result = ((ChoiceNode) result).getCaseNodeByName(node.getLocalName());
848 if (result == null) {
852 boolean fromUses = ((DataSchemaNode) result).isAddedByUses();
853 Iterator<UsesNode> groupingUses = grouping.getUses().iterator();
854 while (groupingUses.hasNext() && fromUses) {
855 result = findOriginalTargetFromGrouping(targetPath, groupingUses.next());
856 if (result != null) {
857 fromUses = ((DataSchemaNode) result).isAddedByUses();
861 // this indicates invalid yang and thus possible bug in code because
862 // invalid yang should be already spotted by parser
863 throw new IllegalStateException("Failed to generate code for augment in " + parentUsesNode);
866 return (DataSchemaNode) result;
870 * Returns a generated type builder for an augmentation.
872 * The name of the type builder is equal to the name of augmented node with
873 * serial number as suffix.
877 * @param augmentPackageName
878 * string with contains the package name to which the augment
880 * @param basePackageName
881 * string with the package name to which the augmented node
883 * @param targetTypeRef
886 * augmentation schema which contains data about the child nodes
887 * and uses of augment
888 * @return generated type builder for augment
890 private GeneratedTypeBuilder addRawAugmentGenTypeDefinition(final Module module, final String augmentPackageName,
891 final String basePackageName, final Type targetTypeRef, final AugmentationSchema augSchema) {
892 Map<String, GeneratedTypeBuilder> augmentBuilders = genTypeBuilders.get(augmentPackageName);
893 if (augmentBuilders == null) {
894 augmentBuilders = new HashMap<>();
895 genTypeBuilders.put(augmentPackageName, augmentBuilders);
897 final String augIdentifier = getAugmentIdentifier(augSchema.getUnknownSchemaNodes());
900 if (augIdentifier != null) {
901 augTypeName = BindingMapping.getClassName(augIdentifier);
903 augTypeName = augGenTypeName(augmentBuilders, targetTypeRef.getName());
906 final GeneratedTypeBuilder augTypeBuilder = new GeneratedTypeBuilderImpl(augmentPackageName, augTypeName);
908 augTypeBuilder.addImplementsType(DATA_OBJECT);
909 augTypeBuilder.addImplementsType(Types.augmentationTypeFor(targetTypeRef));
910 addImplementedInterfaceFromUses(augSchema, augTypeBuilder);
912 augSchemaNodeToMethods(module, basePackageName, augTypeBuilder, augTypeBuilder, augSchema.getChildNodes());
913 augmentBuilders.put(augTypeName, augTypeBuilder);
915 genCtx.get(module).addTargetToAugmentation(targetTypeRef, augTypeBuilder);
916 genCtx.get(module).addAugmentType(augTypeBuilder);
917 genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
918 return augTypeBuilder;
923 * @param unknownSchemaNodes
924 * @return nodeParameter of UnknownSchemaNode
926 private String getAugmentIdentifier(final List<UnknownSchemaNode> unknownSchemaNodes) {
927 for (UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
928 final QName nodeType = unknownSchemaNode.getNodeType();
929 if (AUGMENT_IDENTIFIER_NAME.equals(nodeType.getLocalName())
930 && YANG_EXT_NAMESPACE.equals(nodeType.getNamespace().toString())) {
931 return unknownSchemaNode.getNodeParameter();
938 * Returns first unique name for the augment generated type builder. The
939 * generated type builder name for augment consists from name of augmented
940 * node and serial number of its augmentation.
943 * map of builders which were created in the package to which the
944 * augmentation belongs
946 * string with name of augmented node
947 * @return string with unique name for augmentation builder
949 private String augGenTypeName(final Map<String, GeneratedTypeBuilder> builders, final String genTypeName) {
951 if (builders != null) {
952 while (builders.containsKey(genTypeName + index)) {
956 return genTypeName + index;
960 * Adds the methods to <code>typeBuilder</code> which represent subnodes of
961 * node for which <code>typeBuilder</code> was created.
963 * The subnodes aren't mapped to the methods if they are part of grouping or
964 * augment (in this case are already part of them).
968 * @param basePackageName
969 * string contains the module package name
971 * generated type builder which represents any node. The subnodes
972 * of this node are added to the <code>typeBuilder</code> as
973 * methods. The subnode can be of type leaf, leaf-list, list,
978 * set of data schema nodes which are the children of the node
979 * for which <code>typeBuilder</code> was created
980 * @return generated type builder which is the same builder as input
981 * parameter. The getter methods (representing child nodes) could be
984 private GeneratedTypeBuilder resolveDataSchemaNodes(final Module module, final String basePackageName,
985 final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf, final Set<DataSchemaNode> schemaNodes) {
986 if ((schemaNodes != null) && (parent != null)) {
987 for (DataSchemaNode schemaNode : schemaNodes) {
988 if (!schemaNode.isAugmenting() && !schemaNode.isAddedByUses()) {
989 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, parent, childOf, module);
997 * Adds the methods to <code>typeBuilder</code> what represents subnodes of
998 * node for which <code>typeBuilder</code> was created.
1002 * @param basePackageName
1003 * string contains the module package name
1004 * @param typeBuilder
1005 * generated type builder which represents any node. The subnodes
1006 * of this node are added to the <code>typeBuilder</code> as
1007 * methods. The subnode can be of type leaf, leaf-list, list,
1008 * container, choice.
1011 * @param schemaNodes
1012 * set of data schema nodes which are the children of the node
1013 * for which <code>typeBuilder</code> was created
1014 * @return generated type builder which is the same object as the input
1015 * parameter <code>typeBuilder</code>. The getter method could be
1018 private GeneratedTypeBuilder augSchemaNodeToMethods(final Module module, final String basePackageName,
1019 final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf, final Set<DataSchemaNode> schemaNodes) {
1020 if ((schemaNodes != null) && (typeBuilder != null)) {
1021 for (DataSchemaNode schemaNode : schemaNodes) {
1022 if (!schemaNode.isAugmenting()) {
1023 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, typeBuilder, childOf, module);
1031 * Adds to <code>typeBuilder</code> a method which is derived from
1032 * <code>schemaNode</code>.
1034 * @param basePackageName
1035 * string with the module package name
1037 * data schema node which is added to <code>typeBuilder</code> as
1039 * @param typeBuilder
1040 * generated type builder to which is <code>schemaNode</code>
1041 * added as a method.
1047 private void addSchemaNodeToBuilderAsMethod(final String basePackageName, final DataSchemaNode node,
1048 final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf, final Module module) {
1049 if (node != null && typeBuilder != null) {
1050 if (node instanceof LeafSchemaNode) {
1051 resolveLeafSchemaNodeAsMethod(typeBuilder, (LeafSchemaNode) node);
1052 } else if (node instanceof LeafListSchemaNode) {
1053 resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) node);
1054 } else if (node instanceof ContainerSchemaNode) {
1055 containerToGenType(module, basePackageName, typeBuilder, childOf, (ContainerSchemaNode) node);
1056 } else if (node instanceof ListSchemaNode) {
1057 listToGenType(module, basePackageName, typeBuilder, childOf, (ListSchemaNode) node);
1058 } else if (node instanceof ChoiceNode) {
1059 choiceToGeneratedType(module, basePackageName, typeBuilder, (ChoiceNode) node);
1061 // TODO: anyxml not yet supported
1062 LOG.debug("Unable to add schema node {} as method in {}: unsupported type of node.", node.getClass(),
1063 typeBuilder.getFullyQualifiedName());
1069 * Converts <code>choiceNode</code> to the list of generated types for
1070 * choice and its cases.
1072 * The package names for choice and for its cases are created as
1073 * concatenation of the module package (<code>basePackageName</code>) and
1074 * names of all parents node.
1078 * @param basePackageName
1079 * string with the module package name
1083 * choice node which is mapped to generated type. Also child
1084 * nodes - cases are mapped to generated types.
1085 * @throws IllegalArgumentException
1087 * <li>if <code>basePackageName</code> is null</li>
1088 * <li>if <code>choiceNode</code> is null</li>
1091 private void choiceToGeneratedType(final Module module, final String basePackageName, final GeneratedTypeBuilder parent,
1092 final ChoiceNode choiceNode) {
1093 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
1094 checkArgument(choiceNode != null, "Choice Schema Node cannot be NULL.");
1096 if (!choiceNode.isAddedByUses()) {
1097 final String packageName = packageNameForGeneratedType(basePackageName, choiceNode.getPath());
1098 final GeneratedTypeBuilder choiceTypeBuilder = addRawInterfaceDefinition(packageName, choiceNode);
1099 constructGetter(parent, choiceNode.getQName().getLocalName(), choiceNode.getDescription(),
1101 choiceTypeBuilder.addImplementsType(typeForClass(DataContainer.class));
1102 genCtx.get(module).addChildNodeType(choiceNode.getPath(), choiceTypeBuilder);
1103 generateTypesFromChoiceCases(module, basePackageName, choiceTypeBuilder.toInstance(), choiceNode);
1108 * Converts <code>caseNodes</code> set to list of corresponding generated
1111 * For every <i>case</i> which isn't added through augment or <i>uses</i> is
1112 * created generated type builder. The package names for the builder is
1113 * created as concatenation of the module package (
1114 * <code>basePackageName</code>) and names of all parents nodes of the
1115 * concrete <i>case</i>. There is also relation "<i>implements type</i>"
1116 * between every case builder and <i>choice</i> type
1120 * @param basePackageName
1121 * string with the module package name
1122 * @param refChoiceType
1123 * type which represents superior <i>case</i>
1125 * choice case node which is mapped to generated type
1126 * @return list of generated types for <code>caseNodes</code>.
1127 * @throws IllegalArgumentException
1129 * <li>if <code>basePackageName</code> equals null</li>
1130 * <li>if <code>refChoiceType</code> equals null</li>
1131 * <li>if <code>caseNodes</code> equals null</li>
1134 private void generateTypesFromChoiceCases(final Module module, final String basePackageName, final Type refChoiceType,
1135 final ChoiceNode choiceNode) {
1136 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
1137 checkArgument(refChoiceType != null, "Referenced Choice Type cannot be NULL.");
1138 checkArgument(choiceNode != null, "ChoiceNode cannot be NULL.");
1140 final Set<ChoiceCaseNode> caseNodes = choiceNode.getCases();
1141 if (caseNodes == null) {
1145 for (ChoiceCaseNode caseNode : caseNodes) {
1146 if (caseNode != null && !caseNode.isAddedByUses() && !caseNode.isAugmenting()) {
1147 final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath());
1148 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode);
1149 caseTypeBuilder.addImplementsType(refChoiceType);
1150 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
1151 genCtx.get(module).addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode);
1152 final Set<DataSchemaNode> caseChildNodes = caseNode.getChildNodes();
1153 if (caseChildNodes != null) {
1154 Object parentNode = null;
1155 final SchemaPath nodeSp = choiceNode.getPath();
1156 parentNode = findDataSchemaNode(schemaContext, nodeSp.getParent());
1159 if (parentNode instanceof AugmentationSchema) {
1160 final AugmentationSchema augSchema = (AugmentationSchema) parentNode;
1161 final SchemaPath targetPath = augSchema.getTargetPath();
1162 SchemaNode targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
1163 if (targetSchemaNode instanceof DataSchemaNode
1164 && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
1165 targetSchemaNode = findOriginal((DataSchemaNode) targetSchemaNode, schemaContext);
1166 if (targetSchemaNode == null) {
1167 throw new NullPointerException(
1168 "Failed to find target node from grouping for augmentation " + augSchema
1169 + " in module " + module.getName());
1172 parent = targetSchemaNode;
1174 final SchemaPath sp = choiceNode.getPath();
1175 parent = findDataSchemaNode(schemaContext, sp.getParent());
1177 GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.getPath());
1178 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, caseChildNodes);
1182 processUsesAugments(caseNode, module);
1187 * Generates list of generated types for all the cases of a choice which are
1188 * added to the choice through the augment.
1192 * @param basePackageName
1193 * string contains name of package to which augment belongs. If
1194 * an augmented choice is from an other package (pcg1) than an
1195 * augmenting choice (pcg2) then case's of the augmenting choice
1196 * will belong to pcg2.
1198 * Type which represents target choice
1200 * node which represents target choice
1201 * @param augmentedNodes
1202 * set of choice case nodes for which is checked if are/aren't
1203 * added to choice through augmentation
1204 * @return list of generated types which represents augmented cases of
1205 * choice <code>refChoiceType</code>
1206 * @throws IllegalArgumentException
1208 * <li>if <code>basePackageName</code> is null</li>
1209 * <li>if <code>targetType</code> is null</li>
1210 * <li>if <code>augmentedNodes</code> is null</li>
1213 private void generateTypesFromAugmentedChoiceCases(final Module module, final String basePackageName, final Type targetType,
1214 final ChoiceNode targetNode, final Set<DataSchemaNode> augmentedNodes) {
1215 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
1216 checkArgument(targetType != null, "Referenced Choice Type cannot be NULL.");
1217 checkArgument(augmentedNodes != null, "Set of Choice Case Nodes cannot be NULL.");
1219 for (DataSchemaNode caseNode : augmentedNodes) {
1220 if (caseNode != null) {
1221 final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath());
1222 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode);
1223 caseTypeBuilder.addImplementsType(targetType);
1225 SchemaNode parent = null;
1226 final SchemaPath nodeSp = targetNode.getPath();
1227 parent = findDataSchemaNode(schemaContext, nodeSp.getParent());
1229 GeneratedTypeBuilder childOfType = null;
1230 if (parent instanceof Module) {
1231 childOfType = genCtx.get(parent).getModuleNode();
1232 } else if (parent instanceof ChoiceCaseNode) {
1233 childOfType = findCaseByPath(parent.getPath());
1234 } else if (parent instanceof DataSchemaNode || parent instanceof NotificationDefinition) {
1235 childOfType = findChildNodeByPath(parent.getPath());
1236 } else if (parent instanceof GroupingDefinition) {
1237 childOfType = findGroupingByPath(parent.getPath());
1240 if (childOfType == null) {
1241 throw new IllegalArgumentException("Failed to find parent type of choice " + targetNode);
1244 ChoiceCaseNode node = null;
1245 if (caseNode instanceof ChoiceCaseNode) {
1246 node = (ChoiceCaseNode) caseNode;
1248 node = targetNode.getCaseNodeByName(caseNode.getQName().getLocalName());
1250 final Set<DataSchemaNode> childNodes = node.getChildNodes();
1251 if (childNodes != null) {
1252 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, childNodes);
1254 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
1255 genCtx.get(module).addChoiceToCaseMapping(targetType, caseTypeBuilder, node);
1261 * Converts <code>leaf</code> to the getter method which is added to
1262 * <code>typeBuilder</code>.
1264 * @param typeBuilder
1265 * generated type builder to which is added getter method as
1266 * <code>leaf</code> mapping
1268 * leaf schema node which is mapped as getter method which is
1269 * added to <code>typeBuilder</code>
1270 * @return boolean value
1272 * <li>false - if <code>leaf</code> or <code>typeBuilder</code> are
1274 * <li>true - in other cases</li>
1277 private Type resolveLeafSchemaNodeAsMethod(final GeneratedTypeBuilder typeBuilder, final LeafSchemaNode leaf) {
1278 Type returnType = null;
1279 if ((leaf != null) && (typeBuilder != null)) {
1280 final String leafName = leaf.getQName().getLocalName();
1281 String leafDesc = leaf.getDescription();
1282 if (leafDesc == null) {
1286 final Module parentModule = findParentModule(schemaContext, leaf);
1287 if (leafName != null && !leaf.isAddedByUses()) {
1288 final TypeDefinition<?> typeDef = leaf.getType();
1290 GeneratedTOBuilder genTOBuilder;
1291 if (typeDef instanceof EnumTypeDefinition) {
1292 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1293 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
1294 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(),
1297 if (enumBuilder != null) {
1298 returnType = enumBuilder.toInstance(typeBuilder);
1300 ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
1301 } else if (typeDef instanceof UnionType) {
1302 genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf);
1303 if (genTOBuilder != null) {
1304 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
1306 } else if (typeDef instanceof BitsTypeDefinition) {
1307 genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf);
1308 if (genTOBuilder != null) {
1309 returnType = new ReferencedTypeImpl(genTOBuilder.getPackageName(), genTOBuilder.getName());
1312 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
1313 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
1315 if (returnType != null) {
1316 final MethodSignatureBuilder getter = constructGetter(typeBuilder, leafName, leafDesc, returnType);
1317 processContextRefExtension(leaf, getter, parentModule);
1324 private void processContextRefExtension(final LeafSchemaNode leaf, final MethodSignatureBuilder getter, final Module module) {
1325 for (UnknownSchemaNode node : leaf.getUnknownSchemaNodes()) {
1326 final QName nodeType = node.getNodeType();
1327 if ("context-reference".equals(nodeType.getLocalName())) {
1328 final String nodeParam = node.getNodeParameter();
1329 IdentitySchemaNode identity = null;
1330 String basePackageName = null;
1331 final Iterable<String> splittedElement = Splitter.on(':').split(nodeParam);
1332 final Iterator<String> iterator = splittedElement.iterator();
1333 final int length = Iterables.size(splittedElement);
1335 identity = findIdentityByName(module.getIdentities(), iterator.next());
1336 basePackageName = moduleNamespaceToPackageName(module);
1337 } else if (length == 2) {
1338 String prefix = iterator.next();
1339 final Module dependentModule = findModuleFromImports(module.getImports(), prefix);
1340 if (dependentModule == null) {
1341 throw new IllegalArgumentException("Failed to process context-reference: unknown prefix "
1344 identity = findIdentityByName(dependentModule.getIdentities(), iterator.next());
1345 basePackageName = moduleNamespaceToPackageName(dependentModule);
1347 throw new IllegalArgumentException("Failed to process context-reference: unknown identity "
1350 if (identity == null) {
1351 throw new IllegalArgumentException("Failed to process context-reference: unknown identity "
1355 final Class<RoutingContext> clazz = RoutingContext.class;
1356 final AnnotationTypeBuilder rc = getter.addAnnotation(clazz.getPackage().getName(),
1357 clazz.getSimpleName());
1358 final String packageName = packageNameForGeneratedType(basePackageName, identity.getPath());
1359 final String genTypeName = BindingMapping.getClassName(identity.getQName().getLocalName());
1360 rc.addParameter("value", packageName + "." + genTypeName + ".class");
1365 private IdentitySchemaNode findIdentityByName(final Set<IdentitySchemaNode> identities, final String name) {
1366 for (IdentitySchemaNode id : identities) {
1367 if (id.getQName().getLocalName().equals(name)) {
1374 private Module findModuleFromImports(final Set<ModuleImport> imports, final String prefix) {
1375 for (ModuleImport imp : imports) {
1376 if (imp.getPrefix().equals(prefix)) {
1377 return schemaContext.findModuleByName(imp.getModuleName(), imp.getRevision());
1383 private boolean resolveLeafSchemaNodeAsProperty(final GeneratedTOBuilder toBuilder, final LeafSchemaNode leaf,
1384 final boolean isReadOnly, final Module module) {
1385 if ((leaf != null) && (toBuilder != null)) {
1386 final String leafName = leaf.getQName().getLocalName();
1387 String leafDesc = leaf.getDescription();
1388 if (leafDesc == null) {
1392 if (leafName != null) {
1393 Type returnType = null;
1394 final TypeDefinition<?> typeDef = leaf.getType();
1395 if (typeDef instanceof UnionTypeDefinition) {
1396 // GeneratedType for this type definition should be already
1398 QName qname = typeDef.getQName();
1399 Module unionModule = null;
1400 String prefix = qname.getPrefix();
1401 if (prefix == null || prefix.isEmpty() || prefix.equals(module.getPrefix())) {
1402 unionModule = module;
1404 unionModule = findModuleFromImports(module.getImports(), qname.getPrefix());
1406 final ModuleContext mc = genCtx.get(unionModule);
1407 returnType = mc.getTypedefs().get(typeDef.getPath());
1409 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1411 return resolveLeafSchemaNodeAsProperty(toBuilder, leaf, returnType, isReadOnly);
1418 * Converts <code>leaf</code> schema node to property of generated TO
1422 * generated TO builder to which is <code>leaf</code> added as
1425 * leaf schema node which is added to <code>toBuilder</code> as
1430 * boolean value which says if leaf property is|isn't read only
1431 * @return boolean value
1433 * <li>false - if <code>leaf</code>, <code>toBuilder</code> or leaf
1434 * name equals null or if leaf is added by <i>uses</i>.</li>
1435 * <li>true - other cases</li>
1438 private boolean resolveLeafSchemaNodeAsProperty(final GeneratedTOBuilder toBuilder, final LeafSchemaNode leaf, final Type returnType,
1439 final boolean isReadOnly) {
1440 if (returnType == null) {
1443 final String leafName = leaf.getQName().getLocalName();
1444 final String leafDesc = leaf.getDescription();
1445 final GeneratedPropertyBuilder propBuilder = toBuilder.addProperty(parseToValidParamName(leafName));
1446 propBuilder.setReadOnly(isReadOnly);
1447 propBuilder.setReturnType(returnType);
1448 propBuilder.setComment(leafDesc);
1449 toBuilder.addEqualsIdentity(propBuilder);
1450 toBuilder.addHashIdentity(propBuilder);
1451 toBuilder.addToStringProperty(propBuilder);
1456 * Converts <code>node</code> leaf list schema node to getter method of
1457 * <code>typeBuilder</code>.
1459 * @param typeBuilder
1460 * generated type builder to which is <code>node</code> added as
1463 * leaf list schema node which is added to
1464 * <code>typeBuilder</code> as getter method
1465 * @return boolean value
1467 * <li>true - if <code>node</code>, <code>typeBuilder</code>,
1468 * nodeName equal null or <code>node</code> is added by <i>uses</i></li>
1469 * <li>false - other cases</li>
1472 private boolean resolveLeafListSchemaNode(final GeneratedTypeBuilder typeBuilder, final LeafListSchemaNode node) {
1473 if ((node != null) && (typeBuilder != null)) {
1474 final QName nodeName = node.getQName();
1476 if (nodeName != null && !node.isAddedByUses()) {
1477 final TypeDefinition<?> typeDef = node.getType();
1478 final Module parentModule = findParentModule(schemaContext, node);
1480 Type returnType = null;
1481 if (typeDef instanceof EnumTypeDefinition) {
1482 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node);
1483 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
1484 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, nodeName,
1486 returnType = new ReferencedTypeImpl(enumBuilder.getPackageName(), enumBuilder.getName());
1487 ((TypeProviderImpl) typeProvider).putReferencedType(node.getPath(), returnType);
1488 } else if (typeDef instanceof UnionType) {
1489 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node);
1490 if (genTOBuilder != null) {
1491 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
1493 } else if (typeDef instanceof BitsTypeDefinition) {
1494 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node);
1495 returnType = new ReferencedTypeImpl(genTOBuilder.getPackageName(), genTOBuilder.getName());
1497 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
1498 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
1501 final ParameterizedType listType = Types.listTypeFor(returnType);
1502 constructGetter(typeBuilder, nodeName.getLocalName(), node.getDescription(), listType);
1509 private Type createReturnTypeForUnion(final GeneratedTOBuilder genTOBuilder, final TypeDefinition<?> typeDef,
1510 final GeneratedTypeBuilder typeBuilder, final Module parentModule) {
1511 final GeneratedTOBuilderImpl returnType = new GeneratedTOBuilderImpl(genTOBuilder.getPackageName(),
1512 genTOBuilder.getName());
1513 genTOBuilder.setTypedef(true);
1514 genTOBuilder.setIsUnion(true);
1515 ((TypeProviderImpl) typeProvider).addUnitsToGenTO(genTOBuilder, typeDef.getUnits());
1518 final GeneratedTOBuilder unionBuilder = new GeneratedTOBuilderImpl(typeBuilder.getPackageName(),
1519 genTOBuilder.getName() + "Builder");
1520 unionBuilder.setIsUnionBuilder(true);
1521 final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
1522 method.setReturnType(returnType);
1523 method.addParameter(Types.STRING, "defaultValue");
1524 method.setAccessModifier(AccessModifier.PUBLIC);
1525 method.setStatic(true);
1527 final Set<Type> types = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(parentModule);
1528 if (types == null) {
1529 ((TypeProviderImpl) typeProvider).getAdditionalTypes().put(parentModule,
1530 Sets.<Type> newHashSet(unionBuilder.toInstance()));
1532 types.add(unionBuilder.toInstance());
1534 return returnType.toInstance();
1537 private GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode schemaNode) {
1538 return addDefaultInterfaceDefinition(packageName, schemaNode, null);
1542 * Instantiates generated type builder with <code>packageName</code> and
1543 * <code>schemaNode</code>.
1545 * The new builder always implements
1546 * {@link org.opendaylight.yangtools.yang.binding.DataObject DataObject}.<br />
1547 * If <code>schemaNode</code> is instance of GroupingDefinition it also
1548 * implements {@link org.opendaylight.yangtools.yang.binding.Augmentable
1549 * Augmentable}.<br />
1550 * If <code>schemaNode</code> is instance of
1551 * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer
1552 * DataNodeContainer} it can also implement nodes which are specified in
1555 * @param packageName
1556 * string with the name of the package to which
1557 * <code>schemaNode</code> belongs.
1559 * schema node for which is created generated type builder
1561 * parent type (can be null)
1562 * @return generated type builder <code>schemaNode</code>
1564 private GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode schemaNode, final Type parent) {
1565 final GeneratedTypeBuilder it = addRawInterfaceDefinition(packageName, schemaNode, "");
1566 if (parent == null) {
1567 it.addImplementsType(DATA_OBJECT);
1569 it.addImplementsType(BindingTypes.childOf(parent));
1571 if (!(schemaNode instanceof GroupingDefinition)) {
1572 it.addImplementsType(augmentable(it));
1575 if (schemaNode instanceof DataNodeContainer) {
1576 addImplementedInterfaceFromUses((DataNodeContainer) schemaNode, it);
1583 * Wraps the calling of the same overloaded method.
1585 * @param packageName
1586 * string with the package name to which returning generated type
1589 * schema node which provide data about the schema node name
1590 * @return generated type builder for <code>schemaNode</code>
1592 private GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode) {
1593 return addRawInterfaceDefinition(packageName, schemaNode, "");
1597 * Returns reference to generated type builder for specified
1598 * <code>schemaNode</code> with <code>packageName</code>.
1600 * Firstly the generated type builder is searched in
1601 * {@link BindingGeneratorImpl#genTypeBuilders genTypeBuilders}. If it isn't
1602 * found it is created and added to <code>genTypeBuilders</code>.
1604 * @param packageName
1605 * string with the package name to which returning generated type
1608 * schema node which provide data about the schema node name
1610 * return type name prefix
1611 * @return generated type builder for <code>schemaNode</code>
1612 * @throws IllegalArgumentException
1614 * <li>if <code>schemaNode</code> is null</li>
1615 * <li>if <code>packageName</code> is null</li>
1616 * <li>if QName of schema node is null</li>
1617 * <li>if schemaNode name is null</li>
1621 private GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode, final String prefix) {
1622 checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
1623 checkArgument(packageName != null, "Package Name for Generated Type cannot be NULL.");
1624 checkArgument(schemaNode.getQName() != null, "QName for Data Schema Node cannot be NULL.");
1625 final String schemaNodeName = schemaNode.getQName().getLocalName();
1626 checkArgument(schemaNodeName != null, "Local Name of QName for Data Schema Node cannot be NULL.");
1629 if (prefix == null) {
1630 genTypeName = BindingMapping.getClassName(schemaNodeName);
1632 genTypeName = prefix + BindingMapping.getClassName(schemaNodeName);
1635 // FIXME: Validation of name conflict
1636 final GeneratedTypeBuilderImpl newType = new GeneratedTypeBuilderImpl(packageName, genTypeName);
1637 qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, schemaNode.getQName());
1638 newType.addComment(schemaNode.getDescription());
1639 if (!genTypeBuilders.containsKey(packageName)) {
1640 final Map<String, GeneratedTypeBuilder> builders = new HashMap<>();
1641 builders.put(genTypeName, newType);
1642 genTypeBuilders.put(packageName, builders);
1644 final Map<String, GeneratedTypeBuilder> builders = genTypeBuilders.get(packageName);
1645 if (!builders.containsKey(genTypeName)) {
1646 builders.put(genTypeName, newType);
1653 * Creates the name of the getter method name from <code>localName</code>.
1656 * string with the name of the getter method
1659 * @return string with the name of the getter method for
1660 * <code>methodName</code> in JAVA method format
1662 public static String getterMethodName(final String localName, final Type returnType) {
1663 final StringBuilder method = new StringBuilder();
1664 if (BOOLEAN.equals(returnType)) {
1665 method.append("is");
1667 method.append("get");
1669 String name = BindingMapping.toFirstUpper(BindingMapping.getPropertyName(localName));
1670 method.append(name);
1671 return method.toString();
1675 * Created a method signature builder as part of
1676 * <code>interfaceBuilder</code>.
1678 * The method signature builder is created for the getter method of
1679 * <code>schemaNodeName</code>. Also <code>comment</code> and
1680 * <code>returnType</code> information are added to the builder.
1682 * @param interfaceBuilder
1683 * generated type builder for which the getter method should be
1685 * @param schemaNodeName
1686 * string with schema node name. The name will be the part of the
1687 * getter method name.
1689 * string with comment for the getter method
1691 * type which represents the return type of the getter method
1692 * @return method signature builder which represents the getter method of
1693 * <code>interfaceBuilder</code>
1695 private MethodSignatureBuilder constructGetter(final GeneratedTypeBuilder interfaceBuilder, final String schemaNodeName,
1696 final String comment, final Type returnType) {
1697 final MethodSignatureBuilder getMethod = interfaceBuilder
1698 .addMethod(getterMethodName(schemaNodeName, returnType));
1699 getMethod.setComment(comment);
1700 getMethod.setReturnType(returnType);
1705 * Adds <code>schemaNode</code> to <code>typeBuilder</code> as getter method
1706 * or to <code>genTOBuilder</code> as property.
1708 * @param basePackageName
1709 * string contains the module package name
1711 * data schema node which should be added as getter method to
1712 * <code>typeBuilder</code> or as a property to
1713 * <code>genTOBuilder</code> if is part of the list key
1714 * @param typeBuilder
1715 * generated type builder for the list schema node
1716 * @param genTOBuilder
1717 * generated TO builder for the list keys
1719 * list of string which contains names of the list keys
1722 * @throws IllegalArgumentException
1724 * <li>if <code>schemaNode</code> equals null</li>
1725 * <li>if <code>typeBuilder</code> equals null</li>
1728 private void addSchemaNodeToListBuilders(final String basePackageName, final DataSchemaNode schemaNode,
1729 final GeneratedTypeBuilder typeBuilder, final GeneratedTOBuilder genTOBuilder, final List<String> listKeys, final Module module) {
1730 checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
1731 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
1733 if (schemaNode instanceof LeafSchemaNode) {
1734 final LeafSchemaNode leaf = (LeafSchemaNode) schemaNode;
1735 final String leafName = leaf.getQName().getLocalName();
1736 final Type type = resolveLeafSchemaNodeAsMethod(typeBuilder, leaf);
1737 if (listKeys.contains(leafName)) {
1739 resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, true, module);
1741 resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, type, true);
1744 } else if (!schemaNode.isAddedByUses()) {
1745 if (schemaNode instanceof LeafListSchemaNode) {
1746 resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) schemaNode);
1747 } else if (schemaNode instanceof ContainerSchemaNode) {
1748 containerToGenType(module, basePackageName, typeBuilder, typeBuilder, (ContainerSchemaNode) schemaNode);
1749 } else if (schemaNode instanceof ChoiceNode) {
1750 choiceToGeneratedType(module, basePackageName, typeBuilder, (ChoiceNode) schemaNode);
1751 } else if (schemaNode instanceof ListSchemaNode) {
1752 listToGenType(module, basePackageName, typeBuilder, typeBuilder, (ListSchemaNode) schemaNode);
1757 private void typeBuildersToGenTypes(final Module module, final GeneratedTypeBuilder typeBuilder, final GeneratedTOBuilder genTOBuilder) {
1758 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
1760 if (genTOBuilder != null) {
1761 GeneratedTransferObject genTO = genTOBuilder.toInstance();
1762 constructGetter(typeBuilder, "key", "Returns Primary Key of Yang List Type", genTO);
1763 genCtx.get(module).addGeneratedTOBuilder(genTOBuilder);
1768 * Selects the names of the list keys from <code>list</code> and returns
1769 * them as the list of the strings
1772 * of string with names of the list keys
1773 * @return list of string which represents names of the list keys. If the
1774 * <code>list</code> contains no keys then the empty list is
1777 private List<String> listKeys(final ListSchemaNode list) {
1778 final List<String> listKeys = new ArrayList<>();
1780 List<QName> keyDefinition = list.getKeyDefinition();
1781 if (keyDefinition != null) {
1782 for (QName keyDef : keyDefinition) {
1783 listKeys.add(keyDef.getLocalName());
1790 * Generates for the <code>list</code> which contains any list keys special
1791 * generated TO builder.
1793 * @param packageName
1794 * string with package name to which the list belongs
1796 * list schema node which is source of data about the list name
1797 * @return generated TO builder which represents the keys of the
1798 * <code>list</code> or null if <code>list</code> is null or list of
1799 * key definitions is null or empty.
1801 private GeneratedTOBuilder resolveListKeyTOBuilder(final String packageName, final ListSchemaNode list) {
1802 GeneratedTOBuilder genTOBuilder = null;
1803 if ((list.getKeyDefinition() != null) && (!list.getKeyDefinition().isEmpty())) {
1804 final String listName = list.getQName().getLocalName() + "Key";
1805 final String genTOName = BindingMapping.getClassName(listName);
1806 genTOBuilder = new GeneratedTOBuilderImpl(packageName, genTOName);
1808 return genTOBuilder;
1812 * Builds generated TO builders for <code>typeDef</code> of type
1813 * {@link org.opendaylight.yangtools.yang.model.util.UnionType UnionType} or
1814 * {@link org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
1815 * BitsTypeDefinition} which are also added to <code>typeBuilder</code> as
1816 * enclosing transfer object.
1818 * If more then one generated TO builder is created for enclosing then all
1819 * of the generated TO builders are added to <code>typeBuilder</code> as
1820 * enclosing transfer objects.
1823 * type definition which can be of type <code>UnionType</code> or
1824 * <code>BitsTypeDefinition</code>
1825 * @param typeBuilder
1826 * generated type builder to which is added generated TO created
1827 * from <code>typeDef</code>
1829 * string with name for generated TO builder
1831 * @return generated TO builder for <code>typeDef</code>
1833 private GeneratedTOBuilder addTOToTypeBuilder(final TypeDefinition<?> typeDef, final GeneratedTypeBuilder typeBuilder,
1834 final DataSchemaNode leaf) {
1835 final String classNameFromLeaf = BindingMapping.getClassName(leaf.getQName());
1836 final List<GeneratedTOBuilder> genTOBuilders = new ArrayList<>();
1837 final String packageName = typeBuilder.getFullyQualifiedName();
1838 if (typeDef instanceof UnionTypeDefinition) {
1839 final List<GeneratedTOBuilder> types = ((TypeProviderImpl) typeProvider)
1840 .provideGeneratedTOBuildersForUnionTypeDef(packageName, ((UnionTypeDefinition) typeDef),
1841 classNameFromLeaf, leaf);
1842 genTOBuilders.addAll(types);
1844 GeneratedTOBuilder resultTOBuilder = null;
1845 if (!types.isEmpty()) {
1846 resultTOBuilder = types.remove(0);
1847 for (GeneratedTOBuilder genTOBuilder : types) {
1848 resultTOBuilder.addEnclosingTransferObject(genTOBuilder);
1852 final GeneratedPropertyBuilder genPropBuilder = resultTOBuilder.addProperty("value");
1853 genPropBuilder.setReturnType(Types.primitiveType("char[]", null));
1854 resultTOBuilder.addEqualsIdentity(genPropBuilder);
1855 resultTOBuilder.addHashIdentity(genPropBuilder);
1856 resultTOBuilder.addToStringProperty(genPropBuilder);
1858 } else if (typeDef instanceof BitsTypeDefinition) {
1859 genTOBuilders.add((((TypeProviderImpl) typeProvider)).provideGeneratedTOBuilderForBitsTypeDefinition(
1860 packageName, typeDef, classNameFromLeaf));
1862 if (genTOBuilders != null && !genTOBuilders.isEmpty()) {
1863 for (GeneratedTOBuilder genTOBuilder : genTOBuilders) {
1864 typeBuilder.addEnclosingTransferObject(genTOBuilder);
1866 return genTOBuilders.get(0);
1873 * Adds the implemented types to type builder.
1875 * The method passes through the list of <i>uses</i> in
1876 * {@code dataNodeContainer}. For every <i>use</i> is obtained corresponding
1877 * generated type from {@link BindingGeneratorImpl#allGroupings
1878 * allGroupings} which is added as <i>implements type</i> to
1879 * <code>builder</code>
1881 * @param dataNodeContainer
1882 * element which contains the list of used YANG groupings
1884 * builder to which are added implemented types according to
1885 * <code>dataNodeContainer</code>
1886 * @return generated type builder with all implemented types
1888 private GeneratedTypeBuilder addImplementedInterfaceFromUses(final DataNodeContainer dataNodeContainer,
1889 final GeneratedTypeBuilder builder) {
1890 for (UsesNode usesNode : dataNodeContainer.getUses()) {
1891 if (usesNode.getGroupingPath() != null) {
1892 final GeneratedType genType = findGroupingByPath(usesNode.getGroupingPath()).toInstance();
1893 if (genType == null) {
1894 throw new IllegalStateException("Grouping " + usesNode.getGroupingPath() + "is not resolved for "
1895 + builder.getName());
1897 builder.addImplementsType(genType);
1898 builder.addComment(genType.getComment());
1904 private GeneratedTypeBuilder findChildNodeByPath(final SchemaPath path) {
1905 for (ModuleContext ctx : genCtx.values()) {
1906 GeneratedTypeBuilder result = ctx.getChildNode(path);
1907 if (result != null) {
1914 private GeneratedTypeBuilder findGroupingByPath(final SchemaPath path) {
1915 for (ModuleContext ctx : genCtx.values()) {
1916 GeneratedTypeBuilder result = ctx.getGrouping(path);
1917 if (result != null) {
1924 private GeneratedTypeBuilder findCaseByPath(final SchemaPath path) {
1925 for (ModuleContext ctx : genCtx.values()) {
1926 GeneratedTypeBuilder result = ctx.getCase(path);
1927 if (result != null) {
1934 public Map<Module, ModuleContext> getModuleContexts() {