/
CustomManagedType.kt
400 lines (327 loc) · 13.2 KB
/
CustomManagedType.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
* Copyright (c) 2018 Juniper Networks, Inc. All rights reserved.
*/
package net.juniper.contrail.vro
import com.vmware.o11n.sdk.modeldriven.extension.Interceptor
import com.vmware.o11n.sdk.modeldrivengen.model.FormalParameter
import com.vmware.o11n.sdk.modeldrivengen.model.ManagedConstructor
import com.vmware.o11n.sdk.modeldrivengen.model.ManagedMethod
import com.vmware.o11n.sdk.modeldrivengen.model.ManagedType
import net.juniper.contrail.api.ApiObjectBase
import net.juniper.contrail.vro.config.ObjectClass
import net.juniper.contrail.vro.config.constants.apiTypesPackageName
import net.juniper.contrail.vro.config.isApiObjectClass
import net.juniper.contrail.vro.config.isNodeClass
import net.juniper.contrail.vro.config.isApiPropertyAsObject
import net.juniper.contrail.vro.config.isApiPropertyClass
import net.juniper.contrail.vro.config.isGetter
import net.juniper.contrail.vro.config.isHiddenProperty
import net.juniper.contrail.vro.config.isInternal
import net.juniper.contrail.vro.config.isInventoryProperty
import net.juniper.contrail.vro.config.isModelClass
import net.juniper.contrail.vro.config.isPublic
import net.juniper.contrail.vro.config.isRootClass
import net.juniper.contrail.vro.config.isSubclassOf
import net.juniper.contrail.vro.config.modelClasses
import net.juniper.contrail.vro.config.parameterClass
import net.juniper.contrail.vro.config.pluginName
import net.juniper.contrail.vro.config.returnsApiPropertyOrList
import net.juniper.contrail.vro.config.returnsObjectReferences
import net.juniper.contrail.vro.config.setParentMethodsInModel
import net.juniper.contrail.vro.config.toPluginName
import net.juniper.contrail.vro.model.Connection
import net.juniper.contrail.vro.model.Executor
import java.lang.reflect.Method
import java.lang.reflect.Parameter
private val listClass = List::class.java
private val executorTypes = Executor::class.java.declaredMethods.asSequence()
.filter { it.isPublic }
// only methods with at least one parameter should be in executor
.groupBy { it.parameterTypes[0] }
private fun ManagedType.createExecutorMethods(): List<ManagedMethod> {
if (!modelClass.isApiObjectClass) return emptyList()
val methods = executorTypes[modelClass] ?: emptyList()
return methods.map { it.toExecutorMethod() }
}
private fun Method.toExecutorMethod() = ManagedMethod().apply {
val methodName = this@toExecutorMethod.name
setName(methodName, null)
params = executorMethodParameters()
// trick to avoid generating standard wrapper method
setIsInheritedWrapperMethod(true)
isPropertyReadOnly = true
returns = executorFormalParameter(returnType, "_result", genericReturnType.parameterClass)
}
private fun Method.executorMethodParameters() =
parameters.asSequence().drop(1)
.map { it.toExecutorFormalParameter() }
.toList()
private fun Parameter.toExecutorFormalParameter() =
executorFormalParameter(type, name, parameterizedType.parameterClass)
/**
* Formal parameter created by this function has the following content:
* typeName - always not null, type of plugin class
* componentTypeName - not null if parameter is wrapped model class
* componentClassName - not null if parameter is list
*/
private fun executorFormalParameter(clazz: Class<*>, parameterName: String, component: Class<*>?) : FormalParameter {
val parameter = commonFormalParameter(clazz, parameterName)
return when {
listClass.isAssignableFrom(clazz) -> listParamConfig(parameter, component!!)
clazz.isModelClass -> clazz.modelParamConfig(parameter)
else -> clazz.simpleParamConfig(parameter)
}
}
private fun commonFormalParameter(clazz: Class<*>, parameterName: String) = FormalParameter().apply {
name = parameterName
modelType = clazz
isWrapped = clazz.isModelClass
}
private fun listParamConfig(parameter: FormalParameter, component: Class<*>) = parameter.apply {
componentClassName = component.canonicalName
val className = component.simpleName
fullClassName = "List<$className>"
if (component.isModelClass) {
typeName = "List<${className}_Wrapper>"
componentTypeName = component.pluginName
} else {
typeName = "List<$className>"
}
}
private fun Class<*>.modelParamConfig(parameter: FormalParameter) = parameter.apply {
fullClassName = canonicalName
typeName = "${simpleName}_Wrapper"
componentTypeName = pluginName
}
private fun Class<*>.simpleParamConfig(parameter: FormalParameter) = parameter.apply {
fullClassName = canonicalName
typeName = canonicalName
}
class ClassInfo(val simpleName: String) {
val pluginName = simpleName.toPluginName
}
class CustomManagedType(private val delegate: ManagedType) : ManagedType() {
val isObjectClass get() =
delegate.modelClass?.isApiObjectClass ?: false
val isConnectionClass get() =
delegate.modelClass == Connection::class.java
val isNodeClass get() =
delegate.modelClass?.isNodeClass ?: false
val isConnectionChild get() =
delegate.modelClass?.let {
if (it.isSubclassOf<ApiObjectBase>()) {
@Suppress("UNCHECKED_CAST")
it as ObjectClass
it.isRootClass || it.isInternal
} else {
false
}
} ?: false
val pluginName = delegate.modelClass?.pluginName
val parents = if (isConnectionChild)
listOf(Connection::class.java)
else
delegate.modelClass?.setParentMethodsInModel?.map { it.parameterTypes[0] }?.toList() ?: emptyList()
val executorMethods = delegate.createExecutorMethods()
val references: List<CustomReference> = delegate.modelClass?.run {
methods.asSequence()
.map { it.toCustomReference() }.filterNotNull()
.toList()
} ?: emptyList()
val referenceProperties: List<CustomReferenceProperty> = delegate.modelClass?.run {
if (isApiObjectClass)
methods.asSequence()
.filter { it.returnsObjectReferences }
.filter { it.isReferenceProperty }
.filter { it.referencePropertyClass.isModelClass }
.map { it.toCustomReferenceProperty() }
.toList()
else
null
} ?: emptyList()
val connectionFindClasses: List<ClassInfo> = if (isConnectionClass) {
modelClasses.map { ClassInfo(it) }
} else {
emptyList()
}
val customProperties: List<AdditionalProperty> = delegate.modelClass?.run {
if (isApiPropertyAsObject)
propertyAsObjectNewProperties
else
null
} ?: emptyList()
val propertyViews: List<CustomProperty> = delegate.modelClass?.run {
if (isApiObjectClass)
methods.asSequence()
.filter { it.isGetter }
.filter { it.returnsApiPropertyOrList }
.filter { ! it.returnType.isInventoryProperty }
.map { it.toCustomProperty() }
.filter { ! it.propertyName.isHiddenProperty }
.toList()
else
null
} ?: emptyList()
init {
generateDescription()
removeDuplicateMethods()
generateReferenceMethods()
generatePropertyMethods()
generateReferencePropertiesMethods()
generateCustomProperties()
}
private fun generateDescription() {
delegate.doc = delegate.modelClass?.description
}
private fun removeDuplicateMethods() {
delegate.methods.asSequence()
.filter { it.isPropertyEditor }
.groupBy { it.name }
.filter { it.value.size > 1 }
.values
.forEach { it.removeDuplicates() }
}
private fun List<ManagedMethod>.removeDuplicates() {
val remaining = methodToRetain
asSequence()
.filter { it != remaining }
.forEach { delegate.methods.remove(it) }
}
private val List<ManagedMethod>.methodToRetain get() =
first { it.params.size == 1 && it.params[0].modelType.isApiPropertyClass }
private val ManagedMethod.isPropertyEditor get() =
returns.modelType == Void.TYPE && params.let {
it.size >= 1 && ! it[0].modelType.isApiObjectClass
}
private fun generateReferenceMethods() =
references.forEach { methods.add(it.toManagedMethod()) }
private fun generateCustomProperties() =
customProperties.forEach { methods.add(it.toManagedMethod()) }
private fun generateReferencePropertiesMethods() =
referenceProperties.forEach { methods.add(it.toManagedMethod()) }
private fun generatePropertyMethods() =
propertyViews.forEach { methods.add(it.toManagedMethod()) }
private fun CustomReference.toManagedMethod() = ManagedMethod().apply {
setName(methodName, methodName)
params = emptyList()
// trick to avoid generating standard wrapper method
setIsInheritedWrapperMethod(true)
isPropertyReadOnly = true
returns = collectionReturnFormalParameter()
}
private fun CustomReferenceProperty.toManagedMethod() = ManagedMethod().apply {
val originalProperty = this@toManagedMethod.propertyName
setName(wrapperMethodName, wrapperMethodName)
originalPropertyName = originalProperty
propertyName = originalProperty
params = emptyList()
setIsInheritedWrapperMethod(true)
isPropertyReadOnly = true
returns = stringReturnFormalParameter()
}
private fun CustomProperty.toManagedMethod() = ManagedMethod().apply {
setName(viewMethodName, viewMethodName)
propertyName = viewPropertyName
originalPropertyName = viewPropertyName
params = emptyList()
setIsInheritedWrapperMethod(true)
isPropertyReadOnly = true
returns = stringReturnFormalParameter()
}
private fun CustomReference.collectionReturnFormalParameter() = FormalParameter().apply {
name = "_result"
modelType = List::class.java
fullClassName = List::class.java.name
typeName = "[Contrail$className"
componentTypeName = "Contrail$className"
componentClassName = "$apiTypesPackageName.$className"
isWrapped = true
}
private fun stringReturnFormalParameter() = FormalParameter().apply {
name = "_result"
modelType = String::class.java
fullClassName = String::class.java.name
typeName = "String"
isWrapped = false
}
private fun AdditionalProperty.toManagedMethod() = ManagedMethod().apply {
setName(methodName, methodName)
propertyName = this@toManagedMethod.propertyName
originalPropertyName = this@toManagedMethod.propertyName
params = emptyList()
setIsInheritedWrapperMethod(true)
isPropertyReadOnly = true
returns = stringReturnFormalParameter()
}
override fun isGenerate(): Boolean =
delegate.isGenerate
override fun setGenerate(generate: Boolean) {
delegate.isGenerate = generate
}
override fun isFindable(): Boolean
= delegate.isFindable
override fun setFindable(findable: Boolean) {
delegate.isFindable = findable
}
override fun isRootIdPropagated(): Boolean =
delegate.isRootIdPropagated
override fun setRootIdPropagated(rootIdPropagated: Boolean) {
delegate.isRootIdPropagated = rootIdPropagated
}
override fun getFullSuperClassName(): String? =
delegate.fullSuperClassName
override fun setFullSuperClassName(fullSuperClassName: String) {
delegate.fullSuperClassName = fullSuperClassName
}
override fun getPackageName(): String? =
delegate.packageName
override fun getClassName(): String? =
delegate.className
override fun getName(): String? =
delegate.name
override fun isSingleton(): Boolean =
delegate.isSingleton
override fun setSingleton(singeton: Boolean) {
delegate.isSingleton = singeton
}
override fun isCreateable(): Boolean =
delegate.isCreateable
override fun setCreateable(createable: Boolean) {
delegate.isCreateable = createable
}
override fun getFullClassName(): String? =
delegate.fullClassName
override fun setFullClassName(className: String) {
delegate.fullClassName = className
}
override fun getMethods(): MutableList<ManagedMethod> =
delegate.methods
override fun setMethods(methods: List<ManagedMethod>) {
delegate.methods = methods
}
override fun getDoc(): String? =
delegate.doc
override fun setDoc(doc: String) {
delegate.doc = doc
}
override fun getConstructors(): List<ManagedConstructor?>? =
delegate.constructors
override fun setConstructors(constructors: List<ManagedConstructor>) {
delegate.constructors = constructors
}
override fun toString(): String =
delegate.toString()
override fun setName(name: String) {
delegate.name = name
}
override fun getModelClass(): Class<*>? =
delegate.modelClass
override fun setModelClass(wrappedClass: Class<*>) {
delegate.modelClass = wrappedClass
}
override fun getInterceptor(): Class<out Interceptor>? =
delegate.interceptor
override fun setInterceptor(interceptorClass: Class<out Interceptor>) {
delegate.interceptor = interceptorClass
}
}