2 * Copyright (c) 2014 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.java.api.generator;
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;
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.
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);
32 * Passes via list of implemented types in <code>type</code>.
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>.
38 public boolean isAcceptable(final Type type) {
39 if (type instanceof GeneratedType generated && !(type instanceof GeneratedTransferObject)) {
40 for (Type t : generated.getImplements()) {
41 // "rpc" and "grouping" elements do not implement Augmentable
42 final JavaTypeName name = t.getIdentifier();
43 if (name.equals(AUGMENTABLE) || name.equals(AUGMENTATION)) {
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.
56 public String generate(final Type type) {
57 if (type instanceof GeneratedType generated && !(type instanceof GeneratedTransferObject)) {
58 return templateForType(generated).generate();
64 public String getUnitName(final Type type) {
65 return type.getName() + BindingMapping.BUILDER_SUFFIX;
69 static BuilderTemplate templateForType(final GeneratedType type) {
70 final JavaTypeName origName = type.getIdentifier();
71 final JavaTypeName builderName = origName.createSibling(origName.simpleName() + BindingMapping.BUILDER_SUFFIX);
73 return new BuilderTemplate(new CodegenGeneratedTypeBuilder(builderName)
74 .addEnclosingTransferObject(new CodegenGeneratedTOBuilder(
75 builderName.createEnclosed(origName.simpleName() + "Impl"))
76 .addImplementsType(type)
78 .build(), type, getKey(type));
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();