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.BindingTypes.DATA_OBJECT;
16 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.DATA_ROOT;
17 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.IDENTIFIABLE;
18 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.IDENTIFIER;
19 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.NOTIFICATION;
20 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.augmentable;
21 import static org.opendaylight.yangtools.binding.generator.util.Types.BOOLEAN;
22 import static org.opendaylight.yangtools.binding.generator.util.Types.FUTURE;
23 import static org.opendaylight.yangtools.binding.generator.util.Types.VOID;
24 import static org.opendaylight.yangtools.binding.generator.util.Types.typeForClass;
25 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode;
26 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findNodeInSchemaContext;
27 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
29 import com.google.common.base.Preconditions;
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.ChoiceSchemaNode;
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);
108 private static final Splitter COLON_SPLITTER = Splitter.on(':');
109 private static final Splitter BSDOT_SPLITTER = Splitter.on("\\.");
110 private static final char NEW_LINE = '\n';
113 * Constant with the concrete name of identifier.
115 private static final String AUGMENT_IDENTIFIER_NAME = "augment-identifier";
118 * Constant with the concrete name of namespace.
120 private static final String YANG_EXT_NAMESPACE = "urn:opendaylight:yang:extension:yang-ext";
122 private final Map<Module, ModuleContext> genCtx = new HashMap<>();
125 * When set to true, generated classes will include javadoc comments which
126 * are useful for users.
128 private final boolean verboseClassComments;
131 * Outer key represents the package name. Outer value represents map of all
132 * builders in the same package. Inner key represents the schema node name
133 * (in JAVA class/interface name format). Inner value represents instance of
134 * builder for schema node specified in key part.
136 private Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders;
139 * Provide methods for converting YANG types to JAVA types.
141 private TypeProvider typeProvider;
144 * Holds reference to schema context to resolve data of augmented element
145 * when creating augmentation builder
147 private SchemaContext schemaContext;
150 * Create a new binding generator with verboe comments.
152 * @deprecated Use {@link #BindingGeneratorImpl(boolean)} instead.
155 public BindingGeneratorImpl() {
160 * Create a new binding generator.
162 * @param verboseClassComments generate verbose comments
164 public BindingGeneratorImpl(final boolean verboseClassComments) {
165 this.verboseClassComments = verboseClassComments;
169 * Resolves generated types from <code>context</code> schema nodes of all
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
178 * @return list of types (usually <code>GeneratedType</code>
179 * <code>GeneratedTransferObject</code>which are generated from
180 * <code>context</code> data.
181 * @throws IllegalArgumentException
182 * if arg <code>context</code> is null
183 * @throws IllegalStateException
184 * if <code>context</code> contain no modules
187 public List<Type> generateTypes(final SchemaContext context) {
188 checkArgument(context != null, "Schema Context reference cannot be NULL.");
189 checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
190 schemaContext = context;
191 typeProvider = new TypeProviderImpl(context);
192 final Set<Module> modules = context.getModules();
193 return generateTypes(context, modules);
197 * Resolves generated types from <code>context</code> schema nodes only for
198 * modules specified in <code>modules</code>
200 * Generated types are created for modules, groupings, types, containers,
201 * lists, choices, augments, rpcs, notification, identities.
204 * schema context which contains data about all schema nodes
207 * set of modules for which schema nodes should be generated
209 * @return list of types (usually <code>GeneratedType</code> or
210 * <code>GeneratedTransferObject</code>) which:
212 * <li>are generated from <code>context</code> schema nodes and</li>
213 * <li>are also part of some of the module in <code>modules</code>
216 * @throws IllegalArgumentException
218 * <li>if arg <code>context</code> is null or</li>
219 * <li>if arg <code>modules</code> is null</li>
221 * @throws IllegalStateException
222 * if <code>context</code> contain no modules
225 public List<Type> generateTypes(final SchemaContext context, final Set<Module> modules) {
226 checkArgument(context != null, "Schema Context reference cannot be NULL.");
227 checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
228 checkArgument(modules != null, "Set of Modules cannot be NULL.");
230 schemaContext = context;
231 typeProvider = new TypeProviderImpl(context);
232 final Module[] modulesArray = new Module[context.getModules().size()];
233 context.getModules().toArray(modulesArray);
234 final List<Module> contextModules = ModuleDependencySort.sort(modulesArray);
235 genTypeBuilders = new HashMap<>();
237 for (final Module contextModule : contextModules) {
238 moduleToGenTypes(contextModule, context);
240 for (final Module contextModule : contextModules) {
241 allAugmentsToGenTypes(contextModule);
244 final List<Type> filteredGenTypes = new ArrayList<>();
245 for (final Module m : modules) {
246 final ModuleContext ctx = checkNotNull(genCtx.get(m), "Module context not found for module %s", m);
247 filteredGenTypes.addAll(ctx.getGeneratedTypes());
248 final Set<Type> additionalTypes = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(m);
249 if (additionalTypes != null) {
250 filteredGenTypes.addAll(additionalTypes);
254 return filteredGenTypes;
257 private void moduleToGenTypes(final Module m, final SchemaContext context) {
258 genCtx.put(m, new ModuleContext());
259 allTypeDefinitionsToGenTypes(m);
260 groupingsToGenTypes(m, m.getGroupings());
261 rpcMethodsToGenType(m);
262 allIdentitiesToGenTypes(m, context);
263 notificationsToGenType(m);
265 if (!m.getChildNodes().isEmpty()) {
266 final GeneratedTypeBuilder moduleType = moduleToDataType(m);
267 genCtx.get(m).addModuleNode(moduleType);
268 final String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
269 resolveDataSchemaNodes(m, basePackageName, moduleType, moduleType, m.getChildNodes());
274 * Converts all extended type definitions of module to the list of
275 * <code>Type</code> objects.
278 * module from which is obtained set of type definitions
279 * @throws IllegalArgumentException
281 * <li>if module is null</li>
282 * <li>if name of module is null</li>
284 * @throws IllegalStateException
285 * if set of type definitions from module is null
287 private void allTypeDefinitionsToGenTypes(final Module module) {
288 checkArgument(module != null, "Module reference cannot be NULL.");
289 checkArgument(module.getName() != null, "Module name cannot be NULL.");
290 final DataNodeIterator it = new DataNodeIterator(module);
291 final List<TypeDefinition<?>> typeDefinitions = it.allTypedefs();
292 checkState(typeDefinitions != null, "Type Definitions for module «module.name» cannot be NULL.");
294 for (final TypeDefinition<?> typedef : typeDefinitions) {
295 if (typedef != null) {
296 final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef,
299 final ModuleContext ctx = genCtx.get(module);
300 ctx.addTypedefType(typedef.getPath(), type);
301 ctx.addTypeToSchema(type,typedef);
307 private GeneratedTypeBuilder processDataSchemaNode(final Module module, final String basePackageName,
308 final GeneratedTypeBuilder childOf, final DataSchemaNode node) {
309 if (node.isAugmenting() || node.isAddedByUses()) {
312 final String packageName = packageNameForGeneratedType(basePackageName, node.getPath());
313 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, node, childOf, module);
314 genType.addComment(node.getDescription());
315 genType.setDescription(createDescription(node, genType.getFullyQualifiedName()));
316 genType.setModuleName(module.getName());
317 genType.setReference(node.getReference());
318 genType.setSchemaPath(node.getPath().getPathFromRoot());
319 if (node instanceof DataNodeContainer) {
320 genCtx.get(module).addChildNodeType(node, genType);
321 groupingsToGenTypes(module, ((DataNodeContainer) node).getGroupings());
322 processUsesAugments((DataNodeContainer) node, module);
327 private void containerToGenType(final Module module, final String basePackageName,
328 final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf, final ContainerSchemaNode node) {
329 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node);
330 if (genType != null) {
331 constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), genType);
332 resolveDataSchemaNodes(module, basePackageName, genType, genType, node.getChildNodes());
336 private void listToGenType(final Module module, final String basePackageName, final GeneratedTypeBuilder parent,
337 final GeneratedTypeBuilder childOf, final ListSchemaNode node) {
338 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node);
339 if (genType != null) {
340 constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), Types.listTypeFor(genType));
342 final List<String> listKeys = listKeys(node);
343 final String packageName = packageNameForGeneratedType(basePackageName, node.getPath());
344 final GeneratedTOBuilder genTOBuilder = resolveListKeyTOBuilder(packageName, node);
345 if (genTOBuilder != null) {
346 final Type identifierMarker = Types.parameterizedTypeFor(IDENTIFIER, genType);
347 final Type identifiableMarker = Types.parameterizedTypeFor(IDENTIFIABLE, genTOBuilder);
348 genTOBuilder.addImplementsType(identifierMarker);
349 genType.addImplementsType(identifiableMarker);
352 for (final DataSchemaNode schemaNode : node.getChildNodes()) {
353 if (!schemaNode.isAugmenting()) {
354 addSchemaNodeToListBuilders(basePackageName, schemaNode, genType, genTOBuilder, listKeys, module);
359 if (genTOBuilder != null) {
360 final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
361 prop.setValue(Long.toString(computeDefaultSUID(genTOBuilder)));
362 genTOBuilder.setSUID(prop);
365 typeBuildersToGenTypes(module, genType, genTOBuilder);
369 private void processUsesAugments(final DataNodeContainer node, final Module module) {
370 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
371 for (final UsesNode usesNode : node.getUses()) {
372 for (final AugmentationSchema augment : usesNode.getAugmentations()) {
373 usesAugmentationToGenTypes(basePackageName, augment, module, usesNode, node);
374 processUsesAugments(augment, module);
380 * Converts all <b>augmentation</b> of the module to the list
381 * <code>Type</code> objects.
384 * module from which is obtained list of all augmentation objects
385 * to iterate over them
386 * @throws IllegalArgumentException
388 * <li>if the module is null</li>
389 * <li>if the name of module is null</li>
391 * @throws IllegalStateException
392 * if set of augmentations from module is null
394 private void allAugmentsToGenTypes(final Module module) {
395 checkArgument(module != null, "Module reference cannot be NULL.");
396 checkArgument(module.getName() != null, "Module name cannot be NULL.");
397 checkState(module.getAugmentations() != null, "Augmentations Set cannot be NULL.");
399 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
400 final List<AugmentationSchema> augmentations = resolveAugmentations(module);
401 for (final AugmentationSchema augment : augmentations) {
402 augmentationToGenTypes(basePackageName, augment, module);
407 * Returns list of <code>AugmentationSchema</code> objects. The objects are
408 * sorted according to the length of their target path from the shortest to
412 * module from which is obtained list of all augmentation objects
413 * @return list of sorted <code>AugmentationSchema</code> objects obtained
414 * from <code>module</code>
415 * @throws IllegalArgumentException
417 * @throws IllegalStateException
418 * if set of module augmentations is null
420 private List<AugmentationSchema> resolveAugmentations(final Module module) {
421 checkArgument(module != null, "Module reference cannot be NULL.");
422 checkState(module.getAugmentations() != null, "Augmentations Set cannot be NULL.");
424 final Set<AugmentationSchema> augmentations = module.getAugmentations();
425 final List<AugmentationSchema> sortedAugmentations = new ArrayList<>(augmentations);
426 Collections.sort(sortedAugmentations, Comparators.AUGMENT_COMP);
428 return sortedAugmentations;
432 * Create GeneratedTypeBuilder object from module argument.
435 * Module object from which builder will be created
436 * @return <code>GeneratedTypeBuilder</code> which is internal
437 * representation of the module
438 * @throws IllegalArgumentException
441 private GeneratedTypeBuilder moduleToDataType(final Module module) {
442 checkArgument(module != null, "Module reference cannot be NULL.");
444 final GeneratedTypeBuilder moduleDataTypeBuilder = moduleTypeBuilder(module, "Data");
445 addImplementedInterfaceFromUses(module, moduleDataTypeBuilder);
446 moduleDataTypeBuilder.addImplementsType(DATA_ROOT);
447 moduleDataTypeBuilder.addComment(module.getDescription());
448 moduleDataTypeBuilder.setDescription(createDescription(module));
449 moduleDataTypeBuilder.setReference(module.getReference());
450 return moduleDataTypeBuilder;
454 * Converts all <b>rpcs</b> inputs and outputs substatements of the module
455 * to the list of <code>Type</code> objects. In addition are to containers
456 * and lists which belong to input or output also part of returning list.
459 * module from which is obtained set of all rpc objects to
461 * @throws IllegalArgumentException
463 * <li>if the module is null</li>
464 * <li>if the name of module is null</li>
466 * @throws IllegalStateException
467 * if set of rpcs from module is null
469 private void rpcMethodsToGenType(final Module module) {
470 checkArgument(module != null, "Module reference cannot be NULL.");
471 checkArgument(module.getName() != null, "Module name cannot be NULL.");
472 final Set<RpcDefinition> rpcDefinitions = module.getRpcs();
473 checkState(rpcDefinitions != null, "Set of rpcs from module " + module.getName() + " cannot be NULL.");
474 if (rpcDefinitions.isEmpty()) {
478 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
479 final GeneratedTypeBuilder interfaceBuilder = moduleTypeBuilder(module, "Service");
480 interfaceBuilder.addImplementsType(Types.typeForClass(RpcService.class));
481 interfaceBuilder.setDescription(createDescription(rpcDefinitions, module.getName(), module.getModuleSourcePath()));
483 for (final RpcDefinition rpc : rpcDefinitions) {
485 final String rpcName = BindingMapping.getClassName(rpc.getQName());
486 final String rpcMethodName = BindingMapping.getPropertyName(rpcName);
487 final String rpcComment = rpc.getDescription();
488 final MethodSignatureBuilder method = interfaceBuilder.addMethod(rpcMethodName);
489 final ContainerSchemaNode input = rpc.getInput();
490 final ContainerSchemaNode output = rpc.getOutput();
493 final GeneratedTypeBuilder inType = addRawInterfaceDefinition(basePackageName, input, rpcName);
494 addImplementedInterfaceFromUses(input, inType);
495 inType.addImplementsType(DATA_OBJECT);
496 inType.addImplementsType(augmentable(inType));
497 resolveDataSchemaNodes(module, basePackageName, inType, inType, input.getChildNodes());
498 genCtx.get(module).addChildNodeType(input, inType);
499 final GeneratedType inTypeInstance = inType.toInstance();
500 method.addParameter(inTypeInstance, "input");
503 Type outTypeInstance = VOID;
504 if (output != null) {
505 final GeneratedTypeBuilder outType = addRawInterfaceDefinition(basePackageName, output, rpcName);
506 addImplementedInterfaceFromUses(output, outType);
507 outType.addImplementsType(DATA_OBJECT);
508 outType.addImplementsType(augmentable(outType));
509 resolveDataSchemaNodes(module, basePackageName, outType, outType, output.getChildNodes());
510 genCtx.get(module).addChildNodeType(output, outType);
511 outTypeInstance = outType.toInstance();
514 final Type rpcRes = Types.parameterizedTypeFor(Types.typeForClass(RpcResult.class), outTypeInstance);
515 method.setComment(rpcComment);
516 method.setReturnType(Types.parameterizedTypeFor(FUTURE, rpcRes));
520 genCtx.get(module).addTopLevelNodeType(interfaceBuilder);
524 * Converts all <b>notifications</b> of the module to the list of
525 * <code>Type</code> objects. In addition are to this list added containers
526 * and lists which are part of this notification.
529 * module from which is obtained set of all notification objects
530 * to iterate over them
531 * @throws IllegalArgumentException
533 * <li>if the module equals null</li>
534 * <li>if the name of module equals null</li>
536 * @throws IllegalStateException
537 * if set of notifications from module is null
539 private void notificationsToGenType(final Module module) {
540 checkArgument(module != null, "Module reference cannot be NULL.");
541 checkArgument(module.getName() != null, "Module name cannot be NULL.");
542 final Set<NotificationDefinition> notifications = module.getNotifications();
543 checkState(notifications != null, "Set of notification from module " + module.getName() + " cannot be NULL.");
544 if (notifications.isEmpty()) {
548 final GeneratedTypeBuilder listenerInterface = moduleTypeBuilder(module, "Listener");
549 listenerInterface.addImplementsType(BindingTypes.NOTIFICATION_LISTENER);
550 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
554 for (final NotificationDefinition notification : notifications) {
555 if (notification != null) {
556 processUsesAugments(notification, module);
558 final GeneratedTypeBuilder notificationInterface = addDefaultInterfaceDefinition(basePackageName,
559 notification, BindingTypes.DATA_OBJECT, module);
560 notificationInterface.addImplementsType(NOTIFICATION);
561 genCtx.get(module).addChildNodeType(notification, notificationInterface);
563 // Notification object
564 resolveDataSchemaNodes(module, basePackageName, notificationInterface, notificationInterface,
565 notification.getChildNodes());
567 listenerInterface.addMethod("on" + notificationInterface.getName())
568 .setAccessModifier(AccessModifier.PUBLIC).addParameter(notificationInterface, "notification")
569 .setComment(notification.getDescription()).setReturnType(Types.VOID);
572 listenerInterface.setDescription(createDescription(notifications, module.getName(), module.getModuleSourcePath()));
574 genCtx.get(module).addTopLevelNodeType(listenerInterface);
578 * Converts all <b>identities</b> of the module to the list of
579 * <code>Type</code> objects.
582 * module from which is obtained set of all identity objects to
585 * schema context only used as input parameter for method
586 * {@link identityToGenType}
589 private void allIdentitiesToGenTypes(final Module module, final SchemaContext context) {
590 final Set<IdentitySchemaNode> schemaIdentities = module.getIdentities();
591 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
593 if (schemaIdentities != null && !schemaIdentities.isEmpty()) {
594 for (final IdentitySchemaNode identity : schemaIdentities) {
595 identityToGenType(module, basePackageName, identity, context);
601 * Converts the <b>identity</b> object to GeneratedType. Firstly it is
602 * created transport object builder. If identity contains base identity then
603 * reference to base identity is added to superior identity as its extend.
604 * If identity doesn't contain base identity then only reference to abstract
605 * class {@link org.opendaylight.yangtools.yang.model.api.BaseIdentity
606 * BaseIdentity} is added
610 * @param basePackageName
611 * string contains the module package name
613 * IdentitySchemaNode which contains data about identity
615 * SchemaContext which is used to get package and name
616 * information about base of identity
619 private void identityToGenType(final Module module, final String basePackageName,
620 final IdentitySchemaNode identity, final SchemaContext context) {
621 if (identity == null) {
624 final String packageName = packageNameForGeneratedType(basePackageName, identity.getPath());
625 final String genTypeName = BindingMapping.getClassName(identity.getQName());
626 final GeneratedTOBuilderImpl newType = new GeneratedTOBuilderImpl(packageName, genTypeName);
627 final IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
628 if (baseIdentity == null) {
629 final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(BaseIdentity.class.getPackage().getName(),
630 BaseIdentity.class.getSimpleName());
631 newType.setExtendsType(gto.toInstance());
633 final Module baseIdentityParentModule = SchemaContextUtil.findParentModule(context, baseIdentity);
634 final String returnTypePkgName = BindingMapping.getRootPackageName(baseIdentityParentModule
636 final String returnTypeName = BindingMapping.getClassName(baseIdentity.getQName());
637 final GeneratedTransferObject gto = new GeneratedTOBuilderImpl(returnTypePkgName, returnTypeName)
639 newType.setExtendsType(gto);
641 newType.setAbstract(true);
642 newType.addComment(identity.getDescription());
643 newType.setDescription(createDescription(identity, newType.getFullyQualifiedName()));
644 newType.setReference(identity.getReference());
645 newType.setModuleName(module.getName());
646 newType.setSchemaPath(identity.getPath().getPathFromRoot());
648 final QName qname = identity.getQName();
649 qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, qname);
651 genCtx.get(module).addIdentityType(identity.getQName(), newType);
654 private static Constant qnameConstant(final GeneratedTypeBuilderBase<?> toBuilder, final String constantName,
656 final StringBuilder sb = new StringBuilder("org.opendaylight.yangtools.yang.common.QName");
657 sb.append(".create(");
659 sb.append(name.getNamespace());
661 sb.append(name.getFormattedRevision());
663 sb.append(name.getLocalName());
666 return toBuilder.addConstant(typeForClass(QName.class), constantName, sb.toString());
670 * Converts all <b>groupings</b> of the module to the list of
671 * <code>Type</code> objects. Firstly are groupings sorted according mutual
672 * dependencies. At least dependent (independent) groupings are in the list
673 * saved at first positions. For every grouping the record is added to map
674 * {@link BindingGeneratorImpl#allGroupings allGroupings}
679 * of groupings from which types will be generated
682 private void groupingsToGenTypes(final Module module, final Collection<GroupingDefinition> groupings) {
683 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
684 final List<GroupingDefinition> groupingsSortedByDependencies = new GroupingDefinitionDependencySort()
686 for (final GroupingDefinition grouping : groupingsSortedByDependencies) {
687 groupingToGenType(basePackageName, grouping, module);
692 * Converts individual grouping to GeneratedType. Firstly generated type
693 * builder is created and every child node of grouping is resolved to the
696 * @param basePackageName
697 * string contains the module package name
699 * GroupingDefinition which contains data about grouping
702 * @return GeneratedType which is generated from grouping (object of type
703 * <code>GroupingDefinition</code>)
705 private void groupingToGenType(final String basePackageName, final GroupingDefinition grouping, final Module module) {
706 final String packageName = packageNameForGeneratedType(basePackageName, grouping.getPath());
707 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, grouping, module);
708 genCtx.get(module).addGroupingType(grouping.getPath(), genType);
709 resolveDataSchemaNodes(module, basePackageName, genType, genType, grouping.getChildNodes());
710 groupingsToGenTypes(module, grouping.getGroupings());
711 processUsesAugments(grouping, module);
715 * Adds enumeration builder created from <code>enumTypeDef</code> to
716 * <code>typeBuilder</code>.
718 * Each <code>enumTypeDef</code> item is added to builder with its name and
722 * EnumTypeDefinition contains enum data
724 * string contains name which will be assigned to enumeration
727 * GeneratedTypeBuilder to which will be enum builder assigned
728 * @return enumeration builder which contains data from
729 * <code>enumTypeDef</code>
731 private EnumBuilder resolveInnerEnumFromTypeDefinition(final EnumTypeDefinition enumTypeDef, final QName enumName,
732 final GeneratedTypeBuilder typeBuilder) {
733 if ((enumTypeDef != null) && (typeBuilder != null) && (enumTypeDef.getQName() != null)
734 && (enumTypeDef.getQName().getLocalName() != null)) {
735 final String enumerationName = BindingMapping.getClassName(enumName);
736 final EnumBuilder enumBuilder = typeBuilder.addEnumeration(enumerationName);
737 enumBuilder.setDescription(enumTypeDef.getDescription());
738 enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
745 * Generates type builder for <code>module</code>.
748 * Module which is source of package name for generated type
751 * string which is added to the module class name representation
753 * @return instance of GeneratedTypeBuilder which represents
754 * <code>module</code>.
755 * @throws IllegalArgumentException
756 * if <code>module</code> is null
758 private GeneratedTypeBuilder moduleTypeBuilder(final Module module, final String postfix) {
759 checkArgument(module != null, "Module reference cannot be NULL.");
760 final String packageName = BindingMapping.getRootPackageName(module.getQNameModule());
761 final String moduleName = BindingMapping.getClassName(module.getName()) + postfix;
763 final GeneratedTypeBuilderImpl moduleBuilder = new GeneratedTypeBuilderImpl(packageName, moduleName);
764 moduleBuilder.setDescription(createDescription(module));
765 moduleBuilder.setReference(module.getReference());
766 moduleBuilder.setModuleName(moduleName);
768 return moduleBuilder;
772 * Converts <code>augSchema</code> to list of <code>Type</code> which
773 * contains generated type for augmentation. In addition there are also
774 * generated types for all containers, list and choices which are child of
775 * <code>augSchema</code> node or a generated types for cases are added if
776 * augmented node is choice.
778 * @param augmentPackageName
779 * string with the name of the package to which the augmentation
782 * AugmentationSchema which is contains data about augmentation
783 * (target path, childs...)
786 * @param parentUsesNode
787 * @throws IllegalArgumentException
789 * <li>if <code>augmentPackageName</code> equals null</li>
790 * <li>if <code>augSchema</code> equals null</li>
792 * @throws IllegalStateException
793 * if augment target path is null
795 private void augmentationToGenTypes(final String augmentPackageName, final AugmentationSchema augSchema,
796 final Module module) {
797 checkArgument(augmentPackageName != null, "Package Name cannot be NULL.");
798 checkArgument(augSchema != null, "Augmentation Schema cannot be NULL.");
799 checkState(augSchema.getTargetPath() != null,
800 "Augmentation Schema does not contain Target Path (Target Path is NULL).");
802 processUsesAugments(augSchema, module);
803 final SchemaPath targetPath = augSchema.getTargetPath();
804 SchemaNode targetSchemaNode = null;
806 targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
807 if (targetSchemaNode instanceof DataSchemaNode && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
808 if (targetSchemaNode instanceof DerivableSchemaNode) {
809 targetSchemaNode = ((DerivableSchemaNode) targetSchemaNode).getOriginal().orNull();
811 if (targetSchemaNode == null) {
812 throw new IllegalStateException("Failed to find target node from grouping in augmentation " + augSchema
813 + " in module " + module.getName());
816 if (targetSchemaNode == null) {
817 throw new IllegalArgumentException("augment target not found: " + targetPath);
820 GeneratedTypeBuilder targetTypeBuilder = findChildNodeByPath(targetSchemaNode.getPath());
821 if (targetTypeBuilder == null) {
822 targetTypeBuilder = findCaseByPath(targetSchemaNode.getPath());
824 if (targetTypeBuilder == null) {
825 throw new NullPointerException("Target type not yet generated: " + targetSchemaNode);
828 if (!(targetSchemaNode instanceof ChoiceSchemaNode)) {
829 final String packageName = augmentPackageName;
830 final Type targetType = new ReferencedTypeImpl(targetTypeBuilder.getPackageName(),
831 targetTypeBuilder.getName());
832 addRawAugmentGenTypeDefinition(module, packageName, augmentPackageName, targetType, augSchema);
835 generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance(),
836 (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes());
840 private void usesAugmentationToGenTypes(final String augmentPackageName, final AugmentationSchema augSchema,
841 final Module module, final UsesNode usesNode, final DataNodeContainer usesNodeParent) {
842 checkArgument(augmentPackageName != null, "Package Name cannot be NULL.");
843 checkArgument(augSchema != null, "Augmentation Schema cannot be NULL.");
844 checkState(augSchema.getTargetPath() != null,
845 "Augmentation Schema does not contain Target Path (Target Path is NULL).");
847 processUsesAugments(augSchema, module);
848 final SchemaPath targetPath = augSchema.getTargetPath();
849 final SchemaNode targetSchemaNode = findOriginalTargetFromGrouping(targetPath, usesNode);
850 if (targetSchemaNode == null) {
851 throw new IllegalArgumentException("augment target not found: " + targetPath);
854 GeneratedTypeBuilder targetTypeBuilder = findChildNodeByPath(targetSchemaNode.getPath());
855 if (targetTypeBuilder == null) {
856 targetTypeBuilder = findCaseByPath(targetSchemaNode.getPath());
858 if (targetTypeBuilder == null) {
859 throw new NullPointerException("Target type not yet generated: " + targetSchemaNode);
862 if (!(targetSchemaNode instanceof ChoiceSchemaNode)) {
863 String packageName = augmentPackageName;
864 if (usesNodeParent instanceof SchemaNode) {
865 packageName = packageNameForGeneratedType(augmentPackageName, ((SchemaNode) usesNodeParent).getPath(),
868 addRawAugmentGenTypeDefinition(module, packageName, augmentPackageName, targetTypeBuilder.toInstance(),
871 generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance(),
872 (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes());
877 * Convenient method to find node added by uses statement.
881 * @param parentUsesNode
882 * parent of uses node
883 * @return node from its original location in grouping
885 private DataSchemaNode findOriginalTargetFromGrouping(final SchemaPath targetPath, final UsesNode parentUsesNode) {
886 final SchemaNode targetGrouping = findNodeInSchemaContext(schemaContext, parentUsesNode.getGroupingPath()
888 if (!(targetGrouping instanceof GroupingDefinition)) {
889 throw new IllegalArgumentException("Failed to generate code for augment in " + parentUsesNode);
892 final GroupingDefinition grouping = (GroupingDefinition) targetGrouping;
893 SchemaNode result = grouping;
894 for (final QName node : targetPath.getPathFromRoot()) {
895 // finding by local name is valid, grouping cannot contain nodes
896 // with same name and different namespace
897 if (result instanceof DataNodeContainer) {
898 result = ((DataNodeContainer) result).getDataChildByName(node.getLocalName());
899 } else if (result instanceof ChoiceSchemaNode) {
900 result = ((ChoiceSchemaNode) result).getCaseNodeByName(node.getLocalName());
903 if (result == null) {
907 boolean fromUses = ((DataSchemaNode) result).isAddedByUses();
908 final Iterator<UsesNode> groupingUses = grouping.getUses().iterator();
909 while (groupingUses.hasNext() && fromUses) {
910 result = findOriginalTargetFromGrouping(targetPath, groupingUses.next());
911 if (result != null) {
912 fromUses = ((DataSchemaNode) result).isAddedByUses();
916 // this indicates invalid yang and thus possible bug in code because
917 // invalid yang should be already spotted by parser
918 throw new IllegalStateException("Failed to generate code for augment in " + parentUsesNode);
921 return (DataSchemaNode) result;
925 * Returns a generated type builder for an augmentation.
927 * The name of the type builder is equal to the name of augmented node with
928 * serial number as suffix.
932 * @param augmentPackageName
933 * string with contains the package name to which the augment
935 * @param basePackageName
936 * string with the package name to which the augmented node
938 * @param targetTypeRef
941 * augmentation schema which contains data about the child nodes
942 * and uses of augment
943 * @return generated type builder for augment
945 private GeneratedTypeBuilder addRawAugmentGenTypeDefinition(final Module module, final String augmentPackageName,
946 final String basePackageName, final Type targetTypeRef, final AugmentationSchema augSchema) {
947 Map<String, GeneratedTypeBuilder> augmentBuilders = genTypeBuilders.get(augmentPackageName);
948 if (augmentBuilders == null) {
949 augmentBuilders = new HashMap<>();
950 genTypeBuilders.put(augmentPackageName, augmentBuilders);
952 final String augIdentifier = getAugmentIdentifier(augSchema.getUnknownSchemaNodes());
955 if (augIdentifier != null) {
956 augTypeName = BindingMapping.getClassName(augIdentifier);
958 augTypeName = augGenTypeName(augmentBuilders, targetTypeRef.getName());
961 final GeneratedTypeBuilder augTypeBuilder = new GeneratedTypeBuilderImpl(augmentPackageName, augTypeName);
963 augTypeBuilder.addImplementsType(DATA_OBJECT);
964 augTypeBuilder.addImplementsType(Types.augmentationTypeFor(targetTypeRef));
965 addImplementedInterfaceFromUses(augSchema, augTypeBuilder);
967 augSchemaNodeToMethods(module, basePackageName, augTypeBuilder, augTypeBuilder, augSchema.getChildNodes());
968 augmentBuilders.put(augTypeName, augTypeBuilder);
970 if(!augSchema.getChildNodes().isEmpty()) {
971 genCtx.get(module).addTargetToAugmentation(targetTypeRef, augTypeBuilder);
972 genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
975 genCtx.get(module).addAugmentType(augTypeBuilder);
976 return augTypeBuilder;
981 * @param unknownSchemaNodes
982 * @return nodeParameter of UnknownSchemaNode
984 private String getAugmentIdentifier(final List<UnknownSchemaNode> unknownSchemaNodes) {
985 for (final UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
986 final QName nodeType = unknownSchemaNode.getNodeType();
987 if (AUGMENT_IDENTIFIER_NAME.equals(nodeType.getLocalName())
988 && YANG_EXT_NAMESPACE.equals(nodeType.getNamespace().toString())) {
989 return unknownSchemaNode.getNodeParameter();
996 * Returns first unique name for the augment generated type builder. The
997 * generated type builder name for augment consists from name of augmented
998 * node and serial number of its augmentation.
1001 * map of builders which were created in the package to which the
1002 * augmentation belongs
1003 * @param genTypeName
1004 * string with name of augmented node
1005 * @return string with unique name for augmentation builder
1007 private String augGenTypeName(final Map<String, GeneratedTypeBuilder> builders, final String genTypeName) {
1009 if (builders != null) {
1010 while (builders.containsKey(genTypeName + index)) {
1014 return genTypeName + index;
1018 * Adds the methods to <code>typeBuilder</code> which represent subnodes of
1019 * node for which <code>typeBuilder</code> was created.
1021 * The subnodes aren't mapped to the methods if they are part of grouping or
1022 * augment (in this case are already part of them).
1026 * @param basePackageName
1027 * string contains the module package name
1029 * generated type builder which represents any node. The subnodes
1030 * of this node are added to the <code>typeBuilder</code> as
1031 * methods. The subnode can be of type leaf, leaf-list, list,
1032 * container, choice.
1035 * @param schemaNodes
1036 * set of data schema nodes which are the children of the node
1037 * for which <code>typeBuilder</code> was created
1038 * @return generated type builder which is the same builder as input
1039 * parameter. The getter methods (representing child nodes) could be
1042 private GeneratedTypeBuilder resolveDataSchemaNodes(final Module module, final String basePackageName,
1043 final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf, final Iterable<DataSchemaNode> schemaNodes) {
1044 if (schemaNodes != null && parent != null) {
1045 for (final DataSchemaNode schemaNode : schemaNodes) {
1046 if (!schemaNode.isAugmenting() && !schemaNode.isAddedByUses()) {
1047 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, parent, childOf, module);
1055 * Adds the methods to <code>typeBuilder</code> what represents subnodes of
1056 * node for which <code>typeBuilder</code> was created.
1060 * @param basePackageName
1061 * string contains the module package name
1062 * @param typeBuilder
1063 * generated type builder which represents any node. The subnodes
1064 * of this node are added to the <code>typeBuilder</code> as
1065 * methods. The subnode can be of type leaf, leaf-list, list,
1066 * container, choice.
1069 * @param schemaNodes
1070 * set of data schema nodes which are the children of the node
1071 * for which <code>typeBuilder</code> was created
1072 * @return generated type builder which is the same object as the input
1073 * parameter <code>typeBuilder</code>. The getter method could be
1076 private GeneratedTypeBuilder augSchemaNodeToMethods(final Module module, final String basePackageName,
1077 final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf,
1078 final Iterable<DataSchemaNode> schemaNodes) {
1079 if ((schemaNodes != null) && (typeBuilder != null)) {
1080 for (final DataSchemaNode schemaNode : schemaNodes) {
1081 if (!schemaNode.isAugmenting()) {
1082 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, typeBuilder, childOf, module);
1090 * Adds to <code>typeBuilder</code> a method which is derived from
1091 * <code>schemaNode</code>.
1093 * @param basePackageName
1094 * string with the module package name
1096 * data schema node which is added to <code>typeBuilder</code> as
1098 * @param typeBuilder
1099 * generated type builder to which is <code>schemaNode</code>
1100 * added as a method.
1106 private void addSchemaNodeToBuilderAsMethod(final String basePackageName, final DataSchemaNode node,
1107 final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf, final Module module) {
1108 if (node != null && typeBuilder != null) {
1109 if (node instanceof LeafSchemaNode) {
1110 resolveLeafSchemaNodeAsMethod(typeBuilder, (LeafSchemaNode) node);
1111 } else if (node instanceof LeafListSchemaNode) {
1112 resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) node);
1113 } else if (node instanceof ContainerSchemaNode) {
1114 containerToGenType(module, basePackageName, typeBuilder, childOf, (ContainerSchemaNode) node);
1115 } else if (node instanceof ListSchemaNode) {
1116 listToGenType(module, basePackageName, typeBuilder, childOf, (ListSchemaNode) node);
1117 } else if (node instanceof ChoiceSchemaNode) {
1118 choiceToGeneratedType(module, basePackageName, typeBuilder, (ChoiceSchemaNode) node);
1120 // TODO: anyxml not yet supported
1121 LOG.debug("Unable to add schema node {} as method in {}: unsupported type of node.", node.getClass(),
1122 typeBuilder.getFullyQualifiedName());
1128 * Converts <code>choiceNode</code> to the list of generated types for
1129 * choice and its cases.
1131 * The package names for choice and for its cases are created as
1132 * concatenation of the module package (<code>basePackageName</code>) and
1133 * names of all parents node.
1137 * @param basePackageName
1138 * string with the module package name
1142 * choice node which is mapped to generated type. Also child
1143 * nodes - cases are mapped to generated types.
1144 * @throws IllegalArgumentException
1146 * <li>if <code>basePackageName</code> is null</li>
1147 * <li>if <code>choiceNode</code> is null</li>
1150 private void choiceToGeneratedType(final Module module, final String basePackageName,
1151 final GeneratedTypeBuilder parent, final ChoiceSchemaNode choiceNode) {
1152 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
1153 checkArgument(choiceNode != null, "Choice Schema Node cannot be NULL.");
1155 if (!choiceNode.isAddedByUses()) {
1156 final String packageName = packageNameForGeneratedType(basePackageName, choiceNode.getPath());
1157 final GeneratedTypeBuilder choiceTypeBuilder = addRawInterfaceDefinition(packageName, choiceNode);
1158 constructGetter(parent, choiceNode.getQName().getLocalName(), choiceNode.getDescription(),
1160 choiceTypeBuilder.addImplementsType(typeForClass(DataContainer.class));
1161 genCtx.get(module).addChildNodeType(choiceNode, choiceTypeBuilder);
1162 generateTypesFromChoiceCases(module, basePackageName, choiceTypeBuilder.toInstance(), choiceNode);
1167 * Converts <code>caseNodes</code> set to list of corresponding generated
1170 * For every <i>case</i> which isn't added through augment or <i>uses</i> is
1171 * created generated type builder. The package names for the builder is
1172 * created as concatenation of the module package (
1173 * <code>basePackageName</code>) and names of all parents nodes of the
1174 * concrete <i>case</i>. There is also relation "<i>implements type</i>"
1175 * between every case builder and <i>choice</i> type
1179 * @param basePackageName
1180 * string with the module package name
1181 * @param refChoiceType
1182 * type which represents superior <i>case</i>
1184 * choice case node which is mapped to generated type
1185 * @return list of generated types for <code>caseNodes</code>.
1186 * @throws IllegalArgumentException
1188 * <li>if <code>basePackageName</code> equals null</li>
1189 * <li>if <code>refChoiceType</code> equals null</li>
1190 * <li>if <code>caseNodes</code> equals null</li>
1193 private void generateTypesFromChoiceCases(final Module module, final String basePackageName,
1194 final Type refChoiceType, final ChoiceSchemaNode choiceNode) {
1195 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
1196 checkArgument(refChoiceType != null, "Referenced Choice Type cannot be NULL.");
1197 checkArgument(choiceNode != null, "ChoiceNode cannot be NULL.");
1199 final Set<ChoiceCaseNode> caseNodes = choiceNode.getCases();
1200 if (caseNodes == null) {
1204 for (final ChoiceCaseNode caseNode : caseNodes) {
1205 if (caseNode != null && !caseNode.isAddedByUses() && !caseNode.isAugmenting()) {
1206 final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath());
1207 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode, module);
1208 caseTypeBuilder.addImplementsType(refChoiceType);
1209 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
1210 genCtx.get(module).addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode);
1211 final Iterable<DataSchemaNode> caseChildNodes = caseNode.getChildNodes();
1212 if (caseChildNodes != null) {
1213 final SchemaPath nodeSp = choiceNode.getPath();
1214 final Object parentNode = findDataSchemaNode(schemaContext, nodeSp.getParent());
1217 if (parentNode instanceof AugmentationSchema) {
1218 final AugmentationSchema augSchema = (AugmentationSchema) parentNode;
1219 final SchemaPath targetPath = augSchema.getTargetPath();
1220 SchemaNode targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
1221 if (targetSchemaNode instanceof DataSchemaNode
1222 && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
1223 if (targetSchemaNode instanceof DerivableSchemaNode) {
1224 targetSchemaNode = ((DerivableSchemaNode) targetSchemaNode).getOriginal().orNull();
1226 if (targetSchemaNode == null) {
1227 throw new IllegalStateException(
1228 "Failed to find target node from grouping for augmentation " + augSchema
1229 + " in module " + module.getName());
1232 parent = targetSchemaNode;
1234 final SchemaPath sp = choiceNode.getPath();
1235 parent = findDataSchemaNode(schemaContext, sp.getParent());
1237 Preconditions.checkState(parent != null, "Could not find Choice node parent "+choiceNode.getPath().getParent());
1238 GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.getPath());
1239 if (childOfType == null) {
1240 childOfType = findGroupingByPath(parent.getPath());
1242 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, caseChildNodes);
1245 processUsesAugments(caseNode, module);
1250 * Generates list of generated types for all the cases of a choice which are
1251 * added to the choice through the augment.
1255 * @param basePackageName
1256 * string contains name of package to which augment belongs. If
1257 * an augmented choice is from an other package (pcg1) than an
1258 * augmenting choice (pcg2) then case's of the augmenting choice
1259 * will belong to pcg2.
1261 * Type which represents target choice
1263 * node which represents target choice
1264 * @param augmentedNodes
1265 * set of choice case nodes for which is checked if are/aren't
1266 * added to choice through augmentation
1267 * @return list of generated types which represents augmented cases of
1268 * choice <code>refChoiceType</code>
1269 * @throws IllegalArgumentException
1271 * <li>if <code>basePackageName</code> is null</li>
1272 * <li>if <code>targetType</code> is null</li>
1273 * <li>if <code>augmentedNodes</code> is null</li>
1276 private void generateTypesFromAugmentedChoiceCases(final Module module, final String basePackageName,
1277 final Type targetType, final ChoiceSchemaNode targetNode, final Iterable<DataSchemaNode> augmentedNodes) {
1278 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
1279 checkArgument(targetType != null, "Referenced Choice Type cannot be NULL.");
1280 checkArgument(augmentedNodes != null, "Set of Choice Case Nodes cannot be NULL.");
1282 for (final DataSchemaNode caseNode : augmentedNodes) {
1283 if (caseNode != null) {
1284 final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath());
1285 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode, module);
1286 caseTypeBuilder.addImplementsType(targetType);
1288 SchemaNode parent = null;
1289 final SchemaPath nodeSp = targetNode.getPath();
1290 parent = findDataSchemaNode(schemaContext, nodeSp.getParent());
1292 GeneratedTypeBuilder childOfType = null;
1293 if (parent instanceof Module) {
1294 childOfType = genCtx.get(parent).getModuleNode();
1295 } else if (parent instanceof ChoiceCaseNode) {
1296 childOfType = findCaseByPath(parent.getPath());
1297 } else if (parent instanceof DataSchemaNode || parent instanceof NotificationDefinition) {
1298 childOfType = findChildNodeByPath(parent.getPath());
1299 } else if (parent instanceof GroupingDefinition) {
1300 childOfType = findGroupingByPath(parent.getPath());
1303 if (childOfType == null) {
1304 throw new IllegalArgumentException("Failed to find parent type of choice " + targetNode);
1307 ChoiceCaseNode node = null;
1308 if (caseNode instanceof ChoiceCaseNode) {
1309 node = (ChoiceCaseNode) caseNode;
1311 node = targetNode.getCaseNodeByName(caseNode.getQName().getLocalName());
1313 final Iterable<DataSchemaNode> childNodes = node.getChildNodes();
1314 if (childNodes != null) {
1315 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, childNodes);
1317 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
1318 genCtx.get(module).addChoiceToCaseMapping(targetType, caseTypeBuilder, node);
1324 * Converts <code>leaf</code> to the getter method which is added to
1325 * <code>typeBuilder</code>.
1327 * @param typeBuilder
1328 * generated type builder to which is added getter method as
1329 * <code>leaf</code> mapping
1331 * leaf schema node which is mapped as getter method which is
1332 * added to <code>typeBuilder</code>
1333 * @return boolean value
1335 * <li>false - if <code>leaf</code> or <code>typeBuilder</code> are
1337 * <li>true - in other cases</li>
1340 private Type resolveLeafSchemaNodeAsMethod(final GeneratedTypeBuilder typeBuilder, final LeafSchemaNode leaf) {
1341 Type returnType = null;
1342 if ((leaf != null) && (typeBuilder != null)) {
1343 final String leafName = leaf.getQName().getLocalName();
1344 String leafDesc = leaf.getDescription();
1345 if (leafDesc == null) {
1349 final Module parentModule = findParentModule(schemaContext, leaf);
1350 if (leafName != null && !leaf.isAddedByUses()) {
1351 final TypeDefinition<?> typeDef = leaf.getType();
1353 GeneratedTOBuilder genTOBuilder;
1354 if (typeDef instanceof EnumTypeDefinition) {
1355 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1356 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
1357 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(),
1360 if (enumBuilder != null) {
1361 returnType = enumBuilder.toInstance(typeBuilder);
1363 ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
1364 } else if (typeDef instanceof UnionType) {
1365 genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
1366 if (genTOBuilder != null) {
1367 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
1369 } else if (typeDef instanceof BitsTypeDefinition) {
1370 genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
1371 if (genTOBuilder != null) {
1372 returnType = genTOBuilder.toInstance();
1375 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
1376 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
1378 if (returnType != null) {
1379 final MethodSignatureBuilder getter = constructGetter(typeBuilder, leafName, leafDesc, returnType);
1380 processContextRefExtension(leaf, getter, parentModule);
1387 private void processContextRefExtension(final LeafSchemaNode leaf, final MethodSignatureBuilder getter,
1388 final Module module) {
1389 for (final UnknownSchemaNode node : leaf.getUnknownSchemaNodes()) {
1390 final QName nodeType = node.getNodeType();
1391 if ("context-reference".equals(nodeType.getLocalName())) {
1392 final String nodeParam = node.getNodeParameter();
1393 IdentitySchemaNode identity = null;
1394 String basePackageName = null;
1395 final Iterable<String> splittedElement = COLON_SPLITTER.split(nodeParam);
1396 final Iterator<String> iterator = splittedElement.iterator();
1397 final int length = Iterables.size(splittedElement);
1399 identity = findIdentityByName(module.getIdentities(), iterator.next());
1400 basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
1401 } else if (length == 2) {
1402 final String prefix = iterator.next();
1403 final Module dependentModule = findModuleFromImports(module.getImports(), prefix);
1404 if (dependentModule == null) {
1405 throw new IllegalArgumentException("Failed to process context-reference: unknown prefix "
1408 identity = findIdentityByName(dependentModule.getIdentities(), iterator.next());
1409 basePackageName = BindingMapping.getRootPackageName(dependentModule.getQNameModule());
1411 throw new IllegalArgumentException("Failed to process context-reference: unknown identity "
1414 if (identity == null) {
1415 throw new IllegalArgumentException("Failed to process context-reference: unknown identity "
1419 final Class<RoutingContext> clazz = RoutingContext.class;
1420 final AnnotationTypeBuilder rc = getter.addAnnotation(clazz.getPackage().getName(),
1421 clazz.getSimpleName());
1422 final String packageName = packageNameForGeneratedType(basePackageName, identity.getPath());
1423 final String genTypeName = BindingMapping.getClassName(identity.getQName().getLocalName());
1424 rc.addParameter("value", packageName + "." + genTypeName + ".class");
1429 private IdentitySchemaNode findIdentityByName(final Set<IdentitySchemaNode> identities, final String name) {
1430 for (final IdentitySchemaNode id : identities) {
1431 if (id.getQName().getLocalName().equals(name)) {
1438 private Module findModuleFromImports(final Set<ModuleImport> imports, final String prefix) {
1439 for (final ModuleImport imp : imports) {
1440 if (imp.getPrefix().equals(prefix)) {
1441 return schemaContext.findModuleByName(imp.getModuleName(), imp.getRevision());
1447 private boolean resolveLeafSchemaNodeAsProperty(final GeneratedTOBuilder toBuilder, final LeafSchemaNode leaf,
1448 final boolean isReadOnly, final Module module) {
1449 if ((leaf != null) && (toBuilder != null)) {
1450 final String leafName = leaf.getQName().getLocalName();
1451 String leafDesc = leaf.getDescription();
1452 if (leafDesc == null) {
1456 if (leafName != null) {
1457 Type returnType = null;
1458 final TypeDefinition<?> typeDef = leaf.getType();
1459 if (typeDef instanceof UnionTypeDefinition) {
1460 // GeneratedType for this type definition should be already
1462 final QName qname = typeDef.getQName();
1463 final Module unionModule = schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
1464 qname.getRevision());
1465 final ModuleContext mc = genCtx.get(unionModule);
1466 returnType = mc.getTypedefs().get(typeDef.getPath());
1468 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1470 return resolveLeafSchemaNodeAsProperty(toBuilder, leaf, returnType, isReadOnly);
1477 * Converts <code>leaf</code> schema node to property of generated TO
1481 * generated TO builder to which is <code>leaf</code> added as
1484 * leaf schema node which is added to <code>toBuilder</code> as
1489 * boolean value which says if leaf property is|isn't read only
1490 * @return boolean value
1492 * <li>false - if <code>leaf</code>, <code>toBuilder</code> or leaf
1493 * name equals null or if leaf is added by <i>uses</i>.</li>
1494 * <li>true - other cases</li>
1497 private boolean resolveLeafSchemaNodeAsProperty(final GeneratedTOBuilder toBuilder, final LeafSchemaNode leaf,
1498 final Type returnType, final boolean isReadOnly) {
1499 if (returnType == null) {
1502 final String leafName = leaf.getQName().getLocalName();
1503 final String leafDesc = leaf.getDescription();
1504 final GeneratedPropertyBuilder propBuilder = toBuilder.addProperty(BindingMapping.getPropertyName(leafName));
1505 propBuilder.setReadOnly(isReadOnly);
1506 propBuilder.setReturnType(returnType);
1507 propBuilder.setComment(leafDesc);
1508 toBuilder.addEqualsIdentity(propBuilder);
1509 toBuilder.addHashIdentity(propBuilder);
1510 toBuilder.addToStringProperty(propBuilder);
1515 * Converts <code>node</code> leaf list schema node to getter method of
1516 * <code>typeBuilder</code>.
1518 * @param typeBuilder
1519 * generated type builder to which is <code>node</code> added as
1522 * leaf list schema node which is added to
1523 * <code>typeBuilder</code> as getter method
1524 * @return boolean value
1526 * <li>true - if <code>node</code>, <code>typeBuilder</code>,
1527 * nodeName equal null or <code>node</code> is added by <i>uses</i></li>
1528 * <li>false - other cases</li>
1531 private boolean resolveLeafListSchemaNode(final GeneratedTypeBuilder typeBuilder, final LeafListSchemaNode node) {
1532 if ((node != null) && (typeBuilder != null)) {
1533 final QName nodeName = node.getQName();
1535 if (nodeName != null && !node.isAddedByUses()) {
1536 final TypeDefinition<?> typeDef = node.getType();
1537 final Module parentModule = findParentModule(schemaContext, node);
1539 Type returnType = null;
1540 if (typeDef instanceof EnumTypeDefinition) {
1541 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node);
1542 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
1543 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, nodeName,
1545 returnType = new ReferencedTypeImpl(enumBuilder.getPackageName(), enumBuilder.getName());
1546 ((TypeProviderImpl) typeProvider).putReferencedType(node.getPath(), returnType);
1547 } else if (typeDef instanceof UnionType) {
1548 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule);
1549 if (genTOBuilder != null) {
1550 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
1552 } else if (typeDef instanceof BitsTypeDefinition) {
1553 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule);
1554 returnType = genTOBuilder.toInstance();
1556 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
1557 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
1560 final ParameterizedType listType = Types.listTypeFor(returnType);
1561 constructGetter(typeBuilder, nodeName.getLocalName(), node.getDescription(), listType);
1568 private Type createReturnTypeForUnion(final GeneratedTOBuilder genTOBuilder, final TypeDefinition<?> typeDef,
1569 final GeneratedTypeBuilder typeBuilder, final Module parentModule) {
1570 final GeneratedTOBuilderImpl returnType = new GeneratedTOBuilderImpl(genTOBuilder.getPackageName(),
1571 genTOBuilder.getName());
1573 returnType.setDescription(typeDef.getDescription());
1574 returnType.setReference(typeDef.getReference());
1575 returnType.setSchemaPath(typeDef.getPath().getPathFromRoot());
1576 returnType.setModuleName(parentModule.getName());
1578 genTOBuilder.setTypedef(true);
1579 genTOBuilder.setIsUnion(true);
1580 ((TypeProviderImpl) typeProvider).addUnitsToGenTO(genTOBuilder, typeDef.getUnits());
1584 final GeneratedTOBuilder unionBuilder = createUnionBuilder(genTOBuilder,typeBuilder);
1587 final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
1588 method.setReturnType(returnType);
1589 method.addParameter(Types.STRING, "defaultValue");
1590 method.setAccessModifier(AccessModifier.PUBLIC);
1591 method.setStatic(true);
1593 final Set<Type> types = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(parentModule);
1594 if (types == null) {
1595 ((TypeProviderImpl) typeProvider).getAdditionalTypes().put(parentModule,
1596 Sets.<Type> newHashSet(unionBuilder.toInstance()));
1598 types.add(unionBuilder.toInstance());
1600 return returnType.toInstance();
1603 private GeneratedTOBuilder createUnionBuilder(final GeneratedTOBuilder genTOBuilder, final GeneratedTypeBuilder typeBuilder) {
1605 final String outerCls = Types.getOuterClassName(genTOBuilder);
1606 final StringBuilder name;
1607 if(outerCls != null) {
1608 name = new StringBuilder(outerCls);
1610 name = new StringBuilder();
1612 name.append(genTOBuilder.getName());
1613 name.append("Builder");
1614 final GeneratedTOBuilderImpl unionBuilder = new GeneratedTOBuilderImpl(typeBuilder.getPackageName(),name.toString());
1615 unionBuilder.setIsUnionBuilder(true);
1616 return unionBuilder;
1619 private GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode schemaNode,
1620 final Module module) {
1621 return addDefaultInterfaceDefinition(packageName, schemaNode, null, module);
1625 * Instantiates generated type builder with <code>packageName</code> and
1626 * <code>schemaNode</code>.
1628 * The new builder always implements
1629 * {@link org.opendaylight.yangtools.yang.binding.DataObject DataObject}.<br>
1630 * If <code>schemaNode</code> is instance of GroupingDefinition it also
1631 * implements {@link org.opendaylight.yangtools.yang.binding.Augmentable
1633 * If <code>schemaNode</code> is instance of
1634 * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer
1635 * DataNodeContainer} it can also implement nodes which are specified in
1638 * @param packageName
1639 * string with the name of the package to which
1640 * <code>schemaNode</code> belongs.
1642 * schema node for which is created generated type builder
1644 * parent type (can be null)
1645 * @return generated type builder <code>schemaNode</code>
1647 private GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode schemaNode,
1648 final Type parent, final Module module) {
1649 final GeneratedTypeBuilder it = addRawInterfaceDefinition(packageName, schemaNode, "");
1650 if (parent == null) {
1651 it.addImplementsType(DATA_OBJECT);
1653 it.addImplementsType(BindingTypes.childOf(parent));
1655 if (!(schemaNode instanceof GroupingDefinition)) {
1656 it.addImplementsType(augmentable(it));
1659 if (schemaNode instanceof DataNodeContainer) {
1660 groupingsToGenTypes(module, ((DataNodeContainer) schemaNode).getGroupings());
1661 addImplementedInterfaceFromUses((DataNodeContainer) schemaNode, it);
1668 * Wraps the calling of the same overloaded method.
1670 * @param packageName
1671 * string with the package name to which returning generated type
1674 * schema node which provide data about the schema node name
1675 * @return generated type builder for <code>schemaNode</code>
1677 private GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode) {
1678 return addRawInterfaceDefinition(packageName, schemaNode, "");
1682 * Returns reference to generated type builder for specified
1683 * <code>schemaNode</code> with <code>packageName</code>.
1685 * Firstly the generated type builder is searched in
1686 * {@link BindingGeneratorImpl#genTypeBuilders genTypeBuilders}. If it isn't
1687 * found it is created and added to <code>genTypeBuilders</code>.
1689 * @param packageName
1690 * string with the package name to which returning generated type
1693 * schema node which provide data about the schema node name
1695 * return type name prefix
1696 * @return generated type builder for <code>schemaNode</code>
1697 * @throws IllegalArgumentException
1699 * <li>if <code>schemaNode</code> is null</li>
1700 * <li>if <code>packageName</code> is null</li>
1701 * <li>if QName of schema node is null</li>
1702 * <li>if schemaNode name is null</li>
1706 private GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode,
1707 final String prefix) {
1708 checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
1709 checkArgument(packageName != null, "Package Name for Generated Type cannot be NULL.");
1710 checkArgument(schemaNode.getQName() != null, "QName for Data Schema Node cannot be NULL.");
1711 final String schemaNodeName = schemaNode.getQName().getLocalName();
1712 checkArgument(schemaNodeName != null, "Local Name of QName for Data Schema Node cannot be NULL.");
1715 if (prefix == null) {
1716 genTypeName = BindingMapping.getClassName(schemaNodeName);
1718 genTypeName = prefix + BindingMapping.getClassName(schemaNodeName);
1721 // FIXME: Validation of name conflict
1722 final GeneratedTypeBuilderImpl newType = new GeneratedTypeBuilderImpl(packageName, genTypeName);
1723 final Module module = findParentModule(schemaContext, schemaNode);
1724 qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, schemaNode.getQName());
1725 newType.addComment(schemaNode.getDescription());
1726 newType.setDescription(createDescription(schemaNode, newType.getFullyQualifiedName()));
1727 newType.setReference(schemaNode.getReference());
1728 newType.setSchemaPath(schemaNode.getPath().getPathFromRoot());
1729 newType.setModuleName(module.getName());
1731 if (!genTypeBuilders.containsKey(packageName)) {
1732 final Map<String, GeneratedTypeBuilder> builders = new HashMap<>();
1733 builders.put(genTypeName, newType);
1734 genTypeBuilders.put(packageName, builders);
1736 final Map<String, GeneratedTypeBuilder> builders = genTypeBuilders.get(packageName);
1737 if (!builders.containsKey(genTypeName)) {
1738 builders.put(genTypeName, newType);
1745 * Creates the name of the getter method name from <code>localName</code>.
1748 * string with the name of the getter method
1751 * @return string with the name of the getter method for
1752 * <code>methodName</code> in JAVA method format
1754 public static String getterMethodName(final String localName, final Type returnType) {
1755 final StringBuilder method = new StringBuilder();
1756 if (BOOLEAN.equals(returnType)) {
1757 method.append("is");
1759 method.append("get");
1761 final String name = BindingMapping.toFirstUpper(BindingMapping.getPropertyName(localName));
1762 method.append(name);
1763 return method.toString();
1767 * Created a method signature builder as part of
1768 * <code>interfaceBuilder</code>.
1770 * The method signature builder is created for the getter method of
1771 * <code>schemaNodeName</code>. Also <code>comment</code> and
1772 * <code>returnType</code> information are added to the builder.
1774 * @param interfaceBuilder
1775 * generated type builder for which the getter method should be
1777 * @param schemaNodeName
1778 * string with schema node name. The name will be the part of the
1779 * getter method name.
1781 * string with comment for the getter method
1783 * type which represents the return type of the getter method
1784 * @return method signature builder which represents the getter method of
1785 * <code>interfaceBuilder</code>
1787 private MethodSignatureBuilder constructGetter(final GeneratedTypeBuilder interfaceBuilder,
1788 final String schemaNodeName, final String comment, final Type returnType) {
1789 final MethodSignatureBuilder getMethod = interfaceBuilder
1790 .addMethod(getterMethodName(schemaNodeName, returnType));
1791 getMethod.setComment(comment);
1792 getMethod.setReturnType(returnType);
1797 * Adds <code>schemaNode</code> to <code>typeBuilder</code> as getter method
1798 * or to <code>genTOBuilder</code> as property.
1800 * @param basePackageName
1801 * string contains the module package name
1803 * data schema node which should be added as getter method to
1804 * <code>typeBuilder</code> or as a property to
1805 * <code>genTOBuilder</code> if is part of the list key
1806 * @param typeBuilder
1807 * generated type builder for the list schema node
1808 * @param genTOBuilder
1809 * generated TO builder for the list keys
1811 * list of string which contains names of the list keys
1814 * @throws IllegalArgumentException
1816 * <li>if <code>schemaNode</code> equals null</li>
1817 * <li>if <code>typeBuilder</code> equals null</li>
1820 private void addSchemaNodeToListBuilders(final String basePackageName, final DataSchemaNode schemaNode,
1821 final GeneratedTypeBuilder typeBuilder, final GeneratedTOBuilder genTOBuilder, final List<String> listKeys,
1822 final Module module) {
1823 checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
1824 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
1826 if (schemaNode instanceof LeafSchemaNode) {
1827 final LeafSchemaNode leaf = (LeafSchemaNode) schemaNode;
1828 final String leafName = leaf.getQName().getLocalName();
1829 final Type type = resolveLeafSchemaNodeAsMethod(typeBuilder, leaf);
1830 if (listKeys.contains(leafName)) {
1832 resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, true, module);
1834 resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, type, true);
1837 } else if (!schemaNode.isAddedByUses()) {
1838 if (schemaNode instanceof LeafListSchemaNode) {
1839 resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) schemaNode);
1840 } else if (schemaNode instanceof ContainerSchemaNode) {
1841 containerToGenType(module, basePackageName, typeBuilder, typeBuilder, (ContainerSchemaNode) schemaNode);
1842 } else if (schemaNode instanceof ChoiceSchemaNode) {
1843 choiceToGeneratedType(module, basePackageName, typeBuilder, (ChoiceSchemaNode) schemaNode);
1844 } else if (schemaNode instanceof ListSchemaNode) {
1845 listToGenType(module, basePackageName, typeBuilder, typeBuilder, (ListSchemaNode) schemaNode);
1850 private void typeBuildersToGenTypes(final Module module, final GeneratedTypeBuilder typeBuilder,
1851 final GeneratedTOBuilder genTOBuilder) {
1852 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
1854 if (genTOBuilder != null) {
1855 final GeneratedTransferObject genTO = genTOBuilder.toInstance();
1856 constructGetter(typeBuilder, "key", "Returns Primary Key of Yang List Type", genTO);
1857 genCtx.get(module).addGeneratedTOBuilder(genTOBuilder);
1862 * Selects the names of the list keys from <code>list</code> and returns
1863 * them as the list of the strings
1866 * of string with names of the list keys
1867 * @return list of string which represents names of the list keys. If the
1868 * <code>list</code> contains no keys then the empty list is
1871 private List<String> listKeys(final ListSchemaNode list) {
1872 final List<String> listKeys = new ArrayList<>();
1874 final List<QName> keyDefinition = list.getKeyDefinition();
1875 if (keyDefinition != null) {
1876 for (final QName keyDef : keyDefinition) {
1877 listKeys.add(keyDef.getLocalName());
1884 * Generates for the <code>list</code> which contains any list keys special
1885 * generated TO builder.
1887 * @param packageName
1888 * string with package name to which the list belongs
1890 * list schema node which is source of data about the list name
1891 * @return generated TO builder which represents the keys of the
1892 * <code>list</code> or null if <code>list</code> is null or list of
1893 * key definitions is null or empty.
1895 private GeneratedTOBuilder resolveListKeyTOBuilder(final String packageName, final ListSchemaNode list) {
1896 GeneratedTOBuilder genTOBuilder = null;
1897 if ((list.getKeyDefinition() != null) && (!list.getKeyDefinition().isEmpty())) {
1898 final String listName = list.getQName().getLocalName() + "Key";
1899 final String genTOName = BindingMapping.getClassName(listName);
1900 genTOBuilder = new GeneratedTOBuilderImpl(packageName, genTOName);
1902 return genTOBuilder;
1906 * Builds generated TO builders for <code>typeDef</code> of type
1907 * {@link org.opendaylight.yangtools.yang.model.util.UnionType UnionType} or
1908 * {@link org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
1909 * BitsTypeDefinition} which are also added to <code>typeBuilder</code> as
1910 * enclosing transfer object.
1912 * If more then one generated TO builder is created for enclosing then all
1913 * of the generated TO builders are added to <code>typeBuilder</code> as
1914 * enclosing transfer objects.
1917 * type definition which can be of type <code>UnionType</code> or
1918 * <code>BitsTypeDefinition</code>
1919 * @param typeBuilder
1920 * generated type builder to which is added generated TO created
1921 * from <code>typeDef</code>
1923 * string with name for generated TO builder
1925 * @return generated TO builder for <code>typeDef</code>
1927 private GeneratedTOBuilder addTOToTypeBuilder(final TypeDefinition<?> typeDef,
1928 final GeneratedTypeBuilder typeBuilder, final DataSchemaNode leaf, final Module parentModule) {
1929 final String classNameFromLeaf = BindingMapping.getClassName(leaf.getQName());
1930 final List<GeneratedTOBuilder> genTOBuilders = new ArrayList<>();
1931 final String packageName = typeBuilder.getFullyQualifiedName();
1932 if (typeDef instanceof UnionTypeDefinition) {
1933 final List<GeneratedTOBuilder> types = ((TypeProviderImpl) typeProvider)
1934 .provideGeneratedTOBuildersForUnionTypeDef(packageName, ((UnionTypeDefinition) typeDef),
1935 classNameFromLeaf, leaf);
1936 genTOBuilders.addAll(types);
1938 GeneratedTOBuilder resultTOBuilder = null;
1939 if (types.isEmpty()) {
1940 throw new IllegalStateException("No GeneratedTOBuilder objects generated from union " + typeDef);
1942 resultTOBuilder = types.remove(0);
1943 for (final GeneratedTOBuilder genTOBuilder : types) {
1944 resultTOBuilder.addEnclosingTransferObject(genTOBuilder);
1947 final GeneratedPropertyBuilder genPropBuilder = resultTOBuilder.addProperty("value");
1948 genPropBuilder.setReturnType(Types.CHAR_ARRAY);
1949 resultTOBuilder.addEqualsIdentity(genPropBuilder);
1950 resultTOBuilder.addHashIdentity(genPropBuilder);
1951 resultTOBuilder.addToStringProperty(genPropBuilder);
1953 } else if (typeDef instanceof BitsTypeDefinition) {
1954 genTOBuilders.add((((TypeProviderImpl) typeProvider)).provideGeneratedTOBuilderForBitsTypeDefinition(
1955 packageName, typeDef, classNameFromLeaf, parentModule.getName()));
1957 if (genTOBuilders != null && !genTOBuilders.isEmpty()) {
1958 for (final GeneratedTOBuilder genTOBuilder : genTOBuilders) {
1959 typeBuilder.addEnclosingTransferObject(genTOBuilder);
1961 return genTOBuilders.get(0);
1968 * Adds the implemented types to type builder.
1970 * The method passes through the list of <i>uses</i> in
1971 * {@code dataNodeContainer}. For every <i>use</i> is obtained corresponding
1972 * generated type from {@link BindingGeneratorImpl#allGroupings
1973 * allGroupings} which is added as <i>implements type</i> to
1974 * <code>builder</code>
1976 * @param dataNodeContainer
1977 * element which contains the list of used YANG groupings
1979 * builder to which are added implemented types according to
1980 * <code>dataNodeContainer</code>
1981 * @return generated type builder with all implemented types
1983 private GeneratedTypeBuilder addImplementedInterfaceFromUses(final DataNodeContainer dataNodeContainer,
1984 final GeneratedTypeBuilder builder) {
1985 for (final UsesNode usesNode : dataNodeContainer.getUses()) {
1986 if (usesNode.getGroupingPath() != null) {
1987 final GeneratedType genType = findGroupingByPath(usesNode.getGroupingPath()).toInstance();
1988 if (genType == null) {
1989 throw new IllegalStateException("Grouping " + usesNode.getGroupingPath() + "is not resolved for "
1990 + builder.getName());
1993 builder.addImplementsType(genType);
1995 builder.addComment(genType.getDescription());
1996 builder.setDescription(genType.getDescription());
1997 builder.setModuleName(genType.getModuleName());
1998 builder.setReference(genType.getReference());
1999 builder.setSchemaPath(genType.getSchemaPath());
2006 private boolean isNullOrEmpty(final Collection<?> list) {
2007 return (list == null || list.isEmpty() ? true : false);
2010 private String createDescription(final Set<? extends SchemaNode> schemaNodes, final String moduleName, final String moduleSourcePath) {
2011 final StringBuilder sb = new StringBuilder();
2013 if (!isNullOrEmpty(schemaNodes)) {
2014 final SchemaNode node = schemaNodes.iterator().next();
2016 if (node instanceof RpcDefinition) {
2017 sb.append("Interface for implementing the following YANG RPCs defined in module <b>" + moduleName + "</b>");
2018 } else if (node instanceof NotificationDefinition) {
2019 sb.append("Interface for receiving the following YANG notifications defined in module <b>" + moduleName + "</b>");
2022 sb.append(NEW_LINE);
2023 sb.append("<br>(Source path: <i>");
2024 sb.append(moduleSourcePath);
2025 sb.append("</i>):");
2026 sb.append(NEW_LINE);
2028 if (verboseClassComments) {
2030 sb.append(NEW_LINE);
2031 sb.append(YangTemplate.generateYangSnipet(schemaNodes));
2032 sb.append("</pre>");
2033 sb.append(NEW_LINE);
2036 return sb.toString();
2039 private String createDescription(final SchemaNode schemaNode, final String fullyQualifiedName) {
2040 final StringBuilder sb = new StringBuilder();
2041 final String formattedDescription = YangTemplate.formatToParagraph(schemaNode.getDescription(), 0);
2043 if (!isNullOrEmpty(formattedDescription)) {
2044 sb.append(formattedDescription);
2045 sb.append(NEW_LINE);
2048 if (verboseClassComments) {
2049 final Module module = findParentModule(schemaContext, schemaNode);
2050 final StringBuilder linkToBuilderClass = new StringBuilder();
2051 final String[] namespace = Iterables.toArray(BSDOT_SPLITTER.split(fullyQualifiedName), String.class);
2052 final String className = namespace[namespace.length - 1];
2054 if (hasBuilderClass(schemaNode)) {
2055 linkToBuilderClass.append(className);
2056 linkToBuilderClass.append("Builder");
2060 sb.append("This class represents the following YANG schema fragment defined in module <b>");
2061 sb.append(module.getName());
2063 sb.append(NEW_LINE);
2064 sb.append("<br>(Source path: <i>");
2065 sb.append(module.getModuleSourcePath());
2066 sb.append("</i>):");
2067 sb.append(NEW_LINE);
2069 sb.append(NEW_LINE);
2070 sb.append(YangTemplate.generateYangSnipet(schemaNode));
2071 sb.append("</pre>");
2072 sb.append(NEW_LINE);
2073 sb.append("The schema path to identify an instance is");
2074 sb.append(NEW_LINE);
2076 sb.append(YangTemplate.formatSchemaPath(module.getName(), schemaNode.getPath().getPathFromRoot()));
2078 sb.append(NEW_LINE);
2080 if (hasBuilderClass(schemaNode)) {
2081 sb.append(NEW_LINE);
2082 sb.append("<p>To create instances of this class use " + "{@link " + linkToBuilderClass + "}.");
2083 sb.append(NEW_LINE);
2085 sb.append(linkToBuilderClass);
2086 sb.append(NEW_LINE);
2087 if (schemaNode instanceof ListSchemaNode) {
2088 final List<QName> keyDef = ((ListSchemaNode)schemaNode).getKeyDefinition();
2089 if (keyDef != null && !keyDef.isEmpty()) {
2091 sb.append(className);
2094 sb.append(NEW_LINE);
2099 return sb.toString();
2102 private boolean hasBuilderClass(final SchemaNode schemaNode) {
2103 if (schemaNode instanceof ContainerSchemaNode || schemaNode instanceof ListSchemaNode ||
2104 schemaNode instanceof RpcDefinition || schemaNode instanceof NotificationDefinition) {
2110 private boolean isNullOrEmpty(final String string) {
2111 return (string == null || string.isEmpty() ? true : false);
2114 private String createDescription(final Module module) {
2115 final StringBuilder sb = new StringBuilder();
2116 final String formattedDescription = YangTemplate.formatToParagraph(module.getDescription(), 0);
2118 if (!isNullOrEmpty(formattedDescription)) {
2119 sb.append(formattedDescription);
2120 sb.append(NEW_LINE);
2123 if (verboseClassComments) {
2125 sb.append("This class represents the following YANG schema fragment defined in module <b>");
2126 sb.append(module.getName());
2128 sb.append(NEW_LINE);
2129 sb.append("<br>Source path: <i>");
2130 sb.append(module.getModuleSourcePath());
2131 sb.append("</i>):");
2132 sb.append(NEW_LINE);
2134 sb.append(NEW_LINE);
2135 sb.append(YangTemplate.generateYangSnipet(module));
2136 sb.append("</pre>");
2139 return sb.toString();
2142 private GeneratedTypeBuilder findChildNodeByPath(final SchemaPath path) {
2143 for (final ModuleContext ctx : genCtx.values()) {
2144 final GeneratedTypeBuilder result = ctx.getChildNode(path);
2145 if (result != null) {
2152 private GeneratedTypeBuilder findGroupingByPath(final SchemaPath path) {
2153 for (final ModuleContext ctx : genCtx.values()) {
2154 final GeneratedTypeBuilder result = ctx.getGrouping(path);
2155 if (result != null) {
2162 private GeneratedTypeBuilder findCaseByPath(final SchemaPath path) {
2163 for (final ModuleContext ctx : genCtx.values()) {
2164 final GeneratedTypeBuilder result = ctx.getCase(path);
2165 if (result != null) {
2172 public Map<Module, ModuleContext> getModuleContexts() {