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

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