2 * Copyright (c) 2016 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
9 package org.opendaylight.mdsal.binding2.generator.impl;
11 import static com.google.common.base.Preconditions.checkArgument;
12 import static com.google.common.base.Preconditions.checkState;
14 import com.google.common.annotations.Beta;
15 import java.util.HashMap;
16 import java.util.List;
18 import org.opendaylight.mdsal.binding2.generator.spi.TypeProvider;
19 import org.opendaylight.mdsal.binding2.generator.util.Binding2Mapping;
20 import org.opendaylight.mdsal.binding2.generator.yang.types.TypeProviderImpl;
21 import org.opendaylight.mdsal.binding2.model.api.Type;
22 import org.opendaylight.mdsal.binding2.model.api.type.builder.GeneratedTypeBuilder;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
29 final class ModuleToGenType {
31 private ModuleToGenType() {
32 throw new UnsupportedOperationException("Utility class");
35 static Map<Module, ModuleContext> generate(final Module module, final SchemaContext schemaContext,
36 TypeProvider typeProvider, final boolean verboseClassComments) {
37 Map<Module, ModuleContext> genCtx = new HashMap<>();
39 genCtx.put(module, new ModuleContext());
40 genCtx = allTypeDefinitionsToGenTypes(module, genCtx, typeProvider);
42 //TODO: call generate for other entities (groupings, rpcs, identities, notifications)
44 if (!module.getChildNodes().isEmpty()) {
45 final GeneratedTypeBuilder moduleType = GenHelperUtil.moduleToDataType(module, genCtx, verboseClassComments);
46 genCtx.get(module).addModuleNode(moduleType);
47 final String basePackageName = Binding2Mapping.getRootPackageName(module);
48 GenHelperUtil.resolveDataSchemaNodes(module, basePackageName, moduleType, moduleType, module
56 * Converts all extended type definitions of module to the list of
57 * <code>Type</code> objects.
60 * module from which is obtained set of type definitions
61 * @throws IllegalArgumentException
63 * <li>if module is null</li>
64 * <li>if name of module is null</li>
66 * @throws IllegalStateException
67 * if set of type definitions from module is null
69 private static Map<Module, ModuleContext> allTypeDefinitionsToGenTypes(final Module module, Map<Module, ModuleContext> genCtx,
70 TypeProvider typeProvider) {
71 checkArgument(module != null, "Module reference cannot be NULL.");
72 checkArgument(module.getName() != null, "Module name cannot be NULL.");
73 final DataNodeIterator it = new DataNodeIterator(module);
74 final List<TypeDefinition<?>> typeDefinitions = it.allTypedefs();
75 checkState(typeDefinitions != null, "Type Definitions for module «module.name» cannot be NULL.");
77 typeDefinitions.stream().filter(typedef -> typedef != null).forEach(typedef -> {
78 final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef,
81 final ModuleContext ctx = genCtx.get(module);
82 ctx.addTypedefType(typedef.getPath(), type);
83 ctx.addTypeToSchema(type, typedef);