90412d7bb5dd718afb5527bfc8ae35005a47c83f
[controller.git] / opendaylight / config / yang-jmx-generator-plugin / src / main / resources / freeMarker / module_abs_template_new.ftl
1 <@headerD header=header/>
2 package ${packageName};
3
4 <@javadocD object=javadoc/>
5 <@annotationsD object=annotations/>
6 <@typeDeclarationD object=typeDeclaration/>
7 {
8     // attributes
9     <@moduleFieldsD moduleFields=moduleFields/>
10     //attributes end
11
12     private static final ${loggerType} logger = ${loggerFactoryType}.getLogger(${typeDeclaration.name}.class);
13
14     private final ${typeDeclaration.name} oldModule;
15     private final ${instanceType} oldInstance;
16     private ${instanceType} instance;
17     private final ${dependencyResolverType} dependencyResolver;
18     private final ${moduleNameType} name;
19     <#if runtime=true>
20     private ${registratorType} rootRuntimeBeanRegistratorWrapper;
21     </#if>
22
23     public ${typeDeclaration.name}(${moduleNameType} name, ${dependencyResolverType} dependencyResolver) {
24         this.name = name;
25         this.dependencyResolver = dependencyResolver;
26         this.oldInstance = null;
27         this.oldModule = null;
28     }
29
30     public ${typeDeclaration.name}(${moduleNameType} name, ${dependencyResolverType} dependencyResolver, ${typeDeclaration.name} oldModule, ${instanceType} oldInstance) {
31         this.name = name;
32         this.dependencyResolver = dependencyResolver;
33         this.oldInstance = oldInstance;
34         this.oldModule = oldModule;
35     }
36
37     // getters and setters exported into MXBean
38     <@methodsD object=methods/>
39
40     <#if runtime=true>
41     public ${registratorType} getRootRuntimeBeanRegistratorWrapper(){
42         return rootRuntimeBeanRegistratorWrapper;
43     }
44
45     @Override
46     public void setRuntimeBeanRegistrator(${rootRuntimeRegistratorType} rootRuntimeRegistrator){
47         this.rootRuntimeBeanRegistratorWrapper = new ${registratorType}(rootRuntimeRegistrator);
48     }
49     </#if>
50
51     @Override
52     public void validate(){
53     <#list moduleFields as field>
54         <#if field.dependent==true && field.dependency.mandatory==true>
55         dependencyResolver.validateDependency(${field.dependency.sie.fullyQualifiedName}.class, ${field.name}, ${field.name}JmxAttribute);
56         </#if>
57     </#list>
58     }
59
60     // caches of resolved dependencies
61     <#list moduleFields as field>
62     <#if field.dependent==true>
63         private ${field.dependency.sie.exportedOsgiClassName} ${field.name}Dependency;
64         protected final ${field.dependency.sie.exportedOsgiClassName} get${field.attributeName}Dependency(){
65             return ${field.name}Dependency;
66         }
67     </#if>
68     </#list>
69
70
71     @Override
72     public final ${instanceType} getInstance(){
73         if(instance==null) {
74
75             <#list moduleFields as field>
76                 <#if field.dependent==true>
77
78                     <#if field.dependency.mandatory==false>
79                         if(${field.name}!=null) {
80                     </#if>
81
82                     ${field.name}Dependency = dependencyResolver.resolveInstance(${field.dependency.sie.exportedOsgiClassName}.class, ${field.name}, ${field.name}JmxAttribute);
83
84                     <#if field.dependency.mandatory==false>
85                         }
86                     </#if>
87                 </#if>
88             </#list>
89
90             if(oldInstance!=null && canReuseInstance(oldModule)) {
91                 instance = reuseInstance(oldInstance);
92             } else {
93                 if(oldInstance!=null) {
94                     try {
95                         oldInstance.close();
96                     } catch(Exception e) {
97                         logger.error("An error occurred while closing old instance " + oldInstance, e);
98                     }
99                 }
100                 instance = createInstance();
101             }
102         }
103         return instance;
104     }
105
106     @Override
107     public final ${moduleNameType} getName() {
108         return name;
109     }
110
111     public boolean canReuseInstance(${typeDeclaration.name} oldModule){
112         // allow reusing of old instance if no parameters was changed
113         return equals(oldModule);
114     }
115
116     public ${instanceType} reuseInstance(${instanceType} oldInstance){
117         // implement if instance reuse should be supported. Override canReuseInstance to change the criteria.
118         return oldInstance;
119     }
120
121     public abstract ${instanceType} createInstance();
122
123     @Override
124     public boolean equals(Object obj) {
125         if (this == obj)
126             return true;
127         if (obj == null)
128             return false;
129         if (getClass() != obj.getClass())
130             return false;
131         ${typeDeclaration.name} other = (${typeDeclaration.name}) obj;
132
133
134         <#list moduleFields as field>
135         <#if field.dependent==true>
136         if (${field.name}Dependency == null) {
137             if (other.${field.name}Dependency != null)
138                 return false;
139         } else if (!${field.name}Dependency.equals(other.${field.name}Dependency))
140             return false;
141         <#else>
142         if (${field.name} == null) {
143             if (other.${field.name} != null)
144                 return false;
145         } else if (!${field.name}.equals(other.${field.name}))
146             return false;
147         </#if>
148         </#list>
149
150         return true;
151     }
152
153 }