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;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.Iterator;
39 import java.util.List;
43 import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil;
44 import org.opendaylight.yangtools.binding.generator.util.BindingTypes;
45 import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl;
46 import org.opendaylight.yangtools.binding.generator.util.Types;
47 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedPropertyBuilderImpl;
48 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
49 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
50 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
51 import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider;
52 import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
53 import org.opendaylight.yangtools.sal.binding.model.api.Constant;
54 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
55 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
56 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
57 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
58 import org.opendaylight.yangtools.sal.binding.model.api.Type;
59 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
60 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.EnumBuilder;
61 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;
62 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;
63 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
64 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
65 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.MethodSignatureBuilder;
66 import org.opendaylight.yangtools.sal.binding.yang.types.GroupingDefinitionDependencySort;
67 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
68 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
69 import org.opendaylight.yangtools.yang.binding.BindingMapping;
70 import org.opendaylight.yangtools.yang.binding.DataContainer;
71 import org.opendaylight.yangtools.yang.binding.RpcService;
72 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
73 import org.opendaylight.yangtools.yang.common.QName;
74 import org.opendaylight.yangtools.yang.common.RpcResult;
75 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
76 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
77 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
78 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
79 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
80 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
81 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
82 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
83 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
84 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
85 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
86 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
87 import org.opendaylight.yangtools.yang.model.api.Module;
88 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
89 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
90 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
91 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
92 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
93 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
94 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
95 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
96 import org.opendaylight.yangtools.yang.model.api.UsesNode;
97 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
98 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
99 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
100 import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
101 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
102 import org.opendaylight.yangtools.yang.model.util.UnionType;
103 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
104 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
105 import org.slf4j.Logger;
106 import org.slf4j.LoggerFactory;
108 public class BindingGeneratorImpl implements BindingGenerator {
109 private static final Logger LOG = LoggerFactory.getLogger(BindingGeneratorImpl.class);
111 private final Map<Module, ModuleContext> genCtx = new HashMap<>();
114 * Outer key represents the package name. Outer value represents map of all
115 * builders in the same package. Inner key represents the schema node name
116 * (in JAVA class/interface name format). Inner value represents instance of
117 * builder for schema node specified in key part.
119 private Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders;
122 * Provide methods for converting YANG types to JAVA types.
124 private TypeProvider typeProvider;
127 * Holds reference to schema context to resolve data of augmented element
128 * when creating augmentation builder
130 private SchemaContext schemaContext;
133 * Constant with the concrete name of namespace.
135 private final static String YANG_EXT_NAMESPACE = "urn:opendaylight:yang:extension:yang-ext";
138 * Constant with the concrete name of identifier.
140 private final static String AUGMENT_IDENTIFIER_NAME = "augment-identifier";
143 * Resolves generated types from <code>context</code> schema nodes of all
146 * Generated types are created for modules, groupings, types, containers,
147 * lists, choices, augments, rpcs, notification, identities.
150 * schema context which contains data about all schema nodes
152 * @return list of types (usually <code>GeneratedType</code>
153 * <code>GeneratedTransferObject</code>which are generated from
154 * <code>context</code> data.
155 * @throws IllegalArgumentException
156 * if arg <code>context</code> is null
157 * @throws IllegalStateException
158 * if <code>context</code> contain no modules
161 public List<Type> generateTypes(final SchemaContext context) {
162 checkArgument(context != null, "Schema Context reference cannot be NULL.");
163 checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
164 schemaContext = context;
165 typeProvider = new TypeProviderImpl(context);
166 final Set<Module> modules = context.getModules();
167 return generateTypes(context, modules);
171 * Resolves generated types from <code>context</code> schema nodes only for
172 * modules specified in <code>modules</code>
174 * Generated types are created for modules, groupings, types, containers,
175 * lists, choices, augments, rpcs, notification, identities.
178 * schema context which contains data about all schema nodes
181 * set of modules for which schema nodes should be generated
183 * @return list of types (usually <code>GeneratedType</code> or
184 * <code>GeneratedTransferObject</code>) which:
186 * <li>are generated from <code>context</code> schema nodes and</li>
187 * <li>are also part of some of the module in <code>modules</code>
190 * @throws IllegalArgumentException
192 * <li>if arg <code>context</code> is null or</li>
193 * <li>if arg <code>modules</code> is null</li>
195 * @throws IllegalStateException
196 * if <code>context</code> contain no modules
199 public List<Type> generateTypes(final SchemaContext context, final Set<Module> modules) {
200 checkArgument(context != null, "Schema Context reference cannot be NULL.");
201 checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
202 checkArgument(modules != null, "Set of Modules cannot be NULL.");
204 schemaContext = context;
205 typeProvider = new TypeProviderImpl(context);
206 Module[] modulesArray = new Module[context.getModules().size()];
207 context.getModules().toArray(modulesArray);
208 final List<Module> contextModules = ModuleDependencySort.sort(modulesArray);
209 genTypeBuilders = new HashMap<>();
211 for (Module contextModule : contextModules) {
212 moduleToGenTypes(contextModule, context);
214 for (Module contextModule : contextModules) {
215 allAugmentsToGenTypes(contextModule);
218 final List<Type> filteredGenTypes = new ArrayList<>();
219 for (Module m : modules) {
220 final ModuleContext ctx = checkNotNull(genCtx.get(m), "Module context not found for module %s", m);
221 filteredGenTypes.addAll(ctx.getGeneratedTypes());
222 final Set<Type> additionalTypes = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(m);
223 if (additionalTypes != null) {
224 filteredGenTypes.addAll(additionalTypes);
228 return filteredGenTypes;
231 private void moduleToGenTypes(final Module m, final SchemaContext context) {
232 genCtx.put(m, new ModuleContext());
233 allTypeDefinitionsToGenTypes(m);
234 groupingsToGenTypes(m, m.getGroupings());
235 rpcMethodsToGenType(m);
236 allIdentitiesToGenTypes(m, context);
237 notificationsToGenType(m);
239 if (!m.getChildNodes().isEmpty()) {
240 final GeneratedTypeBuilder moduleType = moduleToDataType(m);
241 genCtx.get(m).addModuleNode(moduleType);
242 final String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
243 resolveDataSchemaNodes(m, basePackageName, moduleType, moduleType, m.getChildNodes());
248 * Converts all extended type definitions of module to the list of
249 * <code>Type</code> objects.
252 * module from which is obtained set of type definitions
253 * @throws IllegalArgumentException
255 * <li>if module is null</li>
256 * <li>if name of module is null</li>
258 * @throws IllegalStateException
259 * if set of type definitions from module is null
261 private void allTypeDefinitionsToGenTypes(final Module module) {
262 checkArgument(module != null, "Module reference cannot be NULL.");
263 checkArgument(module.getName() != null, "Module name cannot be NULL.");
264 final DataNodeIterator it = new DataNodeIterator(module);
265 final List<TypeDefinition<?>> typeDefinitions = it.allTypedefs();
266 checkState(typeDefinitions != null, "Type Definitions for module «module.name» cannot be NULL.");
268 for (TypeDefinition<?> typedef : typeDefinitions) {
269 if (typedef != null) {
270 final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef,
273 genCtx.get(module).addTypedefType(typedef.getPath(), type);
279 private GeneratedTypeBuilder processDataSchemaNode(final Module module, final String basePackageName,
280 final GeneratedTypeBuilder childOf, final DataSchemaNode node) {
281 if (node.isAugmenting() || node.isAddedByUses()) {
284 final String packageName = packageNameForGeneratedType(basePackageName, node.getPath());
285 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, node, childOf);
286 genType.addComment(node.getDescription());
287 if (node instanceof DataNodeContainer) {
288 genCtx.get(module).addChildNodeType(node.getPath(), genType);
289 groupingsToGenTypes(module, ((DataNodeContainer) node).getGroupings());
290 processUsesAugments((DataNodeContainer) node, module);
295 private void containerToGenType(final Module module, final String basePackageName,
296 final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf, final ContainerSchemaNode node) {
297 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node);
298 if (genType != null) {
299 constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), genType);
300 resolveDataSchemaNodes(module, basePackageName, genType, genType, node.getChildNodes());
304 private void listToGenType(final Module module, final String basePackageName, final GeneratedTypeBuilder parent,
305 final GeneratedTypeBuilder childOf, final ListSchemaNode node) {
306 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node);
307 if (genType != null) {
308 constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), Types.listTypeFor(genType));
310 final List<String> listKeys = listKeys(node);
311 final String packageName = packageNameForGeneratedType(basePackageName, node.getPath());
312 final GeneratedTOBuilder genTOBuilder = resolveListKeyTOBuilder(packageName, node);
313 if (genTOBuilder != null) {
314 final Type identifierMarker = Types.parameterizedTypeFor(IDENTIFIER, genType);
315 final Type identifiableMarker = Types.parameterizedTypeFor(IDENTIFIABLE, genTOBuilder);
316 genTOBuilder.addImplementsType(identifierMarker);
317 genType.addImplementsType(identifiableMarker);
320 for (DataSchemaNode schemaNode : node.getChildNodes()) {
321 if (!schemaNode.isAugmenting()) {
322 addSchemaNodeToListBuilders(basePackageName, schemaNode, genType, genTOBuilder, listKeys, module);
327 if (genTOBuilder != null) {
328 final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
329 prop.setValue(Long.toString(computeDefaultSUID(genTOBuilder)));
330 genTOBuilder.setSUID(prop);
333 typeBuildersToGenTypes(module, genType, genTOBuilder);
337 private void processUsesAugments(final DataNodeContainer node, final Module module) {
338 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
339 for (UsesNode usesNode : node.getUses()) {
340 for (AugmentationSchema augment : usesNode.getAugmentations()) {
341 usesAugmentationToGenTypes(basePackageName, augment, module, usesNode, node);
342 processUsesAugments(augment, module);
348 * Converts all <b>augmentation</b> of the module to the list
349 * <code>Type</code> objects.
352 * module from which is obtained list of all augmentation objects
353 * to iterate over them
354 * @throws IllegalArgumentException
356 * <li>if the module is null</li>
357 * <li>if the name of module is null</li>
359 * @throws IllegalStateException
360 * if set of augmentations from module is null
362 private void allAugmentsToGenTypes(final Module module) {
363 checkArgument(module != null, "Module reference cannot be NULL.");
364 checkArgument(module.getName() != null, "Module name cannot be NULL.");
365 checkState(module.getAugmentations() != null, "Augmentations Set cannot be NULL.");
367 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
368 final List<AugmentationSchema> augmentations = resolveAugmentations(module);
369 for (AugmentationSchema augment : augmentations) {
370 augmentationToGenTypes(basePackageName, augment, module);
375 * Returns list of <code>AugmentationSchema</code> objects. The objects are
376 * sorted according to the length of their target path from the shortest to
380 * module from which is obtained list of all augmentation objects
381 * @return list of sorted <code>AugmentationSchema</code> objects obtained
382 * from <code>module</code>
383 * @throws IllegalArgumentException
385 * @throws IllegalStateException
386 * if set of module augmentations is null
388 private List<AugmentationSchema> resolveAugmentations(final Module module) {
389 checkArgument(module != null, "Module reference cannot be NULL.");
390 checkState(module.getAugmentations() != null, "Augmentations Set cannot be NULL.");
392 final Set<AugmentationSchema> augmentations = module.getAugmentations();
393 List<AugmentationSchema> sortedAugmentations = new ArrayList<>(augmentations);
394 Collections.sort(sortedAugmentations, Comparators.AUGMENT_COMP);
396 return sortedAugmentations;
400 * Create GeneratedTypeBuilder object from module argument.
403 * Module object from which builder will be created
404 * @return <code>GeneratedTypeBuilder</code> which is internal
405 * representation of the module
406 * @throws IllegalArgumentException
409 private GeneratedTypeBuilder moduleToDataType(final Module module) {
410 checkArgument(module != null, "Module reference cannot be NULL.");
412 final GeneratedTypeBuilder moduleDataTypeBuilder = moduleTypeBuilder(module, "Data");
413 addImplementedInterfaceFromUses(module, moduleDataTypeBuilder);
414 moduleDataTypeBuilder.addImplementsType(DATA_ROOT);
415 moduleDataTypeBuilder.addComment(module.getDescription());
416 return moduleDataTypeBuilder;
420 * Converts all <b>rpcs</b> inputs and outputs substatements of the module
421 * to the list of <code>Type</code> objects. In addition are to containers
422 * and lists which belong to input or output also part of returning list.
425 * module from which is obtained set of all rpc objects to
427 * @throws IllegalArgumentException
429 * <li>if the module is null</li>
430 * <li>if the name of module is null</li>
432 * @throws IllegalStateException
433 * if set of rpcs from module is null
435 private void rpcMethodsToGenType(final Module module) {
436 checkArgument(module != null, "Module reference cannot be NULL.");
437 checkArgument(module.getName() != null, "Module name cannot be NULL.");
438 final Set<RpcDefinition> rpcDefinitions = module.getRpcs();
439 checkState(rpcDefinitions != null, "Set of rpcs from module " + module.getName() + " cannot be NULL.");
440 if (rpcDefinitions.isEmpty()) {
444 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
445 final GeneratedTypeBuilder interfaceBuilder = moduleTypeBuilder(module, "Service");
446 interfaceBuilder.addImplementsType(Types.typeForClass(RpcService.class));
447 for (RpcDefinition rpc : rpcDefinitions) {
449 final String rpcName = BindingMapping.getClassName(rpc.getQName());
450 final String rpcMethodName = parseToValidParamName(rpcName);
451 final String rpcComment = rpc.getDescription();
452 final MethodSignatureBuilder method = interfaceBuilder.addMethod(rpcMethodName);
453 final ContainerSchemaNode input = rpc.getInput();
454 final ContainerSchemaNode output = rpc.getOutput();
457 final GeneratedTypeBuilder inType = addRawInterfaceDefinition(basePackageName, input, rpcName);
458 addImplementedInterfaceFromUses(input, inType);
459 inType.addImplementsType(DATA_OBJECT);
460 inType.addImplementsType(augmentable(inType));
461 resolveDataSchemaNodes(module, basePackageName, inType, inType, input.getChildNodes());
462 genCtx.get(module).addChildNodeType(input.getPath(), inType);
463 final GeneratedType inTypeInstance = inType.toInstance();
464 method.addParameter(inTypeInstance, "input");
467 Type outTypeInstance = VOID;
468 if (output != null) {
469 final GeneratedTypeBuilder outType = addRawInterfaceDefinition(basePackageName, output, rpcName);
470 addImplementedInterfaceFromUses(output, outType);
471 outType.addImplementsType(DATA_OBJECT);
472 outType.addImplementsType(augmentable(outType));
473 resolveDataSchemaNodes(module, basePackageName, outType, outType, output.getChildNodes());
474 genCtx.get(module).addChildNodeType(output.getPath(), outType);
475 outTypeInstance = outType.toInstance();
478 final Type rpcRes = Types.parameterizedTypeFor(Types.typeForClass(RpcResult.class), outTypeInstance);
479 method.setComment(rpcComment);
480 method.setReturnType(Types.parameterizedTypeFor(FUTURE, rpcRes));
484 genCtx.get(module).addTopLevelNodeType(interfaceBuilder);
488 * Converts all <b>notifications</b> of the module to the list of
489 * <code>Type</code> objects. In addition are to this list added containers
490 * and lists which are part of this notification.
493 * module from which is obtained set of all notification objects
494 * to iterate over them
495 * @throws IllegalArgumentException
497 * <li>if the module equals null</li>
498 * <li>if the name of module equals null</li>
500 * @throws IllegalStateException
501 * if set of notifications from module is null
503 private void notificationsToGenType(final Module module) {
504 checkArgument(module != null, "Module reference cannot be NULL.");
505 checkArgument(module.getName() != null, "Module name cannot be NULL.");
506 final Set<NotificationDefinition> notifications = module.getNotifications();
507 checkState(notifications != null, "Set of notification from module " + module.getName() + " cannot be NULL.");
508 if (notifications.isEmpty()) {
512 final GeneratedTypeBuilder listenerInterface = moduleTypeBuilder(module, "Listener");
513 listenerInterface.addImplementsType(BindingTypes.NOTIFICATION_LISTENER);
514 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
516 for (NotificationDefinition notification : notifications) {
517 if (notification != null) {
518 processUsesAugments(notification, module);
520 final GeneratedTypeBuilder notificationInterface = addDefaultInterfaceDefinition(basePackageName,
521 notification, BindingTypes.DATA_OBJECT);
522 notificationInterface.addImplementsType(NOTIFICATION);
523 genCtx.get(module).addChildNodeType(notification.getPath(), notificationInterface);
525 // Notification object
526 resolveDataSchemaNodes(module, basePackageName, notificationInterface, notificationInterface,
527 notification.getChildNodes());
529 listenerInterface.addMethod("on" + notificationInterface.getName())
530 .setAccessModifier(AccessModifier.PUBLIC).addParameter(notificationInterface, "notification")
531 .setComment(notification.getDescription()).setReturnType(Types.VOID);
535 genCtx.get(module).addTopLevelNodeType(listenerInterface);
539 * Converts all <b>identities</b> of the module to the list of
540 * <code>Type</code> objects.
543 * module from which is obtained set of all identity objects to
546 * schema context only used as input parameter for method
547 * {@link identityToGenType}
550 private void allIdentitiesToGenTypes(final Module module, final SchemaContext context) {
551 final Set<IdentitySchemaNode> schemaIdentities = module.getIdentities();
552 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
554 if (schemaIdentities != null && !schemaIdentities.isEmpty()) {
555 for (IdentitySchemaNode identity : schemaIdentities) {
556 identityToGenType(module, basePackageName, identity, context);
562 * Converts the <b>identity</b> object to GeneratedType. Firstly it is
563 * created transport object builder. If identity contains base identity then
564 * reference to base identity is added to superior identity as its extend.
565 * If identity doesn't contain base identity then only reference to abstract
566 * class {@link org.opendaylight.yangtools.yang.model.api.BaseIdentity
567 * BaseIdentity} is added
571 * @param basePackageName
572 * string contains the module package name
574 * IdentitySchemaNode which contains data about identity
576 * SchemaContext which is used to get package and name
577 * information about base of identity
580 private void identityToGenType(final Module module, final String basePackageName,
581 final IdentitySchemaNode identity, final SchemaContext context) {
582 if (identity == null) {
585 final String packageName = packageNameForGeneratedType(basePackageName, identity.getPath());
586 final String genTypeName = BindingMapping.getClassName(identity.getQName());
587 final GeneratedTOBuilderImpl newType = new GeneratedTOBuilderImpl(packageName, genTypeName);
588 final IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
589 if (baseIdentity == null) {
590 final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(BaseIdentity.class.getPackage().getName(),
591 BaseIdentity.class.getSimpleName());
592 newType.setExtendsType(gto.toInstance());
594 final Module baseIdentityParentModule = SchemaContextUtil.findParentModule(context, baseIdentity);
595 final String returnTypePkgName = BindingMapping.getRootPackageName(baseIdentityParentModule
597 final String returnTypeName = BindingMapping.getClassName(baseIdentity.getQName());
598 final GeneratedTransferObject gto = new GeneratedTOBuilderImpl(returnTypePkgName, returnTypeName)
600 newType.setExtendsType(gto);
602 newType.setAbstract(true);
603 newType.addComment(identity.getDescription());
604 newType.setDescription(identity.getDescription());
605 newType.setReference(identity.getReference());
606 newType.setModuleName(module.getName());
607 newType.setSchemaPath(identity.getPath().getPathFromRoot());
609 final QName qname = identity.getQName();
610 qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, qname);
612 genCtx.get(module).addIdentityType(identity.getQName(), newType);
615 private static Constant qnameConstant(final GeneratedTypeBuilderBase<?> toBuilder, final String constantName,
617 StringBuilder sb = new StringBuilder("org.opendaylight.yangtools.yang.common.QName");
618 sb.append(".create(");
620 sb.append(name.getNamespace());
622 sb.append(name.getFormattedRevision());
624 sb.append(name.getLocalName());
627 return toBuilder.addConstant(typeForClass(QName.class), constantName, sb.toString());
631 * Converts all <b>groupings</b> of the module to the list of
632 * <code>Type</code> objects. Firstly are groupings sorted according mutual
633 * dependencies. At least dependent (independent) groupings are in the list
634 * saved at first positions. For every grouping the record is added to map
635 * {@link BindingGeneratorImpl#allGroupings allGroupings}
640 * of groupings from which types will be generated
643 private void groupingsToGenTypes(final Module module, final Collection<GroupingDefinition> groupings) {
644 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
645 final List<GroupingDefinition> groupingsSortedByDependencies = new GroupingDefinitionDependencySort()
647 for (GroupingDefinition grouping : groupingsSortedByDependencies) {
648 groupingToGenType(basePackageName, grouping, module);
653 * Converts individual grouping to GeneratedType. Firstly generated type
654 * builder is created and every child node of grouping is resolved to the
657 * @param basePackageName
658 * string contains the module package name
660 * GroupingDefinition which contains data about grouping
663 * @return GeneratedType which is generated from grouping (object of type
664 * <code>GroupingDefinition</code>)
666 private void groupingToGenType(final String basePackageName, final GroupingDefinition grouping, final Module module) {
667 final String packageName = packageNameForGeneratedType(basePackageName, grouping.getPath());
668 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, grouping);
669 genCtx.get(module).addGroupingType(grouping.getPath(), genType);
670 resolveDataSchemaNodes(module, basePackageName, genType, genType, grouping.getChildNodes());
671 groupingsToGenTypes(module, grouping.getGroupings());
672 processUsesAugments(grouping, module);
676 * Adds enumeration builder created from <code>enumTypeDef</code> to
677 * <code>typeBuilder</code>.
679 * Each <code>enumTypeDef</code> item is added to builder with its name and
683 * EnumTypeDefinition contains enum data
685 * string contains name which will be assigned to enumeration
688 * GeneratedTypeBuilder to which will be enum builder assigned
689 * @return enumeration builder which contains data from
690 * <code>enumTypeDef</code>
692 private EnumBuilder resolveInnerEnumFromTypeDefinition(final EnumTypeDefinition enumTypeDef, final QName enumName,
693 final GeneratedTypeBuilder typeBuilder) {
694 if ((enumTypeDef != null) && (typeBuilder != null) && (enumTypeDef.getQName() != null)
695 && (enumTypeDef.getQName().getLocalName() != null)) {
696 final String enumerationName = BindingMapping.getClassName(enumName);
697 final EnumBuilder enumBuilder = typeBuilder.addEnumeration(enumerationName);
698 enumBuilder.setDescription(enumTypeDef.getDescription());
699 enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
706 * Generates type builder for <code>module</code>.
709 * Module which is source of package name for generated type
712 * string which is added to the module class name representation
714 * @return instance of GeneratedTypeBuilder which represents
715 * <code>module</code>.
716 * @throws IllegalArgumentException
717 * if <code>module</code> is null
719 private GeneratedTypeBuilder moduleTypeBuilder(final Module module, final String postfix) {
720 checkArgument(module != null, "Module reference cannot be NULL.");
721 final String packageName = BindingMapping.getRootPackageName(module.getQNameModule());
722 final String moduleName = BindingMapping.getClassName(module.getName()) + postfix;
724 final GeneratedTypeBuilderImpl moduleBuilder = new GeneratedTypeBuilderImpl(packageName, moduleName);
725 moduleBuilder.setDescription(module.getDescription());
726 moduleBuilder.setReference(module.getReference());
727 moduleBuilder.setModuleName(moduleName);
729 return moduleBuilder;
733 * Converts <code>augSchema</code> to list of <code>Type</code> which
734 * contains generated type for augmentation. In addition there are also
735 * generated types for all containers, list and choices which are child of
736 * <code>augSchema</code> node or a generated types for cases are added if
737 * augmented node is choice.
739 * @param augmentPackageName
740 * string with the name of the package to which the augmentation
743 * AugmentationSchema which is contains data about augmentation
744 * (target path, childs...)
747 * @param parentUsesNode
748 * @throws IllegalArgumentException
750 * <li>if <code>augmentPackageName</code> equals null</li>
751 * <li>if <code>augSchema</code> equals null</li>
753 * @throws IllegalStateException
754 * if augment target path is null
756 private void augmentationToGenTypes(final String augmentPackageName, final AugmentationSchema augSchema,
757 final Module module) {
758 checkArgument(augmentPackageName != null, "Package Name cannot be NULL.");
759 checkArgument(augSchema != null, "Augmentation Schema cannot be NULL.");
760 checkState(augSchema.getTargetPath() != null,
761 "Augmentation Schema does not contain Target Path (Target Path is NULL).");
763 processUsesAugments(augSchema, module);
764 final SchemaPath targetPath = augSchema.getTargetPath();
765 SchemaNode targetSchemaNode = null;
767 targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
768 if (targetSchemaNode instanceof DataSchemaNode && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
769 if (targetSchemaNode instanceof DerivableSchemaNode) {
770 targetSchemaNode = ((DerivableSchemaNode) targetSchemaNode).getOriginal().orNull();
772 if (targetSchemaNode == null) {
773 throw new IllegalStateException("Failed to find target node from grouping in augmentation " + augSchema
774 + " in module " + module.getName());
777 if (targetSchemaNode == null) {
778 throw new IllegalArgumentException("augment target not found: " + targetPath);
781 GeneratedTypeBuilder targetTypeBuilder = findChildNodeByPath(targetSchemaNode.getPath());
782 if (targetTypeBuilder == null) {
783 targetTypeBuilder = findCaseByPath(targetSchemaNode.getPath());
785 if (targetTypeBuilder == null) {
786 throw new NullPointerException("Target type not yet generated: " + targetSchemaNode);
789 if (!(targetSchemaNode instanceof ChoiceNode)) {
790 String packageName = augmentPackageName;
791 final Type targetType = new ReferencedTypeImpl(targetTypeBuilder.getPackageName(),
792 targetTypeBuilder.getName());
793 addRawAugmentGenTypeDefinition(module, packageName, augmentPackageName, targetType, augSchema);
796 generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance(),
797 (ChoiceNode) targetSchemaNode, augSchema.getChildNodes());
801 private void usesAugmentationToGenTypes(final String augmentPackageName, final AugmentationSchema augSchema,
802 final Module module, final UsesNode usesNode, final DataNodeContainer usesNodeParent) {
803 checkArgument(augmentPackageName != null, "Package Name cannot be NULL.");
804 checkArgument(augSchema != null, "Augmentation Schema cannot be NULL.");
805 checkState(augSchema.getTargetPath() != null,
806 "Augmentation Schema does not contain Target Path (Target Path is NULL).");
808 processUsesAugments(augSchema, module);
809 final SchemaPath targetPath = augSchema.getTargetPath();
810 SchemaNode targetSchemaNode = findOriginalTargetFromGrouping(targetPath, usesNode);
811 if (targetSchemaNode == null) {
812 throw new IllegalArgumentException("augment target not found: " + targetPath);
815 GeneratedTypeBuilder targetTypeBuilder = findChildNodeByPath(targetSchemaNode.getPath());
816 if (targetTypeBuilder == null) {
817 targetTypeBuilder = findCaseByPath(targetSchemaNode.getPath());
819 if (targetTypeBuilder == null) {
820 throw new NullPointerException("Target type not yet generated: " + targetSchemaNode);
823 if (!(targetSchemaNode instanceof ChoiceNode)) {
824 String packageName = augmentPackageName;
825 if (usesNodeParent instanceof SchemaNode) {
826 packageName = packageNameForGeneratedType(augmentPackageName, ((SchemaNode) usesNodeParent).getPath(),
829 addRawAugmentGenTypeDefinition(module, packageName, augmentPackageName, targetTypeBuilder.toInstance(),
832 generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance(),
833 (ChoiceNode) targetSchemaNode, augSchema.getChildNodes());
838 * Convenient method to find node added by uses statement.
842 * @param parentUsesNode
843 * parent of uses node
844 * @return node from its original location in grouping
846 private DataSchemaNode findOriginalTargetFromGrouping(final SchemaPath targetPath, final UsesNode parentUsesNode) {
847 SchemaNode targetGrouping = findNodeInSchemaContext(schemaContext, parentUsesNode.getGroupingPath()
849 if (!(targetGrouping instanceof GroupingDefinition)) {
850 throw new IllegalArgumentException("Failed to generate code for augment in " + parentUsesNode);
853 GroupingDefinition grouping = (GroupingDefinition) targetGrouping;
854 SchemaNode result = grouping;
855 for (QName node : targetPath.getPathFromRoot()) {
856 // finding by local name is valid, grouping cannot contain nodes
857 // with same name and different namespace
858 if (result instanceof DataNodeContainer) {
859 result = ((DataNodeContainer) result).getDataChildByName(node.getLocalName());
860 } else if (result instanceof ChoiceNode) {
861 result = ((ChoiceNode) result).getCaseNodeByName(node.getLocalName());
864 if (result == null) {
868 boolean fromUses = ((DataSchemaNode) result).isAddedByUses();
869 Iterator<UsesNode> groupingUses = grouping.getUses().iterator();
870 while (groupingUses.hasNext() && fromUses) {
871 result = findOriginalTargetFromGrouping(targetPath, groupingUses.next());
872 if (result != null) {
873 fromUses = ((DataSchemaNode) result).isAddedByUses();
877 // this indicates invalid yang and thus possible bug in code because
878 // invalid yang should be already spotted by parser
879 throw new IllegalStateException("Failed to generate code for augment in " + parentUsesNode);
882 return (DataSchemaNode) result;
886 * Returns a generated type builder for an augmentation.
888 * The name of the type builder is equal to the name of augmented node with
889 * serial number as suffix.
893 * @param augmentPackageName
894 * string with contains the package name to which the augment
896 * @param basePackageName
897 * string with the package name to which the augmented node
899 * @param targetTypeRef
902 * augmentation schema which contains data about the child nodes
903 * and uses of augment
904 * @return generated type builder for augment
906 private GeneratedTypeBuilder addRawAugmentGenTypeDefinition(final Module module, final String augmentPackageName,
907 final String basePackageName, final Type targetTypeRef, final AugmentationSchema augSchema) {
908 Map<String, GeneratedTypeBuilder> augmentBuilders = genTypeBuilders.get(augmentPackageName);
909 if (augmentBuilders == null) {
910 augmentBuilders = new HashMap<>();
911 genTypeBuilders.put(augmentPackageName, augmentBuilders);
913 final String augIdentifier = getAugmentIdentifier(augSchema.getUnknownSchemaNodes());
916 if (augIdentifier != null) {
917 augTypeName = BindingMapping.getClassName(augIdentifier);
919 augTypeName = augGenTypeName(augmentBuilders, targetTypeRef.getName());
922 final GeneratedTypeBuilder augTypeBuilder = new GeneratedTypeBuilderImpl(augmentPackageName, augTypeName);
924 augTypeBuilder.addImplementsType(DATA_OBJECT);
925 augTypeBuilder.addImplementsType(Types.augmentationTypeFor(targetTypeRef));
926 addImplementedInterfaceFromUses(augSchema, augTypeBuilder);
928 augSchemaNodeToMethods(module, basePackageName, augTypeBuilder, augTypeBuilder, augSchema.getChildNodes());
929 augmentBuilders.put(augTypeName, augTypeBuilder);
931 genCtx.get(module).addTargetToAugmentation(targetTypeRef, augTypeBuilder);
932 genCtx.get(module).addAugmentType(augTypeBuilder);
933 genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
934 return augTypeBuilder;
939 * @param unknownSchemaNodes
940 * @return nodeParameter of UnknownSchemaNode
942 private String getAugmentIdentifier(final List<UnknownSchemaNode> unknownSchemaNodes) {
943 for (UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
944 final QName nodeType = unknownSchemaNode.getNodeType();
945 if (AUGMENT_IDENTIFIER_NAME.equals(nodeType.getLocalName())
946 && YANG_EXT_NAMESPACE.equals(nodeType.getNamespace().toString())) {
947 return unknownSchemaNode.getNodeParameter();
954 * Returns first unique name for the augment generated type builder. The
955 * generated type builder name for augment consists from name of augmented
956 * node and serial number of its augmentation.
959 * map of builders which were created in the package to which the
960 * augmentation belongs
962 * string with name of augmented node
963 * @return string with unique name for augmentation builder
965 private String augGenTypeName(final Map<String, GeneratedTypeBuilder> builders, final String genTypeName) {
967 if (builders != null) {
968 while (builders.containsKey(genTypeName + index)) {
972 return genTypeName + index;
976 * Adds the methods to <code>typeBuilder</code> which represent subnodes of
977 * node for which <code>typeBuilder</code> was created.
979 * The subnodes aren't mapped to the methods if they are part of grouping or
980 * augment (in this case are already part of them).
984 * @param basePackageName
985 * string contains the module package name
987 * generated type builder which represents any node. The subnodes
988 * of this node are added to the <code>typeBuilder</code> as
989 * methods. The subnode can be of type leaf, leaf-list, list,
994 * set of data schema nodes which are the children of the node
995 * for which <code>typeBuilder</code> was created
996 * @return generated type builder which is the same builder as input
997 * parameter. The getter methods (representing child nodes) could be
1000 private GeneratedTypeBuilder resolveDataSchemaNodes(final Module module, final String basePackageName,
1001 final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf, final Iterable<DataSchemaNode> schemaNodes) {
1002 if (schemaNodes != null && parent != null) {
1003 for (DataSchemaNode schemaNode : schemaNodes) {
1004 if (!schemaNode.isAugmenting() && !schemaNode.isAddedByUses()) {
1005 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, parent, childOf, module);
1013 * Adds the methods to <code>typeBuilder</code> what represents subnodes of
1014 * node for which <code>typeBuilder</code> was created.
1018 * @param basePackageName
1019 * string contains the module package name
1020 * @param typeBuilder
1021 * generated type builder which represents any node. The subnodes
1022 * of this node are added to the <code>typeBuilder</code> as
1023 * methods. The subnode can be of type leaf, leaf-list, list,
1024 * container, choice.
1027 * @param schemaNodes
1028 * set of data schema nodes which are the children of the node
1029 * for which <code>typeBuilder</code> was created
1030 * @return generated type builder which is the same object as the input
1031 * parameter <code>typeBuilder</code>. The getter method could be
1034 private GeneratedTypeBuilder augSchemaNodeToMethods(final Module module, final String basePackageName,
1035 final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf,
1036 final Iterable<DataSchemaNode> schemaNodes) {
1037 if ((schemaNodes != null) && (typeBuilder != null)) {
1038 for (DataSchemaNode schemaNode : schemaNodes) {
1039 if (!schemaNode.isAugmenting()) {
1040 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, typeBuilder, childOf, module);
1048 * Adds to <code>typeBuilder</code> a method which is derived from
1049 * <code>schemaNode</code>.
1051 * @param basePackageName
1052 * string with the module package name
1054 * data schema node which is added to <code>typeBuilder</code> as
1056 * @param typeBuilder
1057 * generated type builder to which is <code>schemaNode</code>
1058 * added as a method.
1064 private void addSchemaNodeToBuilderAsMethod(final String basePackageName, final DataSchemaNode node,
1065 final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf, final Module module) {
1066 if (node != null && typeBuilder != null) {
1067 if (node instanceof LeafSchemaNode) {
1068 resolveLeafSchemaNodeAsMethod(typeBuilder, (LeafSchemaNode) node);
1069 } else if (node instanceof LeafListSchemaNode) {
1070 resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) node);
1071 } else if (node instanceof ContainerSchemaNode) {
1072 containerToGenType(module, basePackageName, typeBuilder, childOf, (ContainerSchemaNode) node);
1073 } else if (node instanceof ListSchemaNode) {
1074 listToGenType(module, basePackageName, typeBuilder, childOf, (ListSchemaNode) node);
1075 } else if (node instanceof ChoiceNode) {
1076 choiceToGeneratedType(module, basePackageName, typeBuilder, (ChoiceNode) node);
1078 // TODO: anyxml not yet supported
1079 LOG.debug("Unable to add schema node {} as method in {}: unsupported type of node.", node.getClass(),
1080 typeBuilder.getFullyQualifiedName());
1086 * Converts <code>choiceNode</code> to the list of generated types for
1087 * choice and its cases.
1089 * The package names for choice and for its cases are created as
1090 * concatenation of the module package (<code>basePackageName</code>) and
1091 * names of all parents node.
1095 * @param basePackageName
1096 * string with the module package name
1100 * choice node which is mapped to generated type. Also child
1101 * nodes - cases are mapped to generated types.
1102 * @throws IllegalArgumentException
1104 * <li>if <code>basePackageName</code> is null</li>
1105 * <li>if <code>choiceNode</code> is null</li>
1108 private void choiceToGeneratedType(final Module module, final String basePackageName,
1109 final GeneratedTypeBuilder parent, final ChoiceNode choiceNode) {
1110 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
1111 checkArgument(choiceNode != null, "Choice Schema Node cannot be NULL.");
1113 if (!choiceNode.isAddedByUses()) {
1114 final String packageName = packageNameForGeneratedType(basePackageName, choiceNode.getPath());
1115 final GeneratedTypeBuilder choiceTypeBuilder = addRawInterfaceDefinition(packageName, choiceNode);
1116 constructGetter(parent, choiceNode.getQName().getLocalName(), choiceNode.getDescription(),
1118 choiceTypeBuilder.addImplementsType(typeForClass(DataContainer.class));
1119 genCtx.get(module).addChildNodeType(choiceNode.getPath(), choiceTypeBuilder);
1120 generateTypesFromChoiceCases(module, basePackageName, choiceTypeBuilder.toInstance(), choiceNode);
1125 * Converts <code>caseNodes</code> set to list of corresponding generated
1128 * For every <i>case</i> which isn't added through augment or <i>uses</i> is
1129 * created generated type builder. The package names for the builder is
1130 * created as concatenation of the module package (
1131 * <code>basePackageName</code>) and names of all parents nodes of the
1132 * concrete <i>case</i>. There is also relation "<i>implements type</i>"
1133 * between every case builder and <i>choice</i> type
1137 * @param basePackageName
1138 * string with the module package name
1139 * @param refChoiceType
1140 * type which represents superior <i>case</i>
1142 * choice case node which is mapped to generated type
1143 * @return list of generated types for <code>caseNodes</code>.
1144 * @throws IllegalArgumentException
1146 * <li>if <code>basePackageName</code> equals null</li>
1147 * <li>if <code>refChoiceType</code> equals null</li>
1148 * <li>if <code>caseNodes</code> equals null</li>
1151 private void generateTypesFromChoiceCases(final Module module, final String basePackageName,
1152 final Type refChoiceType, final ChoiceNode choiceNode) {
1153 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
1154 checkArgument(refChoiceType != null, "Referenced Choice Type cannot be NULL.");
1155 checkArgument(choiceNode != null, "ChoiceNode cannot be NULL.");
1157 final Set<ChoiceCaseNode> caseNodes = choiceNode.getCases();
1158 if (caseNodes == null) {
1162 for (ChoiceCaseNode caseNode : caseNodes) {
1163 if (caseNode != null && !caseNode.isAddedByUses() && !caseNode.isAugmenting()) {
1164 final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath());
1165 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode);
1166 caseTypeBuilder.addImplementsType(refChoiceType);
1167 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
1168 genCtx.get(module).addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode);
1169 final Iterable<DataSchemaNode> caseChildNodes = caseNode.getChildNodes();
1170 if (caseChildNodes != null) {
1171 Object parentNode = null;
1172 final SchemaPath nodeSp = choiceNode.getPath();
1173 parentNode = findDataSchemaNode(schemaContext, nodeSp.getParent());
1176 if (parentNode instanceof AugmentationSchema) {
1177 final AugmentationSchema augSchema = (AugmentationSchema) parentNode;
1178 final SchemaPath targetPath = augSchema.getTargetPath();
1179 SchemaNode targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
1180 if (targetSchemaNode instanceof DataSchemaNode
1181 && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
1182 if (targetSchemaNode instanceof DerivableSchemaNode) {
1183 targetSchemaNode = ((DerivableSchemaNode) targetSchemaNode).getOriginal().orNull();
1185 if (targetSchemaNode == null) {
1186 throw new IllegalStateException(
1187 "Failed to find target node from grouping for augmentation " + augSchema
1188 + " in module " + module.getName());
1191 parent = targetSchemaNode;
1193 final SchemaPath sp = choiceNode.getPath();
1194 parent = findDataSchemaNode(schemaContext, sp.getParent());
1196 GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.getPath());
1197 if (childOfType == null) {
1198 childOfType = findGroupingByPath(parent.getPath());
1200 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, caseChildNodes);
1204 processUsesAugments(caseNode, module);
1209 * Generates list of generated types for all the cases of a choice which are
1210 * added to the choice through the augment.
1214 * @param basePackageName
1215 * string contains name of package to which augment belongs. If
1216 * an augmented choice is from an other package (pcg1) than an
1217 * augmenting choice (pcg2) then case's of the augmenting choice
1218 * will belong to pcg2.
1220 * Type which represents target choice
1222 * node which represents target choice
1223 * @param augmentedNodes
1224 * set of choice case nodes for which is checked if are/aren't
1225 * added to choice through augmentation
1226 * @return list of generated types which represents augmented cases of
1227 * choice <code>refChoiceType</code>
1228 * @throws IllegalArgumentException
1230 * <li>if <code>basePackageName</code> is null</li>
1231 * <li>if <code>targetType</code> is null</li>
1232 * <li>if <code>augmentedNodes</code> is null</li>
1235 private void generateTypesFromAugmentedChoiceCases(final Module module, final String basePackageName,
1236 final Type targetType, final ChoiceNode targetNode, final Iterable<DataSchemaNode> augmentedNodes) {
1237 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
1238 checkArgument(targetType != null, "Referenced Choice Type cannot be NULL.");
1239 checkArgument(augmentedNodes != null, "Set of Choice Case Nodes cannot be NULL.");
1241 for (DataSchemaNode caseNode : augmentedNodes) {
1242 if (caseNode != null) {
1243 final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath());
1244 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode);
1245 caseTypeBuilder.addImplementsType(targetType);
1247 SchemaNode parent = null;
1248 final SchemaPath nodeSp = targetNode.getPath();
1249 parent = findDataSchemaNode(schemaContext, nodeSp.getParent());
1251 GeneratedTypeBuilder childOfType = null;
1252 if (parent instanceof Module) {
1253 childOfType = genCtx.get(parent).getModuleNode();
1254 } else if (parent instanceof ChoiceCaseNode) {
1255 childOfType = findCaseByPath(parent.getPath());
1256 } else if (parent instanceof DataSchemaNode || parent instanceof NotificationDefinition) {
1257 childOfType = findChildNodeByPath(parent.getPath());
1258 } else if (parent instanceof GroupingDefinition) {
1259 childOfType = findGroupingByPath(parent.getPath());
1262 if (childOfType == null) {
1263 throw new IllegalArgumentException("Failed to find parent type of choice " + targetNode);
1266 ChoiceCaseNode node = null;
1267 if (caseNode instanceof ChoiceCaseNode) {
1268 node = (ChoiceCaseNode) caseNode;
1270 node = targetNode.getCaseNodeByName(caseNode.getQName().getLocalName());
1272 final Iterable<DataSchemaNode> childNodes = node.getChildNodes();
1273 if (childNodes != null) {
1274 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, childNodes);
1276 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
1277 genCtx.get(module).addChoiceToCaseMapping(targetType, caseTypeBuilder, node);
1283 * Converts <code>leaf</code> to the getter method which is added to
1284 * <code>typeBuilder</code>.
1286 * @param typeBuilder
1287 * generated type builder to which is added getter method as
1288 * <code>leaf</code> mapping
1290 * leaf schema node which is mapped as getter method which is
1291 * added to <code>typeBuilder</code>
1292 * @return boolean value
1294 * <li>false - if <code>leaf</code> or <code>typeBuilder</code> are
1296 * <li>true - in other cases</li>
1299 private Type resolveLeafSchemaNodeAsMethod(final GeneratedTypeBuilder typeBuilder, final LeafSchemaNode leaf) {
1300 Type returnType = null;
1301 if ((leaf != null) && (typeBuilder != null)) {
1302 final String leafName = leaf.getQName().getLocalName();
1303 String leafDesc = leaf.getDescription();
1304 if (leafDesc == null) {
1308 final Module parentModule = findParentModule(schemaContext, leaf);
1309 if (leafName != null && !leaf.isAddedByUses()) {
1310 final TypeDefinition<?> typeDef = leaf.getType();
1312 GeneratedTOBuilder genTOBuilder;
1313 if (typeDef instanceof EnumTypeDefinition) {
1314 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1315 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
1316 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(),
1319 if (enumBuilder != null) {
1320 returnType = enumBuilder.toInstance(typeBuilder);
1322 ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
1323 } else if (typeDef instanceof UnionType) {
1324 genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
1325 if (genTOBuilder != null) {
1326 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
1328 } else if (typeDef instanceof BitsTypeDefinition) {
1329 genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
1330 if (genTOBuilder != null) {
1331 returnType = new ReferencedTypeImpl(genTOBuilder.getPackageName(), genTOBuilder.getName());
1334 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
1335 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
1337 if (returnType != null) {
1338 final MethodSignatureBuilder getter = constructGetter(typeBuilder, leafName, leafDesc, returnType);
1339 processContextRefExtension(leaf, getter, parentModule);
1346 private void processContextRefExtension(final LeafSchemaNode leaf, final MethodSignatureBuilder getter,
1347 final Module module) {
1348 for (UnknownSchemaNode node : leaf.getUnknownSchemaNodes()) {
1349 final QName nodeType = node.getNodeType();
1350 if ("context-reference".equals(nodeType.getLocalName())) {
1351 final String nodeParam = node.getNodeParameter();
1352 IdentitySchemaNode identity = null;
1353 String basePackageName = null;
1354 final Iterable<String> splittedElement = Splitter.on(':').split(nodeParam);
1355 final Iterator<String> iterator = splittedElement.iterator();
1356 final int length = Iterables.size(splittedElement);
1358 identity = findIdentityByName(module.getIdentities(), iterator.next());
1359 basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
1360 } else if (length == 2) {
1361 String prefix = iterator.next();
1362 final Module dependentModule = findModuleFromImports(module.getImports(), prefix);
1363 if (dependentModule == null) {
1364 throw new IllegalArgumentException("Failed to process context-reference: unknown prefix "
1367 identity = findIdentityByName(dependentModule.getIdentities(), iterator.next());
1368 basePackageName = BindingMapping.getRootPackageName(dependentModule.getQNameModule());
1370 throw new IllegalArgumentException("Failed to process context-reference: unknown identity "
1373 if (identity == null) {
1374 throw new IllegalArgumentException("Failed to process context-reference: unknown identity "
1378 final Class<RoutingContext> clazz = RoutingContext.class;
1379 final AnnotationTypeBuilder rc = getter.addAnnotation(clazz.getPackage().getName(),
1380 clazz.getSimpleName());
1381 final String packageName = packageNameForGeneratedType(basePackageName, identity.getPath());
1382 final String genTypeName = BindingMapping.getClassName(identity.getQName().getLocalName());
1383 rc.addParameter("value", packageName + "." + genTypeName + ".class");
1388 private IdentitySchemaNode findIdentityByName(final Set<IdentitySchemaNode> identities, final String name) {
1389 for (IdentitySchemaNode id : identities) {
1390 if (id.getQName().getLocalName().equals(name)) {
1397 private Module findModuleFromImports(final Set<ModuleImport> imports, final String prefix) {
1398 for (ModuleImport imp : imports) {
1399 if (imp.getPrefix().equals(prefix)) {
1400 return schemaContext.findModuleByName(imp.getModuleName(), imp.getRevision());
1406 private boolean resolveLeafSchemaNodeAsProperty(final GeneratedTOBuilder toBuilder, final LeafSchemaNode leaf,
1407 final boolean isReadOnly, final Module module) {
1408 if ((leaf != null) && (toBuilder != null)) {
1409 final String leafName = leaf.getQName().getLocalName();
1410 String leafDesc = leaf.getDescription();
1411 if (leafDesc == null) {
1415 if (leafName != null) {
1416 Type returnType = null;
1417 final TypeDefinition<?> typeDef = leaf.getType();
1418 if (typeDef instanceof UnionTypeDefinition) {
1419 // GeneratedType for this type definition should be already
1421 QName qname = typeDef.getQName();
1422 Module unionModule = null;
1423 String prefix = qname.getPrefix();
1424 if (prefix == null || prefix.isEmpty() || prefix.equals(module.getPrefix())) {
1425 unionModule = module;
1427 unionModule = findModuleFromImports(module.getImports(), qname.getPrefix());
1429 final ModuleContext mc = genCtx.get(unionModule);
1430 returnType = mc.getTypedefs().get(typeDef.getPath());
1432 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1434 return resolveLeafSchemaNodeAsProperty(toBuilder, leaf, returnType, isReadOnly);
1441 * Converts <code>leaf</code> schema node to property of generated TO
1445 * generated TO builder to which is <code>leaf</code> added as
1448 * leaf schema node which is added to <code>toBuilder</code> as
1453 * boolean value which says if leaf property is|isn't read only
1454 * @return boolean value
1456 * <li>false - if <code>leaf</code>, <code>toBuilder</code> or leaf
1457 * name equals null or if leaf is added by <i>uses</i>.</li>
1458 * <li>true - other cases</li>
1461 private boolean resolveLeafSchemaNodeAsProperty(final GeneratedTOBuilder toBuilder, final LeafSchemaNode leaf,
1462 final Type returnType, final boolean isReadOnly) {
1463 if (returnType == null) {
1466 final String leafName = leaf.getQName().getLocalName();
1467 final String leafDesc = leaf.getDescription();
1468 final GeneratedPropertyBuilder propBuilder = toBuilder.addProperty(parseToValidParamName(leafName));
1469 propBuilder.setReadOnly(isReadOnly);
1470 propBuilder.setReturnType(returnType);
1471 propBuilder.setComment(leafDesc);
1472 toBuilder.addEqualsIdentity(propBuilder);
1473 toBuilder.addHashIdentity(propBuilder);
1474 toBuilder.addToStringProperty(propBuilder);
1479 * Converts <code>node</code> leaf list schema node to getter method of
1480 * <code>typeBuilder</code>.
1482 * @param typeBuilder
1483 * generated type builder to which is <code>node</code> added as
1486 * leaf list schema node which is added to
1487 * <code>typeBuilder</code> as getter method
1488 * @return boolean value
1490 * <li>true - if <code>node</code>, <code>typeBuilder</code>,
1491 * nodeName equal null or <code>node</code> is added by <i>uses</i></li>
1492 * <li>false - other cases</li>
1495 private boolean resolveLeafListSchemaNode(final GeneratedTypeBuilder typeBuilder, final LeafListSchemaNode node) {
1496 if ((node != null) && (typeBuilder != null)) {
1497 final QName nodeName = node.getQName();
1499 if (nodeName != null && !node.isAddedByUses()) {
1500 final TypeDefinition<?> typeDef = node.getType();
1501 final Module parentModule = findParentModule(schemaContext, node);
1503 Type returnType = null;
1504 if (typeDef instanceof EnumTypeDefinition) {
1505 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node);
1506 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
1507 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, nodeName,
1509 returnType = new ReferencedTypeImpl(enumBuilder.getPackageName(), enumBuilder.getName());
1510 ((TypeProviderImpl) typeProvider).putReferencedType(node.getPath(), returnType);
1511 } else if (typeDef instanceof UnionType) {
1512 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule);
1513 if (genTOBuilder != null) {
1514 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
1516 } else if (typeDef instanceof BitsTypeDefinition) {
1517 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule);
1518 returnType = new ReferencedTypeImpl(genTOBuilder.getPackageName(), genTOBuilder.getName());
1520 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
1521 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
1524 final ParameterizedType listType = Types.listTypeFor(returnType);
1525 constructGetter(typeBuilder, nodeName.getLocalName(), node.getDescription(), listType);
1532 private Type createReturnTypeForUnion(final GeneratedTOBuilder genTOBuilder, final TypeDefinition<?> typeDef,
1533 final GeneratedTypeBuilder typeBuilder, final Module parentModule) {
1534 final GeneratedTOBuilderImpl returnType = new GeneratedTOBuilderImpl(genTOBuilder.getPackageName(),
1535 genTOBuilder.getName());
1537 returnType.setDescription(typeDef.getDescription());
1538 returnType.setReference(typeDef.getReference());
1539 returnType.setSchemaPath(typeDef.getPath().getPathFromRoot());
1540 returnType.setModuleName(parentModule.getName());
1542 genTOBuilder.setTypedef(true);
1543 genTOBuilder.setIsUnion(true);
1544 ((TypeProviderImpl) typeProvider).addUnitsToGenTO(genTOBuilder, typeDef.getUnits());
1547 final GeneratedTOBuilder unionBuilder = new GeneratedTOBuilderImpl(typeBuilder.getPackageName(),
1548 genTOBuilder.getName() + "Builder");
1549 unionBuilder.setIsUnionBuilder(true);
1550 final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
1551 method.setReturnType(returnType);
1552 method.addParameter(Types.STRING, "defaultValue");
1553 method.setAccessModifier(AccessModifier.PUBLIC);
1554 method.setStatic(true);
1556 final Set<Type> types = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(parentModule);
1557 if (types == null) {
1558 ((TypeProviderImpl) typeProvider).getAdditionalTypes().put(parentModule,
1559 Sets.<Type> newHashSet(unionBuilder.toInstance()));
1561 types.add(unionBuilder.toInstance());
1563 return returnType.toInstance();
1566 private GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode schemaNode) {
1567 return addDefaultInterfaceDefinition(packageName, schemaNode, null);
1571 * Instantiates generated type builder with <code>packageName</code> and
1572 * <code>schemaNode</code>.
1574 * The new builder always implements
1575 * {@link org.opendaylight.yangtools.yang.binding.DataObject DataObject}.<br />
1576 * If <code>schemaNode</code> is instance of GroupingDefinition it also
1577 * implements {@link org.opendaylight.yangtools.yang.binding.Augmentable
1578 * Augmentable}.<br />
1579 * If <code>schemaNode</code> is instance of
1580 * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer
1581 * DataNodeContainer} it can also implement nodes which are specified in
1584 * @param packageName
1585 * string with the name of the package to which
1586 * <code>schemaNode</code> belongs.
1588 * schema node for which is created generated type builder
1590 * parent type (can be null)
1591 * @return generated type builder <code>schemaNode</code>
1593 private GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode schemaNode,
1594 final Type parent) {
1595 final GeneratedTypeBuilder it = addRawInterfaceDefinition(packageName, schemaNode, "");
1596 if (parent == null) {
1597 it.addImplementsType(DATA_OBJECT);
1599 it.addImplementsType(BindingTypes.childOf(parent));
1601 if (!(schemaNode instanceof GroupingDefinition)) {
1602 it.addImplementsType(augmentable(it));
1605 if (schemaNode instanceof DataNodeContainer) {
1606 addImplementedInterfaceFromUses((DataNodeContainer) schemaNode, it);
1613 * Wraps the calling of the same overloaded method.
1615 * @param packageName
1616 * string with the package name to which returning generated type
1619 * schema node which provide data about the schema node name
1620 * @return generated type builder for <code>schemaNode</code>
1622 private GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode) {
1623 return addRawInterfaceDefinition(packageName, schemaNode, "");
1627 * Returns reference to generated type builder for specified
1628 * <code>schemaNode</code> with <code>packageName</code>.
1630 * Firstly the generated type builder is searched in
1631 * {@link BindingGeneratorImpl#genTypeBuilders genTypeBuilders}. If it isn't
1632 * found it is created and added to <code>genTypeBuilders</code>.
1634 * @param packageName
1635 * string with the package name to which returning generated type
1638 * schema node which provide data about the schema node name
1640 * return type name prefix
1641 * @return generated type builder for <code>schemaNode</code>
1642 * @throws IllegalArgumentException
1644 * <li>if <code>schemaNode</code> is null</li>
1645 * <li>if <code>packageName</code> is null</li>
1646 * <li>if QName of schema node is null</li>
1647 * <li>if schemaNode name is null</li>
1651 private GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode,
1652 final String prefix) {
1653 checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
1654 checkArgument(packageName != null, "Package Name for Generated Type cannot be NULL.");
1655 checkArgument(schemaNode.getQName() != null, "QName for Data Schema Node cannot be NULL.");
1656 final String schemaNodeName = schemaNode.getQName().getLocalName();
1657 checkArgument(schemaNodeName != null, "Local Name of QName for Data Schema Node cannot be NULL.");
1660 if (prefix == null) {
1661 genTypeName = BindingMapping.getClassName(schemaNodeName);
1663 genTypeName = prefix + BindingMapping.getClassName(schemaNodeName);
1666 // FIXME: Validation of name conflict
1667 final GeneratedTypeBuilderImpl newType = new GeneratedTypeBuilderImpl(packageName, genTypeName);
1668 qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, schemaNode.getQName());
1669 newType.addComment(schemaNode.getDescription());
1670 newType.setDescription(schemaNode.getDescription());
1671 newType.setReference(schemaNode.getReference());
1672 newType.setSchemaPath(schemaNode.getPath().getPathFromRoot());
1674 final Module module = findParentModule(schemaContext, schemaNode);
1675 newType.setModuleName(module.getName());
1677 if (!genTypeBuilders.containsKey(packageName)) {
1678 final Map<String, GeneratedTypeBuilder> builders = new HashMap<>();
1679 builders.put(genTypeName, newType);
1680 genTypeBuilders.put(packageName, builders);
1682 final Map<String, GeneratedTypeBuilder> builders = genTypeBuilders.get(packageName);
1683 if (!builders.containsKey(genTypeName)) {
1684 builders.put(genTypeName, newType);
1691 * Creates the name of the getter method name from <code>localName</code>.
1694 * string with the name of the getter method
1697 * @return string with the name of the getter method for
1698 * <code>methodName</code> in JAVA method format
1700 public static String getterMethodName(final String localName, final Type returnType) {
1701 final StringBuilder method = new StringBuilder();
1702 if (BOOLEAN.equals(returnType)) {
1703 method.append("is");
1705 method.append("get");
1707 String name = BindingMapping.toFirstUpper(BindingMapping.getPropertyName(localName));
1708 method.append(name);
1709 return method.toString();
1713 * Created a method signature builder as part of
1714 * <code>interfaceBuilder</code>.
1716 * The method signature builder is created for the getter method of
1717 * <code>schemaNodeName</code>. Also <code>comment</code> and
1718 * <code>returnType</code> information are added to the builder.
1720 * @param interfaceBuilder
1721 * generated type builder for which the getter method should be
1723 * @param schemaNodeName
1724 * string with schema node name. The name will be the part of the
1725 * getter method name.
1727 * string with comment for the getter method
1729 * type which represents the return type of the getter method
1730 * @return method signature builder which represents the getter method of
1731 * <code>interfaceBuilder</code>
1733 private MethodSignatureBuilder constructGetter(final GeneratedTypeBuilder interfaceBuilder,
1734 final String schemaNodeName, final String comment, final Type returnType) {
1735 final MethodSignatureBuilder getMethod = interfaceBuilder
1736 .addMethod(getterMethodName(schemaNodeName, returnType));
1737 getMethod.setComment(comment);
1738 getMethod.setReturnType(returnType);
1743 * Adds <code>schemaNode</code> to <code>typeBuilder</code> as getter method
1744 * or to <code>genTOBuilder</code> as property.
1746 * @param basePackageName
1747 * string contains the module package name
1749 * data schema node which should be added as getter method to
1750 * <code>typeBuilder</code> or as a property to
1751 * <code>genTOBuilder</code> if is part of the list key
1752 * @param typeBuilder
1753 * generated type builder for the list schema node
1754 * @param genTOBuilder
1755 * generated TO builder for the list keys
1757 * list of string which contains names of the list keys
1760 * @throws IllegalArgumentException
1762 * <li>if <code>schemaNode</code> equals null</li>
1763 * <li>if <code>typeBuilder</code> equals null</li>
1766 private void addSchemaNodeToListBuilders(final String basePackageName, final DataSchemaNode schemaNode,
1767 final GeneratedTypeBuilder typeBuilder, final GeneratedTOBuilder genTOBuilder, final List<String> listKeys,
1768 final Module module) {
1769 checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
1770 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
1772 if (schemaNode instanceof LeafSchemaNode) {
1773 final LeafSchemaNode leaf = (LeafSchemaNode) schemaNode;
1774 final String leafName = leaf.getQName().getLocalName();
1775 final Type type = resolveLeafSchemaNodeAsMethod(typeBuilder, leaf);
1776 if (listKeys.contains(leafName)) {
1778 resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, true, module);
1780 resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, type, true);
1783 } else if (!schemaNode.isAddedByUses()) {
1784 if (schemaNode instanceof LeafListSchemaNode) {
1785 resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) schemaNode);
1786 } else if (schemaNode instanceof ContainerSchemaNode) {
1787 containerToGenType(module, basePackageName, typeBuilder, typeBuilder, (ContainerSchemaNode) schemaNode);
1788 } else if (schemaNode instanceof ChoiceNode) {
1789 choiceToGeneratedType(module, basePackageName, typeBuilder, (ChoiceNode) schemaNode);
1790 } else if (schemaNode instanceof ListSchemaNode) {
1791 listToGenType(module, basePackageName, typeBuilder, typeBuilder, (ListSchemaNode) schemaNode);
1796 private void typeBuildersToGenTypes(final Module module, final GeneratedTypeBuilder typeBuilder,
1797 final GeneratedTOBuilder genTOBuilder) {
1798 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
1800 if (genTOBuilder != null) {
1801 GeneratedTransferObject genTO = genTOBuilder.toInstance();
1802 constructGetter(typeBuilder, "key", "Returns Primary Key of Yang List Type", genTO);
1803 genCtx.get(module).addGeneratedTOBuilder(genTOBuilder);
1808 * Selects the names of the list keys from <code>list</code> and returns
1809 * them as the list of the strings
1812 * of string with names of the list keys
1813 * @return list of string which represents names of the list keys. If the
1814 * <code>list</code> contains no keys then the empty list is
1817 private List<String> listKeys(final ListSchemaNode list) {
1818 final List<String> listKeys = new ArrayList<>();
1820 List<QName> keyDefinition = list.getKeyDefinition();
1821 if (keyDefinition != null) {
1822 for (QName keyDef : keyDefinition) {
1823 listKeys.add(keyDef.getLocalName());
1830 * Generates for the <code>list</code> which contains any list keys special
1831 * generated TO builder.
1833 * @param packageName
1834 * string with package name to which the list belongs
1836 * list schema node which is source of data about the list name
1837 * @return generated TO builder which represents the keys of the
1838 * <code>list</code> or null if <code>list</code> is null or list of
1839 * key definitions is null or empty.
1841 private GeneratedTOBuilder resolveListKeyTOBuilder(final String packageName, final ListSchemaNode list) {
1842 GeneratedTOBuilder genTOBuilder = null;
1843 if ((list.getKeyDefinition() != null) && (!list.getKeyDefinition().isEmpty())) {
1844 final String listName = list.getQName().getLocalName() + "Key";
1845 final String genTOName = BindingMapping.getClassName(listName);
1846 genTOBuilder = new GeneratedTOBuilderImpl(packageName, genTOName);
1848 return genTOBuilder;
1852 * Builds generated TO builders for <code>typeDef</code> of type
1853 * {@link org.opendaylight.yangtools.yang.model.util.UnionType UnionType} or
1854 * {@link org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
1855 * BitsTypeDefinition} which are also added to <code>typeBuilder</code> as
1856 * enclosing transfer object.
1858 * If more then one generated TO builder is created for enclosing then all
1859 * of the generated TO builders are added to <code>typeBuilder</code> as
1860 * enclosing transfer objects.
1863 * type definition which can be of type <code>UnionType</code> or
1864 * <code>BitsTypeDefinition</code>
1865 * @param typeBuilder
1866 * generated type builder to which is added generated TO created
1867 * from <code>typeDef</code>
1869 * string with name for generated TO builder
1871 * @return generated TO builder for <code>typeDef</code>
1873 private GeneratedTOBuilder addTOToTypeBuilder(final TypeDefinition<?> typeDef,
1874 final GeneratedTypeBuilder typeBuilder, final DataSchemaNode leaf, final Module parentModule) {
1875 final String classNameFromLeaf = BindingMapping.getClassName(leaf.getQName());
1876 final List<GeneratedTOBuilder> genTOBuilders = new ArrayList<>();
1877 final String packageName = typeBuilder.getFullyQualifiedName();
1878 if (typeDef instanceof UnionTypeDefinition) {
1879 final List<GeneratedTOBuilder> types = ((TypeProviderImpl) typeProvider)
1880 .provideGeneratedTOBuildersForUnionTypeDef(packageName, ((UnionTypeDefinition) typeDef),
1881 classNameFromLeaf, leaf);
1882 genTOBuilders.addAll(types);
1884 GeneratedTOBuilder resultTOBuilder = null;
1885 if (!types.isEmpty()) {
1886 resultTOBuilder = types.remove(0);
1887 for (GeneratedTOBuilder genTOBuilder : types) {
1888 resultTOBuilder.addEnclosingTransferObject(genTOBuilder);
1892 final GeneratedPropertyBuilder genPropBuilder = resultTOBuilder.addProperty("value");
1893 genPropBuilder.setReturnType(Types.primitiveType("char[]", null));
1894 resultTOBuilder.addEqualsIdentity(genPropBuilder);
1895 resultTOBuilder.addHashIdentity(genPropBuilder);
1896 resultTOBuilder.addToStringProperty(genPropBuilder);
1898 } else if (typeDef instanceof BitsTypeDefinition) {
1899 genTOBuilders.add((((TypeProviderImpl) typeProvider)).provideGeneratedTOBuilderForBitsTypeDefinition(
1900 packageName, typeDef, classNameFromLeaf, parentModule.getName()));
1902 if (genTOBuilders != null && !genTOBuilders.isEmpty()) {
1903 for (GeneratedTOBuilder genTOBuilder : genTOBuilders) {
1904 typeBuilder.addEnclosingTransferObject(genTOBuilder);
1906 return genTOBuilders.get(0);
1913 * Adds the implemented types to type builder.
1915 * The method passes through the list of <i>uses</i> in
1916 * {@code dataNodeContainer}. For every <i>use</i> is obtained corresponding
1917 * generated type from {@link BindingGeneratorImpl#allGroupings
1918 * allGroupings} which is added as <i>implements type</i> to
1919 * <code>builder</code>
1921 * @param dataNodeContainer
1922 * element which contains the list of used YANG groupings
1924 * builder to which are added implemented types according to
1925 * <code>dataNodeContainer</code>
1926 * @return generated type builder with all implemented types
1928 private GeneratedTypeBuilder addImplementedInterfaceFromUses(final DataNodeContainer dataNodeContainer,
1929 final GeneratedTypeBuilder builder) {
1930 for (UsesNode usesNode : dataNodeContainer.getUses()) {
1931 if (usesNode.getGroupingPath() != null) {
1932 final GeneratedType genType = findGroupingByPath(usesNode.getGroupingPath()).toInstance();
1933 if (genType == null) {
1934 throw new IllegalStateException("Grouping " + usesNode.getGroupingPath() + "is not resolved for "
1935 + builder.getName());
1937 builder.addImplementsType(genType);
1938 builder.addComment(genType.getComment());
1944 private GeneratedTypeBuilder findChildNodeByPath(final SchemaPath path) {
1945 for (ModuleContext ctx : genCtx.values()) {
1946 GeneratedTypeBuilder result = ctx.getChildNode(path);
1947 if (result != null) {
1954 private GeneratedTypeBuilder findGroupingByPath(final SchemaPath path) {
1955 for (ModuleContext ctx : genCtx.values()) {
1956 GeneratedTypeBuilder result = ctx.getGrouping(path);
1957 if (result != null) {
1964 private GeneratedTypeBuilder findCaseByPath(final SchemaPath path) {
1965 for (ModuleContext ctx : genCtx.values()) {
1966 GeneratedTypeBuilder result = ctx.getCase(path);
1967 if (result != null) {
1974 public Map<Module, ModuleContext> getModuleContexts() {