0498bbb7593c1391db503d115de61b5357616507
[mdsal.git] / binding2 / mdsal-binding2-java-api-generator / src / main / java / org / opendaylight / mdsal / binding2 / java / api / generator / BuilderGenerator.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.mdsal.binding2.java.api.generator;
10
11 import com.google.common.annotations.Beta;
12 import org.opendaylight.mdsal.binding2.java.api.generator.renderers.BuilderRenderer;
13 import org.opendaylight.mdsal.binding2.model.api.CodeGenerator;
14 import org.opendaylight.mdsal.binding2.model.api.GeneratedTransferObject;
15 import org.opendaylight.mdsal.binding2.model.api.GeneratedType;
16 import org.opendaylight.mdsal.binding2.model.api.Type;
17 import org.opendaylight.mdsal.binding2.model.api.UnitName;
18 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentable;
19 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentation;
20 import org.opendaylight.yangtools.concepts.Identifier;
21
22 /**
23  * Transformer of the data from the virtual form to JAVA programming language.
24  * The result source code represent java class. For generation of the source
25  * code is used the template written in Twirl (Scala based) language.
26  */
27 @Beta
28 public final class BuilderGenerator implements CodeGenerator {
29
30     @Override
31     public String generate(Type type) {
32         if ((type instanceof GeneratedType) && !(type instanceof GeneratedTransferObject)) {
33             final GeneratedType genType = (GeneratedType) type;
34             return new BuilderRenderer(genType).generateTemplate();
35         } else {
36             return "";
37         }
38     }
39
40     @Override
41     public boolean isAcceptable(Type type) {
42         if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
43             for (Type t : ((GeneratedType) type).getImplements()) {
44                 // "rpc" and "grouping" elements do not implement Augmentable
45                 if (t.getFullyQualifiedName().equals(Augmentable.class.getName())) {
46                     return true;
47                 } else if (t.getFullyQualifiedName().equals(Augmentation.class.getName())) {
48                     return true;
49                 }
50
51             }
52         }
53         return false;
54     }
55
56     @Override
57     public Identifier getUnitName(Type type) {
58         return new UnitName(type.getName());
59     }
60 }