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.mdsal.binding.generator.impl;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
13 import java.util.IdentityHashMap;
14 import java.util.List;
17 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
18 import org.opendaylight.mdsal.binding.generator.api.BindingRuntimeGenerator;
19 import org.opendaylight.mdsal.binding.generator.api.BindingRuntimeTypes;
20 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
21 import org.opendaylight.mdsal.binding.model.api.Type;
22 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
23 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class BindingGeneratorImpl implements BindingGenerator, BindingRuntimeGenerator {
32 private static final Logger LOG = LoggerFactory.getLogger(BindingGeneratorImpl.class);
35 * Resolves generated types from <code>context</code> schema nodes only for
36 * modules specified in <code>modules</code>
38 * Generated types are created for modules, groupings, types, containers,
39 * lists, choices, augments, rpcs, notification, identities.
42 * schema context which contains data about all schema nodes
45 * set of modules for which schema nodes should be generated
47 * @return list of types (usually <code>GeneratedType</code> or
48 * <code>GeneratedTransferObject</code>) which:
50 * <li>are generated from <code>context</code> schema nodes and</li>
51 * <li>are also part of some of the module in <code>modules</code>
54 * @throws IllegalArgumentException
56 * <li>if arg <code>context</code> is null or</li>
57 * <li>if arg <code>modules</code> is null</li>
59 * @throws IllegalStateException
60 * if <code>context</code> contain no modules
63 public List<Type> generateTypes(final SchemaContext context, final Set<Module> modules) {
64 checkContext(context);
65 checkArgument(modules != null, "Set of Modules cannot be NULL.");
67 final Map<SchemaNode, JavaTypeName> renames = new IdentityHashMap<>();
70 return new CodegenTypeGenerator(context, renames).toTypes(modules);
71 } catch (RenameMappingException e) {
78 public BindingRuntimeTypes generateTypeMapping(final SchemaContext context) {
79 checkContext(context);
81 final Map<SchemaNode, JavaTypeName> renames = new IdentityHashMap<>();
84 return new RuntimeTypeGenerator(context, renames).toTypeMapping();
85 } catch (RenameMappingException e) {
91 private static void checkContext(final SchemaContext context) {
92 checkArgument(context != null, "Schema Context reference cannot be NULL.");
93 checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
96 private static void rename(final Map<SchemaNode, JavaTypeName> renames, final RenameMappingException e) {
97 final JavaTypeName name = e.getName();
98 final SchemaNode def = e.getDefinition();
99 final JavaTypeName existing = renames.get(def);
100 if (existing != null) {
101 throw new IllegalStateException("Attempted to relocate " + def + " to " + name + ", already remapped to "
106 if (def instanceof IdentitySchemaNode) {
108 } else if (def instanceof GroupingDefinition) {
110 } else if (def instanceof TypeDefinition) {
113 throw new IllegalStateException("Unhandled remapping of " + def + " at " + name, e);
116 final JavaTypeName newName = name.createSibling(name.simpleName() + suffix);
117 renames.put(def, newName);
118 LOG.debug("Restarting code generation after remapping {} to {}", name, newName);