Fix checkstyle in mdsal-binding-generator-impl
[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 modules specified
36      * in <code>modules</code>. Generated types are created for modules, groupings, types, containers, lists, choices,
37      * augments, rpcs, notification, identities.
38      *
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:
43      *         <ul>
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>
46      *         set.</li>
47      *         </ul>
48      * @throws IllegalArgumentException
49      *             <ul>
50      *             <li>if arg <code>context</code> is null or</li>
51      *             <li>if arg <code>modules</code> is null</li>
52      *             </ul>
53      * @throws IllegalStateException
54      *             if <code>context</code> contain no modules
55      */
56     @Override
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.");
60
61         final Map<SchemaNode, JavaTypeName> renames = new IdentityHashMap<>();
62         for (;;) {
63             try {
64                 return new CodegenTypeGenerator(context, renames).toTypes(modules);
65             } catch (RenameMappingException e) {
66                 rename(renames, e);
67             }
68         }
69     }
70
71     @Override
72     public BindingRuntimeTypes generateTypeMapping(final SchemaContext context) {
73         checkContext(context);
74
75         final Map<SchemaNode, JavaTypeName> renames = new IdentityHashMap<>();
76         for (;;) {
77             try {
78                 return new RuntimeTypeGenerator(context, renames).toTypeMapping();
79             } catch (RenameMappingException e) {
80                 rename(renames, e);
81             }
82         }
83     }
84
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.");
88     }
89
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 "
96                     + existing, ex);
97         }
98
99         final String suffix;
100         if (def instanceof IdentitySchemaNode) {
101             suffix = "$I";
102         } else if (def instanceof GroupingDefinition) {
103             suffix = "$G";
104         } else if (def instanceof TypeDefinition) {
105             suffix = "$T";
106         } else {
107             throw new IllegalStateException("Unhandled remapping of " + def + " at " + name, ex);
108         }
109
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);
113     }
114 }