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,
294 final GeneratedTypeBuilder parent, 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,
579 final IdentitySchemaNode identity, 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 GeneratedTOBuilderImpl 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
595 final String returnTypeName = BindingMapping.getClassName(baseIdentity.getQName());
596 final GeneratedTransferObject gto = new GeneratedTOBuilderImpl(returnTypePkgName, returnTypeName)
598 newType.setExtendsType(gto);
600 newType.setAbstract(true);
601 newType.addComment(identity.getDescription());
602 newType.setDescription(identity.getDescription());
603 newType.setReference(identity.getReference());
604 newType.setModuleName(module.getName());
605 newType.setSchemaPath(identity.getPath().getPathFromRoot());
607 final QName qname = identity.getQName();
608 qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, qname);
610 genCtx.get(module).addIdentityType(identity.getQName(), newType);
613 private static Constant qnameConstant(final GeneratedTypeBuilderBase<?> toBuilder, final String constantName,
615 StringBuilder sb = new StringBuilder("org.opendaylight.yangtools.yang.common.QName");
616 sb.append(".create(");
618 sb.append(name.getNamespace());
620 sb.append(name.getFormattedRevision());
622 sb.append(name.getLocalName());
625 return toBuilder.addConstant(typeForClass(QName.class), constantName, sb.toString());
629 * Converts all <b>groupings</b> of the module to the list of
630 * <code>Type</code> objects. Firstly are groupings sorted according mutual
631 * dependencies. At least dependent (independent) groupings are in the list
632 * saved at first positions. For every grouping the record is added to map
633 * {@link BindingGeneratorImpl#allGroupings allGroupings}
638 * of groupings from which types will be generated
641 private void groupingsToGenTypes(final Module module, final Collection<GroupingDefinition> groupings) {
642 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
643 final List<GroupingDefinition> groupingsSortedByDependencies = new GroupingDefinitionDependencySort()
645 for (GroupingDefinition grouping : groupingsSortedByDependencies) {
646 groupingToGenType(basePackageName, grouping, module);
651 * Converts individual grouping to GeneratedType. Firstly generated type
652 * builder is created and every child node of grouping is resolved to the
655 * @param basePackageName
656 * string contains the module package name
658 * GroupingDefinition which contains data about grouping
661 * @return GeneratedType which is generated from grouping (object of type
662 * <code>GroupingDefinition</code>)
664 private void groupingToGenType(final String basePackageName, final GroupingDefinition grouping, final Module module) {
665 final String packageName = packageNameForGeneratedType(basePackageName, grouping.getPath());
666 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, grouping);
667 genCtx.get(module).addGroupingType(grouping.getPath(), genType);
668 resolveDataSchemaNodes(module, basePackageName, genType, genType, grouping.getChildNodes());
669 groupingsToGenTypes(module, grouping.getGroupings());
670 processUsesAugments(grouping, module);
674 * Adds enumeration builder created from <code>enumTypeDef</code> to
675 * <code>typeBuilder</code>.
677 * Each <code>enumTypeDef</code> item is added to builder with its name and
681 * EnumTypeDefinition contains enum data
683 * string contains name which will be assigned to enumeration
686 * GeneratedTypeBuilder to which will be enum builder assigned
687 * @return enumeration builder which contains data from
688 * <code>enumTypeDef</code>
690 private EnumBuilder resolveInnerEnumFromTypeDefinition(final EnumTypeDefinition enumTypeDef, final QName enumName,
691 final GeneratedTypeBuilder typeBuilder) {
692 if ((enumTypeDef != null) && (typeBuilder != null) && (enumTypeDef.getQName() != null)
693 && (enumTypeDef.getQName().getLocalName() != null)) {
694 final String enumerationName = BindingMapping.getClassName(enumName);
695 final EnumBuilder enumBuilder = typeBuilder.addEnumeration(enumerationName);
696 enumBuilder.setDescription(enumTypeDef.getDescription());
697 enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
704 * Generates type builder for <code>module</code>.
707 * Module which is source of package name for generated type
710 * string which is added to the module class name representation
712 * @return instance of GeneratedTypeBuilder which represents
713 * <code>module</code>.
714 * @throws IllegalArgumentException
715 * if <code>module</code> is null
717 private GeneratedTypeBuilder moduleTypeBuilder(final Module module, final String postfix) {
718 checkArgument(module != null, "Module reference cannot be NULL.");
719 final String packageName = BindingMapping.getRootPackageName(module.getQNameModule());
720 final String moduleName = BindingMapping.getClassName(module.getName()) + postfix;
722 final GeneratedTypeBuilderImpl moduleBuilder = new GeneratedTypeBuilderImpl(packageName, moduleName);
723 moduleBuilder.setDescription(module.getDescription());
724 moduleBuilder.setReference(module.getReference());
725 moduleBuilder.setModuleName(moduleName);
727 return moduleBuilder;
731 * Converts <code>augSchema</code> to list of <code>Type</code> which
732 * contains generated type for augmentation. In addition there are also
733 * generated types for all containers, list and choices which are child of
734 * <code>augSchema</code> node or a generated types for cases are added if
735 * augmented node is choice.
737 * @param augmentPackageName
738 * string with the name of the package to which the augmentation
741 * AugmentationSchema which is contains data about augmentation
742 * (target path, childs...)
745 * @param parentUsesNode
746 * @throws IllegalArgumentException
748 * <li>if <code>augmentPackageName</code> equals null</li>
749 * <li>if <code>augSchema</code> equals null</li>
751 * @throws IllegalStateException
752 * if augment target path is null
754 private void augmentationToGenTypes(final String augmentPackageName, final AugmentationSchema augSchema,
755 final Module module) {
756 checkArgument(augmentPackageName != null, "Package Name cannot be NULL.");
757 checkArgument(augSchema != null, "Augmentation Schema cannot be NULL.");
758 checkState(augSchema.getTargetPath() != null,
759 "Augmentation Schema does not contain Target Path (Target Path is NULL).");
761 processUsesAugments(augSchema, module);
762 final SchemaPath targetPath = augSchema.getTargetPath();
763 SchemaNode targetSchemaNode = null;
765 targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
766 if (targetSchemaNode instanceof DataSchemaNode && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
767 if (targetSchemaNode instanceof DerivableSchemaNode) {
768 targetSchemaNode = ((DerivableSchemaNode) targetSchemaNode).getOriginal().orNull();
770 if (targetSchemaNode == null) {
771 throw new IllegalStateException("Failed to find target node from grouping in augmentation " + augSchema
772 + " in module " + module.getName());
775 if (targetSchemaNode == null) {
776 throw new IllegalArgumentException("augment target not found: " + targetPath);
779 GeneratedTypeBuilder targetTypeBuilder = findChildNodeByPath(targetSchemaNode.getPath());
780 if (targetTypeBuilder == null) {
781 targetTypeBuilder = findCaseByPath(targetSchemaNode.getPath());
783 if (targetTypeBuilder == null) {
784 throw new NullPointerException("Target type not yet generated: " + targetSchemaNode);
787 if (!(targetSchemaNode instanceof ChoiceNode)) {
788 String packageName = augmentPackageName;
789 final Type targetType = new ReferencedTypeImpl(targetTypeBuilder.getPackageName(),
790 targetTypeBuilder.getName());
791 addRawAugmentGenTypeDefinition(module, packageName, augmentPackageName, targetType, augSchema);
794 generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance(),
795 (ChoiceNode) targetSchemaNode, augSchema.getChildNodes());
799 private void usesAugmentationToGenTypes(final String augmentPackageName, final AugmentationSchema augSchema,
800 final Module module, final UsesNode usesNode, final DataNodeContainer usesNodeParent) {
801 checkArgument(augmentPackageName != null, "Package Name cannot be NULL.");
802 checkArgument(augSchema != null, "Augmentation Schema cannot be NULL.");
803 checkState(augSchema.getTargetPath() != null,
804 "Augmentation Schema does not contain Target Path (Target Path is NULL).");
806 processUsesAugments(augSchema, module);
807 final SchemaPath targetPath = augSchema.getTargetPath();
808 SchemaNode targetSchemaNode = findOriginalTargetFromGrouping(targetPath, usesNode);
809 if (targetSchemaNode == null) {
810 throw new IllegalArgumentException("augment target not found: " + targetPath);
813 GeneratedTypeBuilder targetTypeBuilder = findChildNodeByPath(targetSchemaNode.getPath());
814 if (targetTypeBuilder == null) {
815 targetTypeBuilder = findCaseByPath(targetSchemaNode.getPath());
817 if (targetTypeBuilder == null) {
818 throw new NullPointerException("Target type not yet generated: " + targetSchemaNode);
821 if (!(targetSchemaNode instanceof ChoiceNode)) {
822 String packageName = augmentPackageName;
823 if (usesNodeParent instanceof SchemaNode) {
824 packageName = packageNameForGeneratedType(augmentPackageName, ((SchemaNode) usesNodeParent).getPath(),
827 addRawAugmentGenTypeDefinition(module, packageName, augmentPackageName, targetTypeBuilder.toInstance(),
830 generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance(),
831 (ChoiceNode) targetSchemaNode, augSchema.getChildNodes());
836 * Convenient method to find node added by uses statement.
840 * @param parentUsesNode
841 * parent of uses node
842 * @return node from its original location in grouping
844 private DataSchemaNode findOriginalTargetFromGrouping(final SchemaPath targetPath, final UsesNode parentUsesNode) {
845 SchemaNode targetGrouping = findNodeInSchemaContext(schemaContext, parentUsesNode.getGroupingPath()
847 if (!(targetGrouping instanceof GroupingDefinition)) {
848 throw new IllegalArgumentException("Failed to generate code for augment in " + parentUsesNode);
851 GroupingDefinition grouping = (GroupingDefinition) targetGrouping;
852 SchemaNode result = grouping;
853 for (QName node : targetPath.getPathFromRoot()) {
854 // finding by local name is valid, grouping cannot contain nodes
855 // with same name and different namespace
856 if (result instanceof DataNodeContainer) {
857 result = ((DataNodeContainer) result).getDataChildByName(node.getLocalName());
858 } else if (result instanceof ChoiceNode) {
859 result = ((ChoiceNode) result).getCaseNodeByName(node.getLocalName());
862 if (result == null) {
866 boolean fromUses = ((DataSchemaNode) result).isAddedByUses();
867 Iterator<UsesNode> groupingUses = grouping.getUses().iterator();
868 while (groupingUses.hasNext() && fromUses) {
869 result = findOriginalTargetFromGrouping(targetPath, groupingUses.next());
870 if (result != null) {
871 fromUses = ((DataSchemaNode) result).isAddedByUses();
875 // this indicates invalid yang and thus possible bug in code because
876 // invalid yang should be already spotted by parser
877 throw new IllegalStateException("Failed to generate code for augment in " + parentUsesNode);
880 return (DataSchemaNode) result;
884 * Returns a generated type builder for an augmentation.
886 * The name of the type builder is equal to the name of augmented node with
887 * serial number as suffix.
891 * @param augmentPackageName
892 * string with contains the package name to which the augment
894 * @param basePackageName
895 * string with the package name to which the augmented node
897 * @param targetTypeRef
900 * augmentation schema which contains data about the child nodes
901 * and uses of augment
902 * @return generated type builder for augment
904 private GeneratedTypeBuilder addRawAugmentGenTypeDefinition(final Module module, final String augmentPackageName,
905 final String basePackageName, final Type targetTypeRef, final AugmentationSchema augSchema) {
906 Map<String, GeneratedTypeBuilder> augmentBuilders = genTypeBuilders.get(augmentPackageName);
907 if (augmentBuilders == null) {
908 augmentBuilders = new HashMap<>();
909 genTypeBuilders.put(augmentPackageName, augmentBuilders);
911 final String augIdentifier = getAugmentIdentifier(augSchema.getUnknownSchemaNodes());
914 if (augIdentifier != null) {
915 augTypeName = BindingMapping.getClassName(augIdentifier);
917 augTypeName = augGenTypeName(augmentBuilders, targetTypeRef.getName());
920 final GeneratedTypeBuilder augTypeBuilder = new GeneratedTypeBuilderImpl(augmentPackageName, augTypeName);
922 augTypeBuilder.addImplementsType(DATA_OBJECT);
923 augTypeBuilder.addImplementsType(Types.augmentationTypeFor(targetTypeRef));
924 addImplementedInterfaceFromUses(augSchema, augTypeBuilder);
926 augSchemaNodeToMethods(module, basePackageName, augTypeBuilder, augTypeBuilder, augSchema.getChildNodes());
927 augmentBuilders.put(augTypeName, augTypeBuilder);
929 genCtx.get(module).addTargetToAugmentation(targetTypeRef, augTypeBuilder);
930 genCtx.get(module).addAugmentType(augTypeBuilder);
931 genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
932 return augTypeBuilder;
937 * @param unknownSchemaNodes
938 * @return nodeParameter of UnknownSchemaNode
940 private String getAugmentIdentifier(final List<UnknownSchemaNode> unknownSchemaNodes) {
941 for (UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
942 final QName nodeType = unknownSchemaNode.getNodeType();
943 if (AUGMENT_IDENTIFIER_NAME.equals(nodeType.getLocalName())
944 && YANG_EXT_NAMESPACE.equals(nodeType.getNamespace().toString())) {
945 return unknownSchemaNode.getNodeParameter();
952 * Returns first unique name for the augment generated type builder. The
953 * generated type builder name for augment consists from name of augmented
954 * node and serial number of its augmentation.
957 * map of builders which were created in the package to which the
958 * augmentation belongs
960 * string with name of augmented node
961 * @return string with unique name for augmentation builder
963 private String augGenTypeName(final Map<String, GeneratedTypeBuilder> builders, final String genTypeName) {
965 if (builders != null) {
966 while (builders.containsKey(genTypeName + index)) {
970 return genTypeName + index;
974 * Adds the methods to <code>typeBuilder</code> which represent subnodes of
975 * node for which <code>typeBuilder</code> was created.
977 * The subnodes aren't mapped to the methods if they are part of grouping or
978 * augment (in this case are already part of them).
982 * @param basePackageName
983 * string contains the module package name
985 * generated type builder which represents any node. The subnodes
986 * of this node are added to the <code>typeBuilder</code> as
987 * methods. The subnode can be of type leaf, leaf-list, list,
992 * set of data schema nodes which are the children of the node
993 * for which <code>typeBuilder</code> was created
994 * @return generated type builder which is the same builder as input
995 * parameter. The getter methods (representing child nodes) could be
998 private GeneratedTypeBuilder resolveDataSchemaNodes(final Module module, final String basePackageName,
999 final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf, final Set<DataSchemaNode> schemaNodes) {
1000 if ((schemaNodes != null) && (parent != null)) {
1001 for (DataSchemaNode schemaNode : schemaNodes) {
1002 if (!schemaNode.isAugmenting() && !schemaNode.isAddedByUses()) {
1003 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, parent, childOf, module);
1011 * Adds the methods to <code>typeBuilder</code> what represents subnodes of
1012 * node for which <code>typeBuilder</code> was created.
1016 * @param basePackageName
1017 * string contains the module package name
1018 * @param typeBuilder
1019 * generated type builder which represents any node. The subnodes
1020 * of this node are added to the <code>typeBuilder</code> as
1021 * methods. The subnode can be of type leaf, leaf-list, list,
1022 * container, choice.
1025 * @param schemaNodes
1026 * set of data schema nodes which are the children of the node
1027 * for which <code>typeBuilder</code> was created
1028 * @return generated type builder which is the same object as the input
1029 * parameter <code>typeBuilder</code>. The getter method could be
1032 private GeneratedTypeBuilder augSchemaNodeToMethods(final Module module, final String basePackageName,
1033 final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf,
1034 final Set<DataSchemaNode> schemaNodes) {
1035 if ((schemaNodes != null) && (typeBuilder != null)) {
1036 for (DataSchemaNode schemaNode : schemaNodes) {
1037 if (!schemaNode.isAugmenting()) {
1038 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, typeBuilder, childOf, module);
1046 * Adds to <code>typeBuilder</code> a method which is derived from
1047 * <code>schemaNode</code>.
1049 * @param basePackageName
1050 * string with the module package name
1052 * data schema node which is added to <code>typeBuilder</code> as
1054 * @param typeBuilder
1055 * generated type builder to which is <code>schemaNode</code>
1056 * added as a method.
1062 private void addSchemaNodeToBuilderAsMethod(final String basePackageName, final DataSchemaNode node,
1063 final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf, final Module module) {
1064 if (node != null && typeBuilder != null) {
1065 if (node instanceof LeafSchemaNode) {
1066 resolveLeafSchemaNodeAsMethod(typeBuilder, (LeafSchemaNode) node);
1067 } else if (node instanceof LeafListSchemaNode) {
1068 resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) node);
1069 } else if (node instanceof ContainerSchemaNode) {
1070 containerToGenType(module, basePackageName, typeBuilder, childOf, (ContainerSchemaNode) node);
1071 } else if (node instanceof ListSchemaNode) {
1072 listToGenType(module, basePackageName, typeBuilder, childOf, (ListSchemaNode) node);
1073 } else if (node instanceof ChoiceNode) {
1074 choiceToGeneratedType(module, basePackageName, typeBuilder, (ChoiceNode) node);
1076 // TODO: anyxml not yet supported
1077 LOG.debug("Unable to add schema node {} as method in {}: unsupported type of node.", node.getClass(),
1078 typeBuilder.getFullyQualifiedName());
1084 * Converts <code>choiceNode</code> to the list of generated types for
1085 * choice and its cases.
1087 * The package names for choice and for its cases are created as
1088 * concatenation of the module package (<code>basePackageName</code>) and
1089 * names of all parents node.
1093 * @param basePackageName
1094 * string with the module package name
1098 * choice node which is mapped to generated type. Also child
1099 * nodes - cases are mapped to generated types.
1100 * @throws IllegalArgumentException
1102 * <li>if <code>basePackageName</code> is null</li>
1103 * <li>if <code>choiceNode</code> is null</li>
1106 private void choiceToGeneratedType(final Module module, final String basePackageName,
1107 final GeneratedTypeBuilder parent, final ChoiceNode choiceNode) {
1108 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
1109 checkArgument(choiceNode != null, "Choice Schema Node cannot be NULL.");
1111 if (!choiceNode.isAddedByUses()) {
1112 final String packageName = packageNameForGeneratedType(basePackageName, choiceNode.getPath());
1113 final GeneratedTypeBuilder choiceTypeBuilder = addRawInterfaceDefinition(packageName, choiceNode);
1114 constructGetter(parent, choiceNode.getQName().getLocalName(), choiceNode.getDescription(),
1116 choiceTypeBuilder.addImplementsType(typeForClass(DataContainer.class));
1117 genCtx.get(module).addChildNodeType(choiceNode.getPath(), choiceTypeBuilder);
1118 generateTypesFromChoiceCases(module, basePackageName, choiceTypeBuilder.toInstance(), choiceNode);
1123 * Converts <code>caseNodes</code> set to list of corresponding generated
1126 * For every <i>case</i> which isn't added through augment or <i>uses</i> is
1127 * created generated type builder. The package names for the builder is
1128 * created as concatenation of the module package (
1129 * <code>basePackageName</code>) and names of all parents nodes of the
1130 * concrete <i>case</i>. There is also relation "<i>implements type</i>"
1131 * between every case builder and <i>choice</i> type
1135 * @param basePackageName
1136 * string with the module package name
1137 * @param refChoiceType
1138 * type which represents superior <i>case</i>
1140 * choice case node which is mapped to generated type
1141 * @return list of generated types for <code>caseNodes</code>.
1142 * @throws IllegalArgumentException
1144 * <li>if <code>basePackageName</code> equals null</li>
1145 * <li>if <code>refChoiceType</code> equals null</li>
1146 * <li>if <code>caseNodes</code> equals null</li>
1149 private void generateTypesFromChoiceCases(final Module module, final String basePackageName,
1150 final Type refChoiceType, final ChoiceNode choiceNode) {
1151 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
1152 checkArgument(refChoiceType != null, "Referenced Choice Type cannot be NULL.");
1153 checkArgument(choiceNode != null, "ChoiceNode cannot be NULL.");
1155 final Set<ChoiceCaseNode> caseNodes = choiceNode.getCases();
1156 if (caseNodes == null) {
1160 for (ChoiceCaseNode caseNode : caseNodes) {
1161 if (caseNode != null && !caseNode.isAddedByUses() && !caseNode.isAugmenting()) {
1162 final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath());
1163 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode);
1164 caseTypeBuilder.addImplementsType(refChoiceType);
1165 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
1166 genCtx.get(module).addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode);
1167 final Set<DataSchemaNode> caseChildNodes = caseNode.getChildNodes();
1168 if (caseChildNodes != null) {
1169 Object parentNode = null;
1170 final SchemaPath nodeSp = choiceNode.getPath();
1171 parentNode = findDataSchemaNode(schemaContext, nodeSp.getParent());
1174 if (parentNode instanceof AugmentationSchema) {
1175 final AugmentationSchema augSchema = (AugmentationSchema) parentNode;
1176 final SchemaPath targetPath = augSchema.getTargetPath();
1177 SchemaNode targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
1178 if (targetSchemaNode instanceof DataSchemaNode
1179 && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
1180 if (targetSchemaNode instanceof DerivableSchemaNode) {
1181 targetSchemaNode = ((DerivableSchemaNode) targetSchemaNode).getOriginal().orNull();
1183 if (targetSchemaNode == null) {
1184 throw new IllegalStateException(
1185 "Failed to find target node from grouping for augmentation " + augSchema
1186 + " in module " + module.getName());
1189 parent = targetSchemaNode;
1191 final SchemaPath sp = choiceNode.getPath();
1192 parent = findDataSchemaNode(schemaContext, sp.getParent());
1194 GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.getPath());
1195 if (childOfType == null) {
1196 childOfType = findGroupingByPath(parent.getPath());
1198 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, caseChildNodes);
1202 processUsesAugments(caseNode, module);
1207 * Generates list of generated types for all the cases of a choice which are
1208 * added to the choice through the augment.
1212 * @param basePackageName
1213 * string contains name of package to which augment belongs. If
1214 * an augmented choice is from an other package (pcg1) than an
1215 * augmenting choice (pcg2) then case's of the augmenting choice
1216 * will belong to pcg2.
1218 * Type which represents target choice
1220 * node which represents target choice
1221 * @param augmentedNodes
1222 * set of choice case nodes for which is checked if are/aren't
1223 * added to choice through augmentation
1224 * @return list of generated types which represents augmented cases of
1225 * choice <code>refChoiceType</code>
1226 * @throws IllegalArgumentException
1228 * <li>if <code>basePackageName</code> is null</li>
1229 * <li>if <code>targetType</code> is null</li>
1230 * <li>if <code>augmentedNodes</code> is null</li>
1233 private void generateTypesFromAugmentedChoiceCases(final Module module, final String basePackageName,
1234 final Type targetType, final ChoiceNode targetNode, final Set<DataSchemaNode> augmentedNodes) {
1235 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
1236 checkArgument(targetType != null, "Referenced Choice Type cannot be NULL.");
1237 checkArgument(augmentedNodes != null, "Set of Choice Case Nodes cannot be NULL.");
1239 for (DataSchemaNode caseNode : augmentedNodes) {
1240 if (caseNode != null) {
1241 final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath());
1242 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode);
1243 caseTypeBuilder.addImplementsType(targetType);
1245 SchemaNode parent = null;
1246 final SchemaPath nodeSp = targetNode.getPath();
1247 parent = findDataSchemaNode(schemaContext, nodeSp.getParent());
1249 GeneratedTypeBuilder childOfType = null;
1250 if (parent instanceof Module) {
1251 childOfType = genCtx.get(parent).getModuleNode();
1252 } else if (parent instanceof ChoiceCaseNode) {
1253 childOfType = findCaseByPath(parent.getPath());
1254 } else if (parent instanceof DataSchemaNode || parent instanceof NotificationDefinition) {
1255 childOfType = findChildNodeByPath(parent.getPath());
1256 } else if (parent instanceof GroupingDefinition) {
1257 childOfType = findGroupingByPath(parent.getPath());
1260 if (childOfType == null) {
1261 throw new IllegalArgumentException("Failed to find parent type of choice " + targetNode);
1264 ChoiceCaseNode node = null;
1265 if (caseNode instanceof ChoiceCaseNode) {
1266 node = (ChoiceCaseNode) caseNode;
1268 node = targetNode.getCaseNodeByName(caseNode.getQName().getLocalName());
1270 final Set<DataSchemaNode> childNodes = node.getChildNodes();
1271 if (childNodes != null) {
1272 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, childNodes);
1274 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
1275 genCtx.get(module).addChoiceToCaseMapping(targetType, caseTypeBuilder, node);
1281 * Converts <code>leaf</code> to the getter method which is added to
1282 * <code>typeBuilder</code>.
1284 * @param typeBuilder
1285 * generated type builder to which is added getter method as
1286 * <code>leaf</code> mapping
1288 * leaf schema node which is mapped as getter method which is
1289 * added to <code>typeBuilder</code>
1290 * @return boolean value
1292 * <li>false - if <code>leaf</code> or <code>typeBuilder</code> are
1294 * <li>true - in other cases</li>
1297 private Type resolveLeafSchemaNodeAsMethod(final GeneratedTypeBuilder typeBuilder, final LeafSchemaNode leaf) {
1298 Type returnType = null;
1299 if ((leaf != null) && (typeBuilder != null)) {
1300 final String leafName = leaf.getQName().getLocalName();
1301 String leafDesc = leaf.getDescription();
1302 if (leafDesc == null) {
1306 final Module parentModule = findParentModule(schemaContext, leaf);
1307 if (leafName != null && !leaf.isAddedByUses()) {
1308 final TypeDefinition<?> typeDef = leaf.getType();
1310 GeneratedTOBuilder genTOBuilder;
1311 if (typeDef instanceof EnumTypeDefinition) {
1312 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1313 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
1314 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(),
1317 if (enumBuilder != null) {
1318 returnType = enumBuilder.toInstance(typeBuilder);
1320 ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
1321 } else if (typeDef instanceof UnionType) {
1322 genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
1323 if (genTOBuilder != null) {
1324 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
1326 } else if (typeDef instanceof BitsTypeDefinition) {
1327 genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
1328 if (genTOBuilder != null) {
1329 returnType = new ReferencedTypeImpl(genTOBuilder.getPackageName(), genTOBuilder.getName());
1332 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
1333 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
1335 if (returnType != null) {
1336 final MethodSignatureBuilder getter = constructGetter(typeBuilder, leafName, leafDesc, returnType);
1337 processContextRefExtension(leaf, getter, parentModule);
1344 private void processContextRefExtension(final LeafSchemaNode leaf, final MethodSignatureBuilder getter,
1345 final Module module) {
1346 for (UnknownSchemaNode node : leaf.getUnknownSchemaNodes()) {
1347 final QName nodeType = node.getNodeType();
1348 if ("context-reference".equals(nodeType.getLocalName())) {
1349 final String nodeParam = node.getNodeParameter();
1350 IdentitySchemaNode identity = null;
1351 String basePackageName = null;
1352 final Iterable<String> splittedElement = Splitter.on(':').split(nodeParam);
1353 final Iterator<String> iterator = splittedElement.iterator();
1354 final int length = Iterables.size(splittedElement);
1356 identity = findIdentityByName(module.getIdentities(), iterator.next());
1357 basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
1358 } else if (length == 2) {
1359 String prefix = iterator.next();
1360 final Module dependentModule = findModuleFromImports(module.getImports(), prefix);
1361 if (dependentModule == null) {
1362 throw new IllegalArgumentException("Failed to process context-reference: unknown prefix "
1365 identity = findIdentityByName(dependentModule.getIdentities(), iterator.next());
1366 basePackageName = BindingMapping.getRootPackageName(dependentModule.getQNameModule());
1368 throw new IllegalArgumentException("Failed to process context-reference: unknown identity "
1371 if (identity == null) {
1372 throw new IllegalArgumentException("Failed to process context-reference: unknown identity "
1376 final Class<RoutingContext> clazz = RoutingContext.class;
1377 final AnnotationTypeBuilder rc = getter.addAnnotation(clazz.getPackage().getName(),
1378 clazz.getSimpleName());
1379 final String packageName = packageNameForGeneratedType(basePackageName, identity.getPath());
1380 final String genTypeName = BindingMapping.getClassName(identity.getQName().getLocalName());
1381 rc.addParameter("value", packageName + "." + genTypeName + ".class");
1386 private IdentitySchemaNode findIdentityByName(final Set<IdentitySchemaNode> identities, final String name) {
1387 for (IdentitySchemaNode id : identities) {
1388 if (id.getQName().getLocalName().equals(name)) {
1395 private Module findModuleFromImports(final Set<ModuleImport> imports, final String prefix) {
1396 for (ModuleImport imp : imports) {
1397 if (imp.getPrefix().equals(prefix)) {
1398 return schemaContext.findModuleByName(imp.getModuleName(), imp.getRevision());
1404 private boolean resolveLeafSchemaNodeAsProperty(final GeneratedTOBuilder toBuilder, final LeafSchemaNode leaf,
1405 final boolean isReadOnly, final Module module) {
1406 if ((leaf != null) && (toBuilder != null)) {
1407 final String leafName = leaf.getQName().getLocalName();
1408 String leafDesc = leaf.getDescription();
1409 if (leafDesc == null) {
1413 if (leafName != null) {
1414 Type returnType = null;
1415 final TypeDefinition<?> typeDef = leaf.getType();
1416 if (typeDef instanceof UnionTypeDefinition) {
1417 // GeneratedType for this type definition should be already
1419 QName qname = typeDef.getQName();
1420 Module unionModule = null;
1421 String prefix = qname.getPrefix();
1422 if (prefix == null || prefix.isEmpty() || prefix.equals(module.getPrefix())) {
1423 unionModule = module;
1425 unionModule = findModuleFromImports(module.getImports(), qname.getPrefix());
1427 final ModuleContext mc = genCtx.get(unionModule);
1428 returnType = mc.getTypedefs().get(typeDef.getPath());
1430 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1432 return resolveLeafSchemaNodeAsProperty(toBuilder, leaf, returnType, isReadOnly);
1439 * Converts <code>leaf</code> schema node to property of generated TO
1443 * generated TO builder to which is <code>leaf</code> added as
1446 * leaf schema node which is added to <code>toBuilder</code> as
1451 * boolean value which says if leaf property is|isn't read only
1452 * @return boolean value
1454 * <li>false - if <code>leaf</code>, <code>toBuilder</code> or leaf
1455 * name equals null or if leaf is added by <i>uses</i>.</li>
1456 * <li>true - other cases</li>
1459 private boolean resolveLeafSchemaNodeAsProperty(final GeneratedTOBuilder toBuilder, final LeafSchemaNode leaf,
1460 final Type returnType, final boolean isReadOnly) {
1461 if (returnType == null) {
1464 final String leafName = leaf.getQName().getLocalName();
1465 final String leafDesc = leaf.getDescription();
1466 final GeneratedPropertyBuilder propBuilder = toBuilder.addProperty(parseToValidParamName(leafName));
1467 propBuilder.setReadOnly(isReadOnly);
1468 propBuilder.setReturnType(returnType);
1469 propBuilder.setComment(leafDesc);
1470 toBuilder.addEqualsIdentity(propBuilder);
1471 toBuilder.addHashIdentity(propBuilder);
1472 toBuilder.addToStringProperty(propBuilder);
1477 * Converts <code>node</code> leaf list schema node to getter method of
1478 * <code>typeBuilder</code>.
1480 * @param typeBuilder
1481 * generated type builder to which is <code>node</code> added as
1484 * leaf list schema node which is added to
1485 * <code>typeBuilder</code> as getter method
1486 * @return boolean value
1488 * <li>true - if <code>node</code>, <code>typeBuilder</code>,
1489 * nodeName equal null or <code>node</code> is added by <i>uses</i></li>
1490 * <li>false - other cases</li>
1493 private boolean resolveLeafListSchemaNode(final GeneratedTypeBuilder typeBuilder, final LeafListSchemaNode node) {
1494 if ((node != null) && (typeBuilder != null)) {
1495 final QName nodeName = node.getQName();
1497 if (nodeName != null && !node.isAddedByUses()) {
1498 final TypeDefinition<?> typeDef = node.getType();
1499 final Module parentModule = findParentModule(schemaContext, node);
1501 Type returnType = null;
1502 if (typeDef instanceof EnumTypeDefinition) {
1503 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node);
1504 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
1505 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, nodeName,
1507 returnType = new ReferencedTypeImpl(enumBuilder.getPackageName(), enumBuilder.getName());
1508 ((TypeProviderImpl) typeProvider).putReferencedType(node.getPath(), returnType);
1509 } else if (typeDef instanceof UnionType) {
1510 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule);
1511 if (genTOBuilder != null) {
1512 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
1514 } else if (typeDef instanceof BitsTypeDefinition) {
1515 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule);
1516 returnType = new ReferencedTypeImpl(genTOBuilder.getPackageName(), genTOBuilder.getName());
1518 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
1519 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
1522 final ParameterizedType listType = Types.listTypeFor(returnType);
1523 constructGetter(typeBuilder, nodeName.getLocalName(), node.getDescription(), listType);
1530 private Type createReturnTypeForUnion(final GeneratedTOBuilder genTOBuilder, final TypeDefinition<?> typeDef,
1531 final GeneratedTypeBuilder typeBuilder, final Module parentModule) {
1532 final GeneratedTOBuilderImpl returnType = new GeneratedTOBuilderImpl(genTOBuilder.getPackageName(),
1533 genTOBuilder.getName());
1535 returnType.setDescription(typeDef.getDescription());
1536 returnType.setReference(typeDef.getReference());
1537 returnType.setSchemaPath(typeDef.getPath().getPathFromRoot());
1538 returnType.setModuleName(parentModule.getName());
1540 genTOBuilder.setTypedef(true);
1541 genTOBuilder.setIsUnion(true);
1542 ((TypeProviderImpl) typeProvider).addUnitsToGenTO(genTOBuilder, typeDef.getUnits());
1545 final GeneratedTOBuilder unionBuilder = new GeneratedTOBuilderImpl(typeBuilder.getPackageName(),
1546 genTOBuilder.getName() + "Builder");
1547 unionBuilder.setIsUnionBuilder(true);
1548 final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
1549 method.setReturnType(returnType);
1550 method.addParameter(Types.STRING, "defaultValue");
1551 method.setAccessModifier(AccessModifier.PUBLIC);
1552 method.setStatic(true);
1554 final Set<Type> types = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(parentModule);
1555 if (types == null) {
1556 ((TypeProviderImpl) typeProvider).getAdditionalTypes().put(parentModule,
1557 Sets.<Type> newHashSet(unionBuilder.toInstance()));
1559 types.add(unionBuilder.toInstance());
1561 return returnType.toInstance();
1564 private GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode schemaNode) {
1565 return addDefaultInterfaceDefinition(packageName, schemaNode, null);
1569 * Instantiates generated type builder with <code>packageName</code> and
1570 * <code>schemaNode</code>.
1572 * The new builder always implements
1573 * {@link org.opendaylight.yangtools.yang.binding.DataObject DataObject}.<br />
1574 * If <code>schemaNode</code> is instance of GroupingDefinition it also
1575 * implements {@link org.opendaylight.yangtools.yang.binding.Augmentable
1576 * Augmentable}.<br />
1577 * If <code>schemaNode</code> is instance of
1578 * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer
1579 * DataNodeContainer} it can also implement nodes which are specified in
1582 * @param packageName
1583 * string with the name of the package to which
1584 * <code>schemaNode</code> belongs.
1586 * schema node for which is created generated type builder
1588 * parent type (can be null)
1589 * @return generated type builder <code>schemaNode</code>
1591 private GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode schemaNode,
1592 final Type parent) {
1593 final GeneratedTypeBuilder it = addRawInterfaceDefinition(packageName, schemaNode, "");
1594 if (parent == null) {
1595 it.addImplementsType(DATA_OBJECT);
1597 it.addImplementsType(BindingTypes.childOf(parent));
1599 if (!(schemaNode instanceof GroupingDefinition)) {
1600 it.addImplementsType(augmentable(it));
1603 if (schemaNode instanceof DataNodeContainer) {
1604 addImplementedInterfaceFromUses((DataNodeContainer) schemaNode, it);
1611 * Wraps the calling of the same overloaded method.
1613 * @param packageName
1614 * string with the package name to which returning generated type
1617 * schema node which provide data about the schema node name
1618 * @return generated type builder for <code>schemaNode</code>
1620 private GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode) {
1621 return addRawInterfaceDefinition(packageName, schemaNode, "");
1625 * Returns reference to generated type builder for specified
1626 * <code>schemaNode</code> with <code>packageName</code>.
1628 * Firstly the generated type builder is searched in
1629 * {@link BindingGeneratorImpl#genTypeBuilders genTypeBuilders}. If it isn't
1630 * found it is created and added to <code>genTypeBuilders</code>.
1632 * @param packageName
1633 * string with the package name to which returning generated type
1636 * schema node which provide data about the schema node name
1638 * return type name prefix
1639 * @return generated type builder for <code>schemaNode</code>
1640 * @throws IllegalArgumentException
1642 * <li>if <code>schemaNode</code> is null</li>
1643 * <li>if <code>packageName</code> is null</li>
1644 * <li>if QName of schema node is null</li>
1645 * <li>if schemaNode name is null</li>
1649 private GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode,
1650 final String prefix) {
1651 checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
1652 checkArgument(packageName != null, "Package Name for Generated Type cannot be NULL.");
1653 checkArgument(schemaNode.getQName() != null, "QName for Data Schema Node cannot be NULL.");
1654 final String schemaNodeName = schemaNode.getQName().getLocalName();
1655 checkArgument(schemaNodeName != null, "Local Name of QName for Data Schema Node cannot be NULL.");
1658 if (prefix == null) {
1659 genTypeName = BindingMapping.getClassName(schemaNodeName);
1661 genTypeName = prefix + BindingMapping.getClassName(schemaNodeName);
1664 // FIXME: Validation of name conflict
1665 final GeneratedTypeBuilderImpl newType = new GeneratedTypeBuilderImpl(packageName, genTypeName);
1666 qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, schemaNode.getQName());
1667 newType.addComment(schemaNode.getDescription());
1668 newType.setDescription(schemaNode.getDescription());
1669 newType.setReference(schemaNode.getReference());
1670 newType.setSchemaPath(schemaNode.getPath().getPathFromRoot());
1672 final Module module = findParentModule(schemaContext, schemaNode);
1673 newType.setModuleName(module.getName());
1675 if (!genTypeBuilders.containsKey(packageName)) {
1676 final Map<String, GeneratedTypeBuilder> builders = new HashMap<>();
1677 builders.put(genTypeName, newType);
1678 genTypeBuilders.put(packageName, builders);
1680 final Map<String, GeneratedTypeBuilder> builders = genTypeBuilders.get(packageName);
1681 if (!builders.containsKey(genTypeName)) {
1682 builders.put(genTypeName, newType);
1689 * Creates the name of the getter method name from <code>localName</code>.
1692 * string with the name of the getter method
1695 * @return string with the name of the getter method for
1696 * <code>methodName</code> in JAVA method format
1698 public static String getterMethodName(final String localName, final Type returnType) {
1699 final StringBuilder method = new StringBuilder();
1700 if (BOOLEAN.equals(returnType)) {
1701 method.append("is");
1703 method.append("get");
1705 String name = BindingMapping.toFirstUpper(BindingMapping.getPropertyName(localName));
1706 method.append(name);
1707 return method.toString();
1711 * Created a method signature builder as part of
1712 * <code>interfaceBuilder</code>.
1714 * The method signature builder is created for the getter method of
1715 * <code>schemaNodeName</code>. Also <code>comment</code> and
1716 * <code>returnType</code> information are added to the builder.
1718 * @param interfaceBuilder
1719 * generated type builder for which the getter method should be
1721 * @param schemaNodeName
1722 * string with schema node name. The name will be the part of the
1723 * getter method name.
1725 * string with comment for the getter method
1727 * type which represents the return type of the getter method
1728 * @return method signature builder which represents the getter method of
1729 * <code>interfaceBuilder</code>
1731 private MethodSignatureBuilder constructGetter(final GeneratedTypeBuilder interfaceBuilder,
1732 final String schemaNodeName, final String comment, final Type returnType) {
1733 final MethodSignatureBuilder getMethod = interfaceBuilder
1734 .addMethod(getterMethodName(schemaNodeName, returnType));
1735 getMethod.setComment(comment);
1736 getMethod.setReturnType(returnType);
1741 * Adds <code>schemaNode</code> to <code>typeBuilder</code> as getter method
1742 * or to <code>genTOBuilder</code> as property.
1744 * @param basePackageName
1745 * string contains the module package name
1747 * data schema node which should be added as getter method to
1748 * <code>typeBuilder</code> or as a property to
1749 * <code>genTOBuilder</code> if is part of the list key
1750 * @param typeBuilder
1751 * generated type builder for the list schema node
1752 * @param genTOBuilder
1753 * generated TO builder for the list keys
1755 * list of string which contains names of the list keys
1758 * @throws IllegalArgumentException
1760 * <li>if <code>schemaNode</code> equals null</li>
1761 * <li>if <code>typeBuilder</code> equals null</li>
1764 private void addSchemaNodeToListBuilders(final String basePackageName, final DataSchemaNode schemaNode,
1765 final GeneratedTypeBuilder typeBuilder, final GeneratedTOBuilder genTOBuilder, final List<String> listKeys,
1766 final Module module) {
1767 checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
1768 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
1770 if (schemaNode instanceof LeafSchemaNode) {
1771 final LeafSchemaNode leaf = (LeafSchemaNode) schemaNode;
1772 final String leafName = leaf.getQName().getLocalName();
1773 final Type type = resolveLeafSchemaNodeAsMethod(typeBuilder, leaf);
1774 if (listKeys.contains(leafName)) {
1776 resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, true, module);
1778 resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, type, true);
1781 } else if (!schemaNode.isAddedByUses()) {
1782 if (schemaNode instanceof LeafListSchemaNode) {
1783 resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) schemaNode);
1784 } else if (schemaNode instanceof ContainerSchemaNode) {
1785 containerToGenType(module, basePackageName, typeBuilder, typeBuilder, (ContainerSchemaNode) schemaNode);
1786 } else if (schemaNode instanceof ChoiceNode) {
1787 choiceToGeneratedType(module, basePackageName, typeBuilder, (ChoiceNode) schemaNode);
1788 } else if (schemaNode instanceof ListSchemaNode) {
1789 listToGenType(module, basePackageName, typeBuilder, typeBuilder, (ListSchemaNode) schemaNode);
1794 private void typeBuildersToGenTypes(final Module module, final GeneratedTypeBuilder typeBuilder,
1795 final GeneratedTOBuilder genTOBuilder) {
1796 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
1798 if (genTOBuilder != null) {
1799 GeneratedTransferObject genTO = genTOBuilder.toInstance();
1800 constructGetter(typeBuilder, "key", "Returns Primary Key of Yang List Type", genTO);
1801 genCtx.get(module).addGeneratedTOBuilder(genTOBuilder);
1806 * Selects the names of the list keys from <code>list</code> and returns
1807 * them as the list of the strings
1810 * of string with names of the list keys
1811 * @return list of string which represents names of the list keys. If the
1812 * <code>list</code> contains no keys then the empty list is
1815 private List<String> listKeys(final ListSchemaNode list) {
1816 final List<String> listKeys = new ArrayList<>();
1818 List<QName> keyDefinition = list.getKeyDefinition();
1819 if (keyDefinition != null) {
1820 for (QName keyDef : keyDefinition) {
1821 listKeys.add(keyDef.getLocalName());
1828 * Generates for the <code>list</code> which contains any list keys special
1829 * generated TO builder.
1831 * @param packageName
1832 * string with package name to which the list belongs
1834 * list schema node which is source of data about the list name
1835 * @return generated TO builder which represents the keys of the
1836 * <code>list</code> or null if <code>list</code> is null or list of
1837 * key definitions is null or empty.
1839 private GeneratedTOBuilder resolveListKeyTOBuilder(final String packageName, final ListSchemaNode list) {
1840 GeneratedTOBuilder genTOBuilder = null;
1841 if ((list.getKeyDefinition() != null) && (!list.getKeyDefinition().isEmpty())) {
1842 final String listName = list.getQName().getLocalName() + "Key";
1843 final String genTOName = BindingMapping.getClassName(listName);
1844 genTOBuilder = new GeneratedTOBuilderImpl(packageName, genTOName);
1846 return genTOBuilder;
1850 * Builds generated TO builders for <code>typeDef</code> of type
1851 * {@link org.opendaylight.yangtools.yang.model.util.UnionType UnionType} or
1852 * {@link org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
1853 * BitsTypeDefinition} which are also added to <code>typeBuilder</code> as
1854 * enclosing transfer object.
1856 * If more then one generated TO builder is created for enclosing then all
1857 * of the generated TO builders are added to <code>typeBuilder</code> as
1858 * enclosing transfer objects.
1861 * type definition which can be of type <code>UnionType</code> or
1862 * <code>BitsTypeDefinition</code>
1863 * @param typeBuilder
1864 * generated type builder to which is added generated TO created
1865 * from <code>typeDef</code>
1867 * string with name for generated TO builder
1869 * @return generated TO builder for <code>typeDef</code>
1871 private GeneratedTOBuilder addTOToTypeBuilder(final TypeDefinition<?> typeDef,
1872 final GeneratedTypeBuilder typeBuilder, final DataSchemaNode leaf, final Module parentModule) {
1873 final String classNameFromLeaf = BindingMapping.getClassName(leaf.getQName());
1874 final List<GeneratedTOBuilder> genTOBuilders = new ArrayList<>();
1875 final String packageName = typeBuilder.getFullyQualifiedName();
1876 if (typeDef instanceof UnionTypeDefinition) {
1877 final List<GeneratedTOBuilder> types = ((TypeProviderImpl) typeProvider)
1878 .provideGeneratedTOBuildersForUnionTypeDef(packageName, ((UnionTypeDefinition) typeDef),
1879 classNameFromLeaf, leaf);
1880 genTOBuilders.addAll(types);
1882 GeneratedTOBuilder resultTOBuilder = null;
1883 if (!types.isEmpty()) {
1884 resultTOBuilder = types.remove(0);
1885 for (GeneratedTOBuilder genTOBuilder : types) {
1886 resultTOBuilder.addEnclosingTransferObject(genTOBuilder);
1890 final GeneratedPropertyBuilder genPropBuilder = resultTOBuilder.addProperty("value");
1891 genPropBuilder.setReturnType(Types.primitiveType("char[]", null));
1892 resultTOBuilder.addEqualsIdentity(genPropBuilder);
1893 resultTOBuilder.addHashIdentity(genPropBuilder);
1894 resultTOBuilder.addToStringProperty(genPropBuilder);
1896 } else if (typeDef instanceof BitsTypeDefinition) {
1897 genTOBuilders.add((((TypeProviderImpl) typeProvider)).provideGeneratedTOBuilderForBitsTypeDefinition(
1898 packageName, typeDef, classNameFromLeaf, parentModule.getName()));
1900 if (genTOBuilders != null && !genTOBuilders.isEmpty()) {
1901 for (GeneratedTOBuilder genTOBuilder : genTOBuilders) {
1902 typeBuilder.addEnclosingTransferObject(genTOBuilder);
1904 return genTOBuilders.get(0);
1911 * Adds the implemented types to type builder.
1913 * The method passes through the list of <i>uses</i> in
1914 * {@code dataNodeContainer}. For every <i>use</i> is obtained corresponding
1915 * generated type from {@link BindingGeneratorImpl#allGroupings
1916 * allGroupings} which is added as <i>implements type</i> to
1917 * <code>builder</code>
1919 * @param dataNodeContainer
1920 * element which contains the list of used YANG groupings
1922 * builder to which are added implemented types according to
1923 * <code>dataNodeContainer</code>
1924 * @return generated type builder with all implemented types
1926 private GeneratedTypeBuilder addImplementedInterfaceFromUses(final DataNodeContainer dataNodeContainer,
1927 final GeneratedTypeBuilder builder) {
1928 for (UsesNode usesNode : dataNodeContainer.getUses()) {
1929 if (usesNode.getGroupingPath() != null) {
1930 final GeneratedType genType = findGroupingByPath(usesNode.getGroupingPath()).toInstance();
1931 if (genType == null) {
1932 throw new IllegalStateException("Grouping " + usesNode.getGroupingPath() + "is not resolved for "
1933 + builder.getName());
1935 builder.addImplementsType(genType);
1936 builder.addComment(genType.getComment());
1942 private GeneratedTypeBuilder findChildNodeByPath(final SchemaPath path) {
1943 for (ModuleContext ctx : genCtx.values()) {
1944 GeneratedTypeBuilder result = ctx.getChildNode(path);
1945 if (result != null) {
1952 private GeneratedTypeBuilder findGroupingByPath(final SchemaPath path) {
1953 for (ModuleContext ctx : genCtx.values()) {
1954 GeneratedTypeBuilder result = ctx.getGrouping(path);
1955 if (result != null) {
1962 private GeneratedTypeBuilder findCaseByPath(final SchemaPath path) {
1963 for (ModuleContext ctx : genCtx.values()) {
1964 GeneratedTypeBuilder result = ctx.getCase(path);
1965 if (result != null) {
1972 public Map<Module, ModuleContext> getModuleContexts() {