Split out isBitsType()
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / BuilderGenerator.java
1 /*
2  * Copyright (c) 2014 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.java.api.generator;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import org.opendaylight.mdsal.binding.model.api.CodeGenerator;
12 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
13 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
14 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
15 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
16 import org.opendaylight.mdsal.binding.model.api.Type;
17 import org.opendaylight.mdsal.binding.model.ri.generated.type.builder.CodegenGeneratedTOBuilder;
18 import org.opendaylight.mdsal.binding.model.ri.generated.type.builder.CodegenGeneratedTypeBuilder;
19 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
20 import org.opendaylight.yangtools.yang.binding.Augmentable;
21 import org.opendaylight.yangtools.yang.binding.Augmentation;
22
23 /**
24  * Transformator of the data from the virtual form to JAVA programming language. The result source code represent java
25  * class. For generation of the source code is used the template written in XTEND language.
26  */
27 public final class BuilderGenerator implements CodeGenerator {
28     private static final JavaTypeName AUGMENTABLE = JavaTypeName.create(Augmentable.class);
29     private static final JavaTypeName AUGMENTATION = JavaTypeName.create(Augmentation.class);
30
31     /**
32      * Passes via list of implemented types in <code>type</code>.
33      *
34      * @param type JAVA <code>Type</code>
35      * @return boolean value which is true if any of implemented types is of the type <code>Augmentable</code>.
36      */
37     @Override
38     public boolean isAcceptable(final Type type) {
39         if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
40             for (Type t : ((GeneratedType) type).getImplements()) {
41                 // "rpc" and "grouping" elements do not implement Augmentable
42                 final JavaTypeName name = t.getIdentifier();
43                 if (name.equals(AUGMENTABLE) || name.equals(AUGMENTATION)) {
44                     return true;
45                 }
46             }
47         }
48         return false;
49     }
50
51     /**
52      * Generates JAVA source code for generated type <code>Type</code>. The code is generated according to the template
53      * source code template which is written in XTEND language.
54      */
55     @Override
56     public String generate(final Type type) {
57         if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
58             return templateForType((GeneratedType) type).generate();
59         }
60         return "";
61     }
62
63     @Override
64     public String getUnitName(final Type type) {
65         return type.getName() + BindingMapping.BUILDER_SUFFIX;
66     }
67
68     @VisibleForTesting
69     static BuilderTemplate templateForType(final GeneratedType type) {
70         final JavaTypeName origName = type.getIdentifier();
71         final JavaTypeName builderName = origName.createSibling(origName.simpleName() + BindingMapping.BUILDER_SUFFIX);
72
73         return new BuilderTemplate(new CodegenGeneratedTypeBuilder(builderName)
74             .addEnclosingTransferObject(new CodegenGeneratedTOBuilder(
75                 builderName.createEnclosed(origName.simpleName() + "Impl"))
76                 .addImplementsType(type)
77                 .build())
78             .build(), type, getKey(type));
79     }
80
81     private static Type getKey(final GeneratedType type) {
82         for (MethodSignature m : type.getMethodDefinitions()) {
83             if (BindingMapping.IDENTIFIABLE_KEY_NAME.equals(m.getName())) {
84                 return m.getReturnType();
85             }
86         }
87         return null;
88     }
89 }