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.moduleNamespaceToPackageName;
15 import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.packageNameForGeneratedType;
16 import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.parseToValidParamName;
17 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.DATA_OBJECT;
18 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.DATA_ROOT;
19 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.IDENTIFIABLE;
20 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.IDENTIFIER;
21 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.NOTIFICATION;
22 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.augmentable;
23 import static org.opendaylight.yangtools.binding.generator.util.Types.BOOLEAN;
24 import static org.opendaylight.yangtools.binding.generator.util.Types.FUTURE;
25 import static org.opendaylight.yangtools.binding.generator.util.Types.VOID;
26 import static org.opendaylight.yangtools.binding.generator.util.Types.typeForClass;
27 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode;
28 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findNodeInSchemaContext;
29 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findOriginal;
30 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
32 import com.google.common.base.Splitter;
33 import com.google.common.collect.Iterables;
34 import com.google.common.collect.Sets;
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.Iterator;
40 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.GroupingDefinition;
82 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
83 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
84 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
85 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
86 import org.opendaylight.yangtools.yang.model.api.Module;
87 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
88 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
89 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
90 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
91 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
92 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
93 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
94 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
95 import org.opendaylight.yangtools.yang.model.api.UsesNode;
96 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
97 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
98 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
99 import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
100 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
101 import org.opendaylight.yangtools.yang.model.util.UnionType;
102 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
103 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
104 import org.slf4j.Logger;
105 import org.slf4j.LoggerFactory;
107 public class BindingGeneratorImpl implements BindingGenerator {
108 private static final Logger LOG = LoggerFactory.getLogger(BindingGeneratorImpl.class);
110 private final Map<Module, ModuleContext> genCtx = new HashMap<>();
113 * Outer key represents the package name. Outer value represents map of all
114 * builders in the same package. Inner key represents the schema node name
115 * (in JAVA class/interface name format). Inner value represents instance of
116 * builder for schema node specified in key part.
118 private Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders;
121 * Provide methods for converting YANG types to JAVA types.
123 private TypeProvider typeProvider;
126 * Holds reference to schema context to resolve data of augmented element
127 * when creating augmentation builder
129 private SchemaContext schemaContext;
132 * Constant with the concrete name of namespace.
134 private final static String YANG_EXT_NAMESPACE = "urn:opendaylight:yang:extension:yang-ext";
137 * Constant with the concrete name of identifier.
139 private final static String AUGMENT_IDENTIFIER_NAME = "augment-identifier";
142 * Resolves generated types from <code>context</code> schema nodes of all
145 * Generated types are created for modules, groupings, types, containers,
146 * lists, choices, augments, rpcs, notification, identities.
149 * schema context which contains data about all schema nodes
151 * @return list of types (usually <code>GeneratedType</code>
152 * <code>GeneratedTransferObject</code>which are generated from
153 * <code>context</code> data.
154 * @throws IllegalArgumentException
155 * if arg <code>context</code> is null
156 * @throws IllegalStateException
157 * if <code>context</code> contain no modules
160 public List<Type> generateTypes(SchemaContext context) {
161 checkArgument(context != null, "Schema Context reference cannot be NULL.");
162 checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
163 schemaContext = context;
164 typeProvider = new TypeProviderImpl(context);
165 final Set<Module> modules = context.getModules();
166 return generateTypes(context, modules);
170 * Resolves generated types from <code>context</code> schema nodes only for
171 * modules specified in <code>modules</code>
173 * Generated types are created for modules, groupings, types, containers,
174 * lists, choices, augments, rpcs, notification, identities.
177 * schema context which contains data about all schema nodes
180 * set of modules for which schema nodes should be generated
182 * @return list of types (usually <code>GeneratedType</code> or
183 * <code>GeneratedTransferObject</code>) which:
185 * <li>are generated from <code>context</code> schema nodes and</li>
186 * <li>are also part of some of the module in <code>modules</code>
189 * @throws IllegalArgumentException
191 * <li>if arg <code>context</code> is null or</li>
192 * <li>if arg <code>modules</code> is null</li>
194 * @throws IllegalStateException
195 * if <code>context</code> contain no modules
198 public List<Type> generateTypes(SchemaContext context, Set<Module> modules) {
199 checkArgument(context != null, "Schema Context reference cannot be NULL.");
200 checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
201 checkArgument(modules != null, "Set of Modules cannot be NULL.");
203 schemaContext = context;
204 typeProvider = new TypeProviderImpl(context);
205 Module[] modulesArray = new Module[context.getModules().size()];
206 context.getModules().toArray(modulesArray);
207 final List<Module> contextModules = ModuleDependencySort.sort(modulesArray);
208 genTypeBuilders = new HashMap<>();
210 for (Module contextModule : contextModules) {
211 moduleToGenTypes(contextModule, context);
213 for (Module contextModule : contextModules) {
214 allAugmentsToGenTypes(contextModule);
217 final List<Type> filteredGenTypes = new ArrayList<>();
218 for (Module m : modules) {
219 final ModuleContext ctx = checkNotNull(genCtx.get(m), "Module context not found for module %s", m);
220 filteredGenTypes.addAll(ctx.getGeneratedTypes());
221 final Set<Type> additionalTypes = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(m);
222 if (additionalTypes != null) {
223 filteredGenTypes.addAll(additionalTypes);
227 return filteredGenTypes;
230 private void moduleToGenTypes(Module m, SchemaContext context) {
231 genCtx.put(m, new ModuleContext());
232 allTypeDefinitionsToGenTypes(m);
233 groupingsToGenTypes(m, m.getGroupings());
234 rpcMethodsToGenType(m);
235 allIdentitiesToGenTypes(m, context);
236 notificationsToGenType(m);
238 if (!m.getChildNodes().isEmpty()) {
239 final GeneratedTypeBuilder moduleType = moduleToDataType(m);
240 genCtx.get(m).addModuleNode(moduleType);
241 final String basePackageName = moduleNamespaceToPackageName(m);
242 resolveDataSchemaNodes(m, basePackageName, moduleType, moduleType, m.getChildNodes());
247 * Converts all extended type definitions of module to the list of
248 * <code>Type</code> objects.
251 * module from which is obtained set of type definitions
252 * @throws IllegalArgumentException
254 * <li>if module is null</li>
255 * <li>if name of module is null</li>
257 * @throws IllegalStateException
258 * if set of type definitions from module is null
260 private void allTypeDefinitionsToGenTypes(Module module) {
261 checkArgument(module != null, "Module reference cannot be NULL.");
262 checkArgument(module.getName() != null, "Module name cannot be NULL.");
263 final DataNodeIterator it = new DataNodeIterator(module);
264 final List<TypeDefinition<?>> typeDefinitions = it.allTypedefs();
265 checkState(typeDefinitions != null, "Type Definitions for module «module.name» cannot be NULL.");
267 for (TypeDefinition<?> typedef : typeDefinitions) {
268 if (typedef != null) {
269 final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef,
272 genCtx.get(module).addTypedefType(typedef.getPath(), type);
278 private GeneratedTypeBuilder processDataSchemaNode(Module module, String basePackageName,
279 GeneratedTypeBuilder childOf, DataSchemaNode node) {
280 if (node.isAugmenting() || node.isAddedByUses()) {
283 final String packageName = packageNameForGeneratedType(basePackageName, node.getPath());
284 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, node, childOf);
285 genType.addComment(node.getDescription());
286 if (node instanceof DataNodeContainer) {
287 genCtx.get(module).addChildNodeType(node.getPath(), genType);
288 groupingsToGenTypes(module, ((DataNodeContainer) node).getGroupings());
289 processUsesAugments((DataNodeContainer) node, module);
294 private void containerToGenType(Module module, String basePackageName, GeneratedTypeBuilder parent,
295 GeneratedTypeBuilder childOf, ContainerSchemaNode node) {
296 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node);
297 if (genType != null) {
298 constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), genType);
299 resolveDataSchemaNodes(module, basePackageName, genType, genType, node.getChildNodes());
303 private void listToGenType(Module module, String basePackageName, GeneratedTypeBuilder parent,
304 GeneratedTypeBuilder childOf, ListSchemaNode node) {
305 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node);
306 if (genType != null) {
307 constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), Types.listTypeFor(genType));
309 final List<String> listKeys = listKeys(node);
310 final String packageName = packageNameForGeneratedType(basePackageName, node.getPath());
311 final GeneratedTOBuilder genTOBuilder = resolveListKeyTOBuilder(packageName, node);
312 if (genTOBuilder != null) {
313 final Type identifierMarker = Types.parameterizedTypeFor(IDENTIFIER, genType);
314 final Type identifiableMarker = Types.parameterizedTypeFor(IDENTIFIABLE, genTOBuilder);
315 genTOBuilder.addImplementsType(identifierMarker);
316 genType.addImplementsType(identifiableMarker);
319 for (DataSchemaNode schemaNode : node.getChildNodes()) {
320 if (!schemaNode.isAugmenting()) {
321 addSchemaNodeToListBuilders(basePackageName, schemaNode, genType, genTOBuilder, listKeys, module);
326 if (genTOBuilder != null) {
327 final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
328 prop.setValue(Long.toString(computeDefaultSUID(genTOBuilder)));
329 genTOBuilder.setSUID(prop);
332 typeBuildersToGenTypes(module, genType, genTOBuilder);
336 private void processUsesAugments(DataNodeContainer node, Module module) {
337 final String basePackageName = moduleNamespaceToPackageName(module);
338 for (UsesNode usesNode : node.getUses()) {
339 for (AugmentationSchema augment : usesNode.getAugmentations()) {
340 usesAugmentationToGenTypes(basePackageName, augment, module, usesNode, node);
341 processUsesAugments(augment, module);
347 * Converts all <b>augmentation</b> of the module to the list
348 * <code>Type</code> objects.
351 * module from which is obtained list of all augmentation objects
352 * to iterate over them
353 * @throws IllegalArgumentException
355 * <li>if the module is null</li>
356 * <li>if the name of module is null</li>
358 * @throws IllegalStateException
359 * if set of augmentations from module is null
361 private void allAugmentsToGenTypes(Module module) {
362 checkArgument(module != null, "Module reference cannot be NULL.");
363 checkArgument(module.getName() != null, "Module name cannot be NULL.");
364 checkState(module.getAugmentations() != null, "Augmentations Set cannot be NULL.");
366 final String basePackageName = moduleNamespaceToPackageName(module);
367 final List<AugmentationSchema> augmentations = resolveAugmentations(module);
368 for (AugmentationSchema augment : augmentations) {
369 augmentationToGenTypes(basePackageName, augment, module);
374 * Returns list of <code>AugmentationSchema</code> objects. The objects are
375 * sorted according to the length of their target path from the shortest to
379 * module from which is obtained list of all augmentation objects
380 * @return list of sorted <code>AugmentationSchema</code> objects obtained
381 * from <code>module</code>
382 * @throws IllegalArgumentException
384 * @throws IllegalStateException
385 * if set of module augmentations is null
387 private List<AugmentationSchema> resolveAugmentations(Module module) {
388 checkArgument(module != null, "Module reference cannot be NULL.");
389 checkState(module.getAugmentations() != null, "Augmentations Set cannot be NULL.");
391 final Set<AugmentationSchema> augmentations = module.getAugmentations();
392 List<AugmentationSchema> sortedAugmentations = new ArrayList<>(augmentations);
393 Collections.sort(sortedAugmentations, Comparators.AUGMENT_COMP);
395 return sortedAugmentations;
399 * Create GeneratedTypeBuilder object from module argument.
402 * Module object from which builder will be created
403 * @return <code>GeneratedTypeBuilder</code> which is internal
404 * representation of the module
405 * @throws IllegalArgumentException
408 private GeneratedTypeBuilder moduleToDataType(Module module) {
409 checkArgument(module != null, "Module reference cannot be NULL.");
411 final GeneratedTypeBuilder moduleDataTypeBuilder = moduleTypeBuilder(module, "Data");
412 addImplementedInterfaceFromUses(module, moduleDataTypeBuilder);
413 moduleDataTypeBuilder.addImplementsType(DATA_ROOT);
414 moduleDataTypeBuilder.addComment(module.getDescription());
415 return moduleDataTypeBuilder;
419 * Converts all <b>rpcs</b> inputs and outputs substatements of the module
420 * to the list of <code>Type</code> objects. In addition are to containers
421 * and lists which belong to input or output also part of returning list.
424 * module from which is obtained set of all rpc objects to
426 * @throws IllegalArgumentException
428 * <li>if the module is null</li>
429 * <li>if the name of module is null</li>
431 * @throws IllegalStateException
432 * if set of rpcs from module is null
434 private void rpcMethodsToGenType(Module module) {
435 checkArgument(module != null, "Module reference cannot be NULL.");
436 checkArgument(module.getName() != null, "Module name cannot be NULL.");
437 final Set<RpcDefinition> rpcDefinitions = module.getRpcs();
438 checkState(rpcDefinitions != null, "Set of rpcs from module " + module.getName() + " cannot be NULL.");
439 if (rpcDefinitions.isEmpty()) {
443 final String basePackageName = moduleNamespaceToPackageName(module);
444 final GeneratedTypeBuilder interfaceBuilder = moduleTypeBuilder(module, "Service");
445 interfaceBuilder.addImplementsType(Types.typeForClass(RpcService.class));
446 for (RpcDefinition rpc : rpcDefinitions) {
448 final String rpcName = BindingMapping.getClassName(rpc.getQName());
449 final String rpcMethodName = parseToValidParamName(rpcName);
450 final String rpcComment = rpc.getDescription();
451 final MethodSignatureBuilder method = interfaceBuilder.addMethod(rpcMethodName);
452 final ContainerSchemaNode input = rpc.getInput();
453 final ContainerSchemaNode output = rpc.getOutput();
456 final GeneratedTypeBuilder inType = addRawInterfaceDefinition(basePackageName, input, rpcName);
457 addImplementedInterfaceFromUses(input, inType);
458 inType.addImplementsType(DATA_OBJECT);
459 inType.addImplementsType(augmentable(inType));
460 resolveDataSchemaNodes(module, basePackageName, inType, inType, input.getChildNodes());
461 genCtx.get(module).addChildNodeType(input.getPath(), inType);
462 final GeneratedType inTypeInstance = inType.toInstance();
463 method.addParameter(inTypeInstance, "input");
466 Type outTypeInstance = VOID;
467 if (output != null) {
468 final GeneratedTypeBuilder outType = addRawInterfaceDefinition(basePackageName, output, rpcName);
469 addImplementedInterfaceFromUses(output, outType);
470 outType.addImplementsType(DATA_OBJECT);
471 outType.addImplementsType(augmentable(outType));
472 resolveDataSchemaNodes(module, basePackageName, outType, outType, output.getChildNodes());
473 genCtx.get(module).addChildNodeType(output.getPath(), outType);
474 outTypeInstance = outType.toInstance();
477 final Type rpcRes = Types.parameterizedTypeFor(Types.typeForClass(RpcResult.class), outTypeInstance);
478 method.setComment(rpcComment);
479 method.setReturnType(Types.parameterizedTypeFor(FUTURE, rpcRes));
483 genCtx.get(module).addTopLevelNodeType(interfaceBuilder);
487 * Converts all <b>notifications</b> of the module to the list of
488 * <code>Type</code> objects. In addition are to this list added containers
489 * and lists which are part of this notification.
492 * module from which is obtained set of all notification objects
493 * to iterate over them
494 * @throws IllegalArgumentException
496 * <li>if the module equals null</li>
497 * <li>if the name of module equals null</li>
499 * @throws IllegalStateException
500 * if set of notifications from module is null
502 private void notificationsToGenType(Module module) {
503 checkArgument(module != null, "Module reference cannot be NULL.");
504 checkArgument(module.getName() != null, "Module name cannot be NULL.");
505 final Set<NotificationDefinition> notifications = module.getNotifications();
506 checkState(notifications != null, "Set of notification from module " + module.getName() + " cannot be NULL.");
507 if (notifications.isEmpty()) {
511 final GeneratedTypeBuilder listenerInterface = moduleTypeBuilder(module, "Listener");
512 listenerInterface.addImplementsType(BindingTypes.NOTIFICATION_LISTENER);
513 final String basePackageName = moduleNamespaceToPackageName(module);
515 for (NotificationDefinition notification : notifications) {
516 if (notification != null) {
517 processUsesAugments(notification, module);
519 final GeneratedTypeBuilder notificationInterface = addDefaultInterfaceDefinition(basePackageName,
520 notification, BindingTypes.DATA_OBJECT);
521 notificationInterface.addImplementsType(NOTIFICATION);
522 genCtx.get(module).addChildNodeType(notification.getPath(), notificationInterface);
524 // Notification object
525 resolveDataSchemaNodes(module, basePackageName, notificationInterface, notificationInterface,
526 notification.getChildNodes());
528 listenerInterface.addMethod("on" + notificationInterface.getName())
529 .setAccessModifier(AccessModifier.PUBLIC).addParameter(notificationInterface, "notification")
530 .setComment(notification.getDescription()).setReturnType(Types.VOID);
534 genCtx.get(module).addTopLevelNodeType(listenerInterface);
538 * Converts all <b>identities</b> of the module to the list of
539 * <code>Type</code> objects.
542 * module from which is obtained set of all identity objects to
545 * schema context only used as input parameter for method
546 * {@link identityToGenType}
549 private void allIdentitiesToGenTypes(Module module, SchemaContext context) {
550 final Set<IdentitySchemaNode> schemaIdentities = module.getIdentities();
551 final String basePackageName = moduleNamespaceToPackageName(module);
553 if (schemaIdentities != null && !schemaIdentities.isEmpty()) {
554 for (IdentitySchemaNode identity : schemaIdentities) {
555 identityToGenType(module, basePackageName, identity, context);
561 * Converts the <b>identity</b> object to GeneratedType. Firstly it is
562 * created transport object builder. If identity contains base identity then
563 * reference to base identity is added to superior identity as its extend.
564 * If identity doesn't contain base identity then only reference to abstract
565 * class {@link org.opendaylight.yangtools.yang.model.api.BaseIdentity
566 * BaseIdentity} is added
570 * @param basePackageName
571 * string contains the module package name
573 * IdentitySchemaNode which contains data about identity
575 * SchemaContext which is used to get package and name
576 * information about base of identity
579 private void identityToGenType(Module module, String basePackageName, IdentitySchemaNode identity,
580 SchemaContext context) {
581 if (identity == null) {
584 final String packageName = packageNameForGeneratedType(basePackageName, identity.getPath());
585 final String genTypeName = BindingMapping.getClassName(identity.getQName());
586 final GeneratedTOBuilder newType = new GeneratedTOBuilderImpl(packageName, genTypeName);
587 final IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
588 if (baseIdentity == null) {
589 final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(BaseIdentity.class.getPackage().getName(),
590 BaseIdentity.class.getSimpleName());
591 newType.setExtendsType(gto.toInstance());
593 final Module baseIdentityParentModule = SchemaContextUtil.findParentModule(context, baseIdentity);
594 final String returnTypePkgName = moduleNamespaceToPackageName(baseIdentityParentModule);
595 final String returnTypeName = BindingMapping.getClassName(baseIdentity.getQName());
596 final GeneratedTransferObject gto = new GeneratedTOBuilderImpl(returnTypePkgName, returnTypeName)
598 newType.setExtendsType(gto);
600 newType.setAbstract(true);
601 newType.addComment(identity.getDescription());
602 final QName qname = identity.getQName();
603 qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, qname);
605 genCtx.get(module).addIdentityType(identity.getQName(), newType);
608 private static Constant qnameConstant(GeneratedTypeBuilderBase<?> toBuilder, String constantName, QName name) {
609 StringBuilder sb = new StringBuilder("org.opendaylight.yangtools.yang.common.QName");
610 sb.append(".create(");
612 sb.append(name.getNamespace());
614 sb.append(name.getFormattedRevision());
616 sb.append(name.getLocalName());
619 return toBuilder.addConstant(typeForClass(QName.class), constantName, sb.toString());
623 * Converts all <b>groupings</b> of the module to the list of
624 * <code>Type</code> objects. Firstly are groupings sorted according mutual
625 * dependencies. At least dependent (independent) groupings are in the list
626 * saved at first positions. For every grouping the record is added to map
627 * {@link BindingGeneratorImpl#allGroupings allGroupings}
632 * of groupings from which types will be generated
635 private void groupingsToGenTypes(Module module, Collection<GroupingDefinition> groupings) {
636 final String basePackageName = moduleNamespaceToPackageName(module);
637 final List<GroupingDefinition> groupingsSortedByDependencies = new GroupingDefinitionDependencySort()
639 for (GroupingDefinition grouping : groupingsSortedByDependencies) {
640 groupingToGenType(basePackageName, grouping, module);
645 * Converts individual grouping to GeneratedType. Firstly generated type
646 * builder is created and every child node of grouping is resolved to the
649 * @param basePackageName
650 * string contains the module package name
652 * GroupingDefinition which contains data about grouping
655 * @return GeneratedType which is generated from grouping (object of type
656 * <code>GroupingDefinition</code>)
658 private void groupingToGenType(String basePackageName, GroupingDefinition grouping, Module module) {
659 final String packageName = packageNameForGeneratedType(basePackageName, grouping.getPath());
660 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, grouping);
661 genCtx.get(module).addGroupingType(grouping.getPath(), genType);
662 resolveDataSchemaNodes(module, basePackageName, genType, genType, grouping.getChildNodes());
663 groupingsToGenTypes(module, grouping.getGroupings());
664 processUsesAugments(grouping, module);
668 * Adds enumeration builder created from <code>enumTypeDef</code> to
669 * <code>typeBuilder</code>.
671 * Each <code>enumTypeDef</code> item is added to builder with its name and
675 * EnumTypeDefinition contains enum data
677 * string contains name which will be assigned to enumeration
680 * GeneratedTypeBuilder to which will be enum builder assigned
681 * @return enumeration builder which contains data from
682 * <code>enumTypeDef</code>
684 private EnumBuilder resolveInnerEnumFromTypeDefinition(EnumTypeDefinition enumTypeDef, QName enumName,
685 GeneratedTypeBuilder typeBuilder) {
686 if ((enumTypeDef != null) && (typeBuilder != null) && (enumTypeDef.getQName() != null)
687 && (enumTypeDef.getQName().getLocalName() != null)) {
688 final String enumerationName = BindingMapping.getClassName(enumName);
689 final EnumBuilder enumBuilder = typeBuilder.addEnumeration(enumerationName);
690 enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
697 * Generates type builder for <code>module</code>.
700 * Module which is source of package name for generated type
703 * string which is added to the module class name representation
705 * @return instance of GeneratedTypeBuilder which represents
706 * <code>module</code>.
707 * @throws IllegalArgumentException
708 * if <code>module</code> is null
710 private GeneratedTypeBuilder moduleTypeBuilder(Module module, String postfix) {
711 checkArgument(module != null, "Module reference cannot be NULL.");
712 final String packageName = moduleNamespaceToPackageName(module);
713 final String moduleName = BindingMapping.getClassName(module.getName()) + postfix;
714 return new GeneratedTypeBuilderImpl(packageName, moduleName);
718 * Converts <code>augSchema</code> to list of <code>Type</code> which
719 * contains generated type for augmentation. In addition there are also
720 * generated types for all containers, list and choices which are child of
721 * <code>augSchema</code> node or a generated types for cases are added if
722 * augmented node is choice.
724 * @param augmentPackageName
725 * string with the name of the package to which the augmentation
728 * AugmentationSchema which is contains data about augmentation
729 * (target path, childs...)
732 * @param parentUsesNode
733 * @throws IllegalArgumentException
735 * <li>if <code>augmentPackageName</code> equals null</li>
736 * <li>if <code>augSchema</code> equals null</li>
738 * @throws IllegalStateException
739 * if augment target path is null
741 private void augmentationToGenTypes(String augmentPackageName, AugmentationSchema augSchema, Module module) {
742 checkArgument(augmentPackageName != null, "Package Name cannot be NULL.");
743 checkArgument(augSchema != null, "Augmentation Schema cannot be NULL.");
744 checkState(augSchema.getTargetPath() != null,
745 "Augmentation Schema does not contain Target Path (Target Path is NULL).");
747 processUsesAugments(augSchema, module);
748 final SchemaPath targetPath = augSchema.getTargetPath();
749 SchemaNode targetSchemaNode = null;
751 targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
752 if (targetSchemaNode instanceof DataSchemaNode && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
753 targetSchemaNode = findOriginal((DataSchemaNode) targetSchemaNode, schemaContext);
754 if (targetSchemaNode == null) {
755 throw new NullPointerException("Failed to find target node from grouping in augmentation " + augSchema
756 + " in module " + module.getName());
759 if (targetSchemaNode == null) {
760 throw new IllegalArgumentException("augment target not found: " + targetPath);
763 GeneratedTypeBuilder targetTypeBuilder = findChildNodeByPath(targetSchemaNode.getPath());
764 if (targetTypeBuilder == null) {
765 targetTypeBuilder = findCaseByPath(targetSchemaNode.getPath());
767 if (targetTypeBuilder == null) {
768 throw new NullPointerException("Target type not yet generated: " + targetSchemaNode);
771 if (!(targetSchemaNode instanceof ChoiceNode)) {
772 String packageName = augmentPackageName;
773 final Type targetType = new ReferencedTypeImpl(targetTypeBuilder.getPackageName(),
774 targetTypeBuilder.getName());
775 addRawAugmentGenTypeDefinition(module, packageName, augmentPackageName, targetType, augSchema);
778 generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance(),
779 (ChoiceNode) targetSchemaNode, augSchema.getChildNodes());
783 private void usesAugmentationToGenTypes(String augmentPackageName, AugmentationSchema augSchema, Module module,
784 UsesNode usesNode, DataNodeContainer usesNodeParent) {
785 checkArgument(augmentPackageName != null, "Package Name cannot be NULL.");
786 checkArgument(augSchema != null, "Augmentation Schema cannot be NULL.");
787 checkState(augSchema.getTargetPath() != null,
788 "Augmentation Schema does not contain Target Path (Target Path is NULL).");
790 processUsesAugments(augSchema, module);
791 final SchemaPath targetPath = augSchema.getTargetPath();
792 SchemaNode targetSchemaNode = findOriginalTargetFromGrouping(targetPath, usesNode);
793 if (targetSchemaNode == null) {
794 throw new IllegalArgumentException("augment target not found: " + targetPath);
797 GeneratedTypeBuilder targetTypeBuilder = findChildNodeByPath(targetSchemaNode.getPath());
798 if (targetTypeBuilder == null) {
799 targetTypeBuilder = findCaseByPath(targetSchemaNode.getPath());
801 if (targetTypeBuilder == null) {
802 throw new NullPointerException("Target type not yet generated: " + targetSchemaNode);
805 if (!(targetSchemaNode instanceof ChoiceNode)) {
806 String packageName = augmentPackageName;
807 if (usesNodeParent instanceof SchemaNode) {
808 packageName = packageNameForGeneratedType(augmentPackageName, ((SchemaNode) usesNodeParent).getPath(),
811 addRawAugmentGenTypeDefinition(module, packageName, augmentPackageName, targetTypeBuilder.toInstance(),
814 generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance(),
815 (ChoiceNode) targetSchemaNode, augSchema.getChildNodes());
820 * Convenient method to find node added by uses statement.
824 * @param parentUsesNode
825 * parent of uses node
826 * @return node from its original location in grouping
828 private DataSchemaNode findOriginalTargetFromGrouping(SchemaPath targetPath, UsesNode parentUsesNode) {
829 SchemaNode targetGrouping = findNodeInSchemaContext(schemaContext, parentUsesNode.getGroupingPath()
831 if (!(targetGrouping instanceof GroupingDefinition)) {
832 throw new IllegalArgumentException("Failed to generate code for augment in " + parentUsesNode);
835 GroupingDefinition grouping = (GroupingDefinition) targetGrouping;
836 SchemaNode result = grouping;
837 for (QName node : targetPath.getPathFromRoot()) {
838 // finding by local name is valid, grouping cannot contain nodes
839 // with same name and different namespace
840 if (result instanceof DataNodeContainer) {
841 result = ((DataNodeContainer) result).getDataChildByName(node.getLocalName());
842 } else if (result instanceof ChoiceNode) {
843 result = ((ChoiceNode) result).getCaseNodeByName(node.getLocalName());
846 if (result == null) {
850 boolean fromUses = ((DataSchemaNode) result).isAddedByUses();
851 Iterator<UsesNode> groupingUses = grouping.getUses().iterator();
852 while (groupingUses.hasNext() && fromUses) {
853 result = findOriginalTargetFromGrouping(targetPath, groupingUses.next());
854 if (result != null) {
855 fromUses = ((DataSchemaNode) result).isAddedByUses();
859 // this indicates invalid yang and thus possible bug in code because
860 // invalid yang should be already spotted by parser
861 throw new IllegalStateException("Failed to generate code for augment in " + parentUsesNode);
864 return (DataSchemaNode) result;
868 * Returns a generated type builder for an augmentation.
870 * The name of the type builder is equal to the name of augmented node with
871 * serial number as suffix.
875 * @param augmentPackageName
876 * string with contains the package name to which the augment
878 * @param basePackageName
879 * string with the package name to which the augmented node
881 * @param targetTypeRef
884 * augmentation schema which contains data about the child nodes
885 * and uses of augment
886 * @return generated type builder for augment
888 private GeneratedTypeBuilder addRawAugmentGenTypeDefinition(Module module, String augmentPackageName,
889 String basePackageName, Type targetTypeRef, AugmentationSchema augSchema) {
890 Map<String, GeneratedTypeBuilder> augmentBuilders = genTypeBuilders.get(augmentPackageName);
891 if (augmentBuilders == null) {
892 augmentBuilders = new HashMap<>();
893 genTypeBuilders.put(augmentPackageName, augmentBuilders);
895 final String augIdentifier = getAugmentIdentifier(augSchema.getUnknownSchemaNodes());
898 if (augIdentifier != null) {
899 augTypeName = BindingMapping.getClassName(augIdentifier);
901 augTypeName = augGenTypeName(augmentBuilders, targetTypeRef.getName());
904 final GeneratedTypeBuilder augTypeBuilder = new GeneratedTypeBuilderImpl(augmentPackageName, augTypeName);
906 augTypeBuilder.addImplementsType(DATA_OBJECT);
907 augTypeBuilder.addImplementsType(Types.augmentationTypeFor(targetTypeRef));
908 addImplementedInterfaceFromUses(augSchema, augTypeBuilder);
910 augSchemaNodeToMethods(module, basePackageName, augTypeBuilder, augTypeBuilder, augSchema.getChildNodes());
911 augmentBuilders.put(augTypeName, augTypeBuilder);
913 genCtx.get(module).addTargetToAugmentation(targetTypeRef, augTypeBuilder);
914 genCtx.get(module).addAugmentType(augTypeBuilder);
915 genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
916 return augTypeBuilder;
921 * @param unknownSchemaNodes
922 * @return nodeParameter of UnknownSchemaNode
924 private String getAugmentIdentifier(List<UnknownSchemaNode> unknownSchemaNodes) {
925 for (UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
926 final QName nodeType = unknownSchemaNode.getNodeType();
927 if (AUGMENT_IDENTIFIER_NAME.equals(nodeType.getLocalName())
928 && YANG_EXT_NAMESPACE.equals(nodeType.getNamespace().toString())) {
929 return unknownSchemaNode.getNodeParameter();
936 * Returns first unique name for the augment generated type builder. The
937 * generated type builder name for augment consists from name of augmented
938 * node and serial number of its augmentation.
941 * map of builders which were created in the package to which the
942 * augmentation belongs
944 * string with name of augmented node
945 * @return string with unique name for augmentation builder
947 private String augGenTypeName(Map<String, GeneratedTypeBuilder> builders, String genTypeName) {
949 if (builders != null) {
950 while (builders.containsKey(genTypeName + index)) {
954 return genTypeName + index;
958 * Adds the methods to <code>typeBuilder</code> which represent subnodes of
959 * node for which <code>typeBuilder</code> was created.
961 * The subnodes aren't mapped to the methods if they are part of grouping or
962 * augment (in this case are already part of them).
966 * @param basePackageName
967 * string contains the module package name
969 * generated type builder which represents any node. The subnodes
970 * of this node are added to the <code>typeBuilder</code> as
971 * methods. The subnode can be of type leaf, leaf-list, list,
976 * set of data schema nodes which are the children of the node
977 * for which <code>typeBuilder</code> was created
978 * @return generated type builder which is the same builder as input
979 * parameter. The getter methods (representing child nodes) could be
982 private GeneratedTypeBuilder resolveDataSchemaNodes(Module module, String basePackageName,
983 GeneratedTypeBuilder parent, GeneratedTypeBuilder childOf, Set<DataSchemaNode> schemaNodes) {
984 if ((schemaNodes != null) && (parent != null)) {
985 for (DataSchemaNode schemaNode : schemaNodes) {
986 if (!schemaNode.isAugmenting() && !schemaNode.isAddedByUses()) {
987 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, parent, childOf, module);
995 * Adds the methods to <code>typeBuilder</code> what represents subnodes of
996 * node for which <code>typeBuilder</code> was created.
1000 * @param basePackageName
1001 * string contains the module package name
1002 * @param typeBuilder
1003 * generated type builder which represents any node. The subnodes
1004 * of this node are added to the <code>typeBuilder</code> as
1005 * methods. The subnode can be of type leaf, leaf-list, list,
1006 * container, choice.
1009 * @param schemaNodes
1010 * set of data schema nodes which are the children of the node
1011 * for which <code>typeBuilder</code> was created
1012 * @return generated type builder which is the same object as the input
1013 * parameter <code>typeBuilder</code>. The getter method could be
1016 private GeneratedTypeBuilder augSchemaNodeToMethods(Module module, String basePackageName,
1017 GeneratedTypeBuilder typeBuilder, GeneratedTypeBuilder childOf, Set<DataSchemaNode> schemaNodes) {
1018 if ((schemaNodes != null) && (typeBuilder != null)) {
1019 for (DataSchemaNode schemaNode : schemaNodes) {
1020 if (!schemaNode.isAugmenting()) {
1021 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, typeBuilder, childOf, module);
1029 * Adds to <code>typeBuilder</code> a method which is derived from
1030 * <code>schemaNode</code>.
1032 * @param basePackageName
1033 * string with the module package name
1035 * data schema node which is added to <code>typeBuilder</code> as
1037 * @param typeBuilder
1038 * generated type builder to which is <code>schemaNode</code>
1039 * added as a method.
1045 private void addSchemaNodeToBuilderAsMethod(String basePackageName, DataSchemaNode node,
1046 GeneratedTypeBuilder typeBuilder, GeneratedTypeBuilder childOf, Module module) {
1047 if (node != null && typeBuilder != null) {
1048 if (node instanceof LeafSchemaNode) {
1049 resolveLeafSchemaNodeAsMethod(typeBuilder, (LeafSchemaNode) node);
1050 } else if (node instanceof LeafListSchemaNode) {
1051 resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) node);
1052 } else if (node instanceof ContainerSchemaNode) {
1053 containerToGenType(module, basePackageName, typeBuilder, childOf, (ContainerSchemaNode) node);
1054 } else if (node instanceof ListSchemaNode) {
1055 listToGenType(module, basePackageName, typeBuilder, childOf, (ListSchemaNode) node);
1056 } else if (node instanceof ChoiceNode) {
1057 choiceToGeneratedType(module, basePackageName, typeBuilder, (ChoiceNode) node);
1059 // TODO: anyxml not yet supported
1060 LOG.warn("Unable to add schema node {} as method in {}: unsupported type of node.", node.getClass(),
1061 typeBuilder.getFullyQualifiedName());
1067 * Converts <code>choiceNode</code> to the list of generated types for
1068 * choice and its cases.
1070 * The package names for choice and for its cases are created as
1071 * concatenation of the module package (<code>basePackageName</code>) and
1072 * names of all parents node.
1076 * @param basePackageName
1077 * string with the module package name
1081 * choice node which is mapped to generated type. Also child
1082 * nodes - cases are mapped to generated types.
1083 * @throws IllegalArgumentException
1085 * <li>if <code>basePackageName</code> is null</li>
1086 * <li>if <code>choiceNode</code> is null</li>
1089 private void choiceToGeneratedType(Module module, String basePackageName, GeneratedTypeBuilder parent,
1090 ChoiceNode choiceNode) {
1091 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
1092 checkArgument(choiceNode != null, "Choice Schema Node cannot be NULL.");
1094 if (!choiceNode.isAddedByUses()) {
1095 final String packageName = packageNameForGeneratedType(basePackageName, choiceNode.getPath());
1096 final GeneratedTypeBuilder choiceTypeBuilder = addRawInterfaceDefinition(packageName, choiceNode);
1097 constructGetter(parent, choiceNode.getQName().getLocalName(), choiceNode.getDescription(),
1099 choiceTypeBuilder.addImplementsType(typeForClass(DataContainer.class));
1100 genCtx.get(module).addChildNodeType(choiceNode.getPath(), choiceTypeBuilder);
1101 generateTypesFromChoiceCases(module, basePackageName, choiceTypeBuilder.toInstance(), choiceNode);
1106 * Converts <code>caseNodes</code> set to list of corresponding generated
1109 * For every <i>case</i> which isn't added through augment or <i>uses</i> is
1110 * created generated type builder. The package names for the builder is
1111 * created as concatenation of the module package (
1112 * <code>basePackageName</code>) and names of all parents nodes of the
1113 * concrete <i>case</i>. There is also relation "<i>implements type</i>"
1114 * between every case builder and <i>choice</i> type
1118 * @param basePackageName
1119 * string with the module package name
1120 * @param refChoiceType
1121 * type which represents superior <i>case</i>
1123 * choice case node which is mapped to generated type
1124 * @return list of generated types for <code>caseNodes</code>.
1125 * @throws IllegalArgumentException
1127 * <li>if <code>basePackageName</code> equals null</li>
1128 * <li>if <code>refChoiceType</code> equals null</li>
1129 * <li>if <code>caseNodes</code> equals null</li>
1132 private void generateTypesFromChoiceCases(Module module, String basePackageName, Type refChoiceType,
1133 ChoiceNode choiceNode) {
1134 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
1135 checkArgument(refChoiceType != null, "Referenced Choice Type cannot be NULL.");
1136 checkArgument(choiceNode != null, "ChoiceNode cannot be NULL.");
1138 final Set<ChoiceCaseNode> caseNodes = choiceNode.getCases();
1139 if (caseNodes == null) {
1143 for (ChoiceCaseNode caseNode : caseNodes) {
1144 if (caseNode != null && !caseNode.isAddedByUses() && !caseNode.isAugmenting()) {
1145 final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath());
1146 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode);
1147 caseTypeBuilder.addImplementsType(refChoiceType);
1148 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
1149 genCtx.get(module).addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode);
1150 final Set<DataSchemaNode> caseChildNodes = caseNode.getChildNodes();
1151 if (caseChildNodes != null) {
1152 Object parentNode = null;
1153 final SchemaPath nodeSp = choiceNode.getPath();
1154 parentNode = findDataSchemaNode(schemaContext, nodeSp.getParent());
1157 if (parentNode instanceof AugmentationSchema) {
1158 final AugmentationSchema augSchema = (AugmentationSchema) parentNode;
1159 final SchemaPath targetPath = augSchema.getTargetPath();
1160 SchemaNode targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
1161 if (targetSchemaNode instanceof DataSchemaNode
1162 && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
1163 targetSchemaNode = findOriginal((DataSchemaNode) targetSchemaNode, schemaContext);
1164 if (targetSchemaNode == null) {
1165 throw new NullPointerException(
1166 "Failed to find target node from grouping for augmentation " + augSchema
1167 + " in module " + module.getName());
1170 parent = targetSchemaNode;
1172 final SchemaPath sp = choiceNode.getPath();
1173 parent = findDataSchemaNode(schemaContext, sp.getParent());
1175 GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.getPath());
1176 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, caseChildNodes);
1180 processUsesAugments(caseNode, module);
1185 * Generates list of generated types for all the cases of a choice which are
1186 * added to the choice through the augment.
1190 * @param basePackageName
1191 * string contains name of package to which augment belongs. If
1192 * an augmented choice is from an other package (pcg1) than an
1193 * augmenting choice (pcg2) then case's of the augmenting choice
1194 * will belong to pcg2.
1196 * Type which represents target choice
1198 * node which represents target choice
1199 * @param augmentedNodes
1200 * set of choice case nodes for which is checked if are/aren't
1201 * added to choice through augmentation
1202 * @return list of generated types which represents augmented cases of
1203 * choice <code>refChoiceType</code>
1204 * @throws IllegalArgumentException
1206 * <li>if <code>basePackageName</code> is null</li>
1207 * <li>if <code>targetType</code> is null</li>
1208 * <li>if <code>augmentedNodes</code> is null</li>
1211 private void generateTypesFromAugmentedChoiceCases(Module module, String basePackageName, Type targetType,
1212 ChoiceNode targetNode, Set<DataSchemaNode> augmentedNodes) {
1213 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
1214 checkArgument(targetType != null, "Referenced Choice Type cannot be NULL.");
1215 checkArgument(augmentedNodes != null, "Set of Choice Case Nodes cannot be NULL.");
1217 for (DataSchemaNode caseNode : augmentedNodes) {
1218 if (caseNode != null) {
1219 final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath());
1220 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode);
1221 caseTypeBuilder.addImplementsType(targetType);
1223 SchemaNode parent = null;
1224 final SchemaPath nodeSp = targetNode.getPath();
1225 parent = findDataSchemaNode(schemaContext, nodeSp.getParent());
1227 GeneratedTypeBuilder childOfType = null;
1228 if (parent instanceof Module) {
1229 childOfType = genCtx.get((Module) parent).getModuleNode();
1230 } else if (parent instanceof ChoiceCaseNode) {
1231 childOfType = findCaseByPath(parent.getPath());
1232 } else if (parent instanceof DataSchemaNode || parent instanceof NotificationDefinition) {
1233 childOfType = findChildNodeByPath(parent.getPath());
1234 } else if (parent instanceof GroupingDefinition) {
1235 childOfType = findGroupingByPath(parent.getPath());
1238 if (childOfType == null) {
1239 throw new IllegalArgumentException("Failed to find parent type of choice " + targetNode);
1242 ChoiceCaseNode node = null;
1243 if (caseNode instanceof ChoiceCaseNode) {
1244 node = (ChoiceCaseNode) caseNode;
1246 node = targetNode.getCaseNodeByName(caseNode.getQName().getLocalName());
1248 final Set<DataSchemaNode> childNodes = node.getChildNodes();
1249 if (childNodes != null) {
1250 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, childNodes);
1252 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
1253 genCtx.get(module).addChoiceToCaseMapping(targetType, caseTypeBuilder, node);
1259 * Converts <code>leaf</code> to the getter method which is added to
1260 * <code>typeBuilder</code>.
1262 * @param typeBuilder
1263 * generated type builder to which is added getter method as
1264 * <code>leaf</code> mapping
1266 * leaf schema node which is mapped as getter method which is
1267 * added to <code>typeBuilder</code>
1268 * @return boolean value
1270 * <li>false - if <code>leaf</code> or <code>typeBuilder</code> are
1272 * <li>true - in other cases</li>
1275 private Type resolveLeafSchemaNodeAsMethod(GeneratedTypeBuilder typeBuilder, LeafSchemaNode leaf) {
1276 Type returnType = null;
1277 if ((leaf != null) && (typeBuilder != null)) {
1278 final String leafName = leaf.getQName().getLocalName();
1279 String leafDesc = leaf.getDescription();
1280 if (leafDesc == null) {
1284 final Module parentModule = findParentModule(schemaContext, leaf);
1285 if (leafName != null && !leaf.isAddedByUses()) {
1286 final TypeDefinition<?> typeDef = leaf.getType();
1288 GeneratedTOBuilder genTOBuilder;
1289 if (typeDef instanceof EnumTypeDefinition) {
1290 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1291 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
1292 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(),
1295 if (enumBuilder != null) {
1296 returnType = enumBuilder.toInstance(typeBuilder);
1298 ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
1299 } else if (typeDef instanceof UnionType) {
1300 genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf);
1301 if (genTOBuilder != null) {
1302 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
1304 } else if (typeDef instanceof BitsTypeDefinition) {
1305 genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf);
1306 if (genTOBuilder != null) {
1307 returnType = new ReferencedTypeImpl(genTOBuilder.getPackageName(), genTOBuilder.getName());
1310 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
1311 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
1313 if (returnType != null) {
1314 final MethodSignatureBuilder getter = constructGetter(typeBuilder, leafName, leafDesc, returnType);
1315 processContextRefExtension(leaf, getter, parentModule);
1322 private void processContextRefExtension(LeafSchemaNode leaf, MethodSignatureBuilder getter, Module module) {
1323 for (UnknownSchemaNode node : leaf.getUnknownSchemaNodes()) {
1324 final QName nodeType = node.getNodeType();
1325 if ("context-reference".equals(nodeType.getLocalName())) {
1326 final String nodeParam = node.getNodeParameter();
1327 IdentitySchemaNode identity = null;
1328 String basePackageName = null;
1329 final Iterable<String> splittedElement = Splitter.on(':').split(nodeParam);
1330 final Iterator<String> iterator = splittedElement.iterator();
1331 final int length = Iterables.size(splittedElement);
1333 identity = findIdentityByName(module.getIdentities(), iterator.next());
1334 basePackageName = moduleNamespaceToPackageName(module);
1335 } else if (length == 2) {
1336 String prefix = iterator.next();
1337 final Module dependentModule = findModuleFromImports(module.getImports(), prefix);
1338 if (dependentModule == null) {
1339 throw new IllegalArgumentException("Failed to process context-reference: unknown prefix "
1342 identity = findIdentityByName(dependentModule.getIdentities(), iterator.next());
1343 basePackageName = moduleNamespaceToPackageName(dependentModule);
1345 throw new IllegalArgumentException("Failed to process context-reference: unknown identity "
1348 if (identity == null) {
1349 throw new IllegalArgumentException("Failed to process context-reference: unknown identity "
1353 final Class<RoutingContext> clazz = RoutingContext.class;
1354 final AnnotationTypeBuilder rc = getter.addAnnotation(clazz.getPackage().getName(),
1355 clazz.getSimpleName());
1356 final String packageName = packageNameForGeneratedType(basePackageName, identity.getPath());
1357 final String genTypeName = BindingMapping.getClassName(identity.getQName().getLocalName());
1358 rc.addParameter("value", packageName + "." + genTypeName + ".class");
1363 private IdentitySchemaNode findIdentityByName(Set<IdentitySchemaNode> identities, String name) {
1364 for (IdentitySchemaNode id : identities) {
1365 if (id.getQName().getLocalName().equals(name)) {
1372 private Module findModuleFromImports(Set<ModuleImport> imports, String prefix) {
1373 for (ModuleImport imp : imports) {
1374 if (imp.getPrefix().equals(prefix)) {
1375 return schemaContext.findModuleByName(imp.getModuleName(), imp.getRevision());
1381 private boolean resolveLeafSchemaNodeAsProperty(GeneratedTOBuilder toBuilder, LeafSchemaNode leaf,
1382 boolean isReadOnly, Module module) {
1383 if ((leaf != null) && (toBuilder != null)) {
1384 final String leafName = leaf.getQName().getLocalName();
1385 String leafDesc = leaf.getDescription();
1386 if (leafDesc == null) {
1390 if (leafName != null) {
1391 Type returnType = null;
1392 final TypeDefinition<?> typeDef = leaf.getType();
1393 if (typeDef instanceof UnionTypeDefinition) {
1394 // GeneratedType for this type definition should be already
1396 QName qname = typeDef.getQName();
1397 Module unionModule = null;
1398 String prefix = qname.getPrefix();
1399 if (prefix == null || prefix.isEmpty() || prefix.equals(module.getPrefix())) {
1400 unionModule = module;
1402 unionModule = findModuleFromImports(module.getImports(), qname.getPrefix());
1404 final ModuleContext mc = genCtx.get(unionModule);
1405 returnType = mc.getTypedefs().get(typeDef.getPath());
1407 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1409 return resolveLeafSchemaNodeAsProperty(toBuilder, leaf, returnType, isReadOnly);
1416 * Converts <code>leaf</code> schema node to property of generated TO
1420 * generated TO builder to which is <code>leaf</code> added as
1423 * leaf schema node which is added to <code>toBuilder</code> as
1428 * boolean value which says if leaf property is|isn't read only
1429 * @return boolean value
1431 * <li>false - if <code>leaf</code>, <code>toBuilder</code> or leaf
1432 * name equals null or if leaf is added by <i>uses</i>.</li>
1433 * <li>true - other cases</li>
1436 private boolean resolveLeafSchemaNodeAsProperty(GeneratedTOBuilder toBuilder, LeafSchemaNode leaf, Type returnType,
1437 boolean isReadOnly) {
1438 if (returnType == null) {
1441 final String leafName = leaf.getQName().getLocalName();
1442 final String leafDesc = leaf.getDescription();
1443 final GeneratedPropertyBuilder propBuilder = toBuilder.addProperty(parseToValidParamName(leafName));
1444 propBuilder.setReadOnly(isReadOnly);
1445 propBuilder.setReturnType(returnType);
1446 propBuilder.setComment(leafDesc);
1447 toBuilder.addEqualsIdentity(propBuilder);
1448 toBuilder.addHashIdentity(propBuilder);
1449 toBuilder.addToStringProperty(propBuilder);
1454 * Converts <code>node</code> leaf list schema node to getter method of
1455 * <code>typeBuilder</code>.
1457 * @param typeBuilder
1458 * generated type builder to which is <code>node</code> added as
1461 * leaf list schema node which is added to
1462 * <code>typeBuilder</code> as getter method
1463 * @return boolean value
1465 * <li>true - if <code>node</code>, <code>typeBuilder</code>,
1466 * nodeName equal null or <code>node</code> is added by <i>uses</i></li>
1467 * <li>false - other cases</li>
1470 private boolean resolveLeafListSchemaNode(GeneratedTypeBuilder typeBuilder, LeafListSchemaNode node) {
1471 if ((node != null) && (typeBuilder != null)) {
1472 final QName nodeName = node.getQName();
1474 if (nodeName != null && !node.isAddedByUses()) {
1475 final TypeDefinition<?> typeDef = node.getType();
1476 final Module parentModule = findParentModule(schemaContext, node);
1478 Type returnType = null;
1479 if (typeDef instanceof EnumTypeDefinition) {
1480 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node);
1481 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
1482 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, nodeName,
1484 returnType = new ReferencedTypeImpl(enumBuilder.getPackageName(), enumBuilder.getName());
1485 ((TypeProviderImpl) typeProvider).putReferencedType(node.getPath(), returnType);
1486 } else if (typeDef instanceof UnionType) {
1487 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node);
1488 if (genTOBuilder != null) {
1489 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
1491 } else if (typeDef instanceof BitsTypeDefinition) {
1492 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node);
1493 returnType = new ReferencedTypeImpl(genTOBuilder.getPackageName(), genTOBuilder.getName());
1495 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
1496 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
1499 final ParameterizedType listType = Types.listTypeFor(returnType);
1500 constructGetter(typeBuilder, nodeName.getLocalName(), node.getDescription(), listType);
1507 private Type createReturnTypeForUnion(GeneratedTOBuilder genTOBuilder, TypeDefinition<?> typeDef,
1508 GeneratedTypeBuilder typeBuilder, Module parentModule) {
1509 final GeneratedTOBuilderImpl returnType = new GeneratedTOBuilderImpl(genTOBuilder.getPackageName(),
1510 genTOBuilder.getName());
1511 genTOBuilder.setTypedef(true);
1512 genTOBuilder.setIsUnion(true);
1513 ((TypeProviderImpl) typeProvider).addUnitsToGenTO(genTOBuilder, typeDef.getUnits());
1516 final GeneratedTOBuilder unionBuilder = new GeneratedTOBuilderImpl(typeBuilder.getPackageName(),
1517 genTOBuilder.getName() + "Builder");
1518 unionBuilder.setIsUnionBuilder(true);
1519 final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
1520 method.setReturnType(returnType);
1521 method.addParameter(Types.STRING, "defaultValue");
1522 method.setAccessModifier(AccessModifier.PUBLIC);
1523 method.setStatic(true);
1525 final Set<Type> types = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(parentModule);
1526 if (types == null) {
1527 ((TypeProviderImpl) typeProvider).getAdditionalTypes().put(parentModule,
1528 Sets.<Type> newHashSet(unionBuilder.toInstance()));
1530 types.add(unionBuilder.toInstance());
1532 return returnType.toInstance();
1535 private GeneratedTypeBuilder addDefaultInterfaceDefinition(String packageName, SchemaNode schemaNode) {
1536 return addDefaultInterfaceDefinition(packageName, schemaNode, null);
1540 * Instantiates generated type builder with <code>packageName</code> and
1541 * <code>schemaNode</code>.
1543 * The new builder always implements
1544 * {@link org.opendaylight.yangtools.yang.binding.DataObject DataObject}.<br />
1545 * If <code>schemaNode</code> is instance of GroupingDefinition it also
1546 * implements {@link org.opendaylight.yangtools.yang.binding.Augmentable
1547 * Augmentable}.<br />
1548 * If <code>schemaNode</code> is instance of
1549 * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer
1550 * DataNodeContainer} it can also implement nodes which are specified in
1553 * @param packageName
1554 * string with the name of the package to which
1555 * <code>schemaNode</code> belongs.
1557 * schema node for which is created generated type builder
1559 * parent type (can be null)
1560 * @return generated type builder <code>schemaNode</code>
1562 private GeneratedTypeBuilder addDefaultInterfaceDefinition(String packageName, SchemaNode schemaNode, Type parent) {
1563 final GeneratedTypeBuilder it = addRawInterfaceDefinition(packageName, schemaNode, "");
1564 if (parent == null) {
1565 it.addImplementsType(DATA_OBJECT);
1567 it.addImplementsType(BindingTypes.childOf(parent));
1569 if (!(schemaNode instanceof GroupingDefinition)) {
1570 it.addImplementsType(augmentable(it));
1573 if (schemaNode instanceof DataNodeContainer) {
1574 addImplementedInterfaceFromUses((DataNodeContainer) schemaNode, it);
1581 * Wraps the calling of the same overloaded method.
1583 * @param packageName
1584 * string with the package name to which returning generated type
1587 * schema node which provide data about the schema node name
1588 * @return generated type builder for <code>schemaNode</code>
1590 private GeneratedTypeBuilder addRawInterfaceDefinition(String packageName, SchemaNode schemaNode) {
1591 return addRawInterfaceDefinition(packageName, schemaNode, "");
1595 * Returns reference to generated type builder for specified
1596 * <code>schemaNode</code> with <code>packageName</code>.
1598 * Firstly the generated type builder is searched in
1599 * {@link BindingGeneratorImpl#genTypeBuilders genTypeBuilders}. If it isn't
1600 * found it is created and added to <code>genTypeBuilders</code>.
1602 * @param packageName
1603 * string with the package name to which returning generated type
1606 * schema node which provide data about the schema node name
1608 * return type name prefix
1609 * @return generated type builder for <code>schemaNode</code>
1610 * @throws IllegalArgumentException
1612 * <li>if <code>schemaNode</code> is null</li>
1613 * <li>if <code>packageName</code> is null</li>
1614 * <li>if QName of schema node is null</li>
1615 * <li>if schemaNode name is null</li>
1619 private GeneratedTypeBuilder addRawInterfaceDefinition(String packageName, SchemaNode schemaNode, String prefix) {
1620 checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
1621 checkArgument(packageName != null, "Package Name for Generated Type cannot be NULL.");
1622 checkArgument(schemaNode.getQName() != null, "QName for Data Schema Node cannot be NULL.");
1623 final String schemaNodeName = schemaNode.getQName().getLocalName();
1624 checkArgument(schemaNodeName != null, "Local Name of QName for Data Schema Node cannot be NULL.");
1627 if (prefix == null) {
1628 genTypeName = BindingMapping.getClassName(schemaNodeName);
1630 genTypeName = prefix + BindingMapping.getClassName(schemaNodeName);
1633 // FIXME: Validation of name conflict
1634 final GeneratedTypeBuilderImpl newType = new GeneratedTypeBuilderImpl(packageName, genTypeName);
1635 qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, schemaNode.getQName());
1636 newType.addComment(schemaNode.getDescription());
1637 if (!genTypeBuilders.containsKey(packageName)) {
1638 final Map<String, GeneratedTypeBuilder> builders = new HashMap<>();
1639 builders.put(genTypeName, newType);
1640 genTypeBuilders.put(packageName, builders);
1642 final Map<String, GeneratedTypeBuilder> builders = genTypeBuilders.get(packageName);
1643 if (!builders.containsKey(genTypeName)) {
1644 builders.put(genTypeName, newType);
1651 * Creates the name of the getter method name from <code>localName</code>.
1654 * string with the name of the getter method
1657 * @return string with the name of the getter method for
1658 * <code>methodName</code> in JAVA method format
1660 public static String getterMethodName(String localName, Type returnType) {
1661 final StringBuilder method = new StringBuilder();
1662 if (BOOLEAN.equals(returnType)) {
1663 method.append("is");
1665 method.append("get");
1667 String name = BindingMapping.toFirstUpper(BindingMapping.getPropertyName(localName));
1668 method.append(name);
1669 return method.toString();
1673 * Created a method signature builder as part of
1674 * <code>interfaceBuilder</code>.
1676 * The method signature builder is created for the getter method of
1677 * <code>schemaNodeName</code>. Also <code>comment</code> and
1678 * <code>returnType</code> information are added to the builder.
1680 * @param interfaceBuilder
1681 * generated type builder for which the getter method should be
1683 * @param schemaNodeName
1684 * string with schema node name. The name will be the part of the
1685 * getter method name.
1687 * string with comment for the getter method
1689 * type which represents the return type of the getter method
1690 * @return method signature builder which represents the getter method of
1691 * <code>interfaceBuilder</code>
1693 private MethodSignatureBuilder constructGetter(GeneratedTypeBuilder interfaceBuilder, String schemaNodeName,
1694 String comment, Type returnType) {
1695 final MethodSignatureBuilder getMethod = interfaceBuilder
1696 .addMethod(getterMethodName(schemaNodeName, returnType));
1697 getMethod.setComment(comment);
1698 getMethod.setReturnType(returnType);
1703 * Adds <code>schemaNode</code> to <code>typeBuilder</code> as getter method
1704 * or to <code>genTOBuilder</code> as property.
1706 * @param basePackageName
1707 * string contains the module package name
1709 * data schema node which should be added as getter method to
1710 * <code>typeBuilder</code> or as a property to
1711 * <code>genTOBuilder</code> if is part of the list key
1712 * @param typeBuilder
1713 * generated type builder for the list schema node
1714 * @param genTOBuilder
1715 * generated TO builder for the list keys
1717 * list of string which contains names of the list keys
1720 * @throws IllegalArgumentException
1722 * <li>if <code>schemaNode</code> equals null</li>
1723 * <li>if <code>typeBuilder</code> equals null</li>
1726 private void addSchemaNodeToListBuilders(String basePackageName, DataSchemaNode schemaNode,
1727 GeneratedTypeBuilder typeBuilder, GeneratedTOBuilder genTOBuilder, List<String> listKeys, Module module) {
1728 checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
1729 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
1731 if (schemaNode instanceof LeafSchemaNode) {
1732 final LeafSchemaNode leaf = (LeafSchemaNode) schemaNode;
1733 final String leafName = leaf.getQName().getLocalName();
1734 final Type type = resolveLeafSchemaNodeAsMethod(typeBuilder, leaf);
1735 if (listKeys.contains(leafName)) {
1737 resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, true, module);
1739 resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, type, true);
1742 } else if (!schemaNode.isAddedByUses()) {
1743 if (schemaNode instanceof LeafListSchemaNode) {
1744 resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) schemaNode);
1745 } else if (schemaNode instanceof ContainerSchemaNode) {
1746 containerToGenType(module, basePackageName, typeBuilder, typeBuilder, (ContainerSchemaNode) schemaNode);
1747 } else if (schemaNode instanceof ChoiceNode) {
1748 choiceToGeneratedType(module, basePackageName, typeBuilder, (ChoiceNode) schemaNode);
1749 } else if (schemaNode instanceof ListSchemaNode) {
1750 listToGenType(module, basePackageName, typeBuilder, typeBuilder, (ListSchemaNode) schemaNode);
1755 private void typeBuildersToGenTypes(Module module, GeneratedTypeBuilder typeBuilder, GeneratedTOBuilder genTOBuilder) {
1756 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
1758 if (genTOBuilder != null) {
1759 GeneratedTransferObject genTO = genTOBuilder.toInstance();
1760 constructGetter(typeBuilder, "key", "Returns Primary Key of Yang List Type", genTO);
1761 genCtx.get(module).addGeneratedTOBuilder(genTOBuilder);
1766 * Selects the names of the list keys from <code>list</code> and returns
1767 * them as the list of the strings
1770 * of string with names of the list keys
1771 * @return list of string which represents names of the list keys. If the
1772 * <code>list</code> contains no keys then the empty list is
1775 private List<String> listKeys(final ListSchemaNode list) {
1776 final List<String> listKeys = new ArrayList<>();
1778 List<QName> keyDefinition = list.getKeyDefinition();
1779 if (keyDefinition != null) {
1780 for (QName keyDef : keyDefinition) {
1781 listKeys.add(keyDef.getLocalName());
1788 * Generates for the <code>list</code> which contains any list keys special
1789 * generated TO builder.
1791 * @param packageName
1792 * string with package name to which the list belongs
1794 * list schema node which is source of data about the list name
1795 * @return generated TO builder which represents the keys of the
1796 * <code>list</code> or null if <code>list</code> is null or list of
1797 * key definitions is null or empty.
1799 private GeneratedTOBuilder resolveListKeyTOBuilder(String packageName, ListSchemaNode list) {
1800 GeneratedTOBuilder genTOBuilder = null;
1801 if ((list.getKeyDefinition() != null) && (!list.getKeyDefinition().isEmpty())) {
1802 final String listName = list.getQName().getLocalName() + "Key";
1803 final String genTOName = BindingMapping.getClassName(listName);
1804 genTOBuilder = new GeneratedTOBuilderImpl(packageName, genTOName);
1806 return genTOBuilder;
1810 * Builds generated TO builders for <code>typeDef</code> of type
1811 * {@link org.opendaylight.yangtools.yang.model.util.UnionType UnionType} or
1812 * {@link org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
1813 * BitsTypeDefinition} which are also added to <code>typeBuilder</code> as
1814 * enclosing transfer object.
1816 * If more then one generated TO builder is created for enclosing then all
1817 * of the generated TO builders are added to <code>typeBuilder</code> as
1818 * enclosing transfer objects.
1821 * type definition which can be of type <code>UnionType</code> or
1822 * <code>BitsTypeDefinition</code>
1823 * @param typeBuilder
1824 * generated type builder to which is added generated TO created
1825 * from <code>typeDef</code>
1827 * string with name for generated TO builder
1829 * @return generated TO builder for <code>typeDef</code>
1831 private GeneratedTOBuilder addTOToTypeBuilder(TypeDefinition<?> typeDef, GeneratedTypeBuilder typeBuilder,
1832 DataSchemaNode leaf) {
1833 final String classNameFromLeaf = BindingMapping.getClassName(leaf.getQName());
1834 final List<GeneratedTOBuilder> genTOBuilders = new ArrayList<>();
1835 final String packageName = typeBuilder.getFullyQualifiedName();
1836 if (typeDef instanceof UnionTypeDefinition) {
1837 final List<GeneratedTOBuilder> types = ((TypeProviderImpl) typeProvider)
1838 .provideGeneratedTOBuildersForUnionTypeDef(packageName, ((UnionTypeDefinition) typeDef),
1839 classNameFromLeaf, leaf);
1840 genTOBuilders.addAll(types);
1842 GeneratedTOBuilder resultTOBuilder = null;
1843 if (!types.isEmpty()) {
1844 resultTOBuilder = types.remove(0);
1845 for (GeneratedTOBuilder genTOBuilder : types) {
1846 resultTOBuilder.addEnclosingTransferObject(genTOBuilder);
1850 final GeneratedPropertyBuilder genPropBuilder = resultTOBuilder.addProperty("value");
1851 genPropBuilder.setReturnType(Types.primitiveType("char[]", null));
1852 resultTOBuilder.addEqualsIdentity(genPropBuilder);
1853 resultTOBuilder.addHashIdentity(genPropBuilder);
1854 resultTOBuilder.addToStringProperty(genPropBuilder);
1856 } else if (typeDef instanceof BitsTypeDefinition) {
1857 genTOBuilders.add((((TypeProviderImpl) typeProvider)).provideGeneratedTOBuilderForBitsTypeDefinition(
1858 packageName, typeDef, classNameFromLeaf));
1860 if (genTOBuilders != null && !genTOBuilders.isEmpty()) {
1861 for (GeneratedTOBuilder genTOBuilder : genTOBuilders) {
1862 typeBuilder.addEnclosingTransferObject(genTOBuilder);
1864 return genTOBuilders.get(0);
1871 * Adds the implemented types to type builder.
1873 * The method passes through the list of <i>uses</i> in
1874 * {@code dataNodeContainer}. For every <i>use</i> is obtained corresponding
1875 * generated type from {@link BindingGeneratorImpl#allGroupings
1876 * allGroupings} which is added as <i>implements type</i> to
1877 * <code>builder</code>
1879 * @param dataNodeContainer
1880 * element which contains the list of used YANG groupings
1882 * builder to which are added implemented types according to
1883 * <code>dataNodeContainer</code>
1884 * @return generated type builder with all implemented types
1886 private GeneratedTypeBuilder addImplementedInterfaceFromUses(DataNodeContainer dataNodeContainer,
1887 GeneratedTypeBuilder builder) {
1888 for (UsesNode usesNode : dataNodeContainer.getUses()) {
1889 if (usesNode.getGroupingPath() != null) {
1890 final GeneratedType genType = findGroupingByPath(usesNode.getGroupingPath()).toInstance();
1891 if (genType == null) {
1892 throw new IllegalStateException("Grouping " + usesNode.getGroupingPath() + "is not resolved for "
1893 + builder.getName());
1895 builder.addImplementsType(genType);
1896 builder.addComment(genType.getComment());
1902 private GeneratedTypeBuilder findChildNodeByPath(SchemaPath path) {
1903 for (ModuleContext ctx : genCtx.values()) {
1904 GeneratedTypeBuilder result = ctx.getChildNode(path);
1905 if (result != null) {
1912 private GeneratedTypeBuilder findGroupingByPath(SchemaPath path) {
1913 for (ModuleContext ctx : genCtx.values()) {
1914 GeneratedTypeBuilder result = ctx.getGrouping(path);
1915 if (result != null) {
1922 private GeneratedTypeBuilder findCaseByPath(SchemaPath path) {
1923 for (ModuleContext ctx : genCtx.values()) {
1924 GeneratedTypeBuilder result = ctx.getCase(path);
1925 if (result != null) {
1932 public Map<Module, ModuleContext> getModuleContexts() {