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

©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.