Simplify script compilation implementation.
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / scripts / BluePrintCompileService.kt
1 /*
2  * Copyright © 2019 IBM.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.controllerblueprints.core.scripts
18
19 import kotlinx.coroutines.async
20 import kotlinx.coroutines.coroutineScope
21 import org.jetbrains.kotlin.cli.common.ExitCode
22 import org.jetbrains.kotlin.cli.common.arguments.parseCommandLineArguments
23 import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
24 import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
25 import org.jetbrains.kotlin.cli.common.messages.MessageCollector
26 import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
27 import org.jetbrains.kotlin.config.Services
28 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
29 import org.onap.ccsdk.cds.controllerblueprints.core.checkFileExists
30 import org.onap.ccsdk.cds.controllerblueprints.core.logger
31 import java.io.File
32 import java.net.URLClassLoader
33 import java.util.*
34 import kotlin.script.experimental.api.SourceCode
35 import kotlin.script.experimental.jvm.util.classpathFromClasspathProperty
36 import kotlin.system.measureTimeMillis
37
38
39 open class BluePrintCompileService {
40     val log = logger(BluePrintCompileService::class)
41
42     companion object {
43         val classPaths = classpathFromClasspathProperty()?.joinToString(File.pathSeparator) {
44             it.absolutePath
45         }
46     }
47
48     /** Compile the [bluePrintSourceCode] and get the [kClassName] instance for the constructor [args] */
49     suspend fun <T> eval(bluePrintSourceCode: BluePrintSourceCode, kClassName: String,
50                          args: ArrayList<Any?>?): T {
51         /** Compile the source code */
52         compile(bluePrintSourceCode)
53         /** Get the class loader with compiled jar */
54         val classLoaderWithDependencies = BluePrintCompileCache.classLoader(bluePrintSourceCode.cacheKey)
55         /* Create the instance from the class loader */
56         return instance(classLoaderWithDependencies, kClassName, args)
57     }
58
59     /** Compile [bluePrintSourceCode] and put into cache */
60     suspend fun compile(bluePrintSourceCode: BluePrintSourceCode) {
61         //TODO("Include Multiple folders")
62         val sourcePath = bluePrintSourceCode.blueprintKotlinSources.first()
63         val compiledJarFile = bluePrintSourceCode.targetJarFile
64
65         /** Check cache is present for the blueprint scripts */
66         val hasCompiledCache = BluePrintCompileCache.hasClassLoader(bluePrintSourceCode.cacheKey)
67
68         log.debug("Jar Exists : ${compiledJarFile.exists()}, Regenerate : ${bluePrintSourceCode.regenerate}," +
69                 " Compiled hash(${bluePrintSourceCode.cacheKey}) : $hasCompiledCache")
70
71         if (!compiledJarFile.exists() || bluePrintSourceCode.regenerate || !hasCompiledCache) {
72             log.info("compiling for cache key(${bluePrintSourceCode.cacheKey})")
73             coroutineScope {
74                 val timeTaken = measureTimeMillis {
75                     /** Create compile arguments */
76                     val args = mutableListOf<String>().apply {
77                         add("-no-stdlib")
78                         add("-no-reflect")
79                         add("-module-name")
80                         add(bluePrintSourceCode.moduleName)
81                         add("-cp")
82                         add(classPaths!!)
83                         add(sourcePath)
84                         add("-d")
85                         add(compiledJarFile.absolutePath)
86                     }
87                     val deferredCompile = async {
88                         val k2jvmCompiler = K2JVMCompiler()
89                         /** Construct Arguments */
90                         val arguments = k2jvmCompiler.createArguments()
91                         parseCommandLineArguments(args, arguments)
92                         val messageCollector = CompilationMessageCollector()
93                         /** Compile with arguments */
94                         val exitCode: ExitCode = k2jvmCompiler.exec(messageCollector, Services.EMPTY, arguments)
95                         when (exitCode) {
96                             ExitCode.OK -> {
97                                 checkFileExists(compiledJarFile)
98                                 { "couldn't generate compiled jar(${compiledJarFile.absolutePath})" }
99                             }
100                             else -> {
101                                 throw BluePrintException("$exitCode :${messageCollector.errors().joinToString("\n")}")
102                             }
103                         }
104                     }
105                     deferredCompile.await()
106                 }
107                 log.info("compiled in ($timeTaken)mSec for cache key(${bluePrintSourceCode.cacheKey})")
108             }
109         }
110     }
111
112     /** create class [kClassName] instance from [classLoader] */
113     fun <T> instance(classLoader: URLClassLoader, kClassName: String, args: ArrayList<Any?>? = arrayListOf()): T {
114         val kClazz = classLoader.loadClass(kClassName)
115                 ?: throw BluePrintException("failed to load class($kClassName) from current class loader.")
116
117         val instance = if (args.isNullOrEmpty()) {
118             kClazz.newInstance()
119         } else {
120             kClazz.constructors
121                     .single().newInstance(*args.toArray())
122         } ?: throw BluePrintException("failed to create class($kClassName) instance for constructor argument($args).")
123         return instance as T
124     }
125 }
126
127 /** Compile source code information */
128 open class BluePrintSourceCode : SourceCode {
129     lateinit var blueprintKotlinSources: MutableList<String>
130     lateinit var moduleName: String
131     lateinit var targetJarFile: File
132     lateinit var cacheKey: String
133     var regenerate: Boolean = false
134
135     override val text: String
136         get() = ""
137
138     override val locationId: String? = null
139
140     override val name: String?
141         get() = moduleName
142 }
143
144 /** Class to collect compilation Data */
145 data class CompiledMessageData(
146         val severity: CompilerMessageSeverity,
147         val message: String,
148         val location: CompilerMessageLocation?
149 )
150
151 /** Class to collect compilation results */
152 class CompilationMessageCollector : MessageCollector {
153
154     private val compiledMessages: MutableList<CompiledMessageData> = arrayListOf()
155
156     override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation?) {
157         synchronized(compiledMessages) {
158             compiledMessages.add(CompiledMessageData(severity, message, location))
159         }
160     }
161
162     override fun hasErrors() =
163             synchronized(compiledMessages) {
164                 compiledMessages.any { it.severity.isError }
165             }
166
167     override fun clear() {
168         synchronized(compiledMessages) {
169             compiledMessages.clear()
170         }
171     }
172
173     fun errors(): List<CompiledMessageData> = compiledMessages.filter { it.severity == CompilerMessageSeverity.ERROR }
174 }