Resolve Bug:445 Remove freemarker from config code generator.
[controller.git] / opendaylight / config / yang-jmx-generator-plugin / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / plugin / gofactory / AbsFactoryGeneratedObjectFactory.groovy
1 /*
2  * Copyright (c) 2013 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 import com.google.common.base.Optional
11 import org.opendaylight.controller.config.api.DependencyResolver
12 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance
13 import org.opendaylight.controller.config.api.ModuleIdentifier
14 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface
15 import org.opendaylight.controller.config.api.annotations.Description
16 import org.opendaylight.controller.config.spi.Module
17 import org.opendaylight.controller.config.spi.ModuleFactory
18 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry
19 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.AbstractFactoryTemplate
20 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.TemplateFactory
21 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Annotation
22 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field
23 import org.opendaylight.controller.config.yangjmxgenerator.plugin.java.*
24 import org.opendaylight.yangtools.yang.common.QName
25 import org.osgi.framework.BundleContext
26
27 public class AbsFactoryGeneratedObjectFactory {
28
29     public GeneratedObject toGeneratedObject(ModuleMXBeanEntry mbe, Optional<String> copyright) {
30         FullyQualifiedName absFactoryFQN = new FullyQualifiedName(mbe.packageName, mbe.abstractFactoryName)
31         FullyQualifiedName moduleFQN = new FullyQualifiedName(mbe.packageName, mbe.stubModuleName)
32         Optional<String> classJavaDoc = Optional.fromNullable(mbe.getNullableDescription())
33
34         AbstractFactoryTemplate abstractFactoryTemplate = TemplateFactory.abstractFactoryTemplateFromMbe(mbe)
35         Optional<String> header = abstractFactoryTemplate.headerString;
36         List<FullyQualifiedName> providedServices = mbe.providedServices.keySet().collect {
37             FullyQualifiedName.fromString(it)
38         }
39
40
41         return toGeneratedObject(absFactoryFQN, copyright,
42                 header, classJavaDoc, mbe.yangModuleQName,
43                 mbe.globallyUniqueName,
44                 providedServices,
45                 moduleFQN,
46                 abstractFactoryTemplate.fields)
47     }
48
49     public GeneratedObject toGeneratedObject(FullyQualifiedName absFactoryFQN, Optional<String> copyright,
50                                              Optional<String> header, Optional<String> classJavaDoc, QName yangModuleQName,
51                                              String globallyUniqueName,
52                                              List<FullyQualifiedName> providedServices,
53                                              FullyQualifiedName moduleFQN,
54                                              List<Field> moduleFields) {
55         JavaFileInputBuilder b = new JavaFileInputBuilder()
56         Annotation moduleQNameAnnotation = Annotation.createModuleQNameANnotation(yangModuleQName)
57         b.addClassAnnotation(moduleQNameAnnotation)
58
59         b.setFqn(absFactoryFQN)
60         b.setTypeName(TypeName.absClassType)
61
62         b.setCopyright(copyright);
63         b.setHeader(header);
64         b.setClassJavaDoc(classJavaDoc);
65         b.addImplementsFQN(new FullyQualifiedName(ModuleFactory))
66         if (classJavaDoc.isPresent()) {
67             b.addClassAnnotation("@${Description.canonicalName}(value=\"${classJavaDoc.get()}\")")
68         }
69
70         b.addToBody("public static final java.lang.String NAME = \"${globallyUniqueName}\";")
71         b.addToBody("private static final java.util.Set<Class<? extends ${AbstractServiceInterface.canonicalName}>> serviceIfcs;")
72
73         b.addToBody("@Override\n public final String getImplementationName() { \n return NAME; \n}")
74
75         b.addToBody(getServiceIfcsInitialization(providedServices))
76
77         // createModule
78         b.addToBody("""
79             @Override
80             public ${Module.canonicalName} createModule(String instanceName, ${DependencyResolver.canonicalName} dependencyResolver, ${BundleContext.canonicalName} bundleContext) {
81                 return instantiateModule(instanceName, dependencyResolver, bundleContext);
82             }
83             """)
84
85         b.addToBody(getCreateModule(moduleFQN, moduleFields))
86
87         b.addToBody("""
88             public ${moduleFQN} instantiateModule(String instanceName, ${DependencyResolver.canonicalName} dependencyResolver, ${moduleFQN} oldModule, ${AutoCloseable.canonicalName} oldInstance, ${BundleContext.canonicalName} bundleContext) {
89                 return new ${moduleFQN}(new ${ModuleIdentifier.canonicalName}(NAME, instanceName), dependencyResolver, oldModule, oldInstance);
90             }
91             """)
92
93         b.addToBody("""
94             public ${moduleFQN} instantiateModule(String instanceName, ${DependencyResolver.canonicalName} dependencyResolver, ${BundleContext.canonicalName} bundleContext) {
95                 return new ${moduleFQN}(new ${ModuleIdentifier.canonicalName}(NAME, instanceName), dependencyResolver);
96             }
97             """)
98
99         b.addToBody("""
100             public ${moduleFQN} handleChangedClass(${DynamicMBeanWithInstance.canonicalName} old) throws Exception {
101                 throw new UnsupportedOperationException("Class reloading is not supported");
102             }
103             """)
104
105         b.addToBody("""
106             @Override
107             public java.util.Set<${moduleFQN}> getDefaultModules(org.opendaylight.controller.config.api.DependencyResolverFactory dependencyResolverFactory, ${BundleContext.canonicalName} bundleContext) {
108                 return new java.util.HashSet<${moduleFQN}>();
109             }
110             """)
111
112         return new GeneratedObjectBuilder(b.build()).toGeneratedObject()
113     }
114
115     private static String getCreateModule(FullyQualifiedName moduleFQN, List<Field> moduleFields) {
116         String result = """
117             @Override
118             public ${Module.canonicalName} createModule(String instanceName, ${DependencyResolver.canonicalName} dependencyResolver, ${DynamicMBeanWithInstance.canonicalName} old, ${BundleContext.canonicalName} bundleContext) throws Exception {
119                 ${moduleFQN} oldModule = null;
120                 try {
121                     oldModule = (${moduleFQN}) old.getModule();
122                 } catch(Exception e) {
123                     return handleChangedClass(old);
124                 }
125                 ${moduleFQN} module = instantiateModule(instanceName, dependencyResolver, oldModule, old.getInstance(), bundleContext);
126             """
127         result += moduleFields.collect{"module.set${it.name}(oldModule.get${it.name}());"}.join("\n")
128         result += """
129                 return module;
130             }
131             """
132         return result
133     }
134
135     private static String getServiceIfcsInitialization(List<FullyQualifiedName> providedServices) {
136         String generic = "Class<? extends ${AbstractServiceInterface.canonicalName}>"
137
138         String result = """static {
139             java.util.Set<${generic}> serviceIfcs2 = new java.util.HashSet<${generic}>();
140             """
141         result += providedServices.collect{"serviceIfcs2.add(${it}.class);"}.join("\n")
142         result += """serviceIfcs = java.util.Collections.unmodifiableSet(serviceIfcs2);
143             }
144             """
145
146         // add isModuleImplementingServiceInterface and getImplementedServiceIntefaces methods
147
148         result += """
149             @Override
150             public final boolean isModuleImplementingServiceInterface(Class<? extends ${AbstractServiceInterface.canonicalName}> serviceInterface) {
151                 for (Class<?> ifc: serviceIfcs) {
152                     if (serviceInterface.isAssignableFrom(ifc)){
153                         return true;
154                     }
155                 }
156                 return false;
157             }
158
159             @Override
160             public java.util.Set<Class<? extends ${AbstractServiceInterface.canonicalName}>> getImplementedServiceIntefaces() {
161                 return serviceIfcs;
162             }
163             """
164
165         return result
166     }
167
168 }

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