89f5182c28552933c2b9c14b3ac97a063d540b08
[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.List;
14 import java.util.Set;
15 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
16 import org.opendaylight.mdsal.binding.generator.api.BindingRuntimeGenerator;
17 import org.opendaylight.mdsal.binding.generator.api.BindingRuntimeTypes;
18 import org.opendaylight.mdsal.binding.model.api.Type;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21
22 public class BindingGeneratorImpl implements BindingGenerator, BindingRuntimeGenerator {
23     /**
24      * Resolves generated types from <code>context</code> schema nodes only for
25      * modules specified in <code>modules</code>
26      *
27      * Generated types are created for modules, groupings, types, containers,
28      * lists, choices, augments, rpcs, notification, identities.
29      *
30      * @param context
31      *            schema context which contains data about all schema nodes
32      *            saved in modules
33      * @param modules
34      *            set of modules for which schema nodes should be generated
35      *            types
36      * @return list of types (usually <code>GeneratedType</code> or
37      *         <code>GeneratedTransferObject</code>) which:
38      *         <ul>
39      *         <li>are generated from <code>context</code> schema nodes and</li>
40      *         <li>are also part of some of the module in <code>modules</code>
41      *         set.</li>
42      *         </ul>
43      * @throws IllegalArgumentException
44      *             <ul>
45      *             <li>if arg <code>context</code> is null or</li>
46      *             <li>if arg <code>modules</code> is null</li>
47      *             </ul>
48      * @throws IllegalStateException
49      *             if <code>context</code> contain no modules
50      */
51     @Override
52     public List<Type> generateTypes(final SchemaContext context, final Set<Module> modules) {
53         checkContext(context);
54         checkArgument(modules != null, "Set of Modules cannot be NULL.");
55         return new CodegenTypeGenerator(context).toTypes(modules);
56     }
57
58     @Override
59     public BindingRuntimeTypes generateTypeMapping(final SchemaContext context) {
60         checkContext(context);
61         return new RuntimeTypeGenerator(context).toTypeMapping();
62     }
63
64     private static void checkContext(final SchemaContext context) {
65         checkArgument(context != null, "Schema Context reference cannot be NULL.");
66         checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
67     }
68 }