Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove the need to explicitly include custom workflows and actions.
Change-Id: I914b3f16bba8ad261214164c57c9644299856f98 Closes-Bug: #1770567
- Loading branch information
1 parent
993487b
commit aa876a6
Showing
8 changed files
with
108 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...in-contrail-workflows/src/main/kotlin/net/juniper/contrail/vro/workflows/custom/Custom.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright (c) 2018 Juniper Networks, Inc. All rights reserved. | ||
*/ | ||
|
||
@file:JvmName("Custom") | ||
|
||
package net.juniper.contrail.vro.workflows.custom | ||
|
||
import net.juniper.contrail.vro.workflows.dsl.WorkflowDefinition | ||
import net.juniper.contrail.vro.schema.Schema | ||
import net.juniper.contrail.vro.workflows.model.Action | ||
|
||
fun loadCustomWorkflows(schema: Schema): List<WorkflowDefinition> = | ||
WorkflowLoader.load(schema).toList() | ||
|
||
fun loadCustomActions(version: String, packageName: String): List<Action> = | ||
ActionLoader.load().map { it(version, packageName) }.toList() |
42 changes: 0 additions & 42 deletions
42
...rail-workflows/src/main/kotlin/net/juniper/contrail/vro/workflows/custom/CustomActions.kt
This file was deleted.
Oops, something went wrong.
34 changes: 0 additions & 34 deletions
34
...il-workflows/src/main/kotlin/net/juniper/contrail/vro/workflows/custom/CustomWorkflows.kt
This file was deleted.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
...n-contrail-workflows/src/main/kotlin/net/juniper/contrail/vro/workflows/custom/Loaders.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (c) 2018 Juniper Networks, Inc. All rights reserved. | ||
*/ | ||
|
||
package net.juniper.contrail.vro.workflows.custom | ||
|
||
import net.juniper.contrail.vro.config.classesIn | ||
import net.juniper.contrail.vro.config.isStatic | ||
import net.juniper.contrail.vro.schema.Schema | ||
import net.juniper.contrail.vro.workflows.dsl.WorkflowDefinition | ||
import java.lang.reflect.Method | ||
|
||
private val workflowScriptsDirectory = "workflows" | ||
private val actionScriptsDirectory = "actions" | ||
|
||
object ScriptLoader { | ||
private fun load(path: String): String = | ||
ScriptLoader::class.java | ||
.getResourceAsStream(path) | ||
.bufferedReader() | ||
.use { it.readText() } | ||
|
||
fun loadActionScript(name: String): String = | ||
load("/$actionScriptsDirectory/$name.js") | ||
|
||
fun loadWorkflowScript(name: String): String = | ||
load("/$workflowScriptsDirectory/$name.js") | ||
} | ||
|
||
object WorkflowLoader { | ||
fun load(schema: Schema): Sequence<WorkflowDefinition> = | ||
classesIn(WorkflowLoader::class.java.`package`.name) | ||
.filter { it.hasWorkflowDefinitions } | ||
.flatMap { it.workflowDefinitions(schema) } | ||
} | ||
|
||
object ActionLoader { | ||
fun load(): Sequence<ActionDefinition> = | ||
classesIn(ActionLoader::class.java.`package`.name) | ||
.filter { it.hasActionDefinitions } | ||
.flatMap { it.actionDefinitions() } | ||
} | ||
|
||
private val Class<*>.hasWorkflowDefinitions get() = | ||
declaredMethods.any { it.definesWorkflow } | ||
|
||
private val Class<*>.hasActionDefinitions get() = | ||
declaredMethods.any { it.definesAction } | ||
|
||
private fun Class<*>.workflowDefinitions(schema: Schema) = | ||
declaredMethods.asSequence() | ||
.filter { it.definesWorkflow } | ||
.map { it.createWorkflowDefinition(schema) } | ||
|
||
private fun Class<*>.actionDefinitions() = | ||
declaredMethods.asSequence() | ||
.filter { it.definesAction } | ||
.map { it.createActionDefinition() } | ||
|
||
private fun Method.createWorkflowDefinition(schema: Schema): WorkflowDefinition = when { | ||
takesNoParameters -> invoke(declaringClass) | ||
takesOnlySchema -> invoke(declaringClass, schema) | ||
else -> throw IllegalArgumentException("Cannot create workflow definition using method '$this' of class '$declaringClass'.") | ||
} as WorkflowDefinition | ||
|
||
private fun Method.createActionDefinition(): ActionDefinition = when { | ||
takesNoParameters -> invoke(declaringClass) | ||
else -> throw IllegalArgumentException("Cannot create action definition using method '$this' of class '$declaringClass'.") | ||
} as ActionDefinition | ||
|
||
private val Method.definesWorkflow get() = | ||
isStatic && returnType == WorkflowDefinition::class.java && takesWorkflowDefinitionParameters | ||
|
||
private val Method.takesWorkflowDefinitionParameters get() = | ||
takesNoParameters || takesOnlySchema | ||
|
||
private val Method.takesNoParameters get() = | ||
parameterCount == 0 | ||
|
||
private val Method.takesOnlySchema get() = | ||
parameterCount == 1 && parameters[0].type == Schema::class.java | ||
|
||
private val Method.definesAction get() = | ||
isStatic && returnType == ActionDefinition::class.java && takesNoParameters | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters