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.api.type.builder.GeneratedTOBuilder;
18 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
19 import org.opendaylight.mdsal.binding.model.util.generated.type.builder.CodegenGeneratedTypeBuilder;
20 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
21 import org.opendaylight.yangtools.yang.binding.Augmentable;
22 import org.opendaylight.yangtools.yang.binding.Augmentation;
25 * Transformator of the data from the virtual form to JAVA programming language. The result source code represent java
26 * class. For generation of the source code is used the template written in XTEND language.
28 public final class BuilderGenerator implements CodeGenerator {
29 private static final JavaTypeName AUGMENTABLE = JavaTypeName.create(Augmentable.class);
30 private static final JavaTypeName AUGMENTATION = JavaTypeName.create(Augmentation.class);
33 * Passes via list of implemented types in <code>type</code>.
35 * @param type JAVA <code>Type</code>
36 * @return boolean value which is true if any of implemented types is of the type <code>Augmentable</code>.
39 public boolean isAcceptable(final Type type) {
40 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
41 for (Type t : ((GeneratedType) type).getImplements()) {
42 // "rpc" and "grouping" elements do not implement Augmentable
43 final JavaTypeName name = t.getIdentifier();
44 if (name.equals(AUGMENTABLE) || name.equals(AUGMENTATION)) {
53 * Generates JAVA source code for generated type <code>Type</code>. The code is generated according to the template
54 * source code template which is written in XTEND language.
57 public String generate(final Type type) {
58 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
59 return templateForType((GeneratedType) type).generate();
65 public String getUnitName(final Type type) {
66 return type.getName() + BuilderTemplate.BUILDER_STR;
70 static BuilderTemplate templateForType(final GeneratedType type) {
71 final JavaTypeName origName = type.getIdentifier();
73 final GeneratedTypeBuilder builderTypeBuilder = new CodegenGeneratedTypeBuilder(
74 origName.createSibling(origName.simpleName() + BuilderTemplate.BUILDER_STR));
76 final GeneratedTOBuilder implTypeBuilder = builderTypeBuilder.addEnclosingTransferObject(
77 origName.simpleName() + "Impl");
78 implTypeBuilder.addImplementsType(type);
80 return new BuilderTemplate(builderTypeBuilder.build(), type, getKey(type));
83 private static Type getKey(final GeneratedType type) {
84 for (MethodSignature m : type.getMethodDefinitions()) {
85 if (BindingMapping.IDENTIFIABLE_KEY_NAME.equals(m.getName())) {
86 return m.getReturnType();