341ede58597662624830e71cb0cf5b0a6ff3a451
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
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.apps.blueprintsprocessor.functions.python.executor.utils
18
19 import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.AbstractComponentFunction
20 import org.python.core.PyObject
21 import org.python.util.PythonInterpreter
22 import org.slf4j.LoggerFactory
23 import java.io.File
24 import java.util.*
25
26 class PythonExecutorUtils {
27     companion object {
28
29         private val log = LoggerFactory.getLogger(PythonExecutorUtils::class.java)
30
31         fun getPythonComponent(executePath: String, pythonPath: MutableList<String>, content: String?, interfaceName: String,
32                                properties: MutableMap<String, Any>): AbstractComponentFunction {
33
34             initPython(executePath, pythonPath, arrayListOf())
35             val pythonInterpreter = PythonInterpreter()
36
37             properties.forEach { (name, value) ->
38                 pythonInterpreter.set(name, value)
39             }
40
41             pythonInterpreter.exec("import sys")
42
43             content?.let {
44                 pythonInterpreter.exec(content)
45             }
46
47             val initCommand = interfaceName.plus(" = ").plus(interfaceName).plus("()")
48             pythonInterpreter.exec(initCommand)
49             val pyObject: PyObject = pythonInterpreter.get(interfaceName)
50
51             log.info("Component Object {}", pyObject)
52
53             return pyObject.__tojava__(AbstractComponentFunction::class.java) as AbstractComponentFunction
54         }
55
56         private fun initPython(executablePath: String,
57                        pythonPath: MutableList<String>, argv: MutableList<String>) {
58
59             val props = Properties()
60             // Build up the python.path
61             val sb = StringBuilder()
62             sb.append(System.getProperty("java.class.path"))
63
64             for (p in pythonPath) {
65                 sb.append(File.pathSeparator).append(p)
66             }
67             log.debug("Python Paths : $sb")
68
69             props["python.import.site"] = "true"
70             props.setProperty("python.path", sb.toString())
71             props.setProperty("python.verbose", "error")
72             props.setProperty("python.executable", executablePath)
73
74             PythonInterpreter.initialize(System.getProperties(), props, argv.toTypedArray())
75         }
76
77     }
78 }