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 modules specified
36 * in <code>modules</code>. Generated types are created for modules, groupings, types, containers, lists, choices,
37 * augments, rpcs, notification, identities.
39 * @param context schema context which contains data about all schema nodes saved in modules
40 * @param modules set of modules for which schema nodes should be generated types
41 * @return list of types (usually <code>GeneratedType</code> or
42 * <code>GeneratedTransferObject</code>) which:
44 * <li>are generated from <code>context</code> schema nodes and</li>
45 * <li>are also part of some of the module in <code>modules</code>
48 * @throws IllegalArgumentException
50 * <li>if arg <code>context</code> is null or</li>
51 * <li>if arg <code>modules</code> is null</li>
53 * @throws IllegalStateException
54 * if <code>context</code> contain no modules
57 public List<Type> generateTypes(final SchemaContext context, final Set<Module> modules) {
58 checkContext(context);
59 checkArgument(modules != null, "Set of Modules cannot be NULL.");
61 final Map<SchemaNode, JavaTypeName> renames = new IdentityHashMap<>();
64 return new CodegenTypeGenerator(context, renames).toTypes(modules);
65 } catch (RenameMappingException e) {
72 public BindingRuntimeTypes generateTypeMapping(final SchemaContext context) {
73 checkContext(context);
75 final Map<SchemaNode, JavaTypeName> renames = new IdentityHashMap<>();
78 return new RuntimeTypeGenerator(context, renames).toTypeMapping();
79 } catch (RenameMappingException e) {
85 private static void checkContext(final SchemaContext context) {
86 checkArgument(context != null, "Schema Context reference cannot be NULL.");
87 checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
90 private static void rename(final Map<SchemaNode, JavaTypeName> renames, final RenameMappingException ex) {
91 final JavaTypeName name = ex.getName();
92 final SchemaNode def = ex.getDefinition();
93 final JavaTypeName existing = renames.get(def);
94 if (existing != null) {
95 throw new IllegalStateException("Attempted to relocate " + def + " to " + name + ", already remapped to "
100 if (def instanceof IdentitySchemaNode) {
102 } else if (def instanceof GroupingDefinition) {
104 } else if (def instanceof TypeDefinition) {
107 throw new IllegalStateException("Unhandled remapping of " + def + " at " + name, ex);
110 final JavaTypeName newName = name.createSibling(name.simpleName() + suffix);
111 renames.put(def, newName);
112 LOG.debug("Restarting code generation after remapping {} to {}", name, newName);