Fixed add/delete/modify RPC for group/flow/remove reach the test provider
[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         dependencyResolver.validateDependency(${field.dependency.sie.fullyQualifiedName}.class, ${field.name}, ${field.name}JmxAttribute);
56         </#if>
57     </#list>
58         customValidation();
59     }
60
61     protected void customValidation(){
62
63     }
64
65     // caches of resolved dependencies
66     <#list moduleFields as field>
67     <#if field.dependent==true>
68         private ${field.dependency.sie.exportedOsgiClassName} ${field.name}Dependency;
69         protected final ${field.dependency.sie.exportedOsgiClassName} get${field.attributeName}Dependency(){
70             return ${field.name}Dependency;
71         }
72     </#if>
73     </#list>
74
75
76     @Override
77     public final ${instanceType} getInstance(){
78         if(instance==null) {
79
80             <#list moduleFields as field>
81                 <#if field.dependent==true>
82
83                     <#if field.dependency.mandatory==false>
84                         if(${field.name}!=null) {
85                     </#if>
86
87                     ${field.name}Dependency = dependencyResolver.resolveInstance(${field.dependency.sie.exportedOsgiClassName}.class, ${field.name}, ${field.name}JmxAttribute);
88
89                     <#if field.dependency.mandatory==false>
90                         }
91                     </#if>
92                 </#if>
93             </#list>
94
95             if(oldInstance!=null && canReuseInstance(oldModule)) {
96                 instance = reuseInstance(oldInstance);
97             } else {
98                 if(oldInstance!=null) {
99                     try {
100                         oldInstance.close();
101                     } catch(Exception e) {
102                         logger.error("An error occurred while closing old instance " + oldInstance, e);
103                     }
104                 }
105                 instance = createInstance();
106             }
107         }
108         return instance;
109     }
110
111     @Override
112     public ${moduleNameType} getIdentifier() {
113         return identifier;
114     }
115
116     public boolean canReuseInstance(${typeDeclaration.name} oldModule){
117         // allow reusing of old instance if no parameters was changed
118         return isSame(oldModule);
119     }
120
121     public ${instanceType} reuseInstance(${instanceType} oldInstance){
122         // implement if instance reuse should be supported. Override canReuseInstance to change the criteria.
123         return oldInstance;
124     }
125
126     public abstract ${instanceType} createInstance();
127
128     public boolean isSame(${typeDeclaration.name} other) {
129         if (other == null) {
130             throw new IllegalArgumentException("Parameter 'other' is null");
131         }
132         <#list moduleFields as field>
133         <#if field.dependent==true>
134         if (${field.name}Dependency == null) {
135             if (other.${field.name}Dependency != null)
136                 return false;
137         } else if (!${field.name}Dependency.equals(other.${field.name}Dependency)) {
138             return false;
139         }
140         <#else>
141         if (${field.name} == null) {
142             if (other.${field.name} != null) {
143                 return false;
144             }
145         } else if
146             <#if field.array == false>
147                 (${field.name}.equals(other.${field.name}) == false)
148             <#else>
149                 (java.util.Arrays.equals(${field.name},other.${field.name}) == false)
150             </#if>
151                  {
152             return false;
153         }
154         </#if>
155         </#list>
156
157         return true;
158     }
159
160     @Override
161     public boolean equals(Object o) {
162         if (this == o) return true;
163         if (o == null || getClass() != o.getClass()) return false;
164
165         ${typeDeclaration.name} that = (${typeDeclaration.name}) o;
166
167         return identifier.equals(that.identifier);
168     }
169
170     @Override
171     public int hashCode() {
172         return identifier.hashCode();
173     }
174 }