66c919d4d24990b74326962957ea3781316ca90a
[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.util.*
24
25 class PythonExecutorUtils {
26     companion object {
27
28         private val log = LoggerFactory.getLogger(PythonExecutorUtils::class.java)
29
30         fun getPythonComponent(executePath: String, pythonPath: MutableList<String>, content: String?, interfaceName: String,
31                                properties: MutableMap<String, Any>): AbstractComponentFunction {
32
33             initPython(executePath, pythonPath, arrayListOf())
34             val pythonInterpreter = PythonInterpreter()
35
36             properties.forEach { (name, value) ->
37                 pythonInterpreter.set(name, value)
38             }
39
40             pythonInterpreter.exec("import sys")
41
42             content?.let {
43                 pythonInterpreter.exec(content)
44             }
45
46             val initCommand = interfaceName.plus(" = ").plus(interfaceName).plus("()")
47             pythonInterpreter.exec(initCommand)
48             val pyObject: PyObject = pythonInterpreter.get(interfaceName)
49
50             log.info("Component Object {}", pyObject)
51
52             return pyObject.__tojava__(AbstractComponentFunction::class.java) as AbstractComponentFunction
53         }
54
55         private fun initPython(executablePath: String,
56                        pythonPath: MutableList<String>, argv: MutableList<String>) {
57
58             val props = Properties()
59             // Build up the python.path
60             val sb = StringBuilder()
61             sb.append(System.getProperty("java.class.path"))
62
63             for (p in pythonPath) {
64                 sb.append(":").append(p)
65             }
66             log.debug("Paths : $sb")
67
68             props["python.import.site"] = "true"
69             props.setProperty("python.path", sb.toString())
70             props.setProperty("python.verbose", "error")
71             props.setProperty("python.executable", executablePath)
72
73             PythonInterpreter.initialize(System.getProperties(), props, argv.toTypedArray())
74         }
75
76     }
77 }