Refactor Subnet.isSubnetOf - reduce number of 'if' statements.
[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} identifier;
19     <#if runtime=true>
20     private ${registratorType} rootRuntimeBeanRegistratorWrapper;
21     </#if>
22
23     public ${typeDeclaration.name}(${moduleNameType} identifier, ${dependencyResolverType} dependencyResolver) {
24         this.identifier = identifier;
25         this.dependencyResolver = dependencyResolver;
26         this.oldInstance = null;
27         this.oldModule = null;
28     }
29
30     public ${typeDeclaration.name}(${moduleNameType} identifier, ${dependencyResolverType} dependencyResolver, ${typeDeclaration.name} oldModule, ${instanceType} oldInstance) {
31         this.identifier = identifier;
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         <#if field.type?starts_with("java.util.List")>
56         for(javax.management.ObjectName dep : ${field.name}) {
57             dependencyResolver.validateDependency(${field.dependency.sie.fullyQualifiedName}.class, dep, ${field.name}JmxAttribute);
58         }
59         <#else>
60         dependencyResolver.validateDependency(${field.dependency.sie.fullyQualifiedName}.class, ${field.name}, ${field.name}JmxAttribute);
61         </#if>
62         </#if>
63     </#list>
64         customValidation();
65     }
66
67     protected void customValidation(){
68
69     }
70
71     // caches of resolved dependencies
72     <#list moduleFields as field>
73     <#if field.dependent==true>
74         <#if field.type?starts_with("java.util.List")>
75         private java.util.List<${field.dependency.sie.exportedOsgiClassName}> ${field.name}Dependency = new java.util.ArrayList<${field.dependency.sie.exportedOsgiClassName}>();
76         protected final java.util.List<${field.dependency.sie.exportedOsgiClassName}> get${field.attributeName}Dependency(){
77             return ${field.name}Dependency;
78         }
79         <#else>
80         private ${field.dependency.sie.exportedOsgiClassName} ${field.name}Dependency;
81         protected final ${field.dependency.sie.exportedOsgiClassName} get${field.attributeName}Dependency(){
82             return ${field.name}Dependency;
83         }
84         </#if>
85     </#if>
86     </#list>
87
88
89     @Override
90     public final ${instanceType} getInstance(){
91         if(instance==null) {
92
93             <#list moduleFields as field>
94                 <#if field.dependent==true>
95                     <#if field.dependency.mandatory==false>
96                         if(${field.name}!=null) {
97                     </#if>
98
99                     <#if field.type?starts_with("java.util.List")>
100             ${field.name}Dependency = new java.util.ArrayList<${field.dependency.sie.exportedOsgiClassName}>();
101             for(javax.management.ObjectName dep : ${field.name}) {
102                 ${field.name}Dependency.add(dependencyResolver.resolveInstance(${field.dependency.sie.exportedOsgiClassName}.class, dep, ${field.name}JmxAttribute));
103             }
104                     <#else>
105             ${field.name}Dependency = dependencyResolver.resolveInstance(${field.dependency.sie.exportedOsgiClassName}.class, ${field.name}, ${field.name}JmxAttribute);
106                     </#if>
107
108                     <#if field.dependency.mandatory==false>
109                         }
110                     </#if>
111                 </#if>
112             </#list>
113
114             if(oldInstance!=null && canReuseInstance(oldModule)) {
115                 instance = reuseInstance(oldInstance);
116             } else {
117                 if(oldInstance!=null) {
118                     try {
119                         oldInstance.close();
120                     } catch(Exception e) {
121                         logger.error("An error occurred while closing old instance " + oldInstance, e);
122                     }
123                 }
124                 instance = createInstance();
125             }
126         }
127         return instance;
128     }
129
130     @Override
131     public ${moduleNameType} getIdentifier() {
132         return identifier;
133     }
134
135     public boolean canReuseInstance(${typeDeclaration.name} oldModule){
136         // allow reusing of old instance if no parameters was changed
137         return isSame(oldModule);
138     }
139
140     public ${instanceType} reuseInstance(${instanceType} oldInstance){
141         // implement if instance reuse should be supported. Override canReuseInstance to change the criteria.
142         return oldInstance;
143     }
144
145     public abstract ${instanceType} createInstance();
146
147     public boolean isSame(${typeDeclaration.name} other) {
148         if (other == null) {
149             throw new IllegalArgumentException("Parameter 'other' is null");
150         }
151         <#list moduleFields as field>
152         <#if field.dependent==true>
153         if (${field.name}Dependency == null) {
154             if (other.${field.name}Dependency != null)
155                 return false;
156         } else if (!${field.name}Dependency.equals(other.${field.name}Dependency)) {
157             return false;
158         }
159         <#else>
160         if (${field.name} == null) {
161             if (other.${field.name} != null) {
162                 return false;
163             }
164         } else if
165             <#if field.array == false>
166                 (${field.name}.equals(other.${field.name}) == false)
167             <#else>
168                 (java.util.Arrays.equals(${field.name},other.${field.name}) == false)
169             </#if>
170                  {
171             return false;
172         }
173         </#if>
174         </#list>
175
176         return true;
177     }
178
179     @Override
180     public boolean equals(Object o) {
181         if (this == o) return true;
182         if (o == null || getClass() != o.getClass()) return false;
183
184         ${typeDeclaration.name} that = (${typeDeclaration.name}) o;
185
186         return identifier.equals(that.identifier);
187     }
188
189     @Override
190     public int hashCode() {
191         return identifier.hashCode();
192     }
193 }