Bug 849: Fixed NPE in Translated Data Change Events.
[controller.git] / opendaylight / config / yang-jmx-generator-plugin / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / plugin / gofactory / ConcreteModuleGeneratedObjectFactory.java
1 package org.opendaylight.controller.config.yangjmxgenerator.plugin.gofactory;
2
3 import com.google.common.base.Joiner;
4 import com.google.common.base.Optional;
5 import org.opendaylight.controller.config.api.DependencyResolver;
6 import org.opendaylight.controller.config.api.ModuleIdentifier;
7 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
8 import org.opendaylight.controller.config.yangjmxgenerator.plugin.java.FullyQualifiedName;
9 import org.opendaylight.controller.config.yangjmxgenerator.plugin.java.GeneratedObject;
10 import org.opendaylight.controller.config.yangjmxgenerator.plugin.java.GeneratedObjectBuilder;
11 import org.opendaylight.controller.config.yangjmxgenerator.plugin.java.JavaFileInputBuilder;
12 import org.opendaylight.controller.config.yangjmxgenerator.plugin.java.TypeName;
13
14 import java.util.LinkedHashMap;
15
16 public class ConcreteModuleGeneratedObjectFactory {
17
18     public GeneratedObject toGeneratedObject(ModuleMXBeanEntry mbe, Optional<String> copyright, Optional<String> header) {
19         FullyQualifiedName concreteFQN = new FullyQualifiedName(mbe.getPackageName(), mbe.getStubModuleName());
20         FullyQualifiedName abstractFQN = new FullyQualifiedName(mbe.getPackageName(), mbe.getAbstractModuleName());
21         Optional<String> classJavaDoc = Optional.fromNullable(mbe.getNullableDescription());
22         return toGeneratedObject(concreteFQN, abstractFQN, copyright, header, classJavaDoc);
23     }
24
25     GeneratedObject toGeneratedObject(FullyQualifiedName concreteFQN,
26                                              FullyQualifiedName abstractFQN,
27                                              Optional<String> copyright,
28                                              Optional<String> header,
29                                              Optional<String> classJavaDoc) {
30         // there are two constructors and two methods
31         JavaFileInputBuilder builder = new JavaFileInputBuilder();
32         builder.setTypeName(TypeName.classType);
33         builder.setFqn(concreteFQN);
34         builder.addExtendsFQN(abstractFQN);
35
36         builder.setCopyright(copyright);
37         builder.setHeader(header);
38         builder.setClassJavaDoc(classJavaDoc);
39
40         builder.addToBody(getNewCtor(concreteFQN));
41         builder.addToBody(getCopyCtor(concreteFQN));
42         builder.addToBody(getCustomValidationStub());
43         builder.addToBody(getCreateInstanceStub());
44
45         return new GeneratedObjectBuilder(builder.build()).toGeneratedObject();
46     }
47
48     private static String getNewCtor(FullyQualifiedName fqn) {
49         LinkedHashMap<String, String> parameters = new LinkedHashMap<String, String>(){
50             {
51                 put(ModuleIdentifier.class.getCanonicalName(), "identifier");
52                 put(DependencyResolver.class.getCanonicalName(), "dependencyResolver");
53             }
54         };
55         StringBuilder stringBuilder = getCtor(fqn, parameters);
56         return stringBuilder.toString();
57     }
58
59     private static StringBuilder getCtor(FullyQualifiedName fqn , LinkedHashMap<String, String> parameters) {
60         StringBuilder stringBuilder = new StringBuilder();
61         stringBuilder.append("public ").append(fqn.getTypeName()).append("(");
62         // parameters
63         stringBuilder.append(Joiner.on(", ").withKeyValueSeparator(" ").join(parameters));
64         stringBuilder.append(") {\n");
65         if (parameters.isEmpty() == false) {
66             stringBuilder.append("super(");
67             stringBuilder.append(Joiner.on(", ").join(parameters.values()));
68             stringBuilder.append(");\n");
69         }
70         stringBuilder.append("}");
71         return stringBuilder;
72     }
73
74     private static String getCopyCtor(final FullyQualifiedName fqn) {
75         LinkedHashMap<String, String> parameters = new LinkedHashMap<String, String>(){
76             {
77                 put(ModuleIdentifier.class.getCanonicalName(), "identifier");
78                 put(DependencyResolver.class.getCanonicalName(), "dependencyResolver");
79                 put(fqn.toString(), "oldModule");
80                 put(AutoCloseable.class.getCanonicalName(), "oldInstance");
81             }
82         };
83         StringBuilder stringBuilder = getCtor(fqn, parameters);
84         return stringBuilder.toString();
85     }
86
87     private static String getCustomValidationStub() {
88         return "@Override\n" +
89                 "public void customValidation() {\n" +
90                 "// add custom validation form module attributes here.\n" +
91                 "}";
92     }
93
94     private static String getCreateInstanceStub() {
95         return "@Override\n" +
96                 "public " + AutoCloseable.class.getCanonicalName() + " createInstance() {\n" +
97                 "// TODO:implement\n" +
98                 "throw new " + UnsupportedOperationException.class.getCanonicalName() + "();\n"+
99                 "}";
100     }
101 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.