aboutsummaryrefslogtreecommitdiffstats
path: root/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/ToscaParser.java
blob: ae595f13098a3983a7eab9bb91a0171ae77d7cb4 (plain)
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
package org.openecomp.sdc.toscaparser;

import java.util.Objects;

import org.openecomp.sdc.toscaparser.api.ToscaTemplate;
import org.openecomp.sdc.toscaparser.api.ToscaTemplateFactory;
import org.openecomp.sdc.toscaparser.jython.JyToscaTemplate;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;

public class ToscaParser {
    
    private final ToscaTemplateFactory toscaTemplateFactory;
    private final PyObject jythonToscaTemplate;
    private final PythonInterpreter pythonInterpreter;

    public ToscaParser(ToscaTemplateFactory toscaTemplateFactory, PythonInterpreter pythonInterpreter) {
        this.toscaTemplateFactory = Objects.requireNonNull(toscaTemplateFactory);
        this.pythonInterpreter = Objects.requireNonNull(pythonInterpreter);
        jythonToscaTemplate = getJythonToscaTemplate();
    }

    private PyObject getJythonToscaTemplate() {
        try (PythonInterpreter interpreter = pythonInterpreter) {
            interpreter.exec("from toscaparser.tosca_template import ToscaTemplate");
            return interpreter.get("ToscaTemplate");
        }
    }

    public ToscaTemplate parse(String path) {
        PyObject toscaTemplateInstance = jythonToscaTemplate.__call__(new PyString(path));
        JyToscaTemplate jyToscaTemplate = (JyToscaTemplate) toscaTemplateInstance.__tojava__(JyToscaTemplate.class);
        return toscaTemplateFactory.create(jyToscaTemplate);   
    }
}