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.packageNameForGeneratedType;
15 import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.parseToValidParamName;
16 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.DATA_OBJECT;
17 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.DATA_ROOT;
18 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.IDENTIFIABLE;
19 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.IDENTIFIER;
20 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.NOTIFICATION;
21 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.augmentable;
22 import static org.opendaylight.yangtools.binding.generator.util.Types.BOOLEAN;
23 import static org.opendaylight.yangtools.binding.generator.util.Types.FUTURE;
24 import static org.opendaylight.yangtools.binding.generator.util.Types.VOID;
25 import static org.opendaylight.yangtools.binding.generator.util.Types.typeForClass;
26 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode;
27 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findNodeInSchemaContext;
28 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
30 import com.google.common.base.Splitter;
31 import com.google.common.collect.Iterables;
32 import com.google.common.collect.Sets;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.Iterator;
38 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.DerivableSchemaNode;
80 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
81 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
82 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
83 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
84 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
85 import org.opendaylight.yangtools.yang.model.api.Module;
86 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
87 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
88 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
89 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
90 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
91 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
92 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
93 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
94 import org.opendaylight.yangtools.yang.model.api.UsesNode;
95 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
96 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
97 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
98 import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
99 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
100 import org.opendaylight.yangtools.yang.model.util.UnionType;
101 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
102 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
103 import org.slf4j.Logger;
104 import org.slf4j.LoggerFactory;
106 public class BindingGeneratorImpl implements BindingGenerator {
107 private static final Logger LOG = LoggerFactory.getLogger(BindingGeneratorImpl.class);
109 private final Map<Module, ModuleContext> genCtx = new HashMap<>();
112 * Outer key represents the package name. Outer value represents map of all
113 * builders in the same package. Inner key represents the schema node name
114 * (in JAVA class/interface name format). Inner value represents instance of
115 * builder for schema node specified in key part.
117 private Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders;
120 * Provide methods for converting YANG types to JAVA types.
122 private TypeProvider typeProvider;
125 * Holds reference to schema context to resolve data of augmented element
126 * when creating augmentation builder
128 private SchemaContext schemaContext;
131 * Constant with the concrete name of namespace.
133 private final static String YANG_EXT_NAMESPACE = "urn:opendaylight:yang:extension:yang-ext";
136 * Constant with the concrete name of identifier.
138 private final static String AUGMENT_IDENTIFIER_NAME = "augment-identifier";
141 * Resolves generated types from <code>context</code> schema nodes of all
144 * Generated types are created for modules, groupings, types, containers,
145 * lists, choices, augments, rpcs, notification, identities.
148 * schema context which contains data about all schema nodes
150 * @return list of types (usually <code>GeneratedType</code>
151 * <code>GeneratedTransferObject</code>which are generated from
152 * <code>context</code> data.
153 * @throws IllegalArgumentException
154 * if arg <code>context</code> is null
155 * @throws IllegalStateException
156 * if <code>context</code> contain no modules
159 public List<Type> generateTypes(final SchemaContext context) {
160 checkArgument(context != null, "Schema Context reference cannot be NULL.");
161 checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
162 schemaContext = context;
163 typeProvider = new TypeProviderImpl(context);
164 final Set<Module> modules = context.getModules();
165 return generateTypes(context, modules);
169 * Resolves generated types from <code>context</code> schema nodes only for
170 * modules specified in <code>modules</code>
172 * Generated types are created for modules, groupings, types, containers,
173 * lists, choices, augments, rpcs, notification, identities.
176 * schema context which contains data about all schema nodes
179 * set of modules for which schema nodes should be generated
181 * @return list of types (usually <code>GeneratedType</code> or
182 * <code>GeneratedTransferObject</code>) which:
184 * <li>are generated from <code>context</code> schema nodes and</li>
185 * <li>are also part of some of the module in <code>modules</code>
188 * @throws IllegalArgumentException
190 * <li>if arg <code>context</code> is null or</li>
191 * <li>if arg <code>modules</code> is null</li>
193 * @throws IllegalStateException
194 * if <code>context</code> contain no modules
197 public List<Type> generateTypes(final SchemaContext context, final Set<Module> modules) {
198 checkArgument(context != null, "Schema Context reference cannot be NULL.");
199 checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
200 checkArgument(modules != null, "Set of Modules cannot be NULL.");
202 schemaContext = context;
203 typeProvider = new TypeProviderImpl(context);
204 Module[] modulesArray = new Module[context.getModules().size()];
205 context.getModules().toArray(modulesArray);
206 final List<Module> contextModules = ModuleDependencySort.sort(modulesArray);
207 genTypeBuilders = new HashMap<>();
209 for (Module contextModule : contextModules) {
210 moduleToGenTypes(contextModule, context);
212 for (Module contextModule : contextModules) {
213 allAugmentsToGenTypes(contextModule);
216 final List<Type> filteredGenTypes = new ArrayList<>();
217 for (Module m : modules) {
218 final ModuleContext ctx = checkNotNull(genCtx.get(m), "Module context not found for module %s", m);
219 filteredGenTypes.addAll(ctx.getGeneratedTypes());
220 final Set<Type> additionalTypes = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(m);
221 if (additionalTypes != null) {
222 filteredGenTypes.addAll(additionalTypes);
226 return filteredGenTypes;
229 private void moduleToGenTypes(final Module m, final SchemaContext context) {
230 genCtx.put(m, new ModuleContext());
231 allTypeDefinitionsToGenTypes(m);
232 groupingsToGenTypes(m, m.getGroupings());
233 rpcMethodsToGenType(m);
234 allIdentitiesToGenTypes(m, context);
235 notificationsToGenType(m);
237 if (!m.getChildNodes().isEmpty()) {
238 final GeneratedTypeBuilder moduleType = moduleToDataType(m);
239 genCtx.get(m).addModuleNode(moduleType);
240 final String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
241 resolveDataSchemaNodes(m, basePackageName, moduleType, moduleType, m.getChildNodes());
246 * Converts all extended type definitions of module to the list of
247 * <code>Type</code> objects.
250 * module from which is obtained set of type definitions
251 * @throws IllegalArgumentException
253 * <li>if module is null</li>
254 * <li>if name of module is null</li>
256 * @throws IllegalStateException
257 * if set of type definitions from module is null
259 private void allTypeDefinitionsToGenTypes(final Module module) {
260 checkArgument(module != null, "Module reference cannot be NULL.");
261 checkArgument(module.getName() != null, "Module name cannot be NULL.");
262 final DataNodeIterator it = new DataNodeIterator(module);
263 final List<TypeDefinition<?>> typeDefinitions = it.allTypedefs();
264 checkState(typeDefinitions != null, "Type Definitions for module «module.name» cannot be NULL.");
266 for (TypeDefinition<?> typedef : typeDefinitions) {
267 if (typedef != null) {
268 final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef,
271 genCtx.get(module).addTypedefType(typedef.getPath(), type);
277 private GeneratedTypeBuilder processDataSchemaNode(final Module module, final String basePackageName,
278 final GeneratedTypeBuilder childOf, final DataSchemaNode node) {
279 if (node.isAugmenting() || node.isAddedByUses()) {
282 final String packageName = packageNameForGeneratedType(basePackageName, node.getPath());
283 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, node, childOf);
284 genType.addComment(node.getDescription());
285 if (node instanceof DataNodeContainer) {
286 genCtx.get(module).addChildNodeType(node.getPath(), genType);
287 groupingsToGenTypes(module, ((DataNodeContainer) node).getGroupings());
288 processUsesAugments((DataNodeContainer) node, module);
293 private void containerToGenType(final Module module, final String basePackageName, final GeneratedTypeBuilder parent,
294 final GeneratedTypeBuilder childOf, final ContainerSchemaNode node) {
295 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node);
296 if (genType != null) {
297 constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), genType);
298 resolveDataSchemaNodes(module, basePackageName, genType, genType, node.getChildNodes());
302 private void listToGenType(final Module module, final String basePackageName, final GeneratedTypeBuilder parent,
303 final GeneratedTypeBuilder childOf, final ListSchemaNode node) {
304 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node);
305 if (genType != null) {
306 constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), Types.listTypeFor(genType));
308 final List<String> listKeys = listKeys(node);
309 final String packageName = packageNameForGeneratedType(basePackageName, node.getPath());
310 final GeneratedTOBuilder genTOBuilder = resolveListKeyTOBuilder(packageName, node);
311 if (genTOBuilder != null) {
312 final Type identifierMarker = Types.parameterizedTypeFor(IDENTIFIER, genType);
313 final Type identifiableMarker = Types.parameterizedTypeFor(IDENTIFIABLE, genTOBuilder);
314 genTOBuilder.addImplementsType(identifierMarker);
315 genType.addImplementsType(identifiableMarker);
318 for (DataSchemaNode schemaNode : node.getChildNodes()) {
319 if (!schemaNode.isAugmenting()) {
320 addSchemaNodeToListBuilders(basePackageName, schemaNode, genType, genTOBuilder, listKeys, module);
325 if (genTOBuilder != null) {
326 final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
327 prop.setValue(Long.toString(computeDefaultSUID(genTOBuilder)));
328 genTOBuilder.setSUID(prop);
331 typeBuildersToGenTypes(module, genType, genTOBuilder);
335 private void processUsesAugments(final DataNodeContainer node, final Module module) {
336 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
337 for (UsesNode usesNode : node.getUses()) {
338 for (AugmentationSchema augment : usesNode.getAugmentations()) {
339 usesAugmentationToGenTypes(basePackageName, augment, module, usesNode, node);
340 processUsesAugments(augment, module);
346 * Converts all <b>augmentation</b> of the module to the list
347 * <code>Type</code> objects.
350 * module from which is obtained list of all augmentation objects
351 * to iterate over them
352 * @throws IllegalArgumentException
354 * <li>if the module is null</li>
355 * <li>if the name of module is null</li>
357 * @throws IllegalStateException
358 * if set of augmentations from module is null
360 private void allAugmentsToGenTypes(final Module module) {
361 checkArgument(module != null, "Module reference cannot be NULL.");
362 checkArgument(module.getName() != null, "Module name cannot be NULL.");
363 checkState(module.getAugmentations() != null, "Augmentations Set cannot be NULL.");
365 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
366 final List<AugmentationSchema> augmentations = resolveAugmentations(module);
367 for (AugmentationSchema augment : augmentations) {
368 augmentationToGenTypes(basePackageName, augment, module);
373 * Returns list of <code>AugmentationSchema</code> objects. The objects are
374 * sorted according to the length of their target path from the shortest to
378 * module from which is obtained list of all augmentation objects
379 * @return list of sorted <code>AugmentationSchema</code> objects obtained
380 * from <code>module</code>
381 * @throws IllegalArgumentException
383 * @throws IllegalStateException
384 * if set of module augmentations is null
386 private List<AugmentationSchema> resolveAugmentations(final Module module) {
387 checkArgument(module != null, "Module reference cannot be NULL.");
388 checkState(module.getAugmentations() != null, "Augmentations Set cannot be NULL.");
390 final Set<AugmentationSchema> augmentations = module.getAugmentations();
391 List<AugmentationSchema> sortedAugmentations = new ArrayList<>(augmentations);
392 Collections.sort(sortedAugmentations, Comparators.AUGMENT_COMP);
394 return sortedAugmentations;
398 * Create GeneratedTypeBuilder object from module argument.
401 * Module object from which builder will be created
402 * @return <code>GeneratedTypeBuilder</code> which is internal
403 * representation of the module
404 * @throws IllegalArgumentException
407 private GeneratedTypeBuilder moduleToDataType(final Module module) {
408 checkArgument(module != null, "Module reference cannot be NULL.");
410 final GeneratedTypeBuilder moduleDataTypeBuilder = moduleTypeBuilder(module, "Data");
411 addImplementedInterfaceFromUses(module, moduleDataTypeBuilder);
412 moduleDataTypeBuilder.addImplementsType(DATA_ROOT);
413 moduleDataTypeBuilder.addComment(module.getDescription());
414 return moduleDataTypeBuilder;
418 * Converts all <b>rpcs</b> inputs and outputs substatements of the module
419 * to the list of <code>Type</code> objects. In addition are to containers
420 * and lists which belong to input or output also part of returning list.
423 * module from which is obtained set of all rpc objects to
425 * @throws IllegalArgumentException
427 * <li>if the module is null</li>
428 * <li>if the name of module is null</li>
430 * @throws IllegalStateException
431 * if set of rpcs from module is null
433 private void rpcMethodsToGenType(final Module module) {
434 checkArgument(module != null, "Module reference cannot be NULL.");
435 checkArgument(module.getName() != null, "Module name cannot be NULL.");
436 final Set<RpcDefinition> rpcDefinitions = module.getRpcs();
437 checkState(rpcDefinitions != null, "Set of rpcs from module " + module.getName() + " cannot be NULL.");
438 if (rpcDefinitions.isEmpty()) {
442 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
443 final GeneratedTypeBuilder interfaceBuilder = moduleTypeBuilder(module, "Service");
444 interfaceBuilder.addImplementsType(Types.typeForClass(RpcService.class));
445 for (RpcDefinition rpc : rpcDefinitions) {
447 final String rpcName = BindingMapping.getClassName(rpc.getQName());
448 final String rpcMethodName = parseToValidParamName(rpcName);
449 final String rpcComment = rpc.getDescription();
450 final MethodSignatureBuilder method = interfaceBuilder.addMethod(rpcMethodName);
451 final ContainerSchemaNode input = rpc.getInput();
452 final ContainerSchemaNode output = rpc.getOutput();
455 final GeneratedTypeBuilder inType = addRawInterfaceDefinition(basePackageName, input, rpcName);
456 addImplementedInterfaceFromUses(input, inType);
457 inType.addImplementsType(DATA_OBJECT);
458 inType.addImplementsType(augmentable(inType));
459 resolveDataSchemaNodes(module, basePackageName, inType, inType, input.getChildNodes());
460 genCtx.get(module).addChildNodeType(input.getPath(), inType);
461 final GeneratedType inTypeInstance = inType.toInstance();
462 method.addParameter(inTypeInstance, "input");
465 Type outTypeInstance = VOID;
466 if (output != null) {
467 final GeneratedTypeBuilder outType = addRawInterfaceDefinition(basePackageName, output, rpcName);
468 addImplementedInterfaceFromUses(output, outType);
469 outType.addImplementsType(DATA_OBJECT);
470 outType.addImplementsType(augmentable(outType));
471 resolveDataSchemaNodes(module, basePackageName, outType, outType, output.getChildNodes());
472 genCtx.get(module).addChildNodeType(output.getPath(), outType);
473 outTypeInstance = outType.toInstance();
476 final Type rpcRes = Types.parameterizedTypeFor(Types.typeForClass(RpcResult.class), outTypeInstance);
477 method.setComment(rpcComment);
478 method.setReturnType(Types.parameterizedTypeFor(FUTURE, rpcRes));
482 genCtx.get(module).addTopLevelNodeType(interfaceBuilder);
486 * Converts all <b>notifications</b> of the module to the list of
487 * <code>Type</code> objects. In addition are to this list added containers
488 * and lists which are part of this notification.
491 * module from which is obtained set of all notification objects
492 * to iterate over them
493 * @throws IllegalArgumentException
495 * <li>if the module equals null</li>
496 * <li>if the name of module equals null</li>
498 * @throws IllegalStateException
499 * if set of notifications from module is null
501 private void notificationsToGenType(final Module module) {
502 checkArgument(module != null, "Module reference cannot be NULL.");
503 checkArgument(module.getName() != null, "Module name cannot be NULL.");
504 final Set<NotificationDefinition> notifications = module.getNotifications();
505 checkState(notifications != null, "Set of notification from module " + module.getName() + " cannot be NULL.");
506 if (notifications.isEmpty()) {
510 final GeneratedTypeBuilder listenerInterface = moduleTypeBuilder(module, "Listener");
511 listenerInterface.addImplementsType(BindingTypes.NOTIFICATION_LISTENER);
512 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
514 for (NotificationDefinition notification : notifications) {
515 if (notification != null) {
516 processUsesAugments(notification, module);
518 final GeneratedTypeBuilder notificationInterface = addDefaultInterfaceDefinition(basePackageName,
519 notification, BindingTypes.DATA_OBJECT);
520 notificationInterface.addImplementsType(NOTIFICATION);
521 genCtx.get(module).addChildNodeType(notification.getPath(), notificationInterface);
523 // Notification object
524 resolveDataSchemaNodes(module, basePackageName, notificationInterface, notificationInterface,
525 notification.getChildNodes());
527 listenerInterface.addMethod("on" + notificationInterface.getName())
528 .setAccessModifier(AccessModifier.PUBLIC).addParameter(notificationInterface, "notification")
529 .setComment(notification.getDescription()).setReturnType(Types.VOID);
533 genCtx.get(module).addTopLevelNodeType(listenerInterface);
537 * Converts all <b>identities</b> of the module to the list of
538 * <code>Type</code> objects.
541 * module from which is obtained set of all identity objects to
544 * schema context only used as input parameter for method
545 * {@link identityToGenType}
548 private void allIdentitiesToGenTypes(final Module module, final SchemaContext context) {
549 final Set<IdentitySchemaNode> schemaIdentities = module.getIdentities();
550 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
552 if (schemaIdentities != null && !schemaIdentities.isEmpty()) {
553 for (IdentitySchemaNode identity : schemaIdentities) {
554 identityToGenType(module, basePackageName, identity, context);
560 * Converts the <b>identity</b> object to GeneratedType. Firstly it is
561 * created transport object builder. If identity contains base identity then
562 * reference to base identity is added to superior identity as its extend.
563 * If identity doesn't contain base identity then only reference to abstract
564 * class {@link org.opendaylight.yangtools.yang.model.api.BaseIdentity
565 * BaseIdentity} is added
569 * @param basePackageName
570 * string contains the module package name
572 * IdentitySchemaNode which contains data about identity
574 * SchemaContext which is used to get package and name
575 * information about base of identity
578 private void identityToGenType(final Module module, final String basePackageName, final IdentitySchemaNode identity,
579 final SchemaContext context) {
580 if (identity == null) {
583 final String packageName = packageNameForGeneratedType(basePackageName, identity.getPath());
584 final String genTypeName = BindingMapping.getClassName(identity.getQName());
585 final GeneratedTOBuilder newType = new GeneratedTOBuilderImpl(packageName, genTypeName);
586 final IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
587 if (baseIdentity == null) {
588 final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(BaseIdentity.class.getPackage().getName(),
589 BaseIdentity.class.getSimpleName());
590 newType.setExtendsType(gto.toInstance());
592 final Module baseIdentityParentModule = SchemaContextUtil.findParentModule(context, baseIdentity);
593 final String returnTypePkgName = BindingMapping.getRootPackageName(baseIdentityParentModule.getQNameModule());
594 final String returnTypeName = BindingMapping.getClassName(baseIdentity.getQName());
595 final GeneratedTransferObject gto = new GeneratedTOBuilderImpl(returnTypePkgName, returnTypeName)
597 newType.setExtendsType(gto);
599 newType.setAbstract(true);
600 newType.addComment(identity.getDescription());
601 final QName qname = identity.getQName();
602 qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, qname);
604 genCtx.get(module).addIdentityType(identity.getQName(), newType);
607 private static Constant qnameConstant(final GeneratedTypeBuilderBase<?> toBuilder, final String constantName, final QName name) {
608 StringBuilder sb = new StringBuilder("org.opendaylight.yangtools.yang.common.QName");
609 sb.append(".create(");
611 sb.append(name.getNamespace());
613 sb.append(name.getFormattedRevision());
615 sb.append(name.getLocalName());
618 return toBuilder.addConstant(typeForClass(QName.class), constantName, sb.toString());
622 * Converts all <b>groupings</b> of the module to the list of
623 * <code>Type</code> objects. Firstly are groupings sorted according mutual
624 * dependencies. At least dependent (independent) groupings are in the list
625 * saved at first positions. For every grouping the record is added to map
626 * {@link BindingGeneratorImpl#allGroupings allGroupings}
631 * of groupings from which types will be generated
634 private void groupingsToGenTypes(final Module module, final Collection<GroupingDefinition> groupings) {
635 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
636 final List<GroupingDefinition> groupingsSortedByDependencies = new GroupingDefinitionDependencySort()
638 for (GroupingDefinition grouping : groupingsSortedByDependencies) {
639 groupingToGenType(basePackageName, grouping, module);
644 * Converts individual grouping to GeneratedType. Firstly generated type
645 * builder is created and every child node of grouping is resolved to the
648 * @param basePackageName
649 * string contains the module package name
651 * GroupingDefinition which contains data about grouping
654 * @return GeneratedType which is generated from grouping (object of type
655 * <code>GroupingDefinition</code>)
657 private void groupingToGenType(final String basePackageName, final GroupingDefinition grouping, final Module module) {
658 final String packageName = packageNameForGeneratedType(basePackageName, grouping.getPath());
659 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, grouping);
660 genCtx.get(module).addGroupingType(grouping.getPath(), genType);
661 resolveDataSchemaNodes(module, basePackageName, genType, genType, grouping.getChildNodes());
662 groupingsToGenTypes(module, grouping.getGroupings());
663 processUsesAugments(grouping, module);
667 * Adds enumeration builder created from <code>enumTypeDef</code> to
668 * <code>typeBuilder</code>.
670 * Each <code>enumTypeDef</code> item is added to builder with its name and
674 * EnumTypeDefinition contains enum data
676 * string contains name which will be assigned to enumeration
679 * GeneratedTypeBuilder to which will be enum builder assigned
680 * @return enumeration builder which contains data from
681 * <code>enumTypeDef</code>
683 private EnumBuilder resolveInnerEnumFromTypeDefinition(final EnumTypeDefinition enumTypeDef, final QName enumName,
684 final GeneratedTypeBuilder typeBuilder) {
685 if ((enumTypeDef != null) && (typeBuilder != null) && (enumTypeDef.getQName() != null)
686 && (enumTypeDef.getQName().getLocalName() != null)) {
687 final String enumerationName = BindingMapping.getClassName(enumName);
688 final EnumBuilder enumBuilder = typeBuilder.addEnumeration(enumerationName);
689 enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
696 * Generates type builder for <code>module</code>.
699 * Module which is source of package name for generated type
702 * string which is added to the module class name representation
704 * @return instance of GeneratedTypeBuilder which represents
705 * <code>module</code>.
706 * @throws IllegalArgumentException
707 * if <code>module</code> is null
709 private GeneratedTypeBuilder moduleTypeBuilder(final Module module, final String postfix) {
710 checkArgument(module != null, "Module reference cannot be NULL.");
711 final String packageName = BindingMapping.getRootPackageName(module.getQNameModule());
712 final String moduleName = BindingMapping.getClassName(module.getName()) + postfix;
713 return new GeneratedTypeBuilderImpl(packageName, moduleName);
717 * Converts <code>augSchema</code> to list of <code>Type</code> which
718 * contains generated type for augmentation. In addition there are also
719 * generated types for all containers, list and choices which are child of
720 * <code>augSchema</code> node or a generated types for cases are added if
721 * augmented node is choice.
723 * @param augmentPackageName
724 * string with the name of the package to which the augmentation
727 * AugmentationSchema which is contains data about augmentation
728 * (target path, childs...)
731 * @param parentUsesNode
732 * @throws IllegalArgumentException
734 * <li>if <code>augmentPackageName</code> equals null</li>
735 * <li>if <code>augSchema</code> equals null</li>
737 * @throws IllegalStateException
738 * if augment target path is null
740 private void augmentationToGenTypes(final String augmentPackageName, final AugmentationSchema augSchema, final Module module) {
741 checkArgument(augmentPackageName != null, "Package Name cannot be NULL.");
742 checkArgument(augSchema != null, "Augmentation Schema cannot be NULL.");
743 checkState(augSchema.getTargetPath() != null,
744 "Augmentation Schema does not contain Target Path (Target Path is NULL).");
746 processUsesAugments(augSchema, module);
747 final SchemaPath targetPath = augSchema.getTargetPath();
748 SchemaNode targetSchemaNode = null;
750 targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
751 if (targetSchemaNode instanceof DataSchemaNode && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
752 if (targetSchemaNode instanceof DerivableSchemaNode) {
753 targetSchemaNode = ((DerivableSchemaNode) targetSchemaNode).getOriginal().orNull();
755 if (targetSchemaNode == null) {
756 throw new IllegalStateException("Failed to find target node from grouping in augmentation " + augSchema
757 + " in module " + module.getName());
760 if (targetSchemaNode == null) {
761 throw new IllegalArgumentException("augment target not found: " + targetPath);
764 GeneratedTypeBuilder targetTypeBuilder = findChildNodeByPath(targetSchemaNode.getPath());
765 if (targetTypeBuilder == null) {
766 targetTypeBuilder = findCaseByPath(targetSchemaNode.getPath());
768 if (targetTypeBuilder == null) {
769 throw new NullPointerException("Target type not yet generated: " + targetSchemaNode);
772 if (!(targetSchemaNode instanceof ChoiceNode)) {
773 String packageName = augmentPackageName;
774 final Type targetType = new ReferencedTypeImpl(targetTypeBuilder.getPackageName(),
775 targetTypeBuilder.getName());
776 addRawAugmentGenTypeDefinition(module, packageName, augmentPackageName, targetType, augSchema);
779 generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance(),
780 (ChoiceNode) targetSchemaNode, augSchema.getChildNodes());
784 private void usesAugmentationToGenTypes(final String augmentPackageName, final AugmentationSchema augSchema, final Module module,
785 final UsesNode usesNode, final DataNodeContainer usesNodeParent) {
786 checkArgument(augmentPackageName != null, "Package Name cannot be NULL.");
787 checkArgument(augSchema != null, "Augmentation Schema cannot be NULL.");
788 checkState(augSchema.getTargetPath() != null,
789 "Augmentation Schema does not contain Target Path (Target Path is NULL).");
791 processUsesAugments(augSchema, module);
792 final SchemaPath targetPath = augSchema.getTargetPath();
793 SchemaNode targetSchemaNode = findOriginalTargetFromGrouping(targetPath, usesNode);
794 if (targetSchemaNode == null) {
795 throw new IllegalArgumentException("augment target not found: " + targetPath);
798 GeneratedTypeBuilder targetTypeBuilder = findChildNodeByPath(targetSchemaNode.getPath());
799 if (targetTypeBuilder == null) {
800 targetTypeBuilder = findCaseByPath(targetSchemaNode.getPath());
802 if (targetTypeBuilder == null) {
803 throw new NullPointerException("Target type not yet generated: " + targetSchemaNode);
806 if (!(targetSchemaNode instanceof ChoiceNode)) {
807 String packageName = augmentPackageName;
808 if (usesNodeParent instanceof SchemaNode) {
809 packageName = packageNameForGeneratedType(augmentPackageName, ((SchemaNode) usesNodeParent).getPath(),
812 addRawAugmentGenTypeDefinition(module, packageName, augmentPackageName, targetTypeBuilder.toInstance(),
815 generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance(),
816 (ChoiceNode) targetSchemaNode, augSchema.getChildNodes());
821 * Convenient method to find node added by uses statement.
825 * @param parentUsesNode
826 * parent of uses node
827 * @return node from its original location in grouping
829 private DataSchemaNode findOriginalTargetFromGrouping(final SchemaPath targetPath, final UsesNode parentUsesNode) {
830 SchemaNode targetGrouping = findNodeInSchemaContext(schemaContext, parentUsesNode.getGroupingPath()
832 if (!(targetGrouping instanceof GroupingDefinition)) {
833 throw new IllegalArgumentException("Failed to generate code for augment in " + parentUsesNode);
836 GroupingDefinition grouping = (GroupingDefinition) targetGrouping;
837 SchemaNode result = grouping;
838 for (QName node : targetPath.getPathFromRoot()) {
839 // finding by local name is valid, grouping cannot contain nodes
840 // with same name and different namespace
841 if (result instanceof DataNodeContainer) {
842 result = ((DataNodeContainer) result).getDataChildByName(node.getLocalName());
843 } else if (result instanceof ChoiceNode) {
844 result = ((ChoiceNode) result).getCaseNodeByName(node.getLocalName());
847 if (result == null) {
851 boolean fromUses = ((DataSchemaNode) result).isAddedByUses();
852 Iterator<UsesNode> groupingUses = grouping.getUses().iterator();
853 while (groupingUses.hasNext() && fromUses) {
854 result = findOriginalTargetFromGrouping(targetPath, groupingUses.next());
855 if (result != null) {
856 fromUses = ((DataSchemaNode) result).isAddedByUses();
860 // this indicates invalid yang and thus possible bug in code because
861 // invalid yang should be already spotted by parser
862 throw new IllegalStateException("Failed to generate code for augment in " + parentUsesNode);
865 return (DataSchemaNode) result;
869 * Returns a generated type builder for an augmentation.
871 * The name of the type builder is equal to the name of augmented node with
872 * serial number as suffix.
876 * @param augmentPackageName
877 * string with contains the package name to which the augment
879 * @param basePackageName
880 * string with the package name to which the augmented node
882 * @param targetTypeRef
885 * augmentation schema which contains data about the child nodes
886 * and uses of augment
887 * @return generated type builder for augment
889 private GeneratedTypeBuilder addRawAugmentGenTypeDefinition(final Module module, final String augmentPackageName,
890 final String basePackageName, final Type targetTypeRef, final AugmentationSchema augSchema) {
891 Map<String, GeneratedTypeBuilder> augmentBuilders = genTypeBuilders.get(augmentPackageName);
892 if (augmentBuilders == null) {
893 augmentBuilders = new HashMap<>();
894 genTypeBuilders.put(augmentPackageName, augmentBuilders);
896 final String augIdentifier = getAugmentIdentifier(augSchema.getUnknownSchemaNodes());
899 if (augIdentifier != null) {
900 augTypeName = BindingMapping.getClassName(augIdentifier);
902 augTypeName = augGenTypeName(augmentBuilders, targetTypeRef.getName());
905 final GeneratedTypeBuilder augTypeBuilder = new GeneratedTypeBuilderImpl(augmentPackageName, augTypeName);
907 augTypeBuilder.addImplementsType(DATA_OBJECT);
908 augTypeBuilder.addImplementsType(Types.augmentationTypeFor(targetTypeRef));
909 addImplementedInterfaceFromUses(augSchema, augTypeBuilder);
911 augSchemaNodeToMethods(module, basePackageName, augTypeBuilder, augTypeBuilder, augSchema.getChildNodes());
912 augmentBuilders.put(augTypeName, augTypeBuilder);
914 genCtx.get(module).addTargetToAugmentation(targetTypeRef, augTypeBuilder);
915 genCtx.get(module).addAugmentType(augTypeBuilder);
916 genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
917 return augTypeBuilder;
922 * @param unknownSchemaNodes
923 * @return nodeParameter of UnknownSchemaNode
925 private String getAugmentIdentifier(final List<UnknownSchemaNode> unknownSchemaNodes) {
926 for (UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
927 final QName nodeType = unknownSchemaNode.getNodeType();
928 if (AUGMENT_IDENTIFIER_NAME.equals(nodeType.getLocalName())
929 && YANG_EXT_NAMESPACE.equals(nodeType.getNamespace().toString())) {
930 return unknownSchemaNode.getNodeParameter();
937 * Returns first unique name for the augment generated type builder. The
938 * generated type builder name for augment consists from name of augmented
939 * node and serial number of its augmentation.
942 * map of builders which were created in the package to which the
943 * augmentation belongs
945 * string with name of augmented node
946 * @return string with unique name for augmentation builder
948 private String augGenTypeName(final Map<String, GeneratedTypeBuilder> builders, final String genTypeName) {
950 if (builders != null) {
951 while (builders.containsKey(genTypeName + index)) {
955 return genTypeName + index;
959 * Adds the methods to <code>typeBuilder</code> which represent subnodes of
960 * node for which <code>typeBuilder</code> was created.
962 * The subnodes aren't mapped to the methods if they are part of grouping or
963 * augment (in this case are already part of them).
967 * @param basePackageName
968 * string contains the module package name
970 * generated type builder which represents any node. The subnodes
971 * of this node are added to the <code>typeBuilder</code> as
972 * methods. The subnode can be of type leaf, leaf-list, list,
977 * set of data schema nodes which are the children of the node
978 * for which <code>typeBuilder</code> was created
979 * @return generated type builder which is the same builder as input
980 * parameter. The getter methods (representing child nodes) could be
983 private GeneratedTypeBuilder resolveDataSchemaNodes(final Module module, final String basePackageName,
984 final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf, final Set<DataSchemaNode> schemaNodes) {
985 if ((schemaNodes != null) && (parent != null)) {
986 for (DataSchemaNode schemaNode : schemaNodes) {
987 if (!schemaNode.isAugmenting() && !schemaNode.isAddedByUses()) {
988 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, parent, childOf, module);
996 * Adds the methods to <code>typeBuilder</code> what represents subnodes of
997 * node for which <code>typeBuilder</code> was created.
1001 * @param basePackageName
1002 * string contains the module package name
1003 * @param typeBuilder
1004 * generated type builder which represents any node. The subnodes
1005 * of this node are added to the <code>typeBuilder</code> as
1006 * methods. The subnode can be of type leaf, leaf-list, list,
1007 * container, choice.
1010 * @param schemaNodes
1011 * set of data schema nodes which are the children of the node
1012 * for which <code>typeBuilder</code> was created
1013 * @return generated type builder which is the same object as the input
1014 * parameter <code>typeBuilder</code>. The getter method could be
1017 private GeneratedTypeBuilder augSchemaNodeToMethods(final Module module, final String basePackageName,
1018 final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf, final Set<DataSchemaNode> schemaNodes) {
1019 if ((schemaNodes != null) && (typeBuilder != null)) {
1020 for (DataSchemaNode schemaNode : schemaNodes) {
1021 if (!schemaNode.isAugmenting()) {
1022 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, typeBuilder, childOf, module);
1030 * Adds to <code>typeBuilder</code> a method which is derived from
1031 * <code>schemaNode</code>.
1033 * @param basePackageName
1034 * string with the module package name
1036 * data schema node which is added to <code>typeBuilder</code> as
1038 * @param typeBuilder
1039 * generated type builder to which is <code>schemaNode</code>
1040 * added as a method.
1046 private void addSchemaNodeToBuilderAsMethod(final String basePackageName, final DataSchemaNode node,
1047 final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf, final Module module) {
1048 if (node != null && typeBuilder != null) {
1049 if (node instanceof LeafSchemaNode) {
1050 resolveLeafSchemaNodeAsMethod(typeBuilder, (LeafSchemaNode) node);
1051 } else if (node instanceof LeafListSchemaNode) {
1052 resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) node);
1053 } else if (node instanceof ContainerSchemaNode) {
1054 containerToGenType(module, basePackageName, typeBuilder, childOf, (ContainerSchemaNode) node);
1055 } else if (node instanceof ListSchemaNode) {
1056 listToGenType(module, basePackageName, typeBuilder, childOf, (ListSchemaNode) node);
1057 } else if (node instanceof ChoiceNode) {
1058 choiceToGeneratedType(module, basePackageName, typeBuilder, (ChoiceNode) node);
1060 // TODO: anyxml not yet supported
1061 LOG.debug("Unable to add schema node {} as method in {}: unsupported type of node.", node.getClass(),
1062 typeBuilder.getFullyQualifiedName());
1068 * Converts <code>choiceNode</code> to the list of generated types for
1069 * choice and its cases.
1071 * The package names for choice and for its cases are created as
1072 * concatenation of the module package (<code>basePackageName</code>) and
1073 * names of all parents node.
1077 * @param basePackageName
1078 * string with the module package name
1082 * choice node which is mapped to generated type. Also child
1083 * nodes - cases are mapped to generated types.
1084 * @throws IllegalArgumentException
1086 * <li>if <code>basePackageName</code> is null</li>
1087 * <li>if <code>choiceNode</code> is null</li>
1090 private void choiceToGeneratedType(final Module module, final String basePackageName, final GeneratedTypeBuilder parent,
1091 final ChoiceNode choiceNode) {
1092 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
1093 checkArgument(choiceNode != null, "Choice Schema Node cannot be NULL.");
1095 if (!choiceNode.isAddedByUses()) {
1096 final String packageName = packageNameForGeneratedType(basePackageName, choiceNode.getPath());
1097 final GeneratedTypeBuilder choiceTypeBuilder = addRawInterfaceDefinition(packageName, choiceNode);
1098 constructGetter(parent, choiceNode.getQName().getLocalName(), choiceNode.getDescription(),
1100 choiceTypeBuilder.addImplementsType(typeForClass(DataContainer.class));
1101 genCtx.get(module).addChildNodeType(choiceNode.getPath(), choiceTypeBuilder);
1102 generateTypesFromChoiceCases(module, basePackageName, choiceTypeBuilder.toInstance(), choiceNode);
1107 * Converts <code>caseNodes</code> set to list of corresponding generated
1110 * For every <i>case</i> which isn't added through augment or <i>uses</i> is
1111 * created generated type builder. The package names for the builder is
1112 * created as concatenation of the module package (
1113 * <code>basePackageName</code>) and names of all parents nodes of the
1114 * concrete <i>case</i>. There is also relation "<i>implements type</i>"
1115 * between every case builder and <i>choice</i> type
1119 * @param basePackageName
1120 * string with the module package name
1121 * @param refChoiceType
1122 * type which represents superior <i>case</i>
1124 * choice case node which is mapped to generated type
1125 * @return list of generated types for <code>caseNodes</code>.
1126 * @throws IllegalArgumentException
1128 * <li>if <code>basePackageName</code> equals null</li>
1129 * <li>if <code>refChoiceType</code> equals null</li>
1130 * <li>if <code>caseNodes</code> equals null</li>
1133 private void generateTypesFromChoiceCases(final Module module, final String basePackageName, final Type refChoiceType,
1134 final ChoiceNode choiceNode) {
1135 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
1136 checkArgument(refChoiceType != null, "Referenced Choice Type cannot be NULL.");
1137 checkArgument(choiceNode != null, "ChoiceNode cannot be NULL.");
1139 final Set<ChoiceCaseNode> caseNodes = choiceNode.getCases();
1140 if (caseNodes == null) {
1144 for (ChoiceCaseNode caseNode : caseNodes) {
1145 if (caseNode != null && !caseNode.isAddedByUses() && !caseNode.isAugmenting()) {
1146 final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath());
1147 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode);
1148 caseTypeBuilder.addImplementsType(refChoiceType);
1149 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
1150 genCtx.get(module).addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode);
1151 final Set<DataSchemaNode> caseChildNodes = caseNode.getChildNodes();
1152 if (caseChildNodes != null) {
1153 Object parentNode = null;
1154 final SchemaPath nodeSp = choiceNode.getPath();
1155 parentNode = findDataSchemaNode(schemaContext, nodeSp.getParent());
1158 if (parentNode instanceof AugmentationSchema) {
1159 final AugmentationSchema augSchema = (AugmentationSchema) parentNode;
1160 final SchemaPath targetPath = augSchema.getTargetPath();
1161 SchemaNode targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
1162 if (targetSchemaNode instanceof DataSchemaNode
1163 && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
1164 if (targetSchemaNode instanceof DerivableSchemaNode) {
1165 targetSchemaNode = ((DerivableSchemaNode) targetSchemaNode).getOriginal().orNull();
1167 if (targetSchemaNode == null) {
1168 throw new IllegalStateException(
1169 "Failed to find target node from grouping for augmentation " + augSchema
1170 + " in module " + module.getName());
1173 parent = targetSchemaNode;
1175 final SchemaPath sp = choiceNode.getPath();
1176 parent = findDataSchemaNode(schemaContext, sp.getParent());
1178 GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.getPath());
1179 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, caseChildNodes);
1183 processUsesAugments(caseNode, module);
1188 * Generates list of generated types for all the cases of a choice which are
1189 * added to the choice through the augment.
1193 * @param basePackageName
1194 * string contains name of package to which augment belongs. If
1195 * an augmented choice is from an other package (pcg1) than an
1196 * augmenting choice (pcg2) then case's of the augmenting choice
1197 * will belong to pcg2.
1199 * Type which represents target choice
1201 * node which represents target choice
1202 * @param augmentedNodes
1203 * set of choice case nodes for which is checked if are/aren't
1204 * added to choice through augmentation
1205 * @return list of generated types which represents augmented cases of
1206 * choice <code>refChoiceType</code>
1207 * @throws IllegalArgumentException
1209 * <li>if <code>basePackageName</code> is null</li>
1210 * <li>if <code>targetType</code> is null</li>
1211 * <li>if <code>augmentedNodes</code> is null</li>
1214 private void generateTypesFromAugmentedChoiceCases(final Module module, final String basePackageName, final Type targetType,
1215 final ChoiceNode targetNode, final Set<DataSchemaNode> augmentedNodes) {
1216 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
1217 checkArgument(targetType != null, "Referenced Choice Type cannot be NULL.");
1218 checkArgument(augmentedNodes != null, "Set of Choice Case Nodes cannot be NULL.");
1220 for (DataSchemaNode caseNode : augmentedNodes) {
1221 if (caseNode != null) {
1222 final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath());
1223 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode);
1224 caseTypeBuilder.addImplementsType(targetType);
1226 SchemaNode parent = null;
1227 final SchemaPath nodeSp = targetNode.getPath();
1228 parent = findDataSchemaNode(schemaContext, nodeSp.getParent());
1230 GeneratedTypeBuilder childOfType = null;
1231 if (parent instanceof Module) {
1232 childOfType = genCtx.get(parent).getModuleNode();
1233 } else if (parent instanceof ChoiceCaseNode) {
1234 childOfType = findCaseByPath(parent.getPath());
1235 } else if (parent instanceof DataSchemaNode || parent instanceof NotificationDefinition) {
1236 childOfType = findChildNodeByPath(parent.getPath());
1237 } else if (parent instanceof GroupingDefinition) {
1238 childOfType = findGroupingByPath(parent.getPath());
1241 if (childOfType == null) {
1242 throw new IllegalArgumentException("Failed to find parent type of choice " + targetNode);
1245 ChoiceCaseNode node = null;
1246 if (caseNode instanceof ChoiceCaseNode) {
1247 node = (ChoiceCaseNode) caseNode;
1249 node = targetNode.getCaseNodeByName(caseNode.getQName().getLocalName());
1251 final Set<DataSchemaNode> childNodes = node.getChildNodes();
1252 if (childNodes != null) {
1253 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, childNodes);
1255 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
1256 genCtx.get(module).addChoiceToCaseMapping(targetType, caseTypeBuilder, node);
1262 * Converts <code>leaf</code> to the getter method which is added to
1263 * <code>typeBuilder</code>.
1265 * @param typeBuilder
1266 * generated type builder to which is added getter method as
1267 * <code>leaf</code> mapping
1269 * leaf schema node which is mapped as getter method which is
1270 * added to <code>typeBuilder</code>
1271 * @return boolean value
1273 * <li>false - if <code>leaf</code> or <code>typeBuilder</code> are
1275 * <li>true - in other cases</li>
1278 private Type resolveLeafSchemaNodeAsMethod(final GeneratedTypeBuilder typeBuilder, final LeafSchemaNode leaf) {
1279 Type returnType = null;
1280 if ((leaf != null) && (typeBuilder != null)) {
1281 final String leafName = leaf.getQName().getLocalName();
1282 String leafDesc = leaf.getDescription();
1283 if (leafDesc == null) {
1287 final Module parentModule = findParentModule(schemaContext, leaf);
1288 if (leafName != null && !leaf.isAddedByUses()) {
1289 final TypeDefinition<?> typeDef = leaf.getType();
1291 GeneratedTOBuilder genTOBuilder;
1292 if (typeDef instanceof EnumTypeDefinition) {
1293 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1294 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
1295 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(),
1298 if (enumBuilder != null) {
1299 returnType = enumBuilder.toInstance(typeBuilder);
1301 ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
1302 } else if (typeDef instanceof UnionType) {
1303 genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf);
1304 if (genTOBuilder != null) {
1305 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
1307 } else if (typeDef instanceof BitsTypeDefinition) {
1308 genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf);
1309 if (genTOBuilder != null) {
1310 returnType = new ReferencedTypeImpl(genTOBuilder.getPackageName(), genTOBuilder.getName());
1313 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
1314 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
1316 if (returnType != null) {
1317 final MethodSignatureBuilder getter = constructGetter(typeBuilder, leafName, leafDesc, returnType);
1318 processContextRefExtension(leaf, getter, parentModule);
1325 private void processContextRefExtension(final LeafSchemaNode leaf, final MethodSignatureBuilder getter, final Module module) {
1326 for (UnknownSchemaNode node : leaf.getUnknownSchemaNodes()) {
1327 final QName nodeType = node.getNodeType();
1328 if ("context-reference".equals(nodeType.getLocalName())) {
1329 final String nodeParam = node.getNodeParameter();
1330 IdentitySchemaNode identity = null;
1331 String basePackageName = null;
1332 final Iterable<String> splittedElement = Splitter.on(':').split(nodeParam);
1333 final Iterator<String> iterator = splittedElement.iterator();
1334 final int length = Iterables.size(splittedElement);
1336 identity = findIdentityByName(module.getIdentities(), iterator.next());
1337 basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
1338 } else if (length == 2) {
1339 String prefix = iterator.next();
1340 final Module dependentModule = findModuleFromImports(module.getImports(), prefix);
1341 if (dependentModule == null) {
1342 throw new IllegalArgumentException("Failed to process context-reference: unknown prefix "
1345 identity = findIdentityByName(dependentModule.getIdentities(), iterator.next());
1346 basePackageName = BindingMapping.getRootPackageName(dependentModule.getQNameModule());
1348 throw new IllegalArgumentException("Failed to process context-reference: unknown identity "
1351 if (identity == null) {
1352 throw new IllegalArgumentException("Failed to process context-reference: unknown identity "
1356 final Class<RoutingContext> clazz = RoutingContext.class;
1357 final AnnotationTypeBuilder rc = getter.addAnnotation(clazz.getPackage().getName(),
1358 clazz.getSimpleName());
1359 final String packageName = packageNameForGeneratedType(basePackageName, identity.getPath());
1360 final String genTypeName = BindingMapping.getClassName(identity.getQName().getLocalName());
1361 rc.addParameter("value", packageName + "." + genTypeName + ".class");
1366 private IdentitySchemaNode findIdentityByName(final Set<IdentitySchemaNode> identities, final String name) {
1367 for (IdentitySchemaNode id : identities) {
1368 if (id.getQName().getLocalName().equals(name)) {
1375 private Module findModuleFromImports(final Set<ModuleImport> imports, final String prefix) {
1376 for (ModuleImport imp : imports) {
1377 if (imp.getPrefix().equals(prefix)) {
1378 return schemaContext.findModuleByName(imp.getModuleName(), imp.getRevision());
1384 private boolean resolveLeafSchemaNodeAsProperty(final GeneratedTOBuilder toBuilder, final LeafSchemaNode leaf,
1385 final boolean isReadOnly, final Module module) {
1386 if ((leaf != null) && (toBuilder != null)) {
1387 final String leafName = leaf.getQName().getLocalName();
1388 String leafDesc = leaf.getDescription();
1389 if (leafDesc == null) {
1393 if (leafName != null) {
1394 Type returnType = null;
1395 final TypeDefinition<?> typeDef = leaf.getType();
1396 if (typeDef instanceof UnionTypeDefinition) {
1397 // GeneratedType for this type definition should be already
1399 QName qname = typeDef.getQName();
1400 Module unionModule = null;
1401 String prefix = qname.getPrefix();
1402 if (prefix == null || prefix.isEmpty() || prefix.equals(module.getPrefix())) {
1403 unionModule = module;
1405 unionModule = findModuleFromImports(module.getImports(), qname.getPrefix());
1407 final ModuleContext mc = genCtx.get(unionModule);
1408 returnType = mc.getTypedefs().get(typeDef.getPath());
1410 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1412 return resolveLeafSchemaNodeAsProperty(toBuilder, leaf, returnType, isReadOnly);
1419 * Converts <code>leaf</code> schema node to property of generated TO
1423 * generated TO builder to which is <code>leaf</code> added as
1426 * leaf schema node which is added to <code>toBuilder</code> as
1431 * boolean value which says if leaf property is|isn't read only
1432 * @return boolean value
1434 * <li>false - if <code>leaf</code>, <code>toBuilder</code> or leaf
1435 * name equals null or if leaf is added by <i>uses</i>.</li>
1436 * <li>true - other cases</li>
1439 private boolean resolveLeafSchemaNodeAsProperty(final GeneratedTOBuilder toBuilder, final LeafSchemaNode leaf, final Type returnType,
1440 final boolean isReadOnly) {
1441 if (returnType == null) {
1444 final String leafName = leaf.getQName().getLocalName();
1445 final String leafDesc = leaf.getDescription();
1446 final GeneratedPropertyBuilder propBuilder = toBuilder.addProperty(parseToValidParamName(leafName));
1447 propBuilder.setReadOnly(isReadOnly);
1448 propBuilder.setReturnType(returnType);
1449 propBuilder.setComment(leafDesc);
1450 toBuilder.addEqualsIdentity(propBuilder);
1451 toBuilder.addHashIdentity(propBuilder);
1452 toBuilder.addToStringProperty(propBuilder);
1457 * Converts <code>node</code> leaf list schema node to getter method of
1458 * <code>typeBuilder</code>.
1460 * @param typeBuilder
1461 * generated type builder to which is <code>node</code> added as
1464 * leaf list schema node which is added to
1465 * <code>typeBuilder</code> as getter method
1466 * @return boolean value
1468 * <li>true - if <code>node</code>, <code>typeBuilder</code>,
1469 * nodeName equal null or <code>node</code> is added by <i>uses</i></li>
1470 * <li>false - other cases</li>
1473 private boolean resolveLeafListSchemaNode(final GeneratedTypeBuilder typeBuilder, final LeafListSchemaNode node) {
1474 if ((node != null) && (typeBuilder != null)) {
1475 final QName nodeName = node.getQName();
1477 if (nodeName != null && !node.isAddedByUses()) {
1478 final TypeDefinition<?> typeDef = node.getType();
1479 final Module parentModule = findParentModule(schemaContext, node);
1481 Type returnType = null;
1482 if (typeDef instanceof EnumTypeDefinition) {
1483 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node);
1484 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
1485 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, nodeName,
1487 returnType = new ReferencedTypeImpl(enumBuilder.getPackageName(), enumBuilder.getName());
1488 ((TypeProviderImpl) typeProvider).putReferencedType(node.getPath(), returnType);
1489 } else if (typeDef instanceof UnionType) {
1490 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node);
1491 if (genTOBuilder != null) {
1492 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
1494 } else if (typeDef instanceof BitsTypeDefinition) {
1495 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node);
1496 returnType = new ReferencedTypeImpl(genTOBuilder.getPackageName(), genTOBuilder.getName());
1498 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
1499 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
1502 final ParameterizedType listType = Types.listTypeFor(returnType);
1503 constructGetter(typeBuilder, nodeName.getLocalName(), node.getDescription(), listType);
1510 private Type createReturnTypeForUnion(final GeneratedTOBuilder genTOBuilder, final TypeDefinition<?> typeDef,
1511 final GeneratedTypeBuilder typeBuilder, final Module parentModule) {
1512 final GeneratedTOBuilderImpl returnType = new GeneratedTOBuilderImpl(genTOBuilder.getPackageName(),
1513 genTOBuilder.getName());
1514 genTOBuilder.setTypedef(true);
1515 genTOBuilder.setIsUnion(true);
1516 ((TypeProviderImpl) typeProvider).addUnitsToGenTO(genTOBuilder, typeDef.getUnits());
1519 final GeneratedTOBuilder unionBuilder = new GeneratedTOBuilderImpl(typeBuilder.getPackageName(),
1520 genTOBuilder.getName() + "Builder");
1521 unionBuilder.setIsUnionBuilder(true);
1522 final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
1523 method.setReturnType(returnType);
1524 method.addParameter(Types.STRING, "defaultValue");
1525 method.setAccessModifier(AccessModifier.PUBLIC);
1526 method.setStatic(true);
1528 final Set<Type> types = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(parentModule);
1529 if (types == null) {
1530 ((TypeProviderImpl) typeProvider).getAdditionalTypes().put(parentModule,
1531 Sets.<Type> newHashSet(unionBuilder.toInstance()));
1533 types.add(unionBuilder.toInstance());
1535 return returnType.toInstance();
1538 private GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode schemaNode) {
1539 return addDefaultInterfaceDefinition(packageName, schemaNode, null);
1543 * Instantiates generated type builder with <code>packageName</code> and
1544 * <code>schemaNode</code>.
1546 * The new builder always implements
1547 * {@link org.opendaylight.yangtools.yang.binding.DataObject DataObject}.<br />
1548 * If <code>schemaNode</code> is instance of GroupingDefinition it also
1549 * implements {@link org.opendaylight.yangtools.yang.binding.Augmentable
1550 * Augmentable}.<br />
1551 * If <code>schemaNode</code> is instance of
1552 * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer
1553 * DataNodeContainer} it can also implement nodes which are specified in
1556 * @param packageName
1557 * string with the name of the package to which
1558 * <code>schemaNode</code> belongs.
1560 * schema node for which is created generated type builder
1562 * parent type (can be null)
1563 * @return generated type builder <code>schemaNode</code>
1565 private GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode schemaNode, final Type parent) {
1566 final GeneratedTypeBuilder it = addRawInterfaceDefinition(packageName, schemaNode, "");
1567 if (parent == null) {
1568 it.addImplementsType(DATA_OBJECT);
1570 it.addImplementsType(BindingTypes.childOf(parent));
1572 if (!(schemaNode instanceof GroupingDefinition)) {
1573 it.addImplementsType(augmentable(it));
1576 if (schemaNode instanceof DataNodeContainer) {
1577 addImplementedInterfaceFromUses((DataNodeContainer) schemaNode, it);
1584 * Wraps the calling of the same overloaded method.
1586 * @param packageName
1587 * string with the package name to which returning generated type
1590 * schema node which provide data about the schema node name
1591 * @return generated type builder for <code>schemaNode</code>
1593 private GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode) {
1594 return addRawInterfaceDefinition(packageName, schemaNode, "");
1598 * Returns reference to generated type builder for specified
1599 * <code>schemaNode</code> with <code>packageName</code>.
1601 * Firstly the generated type builder is searched in
1602 * {@link BindingGeneratorImpl#genTypeBuilders genTypeBuilders}. If it isn't
1603 * found it is created and added to <code>genTypeBuilders</code>.
1605 * @param packageName
1606 * string with the package name to which returning generated type
1609 * schema node which provide data about the schema node name
1611 * return type name prefix
1612 * @return generated type builder for <code>schemaNode</code>
1613 * @throws IllegalArgumentException
1615 * <li>if <code>schemaNode</code> is null</li>
1616 * <li>if <code>packageName</code> is null</li>
1617 * <li>if QName of schema node is null</li>
1618 * <li>if schemaNode name is null</li>
1622 private GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode, final String prefix) {
1623 checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
1624 checkArgument(packageName != null, "Package Name for Generated Type cannot be NULL.");
1625 checkArgument(schemaNode.getQName() != null, "QName for Data Schema Node cannot be NULL.");
1626 final String schemaNodeName = schemaNode.getQName().getLocalName();
1627 checkArgument(schemaNodeName != null, "Local Name of QName for Data Schema Node cannot be NULL.");
1630 if (prefix == null) {
1631 genTypeName = BindingMapping.getClassName(schemaNodeName);
1633 genTypeName = prefix + BindingMapping.getClassName(schemaNodeName);
1636 // FIXME: Validation of name conflict
1637 final GeneratedTypeBuilderImpl newType = new GeneratedTypeBuilderImpl(packageName, genTypeName);
1638 qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, schemaNode.getQName());
1639 newType.addComment(schemaNode.getDescription());
1640 if (!genTypeBuilders.containsKey(packageName)) {
1641 final Map<String, GeneratedTypeBuilder> builders = new HashMap<>();
1642 builders.put(genTypeName, newType);
1643 genTypeBuilders.put(packageName, builders);
1645 final Map<String, GeneratedTypeBuilder> builders = genTypeBuilders.get(packageName);
1646 if (!builders.containsKey(genTypeName)) {
1647 builders.put(genTypeName, newType);
1654 * Creates the name of the getter method name from <code>localName</code>.
1657 * string with the name of the getter method
1660 * @return string with the name of the getter method for
1661 * <code>methodName</code> in JAVA method format
1663 public static String getterMethodName(final String localName, final Type returnType) {
1664 final StringBuilder method = new StringBuilder();
1665 if (BOOLEAN.equals(returnType)) {
1666 method.append("is");
1668 method.append("get");
1670 String name = BindingMapping.toFirstUpper(BindingMapping.getPropertyName(localName));
1671 method.append(name);
1672 return method.toString();
1676 * Created a method signature builder as part of
1677 * <code>interfaceBuilder</code>.
1679 * The method signature builder is created for the getter method of
1680 * <code>schemaNodeName</code>. Also <code>comment</code> and
1681 * <code>returnType</code> information are added to the builder.
1683 * @param interfaceBuilder
1684 * generated type builder for which the getter method should be
1686 * @param schemaNodeName
1687 * string with schema node name. The name will be the part of the
1688 * getter method name.
1690 * string with comment for the getter method
1692 * type which represents the return type of the getter method
1693 * @return method signature builder which represents the getter method of
1694 * <code>interfaceBuilder</code>
1696 private MethodSignatureBuilder constructGetter(final GeneratedTypeBuilder interfaceBuilder, final String schemaNodeName,
1697 final String comment, final Type returnType) {
1698 final MethodSignatureBuilder getMethod = interfaceBuilder
1699 .addMethod(getterMethodName(schemaNodeName, returnType));
1700 getMethod.setComment(comment);
1701 getMethod.setReturnType(returnType);
1706 * Adds <code>schemaNode</code> to <code>typeBuilder</code> as getter method
1707 * or to <code>genTOBuilder</code> as property.
1709 * @param basePackageName
1710 * string contains the module package name
1712 * data schema node which should be added as getter method to
1713 * <code>typeBuilder</code> or as a property to
1714 * <code>genTOBuilder</code> if is part of the list key
1715 * @param typeBuilder
1716 * generated type builder for the list schema node
1717 * @param genTOBuilder
1718 * generated TO builder for the list keys
1720 * list of string which contains names of the list keys
1723 * @throws IllegalArgumentException
1725 * <li>if <code>schemaNode</code> equals null</li>
1726 * <li>if <code>typeBuilder</code> equals null</li>
1729 private void addSchemaNodeToListBuilders(final String basePackageName, final DataSchemaNode schemaNode,
1730 final GeneratedTypeBuilder typeBuilder, final GeneratedTOBuilder genTOBuilder, final List<String> listKeys, final Module module) {
1731 checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
1732 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
1734 if (schemaNode instanceof LeafSchemaNode) {
1735 final LeafSchemaNode leaf = (LeafSchemaNode) schemaNode;
1736 final String leafName = leaf.getQName().getLocalName();
1737 final Type type = resolveLeafSchemaNodeAsMethod(typeBuilder, leaf);
1738 if (listKeys.contains(leafName)) {
1740 resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, true, module);
1742 resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, type, true);
1745 } else if (!schemaNode.isAddedByUses()) {
1746 if (schemaNode instanceof LeafListSchemaNode) {
1747 resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) schemaNode);
1748 } else if (schemaNode instanceof ContainerSchemaNode) {
1749 containerToGenType(module, basePackageName, typeBuilder, typeBuilder, (ContainerSchemaNode) schemaNode);
1750 } else if (schemaNode instanceof ChoiceNode) {
1751 choiceToGeneratedType(module, basePackageName, typeBuilder, (ChoiceNode) schemaNode);
1752 } else if (schemaNode instanceof ListSchemaNode) {
1753 listToGenType(module, basePackageName, typeBuilder, typeBuilder, (ListSchemaNode) schemaNode);
1758 private void typeBuildersToGenTypes(final Module module, final GeneratedTypeBuilder typeBuilder, final GeneratedTOBuilder genTOBuilder) {
1759 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
1761 if (genTOBuilder != null) {
1762 GeneratedTransferObject genTO = genTOBuilder.toInstance();
1763 constructGetter(typeBuilder, "key", "Returns Primary Key of Yang List Type", genTO);
1764 genCtx.get(module).addGeneratedTOBuilder(genTOBuilder);
1769 * Selects the names of the list keys from <code>list</code> and returns
1770 * them as the list of the strings
1773 * of string with names of the list keys
1774 * @return list of string which represents names of the list keys. If the
1775 * <code>list</code> contains no keys then the empty list is
1778 private List<String> listKeys(final ListSchemaNode list) {
1779 final List<String> listKeys = new ArrayList<>();
1781 List<QName> keyDefinition = list.getKeyDefinition();
1782 if (keyDefinition != null) {
1783 for (QName keyDef : keyDefinition) {
1784 listKeys.add(keyDef.getLocalName());
1791 * Generates for the <code>list</code> which contains any list keys special
1792 * generated TO builder.
1794 * @param packageName
1795 * string with package name to which the list belongs
1797 * list schema node which is source of data about the list name
1798 * @return generated TO builder which represents the keys of the
1799 * <code>list</code> or null if <code>list</code> is null or list of
1800 * key definitions is null or empty.
1802 private GeneratedTOBuilder resolveListKeyTOBuilder(final String packageName, final ListSchemaNode list) {
1803 GeneratedTOBuilder genTOBuilder = null;
1804 if ((list.getKeyDefinition() != null) && (!list.getKeyDefinition().isEmpty())) {
1805 final String listName = list.getQName().getLocalName() + "Key";
1806 final String genTOName = BindingMapping.getClassName(listName);
1807 genTOBuilder = new GeneratedTOBuilderImpl(packageName, genTOName);
1809 return genTOBuilder;
1813 * Builds generated TO builders for <code>typeDef</code> of type
1814 * {@link org.opendaylight.yangtools.yang.model.util.UnionType UnionType} or
1815 * {@link org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
1816 * BitsTypeDefinition} which are also added to <code>typeBuilder</code> as
1817 * enclosing transfer object.
1819 * If more then one generated TO builder is created for enclosing then all
1820 * of the generated TO builders are added to <code>typeBuilder</code> as
1821 * enclosing transfer objects.
1824 * type definition which can be of type <code>UnionType</code> or
1825 * <code>BitsTypeDefinition</code>
1826 * @param typeBuilder
1827 * generated type builder to which is added generated TO created
1828 * from <code>typeDef</code>
1830 * string with name for generated TO builder
1832 * @return generated TO builder for <code>typeDef</code>
1834 private GeneratedTOBuilder addTOToTypeBuilder(final TypeDefinition<?> typeDef, final GeneratedTypeBuilder typeBuilder,
1835 final DataSchemaNode leaf) {
1836 final String classNameFromLeaf = BindingMapping.getClassName(leaf.getQName());
1837 final List<GeneratedTOBuilder> genTOBuilders = new ArrayList<>();
1838 final String packageName = typeBuilder.getFullyQualifiedName();
1839 if (typeDef instanceof UnionTypeDefinition) {
1840 final List<GeneratedTOBuilder> types = ((TypeProviderImpl) typeProvider)
1841 .provideGeneratedTOBuildersForUnionTypeDef(packageName, ((UnionTypeDefinition) typeDef),
1842 classNameFromLeaf, leaf);
1843 genTOBuilders.addAll(types);
1845 GeneratedTOBuilder resultTOBuilder = null;
1846 if (!types.isEmpty()) {
1847 resultTOBuilder = types.remove(0);
1848 for (GeneratedTOBuilder genTOBuilder : types) {
1849 resultTOBuilder.addEnclosingTransferObject(genTOBuilder);
1853 final GeneratedPropertyBuilder genPropBuilder = resultTOBuilder.addProperty("value");
1854 genPropBuilder.setReturnType(Types.primitiveType("char[]", null));
1855 resultTOBuilder.addEqualsIdentity(genPropBuilder);
1856 resultTOBuilder.addHashIdentity(genPropBuilder);
1857 resultTOBuilder.addToStringProperty(genPropBuilder);
1859 } else if (typeDef instanceof BitsTypeDefinition) {
1860 genTOBuilders.add((((TypeProviderImpl) typeProvider)).provideGeneratedTOBuilderForBitsTypeDefinition(
1861 packageName, typeDef, classNameFromLeaf));
1863 if (genTOBuilders != null && !genTOBuilders.isEmpty()) {
1864 for (GeneratedTOBuilder genTOBuilder : genTOBuilders) {
1865 typeBuilder.addEnclosingTransferObject(genTOBuilder);
1867 return genTOBuilders.get(0);
1874 * Adds the implemented types to type builder.
1876 * The method passes through the list of <i>uses</i> in
1877 * {@code dataNodeContainer}. For every <i>use</i> is obtained corresponding
1878 * generated type from {@link BindingGeneratorImpl#allGroupings
1879 * allGroupings} which is added as <i>implements type</i> to
1880 * <code>builder</code>
1882 * @param dataNodeContainer
1883 * element which contains the list of used YANG groupings
1885 * builder to which are added implemented types according to
1886 * <code>dataNodeContainer</code>
1887 * @return generated type builder with all implemented types
1889 private GeneratedTypeBuilder addImplementedInterfaceFromUses(final DataNodeContainer dataNodeContainer,
1890 final GeneratedTypeBuilder builder) {
1891 for (UsesNode usesNode : dataNodeContainer.getUses()) {
1892 if (usesNode.getGroupingPath() != null) {
1893 final GeneratedType genType = findGroupingByPath(usesNode.getGroupingPath()).toInstance();
1894 if (genType == null) {
1895 throw new IllegalStateException("Grouping " + usesNode.getGroupingPath() + "is not resolved for "
1896 + builder.getName());
1898 builder.addImplementsType(genType);
1899 builder.addComment(genType.getComment());
1905 private GeneratedTypeBuilder findChildNodeByPath(final SchemaPath path) {
1906 for (ModuleContext ctx : genCtx.values()) {
1907 GeneratedTypeBuilder result = ctx.getChildNode(path);
1908 if (result != null) {
1915 private GeneratedTypeBuilder findGroupingByPath(final SchemaPath path) {
1916 for (ModuleContext ctx : genCtx.values()) {
1917 GeneratedTypeBuilder result = ctx.getGrouping(path);
1918 if (result != null) {
1925 private GeneratedTypeBuilder findCaseByPath(final SchemaPath path) {
1926 for (ModuleContext ctx : genCtx.values()) {
1927 GeneratedTypeBuilder result = ctx.getCase(path);
1928 if (result != null) {
1935 public Map<Module, ModuleContext> getModuleContexts() {