b53ec144f6e0d373c54a23ea3c16d9f32bd07b46
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / mdsal / binding / generator / impl / BindingGeneratorImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.generator.impl;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12
13 import java.util.IdentityHashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
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;
30
31 public class BindingGeneratorImpl implements BindingGenerator, BindingRuntimeGenerator {
32     private static final Logger LOG = LoggerFactory.getLogger(BindingGeneratorImpl.class);
33
34     /**
35      * Resolves generated types from <code>context</code> schema nodes only for
36      * modules specified in <code>modules</code>
37      *
38      * Generated types are created for modules, groupings, types, containers,
39      * lists, choices, augments, rpcs, notification, identities.
40      *
41      * @param context
42      *            schema context which contains data about all schema nodes
43      *            saved in modules
44      * @param modules
45      *            set of modules for which schema nodes should be generated
46      *            types
47      * @return list of types (usually <code>GeneratedType</code> or
48      *         <code>GeneratedTransferObject</code>) which:
49      *         <ul>
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>
52      *         set.</li>
53      *         </ul>
54      * @throws IllegalArgumentException
55      *             <ul>
56      *             <li>if arg <code>context</code> is null or</li>
57      *             <li>if arg <code>modules</code> is null</li>
58      *             </ul>
59      * @throws IllegalStateException
60      *             if <code>context</code> contain no modules
61      */
62     @Override
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.");
66
67         final Map<SchemaNode, JavaTypeName> renames = new IdentityHashMap<>();
68         for (;;) {
69             try {
70                 return new CodegenTypeGenerator(context, renames).toTypes(modules);
71             } catch (RenameMappingException e) {
72                 rename(renames, e);
73             }
74         }
75     }
76
77     @Override
78     public BindingRuntimeTypes generateTypeMapping(final SchemaContext context) {
79         checkContext(context);
80
81         final Map<SchemaNode, JavaTypeName> renames = new IdentityHashMap<>();
82         for (;;) {
83             try {
84                 return new RuntimeTypeGenerator(context, renames).toTypeMapping();
85             } catch (RenameMappingException e) {
86                 rename(renames, e);
87             }
88         }
89     }
90
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.");
94     }
95
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 "
102                     + existing, e);
103         }
104
105         final String suffix;
106         if (def instanceof IdentitySchemaNode) {
107             suffix = "$I";
108         } else if (def instanceof GroupingDefinition) {
109             suffix = "$G";
110         } else if (def instanceof TypeDefinition) {
111             suffix = "$T";
112         } else {
113             throw new IllegalStateException("Unhandled remapping of " + def + " at " + name, e);
114         }
115
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);
119     }
120 }