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.findOriginal;
29 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
40 import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil;
41 import org.opendaylight.yangtools.binding.generator.util.BindingTypes;
42 import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl;
43 import org.opendaylight.yangtools.binding.generator.util.Types;
44 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedPropertyBuilderImpl;
45 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
46 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
47 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
48 import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider;
49 import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
50 import org.opendaylight.yangtools.sal.binding.model.api.Constant;
51 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
52 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
53 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
54 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
55 import org.opendaylight.yangtools.sal.binding.model.api.Type;
56 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
57 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.EnumBuilder;
58 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;
59 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;
60 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
61 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
62 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.MethodSignatureBuilder;
63 import org.opendaylight.yangtools.sal.binding.yang.types.GroupingDefinitionDependencySort;
64 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
65 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
66 import org.opendaylight.yangtools.yang.binding.BindingMapping;
67 import org.opendaylight.yangtools.yang.binding.DataContainer;
68 import org.opendaylight.yangtools.yang.binding.RpcService;
69 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
70 import org.opendaylight.yangtools.yang.common.QName;
71 import org.opendaylight.yangtools.yang.common.RpcResult;
72 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
73 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
74 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
75 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
76 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
77 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
78 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
79 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
80 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
81 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
82 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
83 import org.opendaylight.yangtools.yang.model.api.Module;
84 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
85 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
86 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
87 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
88 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
89 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
90 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
91 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
92 import org.opendaylight.yangtools.yang.model.api.UsesNode;
93 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
94 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
95 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
96 import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
97 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
98 import org.opendaylight.yangtools.yang.model.util.UnionType;
99 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
100 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
101 import org.slf4j.Logger;
102 import org.slf4j.LoggerFactory;
104 import com.google.common.base.Splitter;
105 import com.google.common.collect.Iterables;
106 import com.google.common.collect.Sets;
108 public class BindingGeneratorImpl implements BindingGenerator {
109 private static final Logger LOG = LoggerFactory.getLogger(BindingGeneratorImpl.class);
111 private final Map<Module, ModuleContext> genCtx = new HashMap<>();
114 * Outer key represents the package name. Outer value represents map of all
115 * builders in the same package. Inner key represents the schema node name
116 * (in JAVA class/interface name format). Inner value represents instance of
117 * builder for schema node specified in key part.
119 private Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders;
122 * Provide methods for converting YANG types to JAVA types.
124 private TypeProvider typeProvider;
127 * Holds reference to schema context to resolve data of augmented element
128 * when creating augmentation builder
130 private SchemaContext schemaContext;
133 * Constant with the concrete name of namespace.
135 private final static String YANG_EXT_NAMESPACE = "urn:opendaylight:yang:extension:yang-ext";
138 * Constant with the concrete name of identifier.
140 private final static String AUGMENT_IDENTIFIER_NAME = "augment-identifier";
143 * Resolves generated types from <code>context</code> schema nodes of all
146 * Generated types are created for modules, groupings, types, containers,
147 * lists, choices, augments, rpcs, notification, identities.
150 * schema context which contains data about all schema nodes
152 * @return list of types (usually <code>GeneratedType</code>
153 * <code>GeneratedTransferObject</code>which are generated from
154 * <code>context</code> data.
155 * @throws IllegalArgumentException
156 * if arg <code>context</code> is null
157 * @throws IllegalStateException
158 * if <code>context</code> contain no modules
161 public List<Type> generateTypes(final SchemaContext context) {
162 checkArgument(context != null, "Schema Context reference cannot be NULL.");
163 checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
164 schemaContext = context;
165 typeProvider = new TypeProviderImpl(context);
166 final Set<Module> modules = context.getModules();
167 return generateTypes(context, modules);
171 * Resolves generated types from <code>context</code> schema nodes only for
172 * modules specified in <code>modules</code>
174 * Generated types are created for modules, groupings, types, containers,
175 * lists, choices, augments, rpcs, notification, identities.
178 * schema context which contains data about all schema nodes
181 * set of modules for which schema nodes should be generated
183 * @return list of types (usually <code>GeneratedType</code> or
184 * <code>GeneratedTransferObject</code>) which:
186 * <li>are generated from <code>context</code> schema nodes and</li>
187 * <li>are also part of some of the module in <code>modules</code>
190 * @throws IllegalArgumentException
192 * <li>if arg <code>context</code> is null or</li>
193 * <li>if arg <code>modules</code> is null</li>
195 * @throws IllegalStateException
196 * if <code>context</code> contain no modules
199 public List<Type> generateTypes(final SchemaContext context, final Set<Module> modules) {
200 checkArgument(context != null, "Schema Context reference cannot be NULL.");
201 checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
202 checkArgument(modules != null, "Set of Modules cannot be NULL.");
204 schemaContext = context;
205 typeProvider = new TypeProviderImpl(context);
206 Module[] modulesArray = new Module[context.getModules().size()];
207 context.getModules().toArray(modulesArray);
208 final List<Module> contextModules = ModuleDependencySort.sort(modulesArray);
209 genTypeBuilders = new HashMap<>();
211 for (Module contextModule : contextModules) {
212 moduleToGenTypes(contextModule, context);
214 for (Module contextModule : contextModules) {
215 allAugmentsToGenTypes(contextModule);
218 final List<Type> filteredGenTypes = new ArrayList<>();
219 for (Module m : modules) {
220 final ModuleContext ctx = checkNotNull(genCtx.get(m), "Module context not found for module %s", m);
221 filteredGenTypes.addAll(ctx.getGeneratedTypes());
222 final Set<Type> additionalTypes = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(m);
223 if (additionalTypes != null) {
224 filteredGenTypes.addAll(additionalTypes);
228 return filteredGenTypes;
231 private void moduleToGenTypes(final Module m, final SchemaContext context) {
232 genCtx.put(m, new ModuleContext());
233 allTypeDefinitionsToGenTypes(m);
234 groupingsToGenTypes(m, m.getGroupings());
235 rpcMethodsToGenType(m);
236 allIdentitiesToGenTypes(m, context);
237 notificationsToGenType(m);
239 if (!m.getChildNodes().isEmpty()) {
240 final GeneratedTypeBuilder moduleType = moduleToDataType(m);
241 genCtx.get(m).addModuleNode(moduleType);
242 final String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
243 resolveDataSchemaNodes(m, basePackageName, moduleType, moduleType, m.getChildNodes());
248 * Converts all extended type definitions of module to the list of
249 * <code>Type</code> objects.
252 * module from which is obtained set of type definitions
253 * @throws IllegalArgumentException
255 * <li>if module is null</li>
256 * <li>if name of module is null</li>
258 * @throws IllegalStateException
259 * if set of type definitions from module is null
261 private void allTypeDefinitionsToGenTypes(final Module module) {
262 checkArgument(module != null, "Module reference cannot be NULL.");
263 checkArgument(module.getName() != null, "Module name cannot be NULL.");
264 final DataNodeIterator it = new DataNodeIterator(module);
265 final List<TypeDefinition<?>> typeDefinitions = it.allTypedefs();
266 checkState(typeDefinitions != null, "Type Definitions for module «module.name» cannot be NULL.");
268 for (TypeDefinition<?> typedef : typeDefinitions) {
269 if (typedef != null) {
270 final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef,
273 genCtx.get(module).addTypedefType(typedef.getPath(), type);
279 private GeneratedTypeBuilder processDataSchemaNode(final Module module, final String basePackageName,
280 final GeneratedTypeBuilder childOf, final DataSchemaNode node) {
281 if (node.isAugmenting() || node.isAddedByUses()) {
284 final String packageName = packageNameForGeneratedType(basePackageName, node.getPath());
285 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, node, childOf);
286 genType.addComment(node.getDescription());
287 if (node instanceof DataNodeContainer) {
288 genCtx.get(module).addChildNodeType(node.getPath(), genType);
289 groupingsToGenTypes(module, ((DataNodeContainer) node).getGroupings());
290 processUsesAugments((DataNodeContainer) node, module);
295 private void containerToGenType(final Module module, final String basePackageName, final GeneratedTypeBuilder parent,
296 final GeneratedTypeBuilder childOf, final ContainerSchemaNode node) {
297 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node);
298 if (genType != null) {
299 constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), genType);
300 resolveDataSchemaNodes(module, basePackageName, genType, genType, node.getChildNodes());
304 private void listToGenType(final Module module, final String basePackageName, final GeneratedTypeBuilder parent,
305 final GeneratedTypeBuilder childOf, final ListSchemaNode node) {
306 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node);
307 if (genType != null) {
308 constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), Types.listTypeFor(genType));
310 final List<String> listKeys = listKeys(node);
311 final String packageName = packageNameForGeneratedType(basePackageName, node.getPath());
312 final GeneratedTOBuilder genTOBuilder = resolveListKeyTOBuilder(packageName, node);
313 if (genTOBuilder != null) {
314 final Type identifierMarker = Types.parameterizedTypeFor(IDENTIFIER, genType);
315 final Type identifiableMarker = Types.parameterizedTypeFor(IDENTIFIABLE, genTOBuilder);
316 genTOBuilder.addImplementsType(identifierMarker);
317 genType.addImplementsType(identifiableMarker);
320 for (DataSchemaNode schemaNode : node.getChildNodes()) {
321 if (!schemaNode.isAugmenting()) {
322 addSchemaNodeToListBuilders(basePackageName, schemaNode, genType, genTOBuilder, listKeys, module);
327 if (genTOBuilder != null) {
328 final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
329 prop.setValue(Long.toString(computeDefaultSUID(genTOBuilder)));
330 genTOBuilder.setSUID(prop);
333 typeBuildersToGenTypes(module, genType, genTOBuilder);
337 private void processUsesAugments(final DataNodeContainer node, final Module module) {
338 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
339 for (UsesNode usesNode : node.getUses()) {
340 for (AugmentationSchema augment : usesNode.getAugmentations()) {
341 usesAugmentationToGenTypes(basePackageName, augment, module, usesNode, node);
342 processUsesAugments(augment, module);
348 * Converts all <b>augmentation</b> of the module to the list
349 * <code>Type</code> objects.
352 * module from which is obtained list of all augmentation objects
353 * to iterate over them
354 * @throws IllegalArgumentException
356 * <li>if the module is null</li>
357 * <li>if the name of module is null</li>
359 * @throws IllegalStateException
360 * if set of augmentations from module is null
362 private void allAugmentsToGenTypes(final Module module) {
363 checkArgument(module != null, "Module reference cannot be NULL.");
364 checkArgument(module.getName() != null, "Module name cannot be NULL.");
365 checkState(module.getAugmentations() != null, "Augmentations Set cannot be NULL.");
367 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
368 final List<AugmentationSchema> augmentations = resolveAugmentations(module);
369 for (AugmentationSchema augment : augmentations) {
370 augmentationToGenTypes(basePackageName, augment, module);
375 * Returns list of <code>AugmentationSchema</code> objects. The objects are
376 * sorted according to the length of their target path from the shortest to
380 * module from which is obtained list of all augmentation objects
381 * @return list of sorted <code>AugmentationSchema</code> objects obtained
382 * from <code>module</code>
383 * @throws IllegalArgumentException
385 * @throws IllegalStateException
386 * if set of module augmentations is null
388 private List<AugmentationSchema> resolveAugmentations(final Module module) {
389 checkArgument(module != null, "Module reference cannot be NULL.");
390 checkState(module.getAugmentations() != null, "Augmentations Set cannot be NULL.");
392 final Set<AugmentationSchema> augmentations = module.getAugmentations();
393 List<AugmentationSchema> sortedAugmentations = new ArrayList<>(augmentations);
394 Collections.sort(sortedAugmentations, Comparators.AUGMENT_COMP);
396 return sortedAugmentations;
400 * Create GeneratedTypeBuilder object from module argument.
403 * Module object from which builder will be created
404 * @return <code>GeneratedTypeBuilder</code> which is internal
405 * representation of the module
406 * @throws IllegalArgumentException
409 private GeneratedTypeBuilder moduleToDataType(final Module module) {
410 checkArgument(module != null, "Module reference cannot be NULL.");
412 final GeneratedTypeBuilder moduleDataTypeBuilder = moduleTypeBuilder(module, "Data");
413 addImplementedInterfaceFromUses(module, moduleDataTypeBuilder);
414 moduleDataTypeBuilder.addImplementsType(DATA_ROOT);
415 moduleDataTypeBuilder.addComment(module.getDescription());
416 return moduleDataTypeBuilder;
420 * Converts all <b>rpcs</b> inputs and outputs substatements of the module
421 * to the list of <code>Type</code> objects. In addition are to containers
422 * and lists which belong to input or output also part of returning list.
425 * module from which is obtained set of all rpc objects to
427 * @throws IllegalArgumentException
429 * <li>if the module is null</li>
430 * <li>if the name of module is null</li>
432 * @throws IllegalStateException
433 * if set of rpcs from module is null
435 private void rpcMethodsToGenType(final Module module) {
436 checkArgument(module != null, "Module reference cannot be NULL.");
437 checkArgument(module.getName() != null, "Module name cannot be NULL.");
438 final Set<RpcDefinition> rpcDefinitions = module.getRpcs();
439 checkState(rpcDefinitions != null, "Set of rpcs from module " + module.getName() + " cannot be NULL.");
440 if (rpcDefinitions.isEmpty()) {
444 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
445 final GeneratedTypeBuilder interfaceBuilder = moduleTypeBuilder(module, "Service");
446 interfaceBuilder.addImplementsType(Types.typeForClass(RpcService.class));
447 for (RpcDefinition rpc : rpcDefinitions) {
449 final String rpcName = BindingMapping.getClassName(rpc.getQName());
450 final String rpcMethodName = parseToValidParamName(rpcName);
451 final String rpcComment = rpc.getDescription();
452 final MethodSignatureBuilder method = interfaceBuilder.addMethod(rpcMethodName);
453 final ContainerSchemaNode input = rpc.getInput();
454 final ContainerSchemaNode output = rpc.getOutput();
457 final GeneratedTypeBuilder inType = addRawInterfaceDefinition(basePackageName, input, rpcName);
458 addImplementedInterfaceFromUses(input, inType);
459 inType.addImplementsType(DATA_OBJECT);
460 inType.addImplementsType(augmentable(inType));
461 resolveDataSchemaNodes(module, basePackageName, inType, inType, input.getChildNodes());
462 genCtx.get(module).addChildNodeType(input.getPath(), inType);
463 final GeneratedType inTypeInstance = inType.toInstance();
464 method.addParameter(inTypeInstance, "input");
467 Type outTypeInstance = VOID;
468 if (output != null) {
469 final GeneratedTypeBuilder outType = addRawInterfaceDefinition(basePackageName, output, rpcName);
470 addImplementedInterfaceFromUses(output, outType);
471 outType.addImplementsType(DATA_OBJECT);
472 outType.addImplementsType(augmentable(outType));
473 resolveDataSchemaNodes(module, basePackageName, outType, outType, output.getChildNodes());
474 genCtx.get(module).addChildNodeType(output.getPath(), outType);
475 outTypeInstance = outType.toInstance();
478 final Type rpcRes = Types.parameterizedTypeFor(Types.typeForClass(RpcResult.class), outTypeInstance);
479 method.setComment(rpcComment);
480 method.setReturnType(Types.parameterizedTypeFor(FUTURE, rpcRes));
484 genCtx.get(module).addTopLevelNodeType(interfaceBuilder);
488 * Converts all <b>notifications</b> of the module to the list of
489 * <code>Type</code> objects. In addition are to this list added containers
490 * and lists which are part of this notification.
493 * module from which is obtained set of all notification objects
494 * to iterate over them
495 * @throws IllegalArgumentException
497 * <li>if the module equals null</li>
498 * <li>if the name of module equals null</li>
500 * @throws IllegalStateException
501 * if set of notifications from module is null
503 private void notificationsToGenType(final Module module) {
504 checkArgument(module != null, "Module reference cannot be NULL.");
505 checkArgument(module.getName() != null, "Module name cannot be NULL.");
506 final Set<NotificationDefinition> notifications = module.getNotifications();
507 checkState(notifications != null, "Set of notification from module " + module.getName() + " cannot be NULL.");
508 if (notifications.isEmpty()) {
512 final GeneratedTypeBuilder listenerInterface = moduleTypeBuilder(module, "Listener");
513 listenerInterface.addImplementsType(BindingTypes.NOTIFICATION_LISTENER);
514 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
516 for (NotificationDefinition notification : notifications) {
517 if (notification != null) {
518 processUsesAugments(notification, module);
520 final GeneratedTypeBuilder notificationInterface = addDefaultInterfaceDefinition(basePackageName,
521 notification, BindingTypes.DATA_OBJECT);
522 notificationInterface.addImplementsType(NOTIFICATION);
523 genCtx.get(module).addChildNodeType(notification.getPath(), notificationInterface);
525 // Notification object
526 resolveDataSchemaNodes(module, basePackageName, notificationInterface, notificationInterface,
527 notification.getChildNodes());
529 listenerInterface.addMethod("on" + notificationInterface.getName())
530 .setAccessModifier(AccessModifier.PUBLIC).addParameter(notificationInterface, "notification")
531 .setComment(notification.getDescription()).setReturnType(Types.VOID);
535 genCtx.get(module).addTopLevelNodeType(listenerInterface);
539 * Converts all <b>identities</b> of the module to the list of
540 * <code>Type</code> objects.
543 * module from which is obtained set of all identity objects to
546 * schema context only used as input parameter for method
547 * {@link identityToGenType}
550 private void allIdentitiesToGenTypes(final Module module, final SchemaContext context) {
551 final Set<IdentitySchemaNode> schemaIdentities = module.getIdentities();
552 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
554 if (schemaIdentities != null && !schemaIdentities.isEmpty()) {
555 for (IdentitySchemaNode identity : schemaIdentities) {
556 identityToGenType(module, basePackageName, identity, context);
562 * Converts the <b>identity</b> object to GeneratedType. Firstly it is
563 * created transport object builder. If identity contains base identity then
564 * reference to base identity is added to superior identity as its extend.
565 * If identity doesn't contain base identity then only reference to abstract
566 * class {@link org.opendaylight.yangtools.yang.model.api.BaseIdentity
567 * BaseIdentity} is added
571 * @param basePackageName
572 * string contains the module package name
574 * IdentitySchemaNode which contains data about identity
576 * SchemaContext which is used to get package and name
577 * information about base of identity
580 private void identityToGenType(final Module module, final String basePackageName, final IdentitySchemaNode identity,
581 final SchemaContext context) {
582 if (identity == null) {
585 final String packageName = packageNameForGeneratedType(basePackageName, identity.getPath());
586 final String genTypeName = BindingMapping.getClassName(identity.getQName());
587 final GeneratedTOBuilder newType = new GeneratedTOBuilderImpl(packageName, genTypeName);
588 final IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
589 if (baseIdentity == null) {
590 final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(BaseIdentity.class.getPackage().getName(),
591 BaseIdentity.class.getSimpleName());
592 newType.setExtendsType(gto.toInstance());
594 final Module baseIdentityParentModule = SchemaContextUtil.findParentModule(context, baseIdentity);
595 final String returnTypePkgName = BindingMapping.getRootPackageName(module.getQNameModule());
596 final String returnTypeName = BindingMapping.getClassName(baseIdentity.getQName());
597 final GeneratedTransferObject gto = new GeneratedTOBuilderImpl(returnTypePkgName, returnTypeName)
599 newType.setExtendsType(gto);
601 newType.setAbstract(true);
602 newType.addComment(identity.getDescription());
603 final QName qname = identity.getQName();
604 qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, qname);
606 genCtx.get(module).addIdentityType(identity.getQName(), newType);
609 private static Constant qnameConstant(final GeneratedTypeBuilderBase<?> toBuilder, final String constantName, final QName name) {
610 StringBuilder sb = new StringBuilder("org.opendaylight.yangtools.yang.common.QName");
611 sb.append(".create(");
613 sb.append(name.getNamespace());
615 sb.append(name.getFormattedRevision());
617 sb.append(name.getLocalName());
620 return toBuilder.addConstant(typeForClass(QName.class), constantName, sb.toString());
624 * Converts all <b>groupings</b> of the module to the list of
625 * <code>Type</code> objects. Firstly are groupings sorted according mutual
626 * dependencies. At least dependent (independent) groupings are in the list
627 * saved at first positions. For every grouping the record is added to map
628 * {@link BindingGeneratorImpl#allGroupings allGroupings}
633 * of groupings from which types will be generated
636 private void groupingsToGenTypes(final Module module, final Collection<GroupingDefinition> groupings) {
637 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
638 final List<GroupingDefinition> groupingsSortedByDependencies = new GroupingDefinitionDependencySort()
640 for (GroupingDefinition grouping : groupingsSortedByDependencies) {
641 groupingToGenType(basePackageName, grouping, module);
646 * Converts individual grouping to GeneratedType. Firstly generated type
647 * builder is created and every child node of grouping is resolved to the
650 * @param basePackageName
651 * string contains the module package name
653 * GroupingDefinition which contains data about grouping
656 * @return GeneratedType which is generated from grouping (object of type
657 * <code>GroupingDefinition</code>)
659 private void groupingToGenType(final String basePackageName, final GroupingDefinition grouping, final Module module) {
660 final String packageName = packageNameForGeneratedType(basePackageName, grouping.getPath());
661 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, grouping);
662 genCtx.get(module).addGroupingType(grouping.getPath(), genType);
663 resolveDataSchemaNodes(module, basePackageName, genType, genType, grouping.getChildNodes());
664 groupingsToGenTypes(module, grouping.getGroupings());
665 processUsesAugments(grouping, module);
669 * Adds enumeration builder created from <code>enumTypeDef</code> to
670 * <code>typeBuilder</code>.
672 * Each <code>enumTypeDef</code> item is added to builder with its name and
676 * EnumTypeDefinition contains enum data
678 * string contains name which will be assigned to enumeration
681 * GeneratedTypeBuilder to which will be enum builder assigned
682 * @return enumeration builder which contains data from
683 * <code>enumTypeDef</code>
685 private EnumBuilder resolveInnerEnumFromTypeDefinition(final EnumTypeDefinition enumTypeDef, final QName enumName,
686 final GeneratedTypeBuilder typeBuilder) {
687 if ((enumTypeDef != null) && (typeBuilder != null) && (enumTypeDef.getQName() != null)
688 && (enumTypeDef.getQName().getLocalName() != null)) {
689 final String enumerationName = BindingMapping.getClassName(enumName);
690 final EnumBuilder enumBuilder = typeBuilder.addEnumeration(enumerationName);
691 enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
698 * Generates type builder for <code>module</code>.
701 * Module which is source of package name for generated type
704 * string which is added to the module class name representation
706 * @return instance of GeneratedTypeBuilder which represents
707 * <code>module</code>.
708 * @throws IllegalArgumentException
709 * if <code>module</code> is null
711 private GeneratedTypeBuilder moduleTypeBuilder(final Module module, final String postfix) {
712 checkArgument(module != null, "Module reference cannot be NULL.");
713 final String packageName = BindingMapping.getRootPackageName(module.getQNameModule());
714 final String moduleName = BindingMapping.getClassName(module.getName()) + postfix;
715 return new GeneratedTypeBuilderImpl(packageName, moduleName);
719 * Converts <code>augSchema</code> to list of <code>Type</code> which
720 * contains generated type for augmentation. In addition there are also
721 * generated types for all containers, list and choices which are child of
722 * <code>augSchema</code> node or a generated types for cases are added if
723 * augmented node is choice.
725 * @param augmentPackageName
726 * string with the name of the package to which the augmentation
729 * AugmentationSchema which is contains data about augmentation
730 * (target path, childs...)
733 * @param parentUsesNode
734 * @throws IllegalArgumentException
736 * <li>if <code>augmentPackageName</code> equals null</li>
737 * <li>if <code>augSchema</code> equals null</li>
739 * @throws IllegalStateException
740 * if augment target path is null
742 private void augmentationToGenTypes(final String augmentPackageName, final AugmentationSchema augSchema, final Module module) {
743 checkArgument(augmentPackageName != null, "Package Name cannot be NULL.");
744 checkArgument(augSchema != null, "Augmentation Schema cannot be NULL.");
745 checkState(augSchema.getTargetPath() != null,
746 "Augmentation Schema does not contain Target Path (Target Path is NULL).");
748 processUsesAugments(augSchema, module);
749 final SchemaPath targetPath = augSchema.getTargetPath();
750 SchemaNode targetSchemaNode = null;
752 targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
753 if (targetSchemaNode instanceof DataSchemaNode && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
754 targetSchemaNode = findOriginal((DataSchemaNode) targetSchemaNode, schemaContext);
755 if (targetSchemaNode == null) {
756 throw new NullPointerException("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 targetSchemaNode = findOriginal((DataSchemaNode) targetSchemaNode, schemaContext);
1165 if (targetSchemaNode == null) {
1166 throw new NullPointerException(
1167 "Failed to find target node from grouping for augmentation " + augSchema
1168 + " in module " + module.getName());
1171 parent = targetSchemaNode;
1173 final SchemaPath sp = choiceNode.getPath();
1174 parent = findDataSchemaNode(schemaContext, sp.getParent());
1176 GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.getPath());
1177 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, caseChildNodes);
1181 processUsesAugments(caseNode, module);
1186 * Generates list of generated types for all the cases of a choice which are
1187 * added to the choice through the augment.
1191 * @param basePackageName
1192 * string contains name of package to which augment belongs. If
1193 * an augmented choice is from an other package (pcg1) than an
1194 * augmenting choice (pcg2) then case's of the augmenting choice
1195 * will belong to pcg2.
1197 * Type which represents target choice
1199 * node which represents target choice
1200 * @param augmentedNodes
1201 * set of choice case nodes for which is checked if are/aren't
1202 * added to choice through augmentation
1203 * @return list of generated types which represents augmented cases of
1204 * choice <code>refChoiceType</code>
1205 * @throws IllegalArgumentException
1207 * <li>if <code>basePackageName</code> is null</li>
1208 * <li>if <code>targetType</code> is null</li>
1209 * <li>if <code>augmentedNodes</code> is null</li>
1212 private void generateTypesFromAugmentedChoiceCases(final Module module, final String basePackageName, final Type targetType,
1213 final ChoiceNode targetNode, final Set<DataSchemaNode> augmentedNodes) {
1214 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
1215 checkArgument(targetType != null, "Referenced Choice Type cannot be NULL.");
1216 checkArgument(augmentedNodes != null, "Set of Choice Case Nodes cannot be NULL.");
1218 for (DataSchemaNode caseNode : augmentedNodes) {
1219 if (caseNode != null) {
1220 final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath());
1221 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode);
1222 caseTypeBuilder.addImplementsType(targetType);
1224 SchemaNode parent = null;
1225 final SchemaPath nodeSp = targetNode.getPath();
1226 parent = findDataSchemaNode(schemaContext, nodeSp.getParent());
1228 GeneratedTypeBuilder childOfType = null;
1229 if (parent instanceof Module) {
1230 childOfType = genCtx.get(parent).getModuleNode();
1231 } else if (parent instanceof ChoiceCaseNode) {
1232 childOfType = findCaseByPath(parent.getPath());
1233 } else if (parent instanceof DataSchemaNode || parent instanceof NotificationDefinition) {
1234 childOfType = findChildNodeByPath(parent.getPath());
1235 } else if (parent instanceof GroupingDefinition) {
1236 childOfType = findGroupingByPath(parent.getPath());
1239 if (childOfType == null) {
1240 throw new IllegalArgumentException("Failed to find parent type of choice " + targetNode);
1243 ChoiceCaseNode node = null;
1244 if (caseNode instanceof ChoiceCaseNode) {
1245 node = (ChoiceCaseNode) caseNode;
1247 node = targetNode.getCaseNodeByName(caseNode.getQName().getLocalName());
1249 final Set<DataSchemaNode> childNodes = node.getChildNodes();
1250 if (childNodes != null) {
1251 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, childNodes);
1253 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
1254 genCtx.get(module).addChoiceToCaseMapping(targetType, caseTypeBuilder, node);
1260 * Converts <code>leaf</code> to the getter method which is added to
1261 * <code>typeBuilder</code>.
1263 * @param typeBuilder
1264 * generated type builder to which is added getter method as
1265 * <code>leaf</code> mapping
1267 * leaf schema node which is mapped as getter method which is
1268 * added to <code>typeBuilder</code>
1269 * @return boolean value
1271 * <li>false - if <code>leaf</code> or <code>typeBuilder</code> are
1273 * <li>true - in other cases</li>
1276 private Type resolveLeafSchemaNodeAsMethod(final GeneratedTypeBuilder typeBuilder, final LeafSchemaNode leaf) {
1277 Type returnType = null;
1278 if ((leaf != null) && (typeBuilder != null)) {
1279 final String leafName = leaf.getQName().getLocalName();
1280 String leafDesc = leaf.getDescription();
1281 if (leafDesc == null) {
1285 final Module parentModule = findParentModule(schemaContext, leaf);
1286 if (leafName != null && !leaf.isAddedByUses()) {
1287 final TypeDefinition<?> typeDef = leaf.getType();
1289 GeneratedTOBuilder genTOBuilder;
1290 if (typeDef instanceof EnumTypeDefinition) {
1291 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1292 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
1293 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(),
1296 if (enumBuilder != null) {
1297 returnType = enumBuilder.toInstance(typeBuilder);
1299 ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
1300 } else if (typeDef instanceof UnionType) {
1301 genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf);
1302 if (genTOBuilder != null) {
1303 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
1305 } else if (typeDef instanceof BitsTypeDefinition) {
1306 genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf);
1307 if (genTOBuilder != null) {
1308 returnType = new ReferencedTypeImpl(genTOBuilder.getPackageName(), genTOBuilder.getName());
1311 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
1312 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
1314 if (returnType != null) {
1315 final MethodSignatureBuilder getter = constructGetter(typeBuilder, leafName, leafDesc, returnType);
1316 processContextRefExtension(leaf, getter, parentModule);
1323 private void processContextRefExtension(final LeafSchemaNode leaf, final MethodSignatureBuilder getter, final Module module) {
1324 for (UnknownSchemaNode node : leaf.getUnknownSchemaNodes()) {
1325 final QName nodeType = node.getNodeType();
1326 if ("context-reference".equals(nodeType.getLocalName())) {
1327 final String nodeParam = node.getNodeParameter();
1328 IdentitySchemaNode identity = null;
1329 String basePackageName = null;
1330 final Iterable<String> splittedElement = Splitter.on(':').split(nodeParam);
1331 final Iterator<String> iterator = splittedElement.iterator();
1332 final int length = Iterables.size(splittedElement);
1334 identity = findIdentityByName(module.getIdentities(), iterator.next());
1335 basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
1336 } else if (length == 2) {
1337 String prefix = iterator.next();
1338 final Module dependentModule = findModuleFromImports(module.getImports(), prefix);
1339 if (dependentModule == null) {
1340 throw new IllegalArgumentException("Failed to process context-reference: unknown prefix "
1343 identity = findIdentityByName(dependentModule.getIdentities(), iterator.next());
1344 basePackageName = BindingMapping.getRootPackageName(dependentModule.getQNameModule());
1346 throw new IllegalArgumentException("Failed to process context-reference: unknown identity "
1349 if (identity == null) {
1350 throw new IllegalArgumentException("Failed to process context-reference: unknown identity "
1354 final Class<RoutingContext> clazz = RoutingContext.class;
1355 final AnnotationTypeBuilder rc = getter.addAnnotation(clazz.getPackage().getName(),
1356 clazz.getSimpleName());
1357 final String packageName = packageNameForGeneratedType(basePackageName, identity.getPath());
1358 final String genTypeName = BindingMapping.getClassName(identity.getQName().getLocalName());
1359 rc.addParameter("value", packageName + "." + genTypeName + ".class");
1364 private IdentitySchemaNode findIdentityByName(final Set<IdentitySchemaNode> identities, final String name) {
1365 for (IdentitySchemaNode id : identities) {
1366 if (id.getQName().getLocalName().equals(name)) {
1373 private Module findModuleFromImports(final Set<ModuleImport> imports, final String prefix) {
1374 for (ModuleImport imp : imports) {
1375 if (imp.getPrefix().equals(prefix)) {
1376 return schemaContext.findModuleByName(imp.getModuleName(), imp.getRevision());
1382 private boolean resolveLeafSchemaNodeAsProperty(final GeneratedTOBuilder toBuilder, final LeafSchemaNode leaf,
1383 final boolean isReadOnly, final Module module) {
1384 if ((leaf != null) && (toBuilder != null)) {
1385 final String leafName = leaf.getQName().getLocalName();
1386 String leafDesc = leaf.getDescription();
1387 if (leafDesc == null) {
1391 if (leafName != null) {
1392 Type returnType = null;
1393 final TypeDefinition<?> typeDef = leaf.getType();
1394 if (typeDef instanceof UnionTypeDefinition) {
1395 // GeneratedType for this type definition should be already
1397 QName qname = typeDef.getQName();
1398 Module unionModule = null;
1399 String prefix = qname.getPrefix();
1400 if (prefix == null || prefix.isEmpty() || prefix.equals(module.getPrefix())) {
1401 unionModule = module;
1403 unionModule = findModuleFromImports(module.getImports(), qname.getPrefix());
1405 final ModuleContext mc = genCtx.get(unionModule);
1406 returnType = mc.getTypedefs().get(typeDef.getPath());
1408 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1410 return resolveLeafSchemaNodeAsProperty(toBuilder, leaf, returnType, isReadOnly);
1417 * Converts <code>leaf</code> schema node to property of generated TO
1421 * generated TO builder to which is <code>leaf</code> added as
1424 * leaf schema node which is added to <code>toBuilder</code> as
1429 * boolean value which says if leaf property is|isn't read only
1430 * @return boolean value
1432 * <li>false - if <code>leaf</code>, <code>toBuilder</code> or leaf
1433 * name equals null or if leaf is added by <i>uses</i>.</li>
1434 * <li>true - other cases</li>
1437 private boolean resolveLeafSchemaNodeAsProperty(final GeneratedTOBuilder toBuilder, final LeafSchemaNode leaf, final Type returnType,
1438 final boolean isReadOnly) {
1439 if (returnType == null) {
1442 final String leafName = leaf.getQName().getLocalName();
1443 final String leafDesc = leaf.getDescription();
1444 final GeneratedPropertyBuilder propBuilder = toBuilder.addProperty(parseToValidParamName(leafName));
1445 propBuilder.setReadOnly(isReadOnly);
1446 propBuilder.setReturnType(returnType);
1447 propBuilder.setComment(leafDesc);
1448 toBuilder.addEqualsIdentity(propBuilder);
1449 toBuilder.addHashIdentity(propBuilder);
1450 toBuilder.addToStringProperty(propBuilder);
1455 * Converts <code>node</code> leaf list schema node to getter method of
1456 * <code>typeBuilder</code>.
1458 * @param typeBuilder
1459 * generated type builder to which is <code>node</code> added as
1462 * leaf list schema node which is added to
1463 * <code>typeBuilder</code> as getter method
1464 * @return boolean value
1466 * <li>true - if <code>node</code>, <code>typeBuilder</code>,
1467 * nodeName equal null or <code>node</code> is added by <i>uses</i></li>
1468 * <li>false - other cases</li>
1471 private boolean resolveLeafListSchemaNode(final GeneratedTypeBuilder typeBuilder, final LeafListSchemaNode node) {
1472 if ((node != null) && (typeBuilder != null)) {
1473 final QName nodeName = node.getQName();
1475 if (nodeName != null && !node.isAddedByUses()) {
1476 final TypeDefinition<?> typeDef = node.getType();
1477 final Module parentModule = findParentModule(schemaContext, node);
1479 Type returnType = null;
1480 if (typeDef instanceof EnumTypeDefinition) {
1481 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node);
1482 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
1483 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, nodeName,
1485 returnType = new ReferencedTypeImpl(enumBuilder.getPackageName(), enumBuilder.getName());
1486 ((TypeProviderImpl) typeProvider).putReferencedType(node.getPath(), returnType);
1487 } else if (typeDef instanceof UnionType) {
1488 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node);
1489 if (genTOBuilder != null) {
1490 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
1492 } else if (typeDef instanceof BitsTypeDefinition) {
1493 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node);
1494 returnType = new ReferencedTypeImpl(genTOBuilder.getPackageName(), genTOBuilder.getName());
1496 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
1497 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
1500 final ParameterizedType listType = Types.listTypeFor(returnType);
1501 constructGetter(typeBuilder, nodeName.getLocalName(), node.getDescription(), listType);
1508 private Type createReturnTypeForUnion(final GeneratedTOBuilder genTOBuilder, final TypeDefinition<?> typeDef,
1509 final GeneratedTypeBuilder typeBuilder, final Module parentModule) {
1510 final GeneratedTOBuilderImpl returnType = new GeneratedTOBuilderImpl(genTOBuilder.getPackageName(),
1511 genTOBuilder.getName());
1512 genTOBuilder.setTypedef(true);
1513 genTOBuilder.setIsUnion(true);
1514 ((TypeProviderImpl) typeProvider).addUnitsToGenTO(genTOBuilder, typeDef.getUnits());
1517 final GeneratedTOBuilder unionBuilder = new GeneratedTOBuilderImpl(typeBuilder.getPackageName(),
1518 genTOBuilder.getName() + "Builder");
1519 unionBuilder.setIsUnionBuilder(true);
1520 final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
1521 method.setReturnType(returnType);
1522 method.addParameter(Types.STRING, "defaultValue");
1523 method.setAccessModifier(AccessModifier.PUBLIC);
1524 method.setStatic(true);
1526 final Set<Type> types = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(parentModule);
1527 if (types == null) {
1528 ((TypeProviderImpl) typeProvider).getAdditionalTypes().put(parentModule,
1529 Sets.<Type> newHashSet(unionBuilder.toInstance()));
1531 types.add(unionBuilder.toInstance());
1533 return returnType.toInstance();
1536 private GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode schemaNode) {
1537 return addDefaultInterfaceDefinition(packageName, schemaNode, null);
1541 * Instantiates generated type builder with <code>packageName</code> and
1542 * <code>schemaNode</code>.
1544 * The new builder always implements
1545 * {@link org.opendaylight.yangtools.yang.binding.DataObject DataObject}.<br />
1546 * If <code>schemaNode</code> is instance of GroupingDefinition it also
1547 * implements {@link org.opendaylight.yangtools.yang.binding.Augmentable
1548 * Augmentable}.<br />
1549 * If <code>schemaNode</code> is instance of
1550 * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer
1551 * DataNodeContainer} it can also implement nodes which are specified in
1554 * @param packageName
1555 * string with the name of the package to which
1556 * <code>schemaNode</code> belongs.
1558 * schema node for which is created generated type builder
1560 * parent type (can be null)
1561 * @return generated type builder <code>schemaNode</code>
1563 private GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode schemaNode, final Type parent) {
1564 final GeneratedTypeBuilder it = addRawInterfaceDefinition(packageName, schemaNode, "");
1565 if (parent == null) {
1566 it.addImplementsType(DATA_OBJECT);
1568 it.addImplementsType(BindingTypes.childOf(parent));
1570 if (!(schemaNode instanceof GroupingDefinition)) {
1571 it.addImplementsType(augmentable(it));
1574 if (schemaNode instanceof DataNodeContainer) {
1575 addImplementedInterfaceFromUses((DataNodeContainer) schemaNode, it);
1582 * Wraps the calling of the same overloaded method.
1584 * @param packageName
1585 * string with the package name to which returning generated type
1588 * schema node which provide data about the schema node name
1589 * @return generated type builder for <code>schemaNode</code>
1591 private GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode) {
1592 return addRawInterfaceDefinition(packageName, schemaNode, "");
1596 * Returns reference to generated type builder for specified
1597 * <code>schemaNode</code> with <code>packageName</code>.
1599 * Firstly the generated type builder is searched in
1600 * {@link BindingGeneratorImpl#genTypeBuilders genTypeBuilders}. If it isn't
1601 * found it is created and added to <code>genTypeBuilders</code>.
1603 * @param packageName
1604 * string with the package name to which returning generated type
1607 * schema node which provide data about the schema node name
1609 * return type name prefix
1610 * @return generated type builder for <code>schemaNode</code>
1611 * @throws IllegalArgumentException
1613 * <li>if <code>schemaNode</code> is null</li>
1614 * <li>if <code>packageName</code> is null</li>
1615 * <li>if QName of schema node is null</li>
1616 * <li>if schemaNode name is null</li>
1620 private GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode, final String prefix) {
1621 checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
1622 checkArgument(packageName != null, "Package Name for Generated Type cannot be NULL.");
1623 checkArgument(schemaNode.getQName() != null, "QName for Data Schema Node cannot be NULL.");
1624 final String schemaNodeName = schemaNode.getQName().getLocalName();
1625 checkArgument(schemaNodeName != null, "Local Name of QName for Data Schema Node cannot be NULL.");
1628 if (prefix == null) {
1629 genTypeName = BindingMapping.getClassName(schemaNodeName);
1631 genTypeName = prefix + BindingMapping.getClassName(schemaNodeName);
1634 // FIXME: Validation of name conflict
1635 final GeneratedTypeBuilderImpl newType = new GeneratedTypeBuilderImpl(packageName, genTypeName);
1636 qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, schemaNode.getQName());
1637 newType.addComment(schemaNode.getDescription());
1638 if (!genTypeBuilders.containsKey(packageName)) {
1639 final Map<String, GeneratedTypeBuilder> builders = new HashMap<>();
1640 builders.put(genTypeName, newType);
1641 genTypeBuilders.put(packageName, builders);
1643 final Map<String, GeneratedTypeBuilder> builders = genTypeBuilders.get(packageName);
1644 if (!builders.containsKey(genTypeName)) {
1645 builders.put(genTypeName, newType);
1652 * Creates the name of the getter method name from <code>localName</code>.
1655 * string with the name of the getter method
1658 * @return string with the name of the getter method for
1659 * <code>methodName</code> in JAVA method format
1661 public static String getterMethodName(final String localName, final Type returnType) {
1662 final StringBuilder method = new StringBuilder();
1663 if (BOOLEAN.equals(returnType)) {
1664 method.append("is");
1666 method.append("get");
1668 String name = BindingMapping.toFirstUpper(BindingMapping.getPropertyName(localName));
1669 method.append(name);
1670 return method.toString();
1674 * Created a method signature builder as part of
1675 * <code>interfaceBuilder</code>.
1677 * The method signature builder is created for the getter method of
1678 * <code>schemaNodeName</code>. Also <code>comment</code> and
1679 * <code>returnType</code> information are added to the builder.
1681 * @param interfaceBuilder
1682 * generated type builder for which the getter method should be
1684 * @param schemaNodeName
1685 * string with schema node name. The name will be the part of the
1686 * getter method name.
1688 * string with comment for the getter method
1690 * type which represents the return type of the getter method
1691 * @return method signature builder which represents the getter method of
1692 * <code>interfaceBuilder</code>
1694 private MethodSignatureBuilder constructGetter(final GeneratedTypeBuilder interfaceBuilder, final String schemaNodeName,
1695 final String comment, final Type returnType) {
1696 final MethodSignatureBuilder getMethod = interfaceBuilder
1697 .addMethod(getterMethodName(schemaNodeName, returnType));
1698 getMethod.setComment(comment);
1699 getMethod.setReturnType(returnType);
1704 * Adds <code>schemaNode</code> to <code>typeBuilder</code> as getter method
1705 * or to <code>genTOBuilder</code> as property.
1707 * @param basePackageName
1708 * string contains the module package name
1710 * data schema node which should be added as getter method to
1711 * <code>typeBuilder</code> or as a property to
1712 * <code>genTOBuilder</code> if is part of the list key
1713 * @param typeBuilder
1714 * generated type builder for the list schema node
1715 * @param genTOBuilder
1716 * generated TO builder for the list keys
1718 * list of string which contains names of the list keys
1721 * @throws IllegalArgumentException
1723 * <li>if <code>schemaNode</code> equals null</li>
1724 * <li>if <code>typeBuilder</code> equals null</li>
1727 private void addSchemaNodeToListBuilders(final String basePackageName, final DataSchemaNode schemaNode,
1728 final GeneratedTypeBuilder typeBuilder, final GeneratedTOBuilder genTOBuilder, final List<String> listKeys, final Module module) {
1729 checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
1730 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
1732 if (schemaNode instanceof LeafSchemaNode) {
1733 final LeafSchemaNode leaf = (LeafSchemaNode) schemaNode;
1734 final String leafName = leaf.getQName().getLocalName();
1735 final Type type = resolveLeafSchemaNodeAsMethod(typeBuilder, leaf);
1736 if (listKeys.contains(leafName)) {
1738 resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, true, module);
1740 resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, type, true);
1743 } else if (!schemaNode.isAddedByUses()) {
1744 if (schemaNode instanceof LeafListSchemaNode) {
1745 resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) schemaNode);
1746 } else if (schemaNode instanceof ContainerSchemaNode) {
1747 containerToGenType(module, basePackageName, typeBuilder, typeBuilder, (ContainerSchemaNode) schemaNode);
1748 } else if (schemaNode instanceof ChoiceNode) {
1749 choiceToGeneratedType(module, basePackageName, typeBuilder, (ChoiceNode) schemaNode);
1750 } else if (schemaNode instanceof ListSchemaNode) {
1751 listToGenType(module, basePackageName, typeBuilder, typeBuilder, (ListSchemaNode) schemaNode);
1756 private void typeBuildersToGenTypes(final Module module, final GeneratedTypeBuilder typeBuilder, final GeneratedTOBuilder genTOBuilder) {
1757 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
1759 if (genTOBuilder != null) {
1760 GeneratedTransferObject genTO = genTOBuilder.toInstance();
1761 constructGetter(typeBuilder, "key", "Returns Primary Key of Yang List Type", genTO);
1762 genCtx.get(module).addGeneratedTOBuilder(genTOBuilder);
1767 * Selects the names of the list keys from <code>list</code> and returns
1768 * them as the list of the strings
1771 * of string with names of the list keys
1772 * @return list of string which represents names of the list keys. If the
1773 * <code>list</code> contains no keys then the empty list is
1776 private List<String> listKeys(final ListSchemaNode list) {
1777 final List<String> listKeys = new ArrayList<>();
1779 List<QName> keyDefinition = list.getKeyDefinition();
1780 if (keyDefinition != null) {
1781 for (QName keyDef : keyDefinition) {
1782 listKeys.add(keyDef.getLocalName());
1789 * Generates for the <code>list</code> which contains any list keys special
1790 * generated TO builder.
1792 * @param packageName
1793 * string with package name to which the list belongs
1795 * list schema node which is source of data about the list name
1796 * @return generated TO builder which represents the keys of the
1797 * <code>list</code> or null if <code>list</code> is null or list of
1798 * key definitions is null or empty.
1800 private GeneratedTOBuilder resolveListKeyTOBuilder(final String packageName, final ListSchemaNode list) {
1801 GeneratedTOBuilder genTOBuilder = null;
1802 if ((list.getKeyDefinition() != null) && (!list.getKeyDefinition().isEmpty())) {
1803 final String listName = list.getQName().getLocalName() + "Key";
1804 final String genTOName = BindingMapping.getClassName(listName);
1805 genTOBuilder = new GeneratedTOBuilderImpl(packageName, genTOName);
1807 return genTOBuilder;
1811 * Builds generated TO builders for <code>typeDef</code> of type
1812 * {@link org.opendaylight.yangtools.yang.model.util.UnionType UnionType} or
1813 * {@link org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
1814 * BitsTypeDefinition} which are also added to <code>typeBuilder</code> as
1815 * enclosing transfer object.
1817 * If more then one generated TO builder is created for enclosing then all
1818 * of the generated TO builders are added to <code>typeBuilder</code> as
1819 * enclosing transfer objects.
1822 * type definition which can be of type <code>UnionType</code> or
1823 * <code>BitsTypeDefinition</code>
1824 * @param typeBuilder
1825 * generated type builder to which is added generated TO created
1826 * from <code>typeDef</code>
1828 * string with name for generated TO builder
1830 * @return generated TO builder for <code>typeDef</code>
1832 private GeneratedTOBuilder addTOToTypeBuilder(final TypeDefinition<?> typeDef, final GeneratedTypeBuilder typeBuilder,
1833 final DataSchemaNode leaf) {
1834 final String classNameFromLeaf = BindingMapping.getClassName(leaf.getQName());
1835 final List<GeneratedTOBuilder> genTOBuilders = new ArrayList<>();
1836 final String packageName = typeBuilder.getFullyQualifiedName();
1837 if (typeDef instanceof UnionTypeDefinition) {
1838 final List<GeneratedTOBuilder> types = ((TypeProviderImpl) typeProvider)
1839 .provideGeneratedTOBuildersForUnionTypeDef(packageName, ((UnionTypeDefinition) typeDef),
1840 classNameFromLeaf, leaf);
1841 genTOBuilders.addAll(types);
1843 GeneratedTOBuilder resultTOBuilder = null;
1844 if (!types.isEmpty()) {
1845 resultTOBuilder = types.remove(0);
1846 for (GeneratedTOBuilder genTOBuilder : types) {
1847 resultTOBuilder.addEnclosingTransferObject(genTOBuilder);
1851 final GeneratedPropertyBuilder genPropBuilder = resultTOBuilder.addProperty("value");
1852 genPropBuilder.setReturnType(Types.primitiveType("char[]", null));
1853 resultTOBuilder.addEqualsIdentity(genPropBuilder);
1854 resultTOBuilder.addHashIdentity(genPropBuilder);
1855 resultTOBuilder.addToStringProperty(genPropBuilder);
1857 } else if (typeDef instanceof BitsTypeDefinition) {
1858 genTOBuilders.add((((TypeProviderImpl) typeProvider)).provideGeneratedTOBuilderForBitsTypeDefinition(
1859 packageName, typeDef, classNameFromLeaf));
1861 if (genTOBuilders != null && !genTOBuilders.isEmpty()) {
1862 for (GeneratedTOBuilder genTOBuilder : genTOBuilders) {
1863 typeBuilder.addEnclosingTransferObject(genTOBuilder);
1865 return genTOBuilders.get(0);
1872 * Adds the implemented types to type builder.
1874 * The method passes through the list of <i>uses</i> in
1875 * {@code dataNodeContainer}. For every <i>use</i> is obtained corresponding
1876 * generated type from {@link BindingGeneratorImpl#allGroupings
1877 * allGroupings} which is added as <i>implements type</i> to
1878 * <code>builder</code>
1880 * @param dataNodeContainer
1881 * element which contains the list of used YANG groupings
1883 * builder to which are added implemented types according to
1884 * <code>dataNodeContainer</code>
1885 * @return generated type builder with all implemented types
1887 private GeneratedTypeBuilder addImplementedInterfaceFromUses(final DataNodeContainer dataNodeContainer,
1888 final GeneratedTypeBuilder builder) {
1889 for (UsesNode usesNode : dataNodeContainer.getUses()) {
1890 if (usesNode.getGroupingPath() != null) {
1891 final GeneratedType genType = findGroupingByPath(usesNode.getGroupingPath()).toInstance();
1892 if (genType == null) {
1893 throw new IllegalStateException("Grouping " + usesNode.getGroupingPath() + "is not resolved for "
1894 + builder.getName());
1896 builder.addImplementsType(genType);
1897 builder.addComment(genType.getComment());
1903 private GeneratedTypeBuilder findChildNodeByPath(final SchemaPath path) {
1904 for (ModuleContext ctx : genCtx.values()) {
1905 GeneratedTypeBuilder result = ctx.getChildNode(path);
1906 if (result != null) {
1913 private GeneratedTypeBuilder findGroupingByPath(final SchemaPath path) {
1914 for (ModuleContext ctx : genCtx.values()) {
1915 GeneratedTypeBuilder result = ctx.getGrouping(path);
1916 if (result != null) {
1923 private GeneratedTypeBuilder findCaseByPath(final SchemaPath path) {
1924 for (ModuleContext ctx : genCtx.values()) {
1925 GeneratedTypeBuilder result = ctx.getCase(path);
1926 if (result != null) {
1933 public Map<Module, ModuleContext> getModuleContexts() {