diff options
author | Pavel Aharoni <pa0916@att.com> | 2017-03-29 13:35:45 +0300 |
---|---|---|
committer | Pavel Aharoni <pa0916@att.com> | 2017-03-29 13:35:45 +0300 |
commit | e2cc2530fc6d54ebc975c01a4ff887ce12f0a736 (patch) | |
tree | 38385867295c8a09fb0d7f8eaf5fa78179e5b13a /jython-tosca-parser/src/main/java/org | |
parent | bccebaa9888906f8ff78172f62ec592956066d82 (diff) |
[SDC-6] sdc-distribution-client 1707 rebasing
Change-Id: I322a05fd79beb6ba4fee4d32afffecf531b86e98
Signed-off-by: Pavel Aharoni <pa0916@att.com>
Diffstat (limited to 'jython-tosca-parser/src/main/java/org')
32 files changed, 906 insertions, 0 deletions
diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/JythonRuntime.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/JythonRuntime.java new file mode 100644 index 0000000..c5810a9 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/JythonRuntime.java @@ -0,0 +1,53 @@ +package org.openecomp.sdc.toscaparser; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.Properties; + +import org.openecomp.sdc.toscaparser.utils.JarExtractor; +import org.python.util.PythonInterpreter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class JythonRuntime { + + private static final Logger LOGGER = LoggerFactory.getLogger(JythonRuntime.class); + private final JarExtractor jarExtractor; + private Path homePath; + + public JythonRuntime(JarExtractor jarExtractor) { + this.jarExtractor = jarExtractor; + } + + public void initialize() throws IOException { + tryExtractPyhtonPackages(); + initRuntime(); + } + + private void initRuntime() { + Properties systemProperties = System.getProperties(); + Properties properties = getPythonProperties(); + PythonInterpreter.initialize(systemProperties, properties, new String[0]); + } + + private void tryExtractPyhtonPackages() throws IOException { + homePath = jarExtractor.extractPyhtonPackages(); + } + + private Properties getPythonProperties() { + Properties properties = new Properties(); + if (homePath != null) { + LOGGER.debug("getPythonProperties - Setting python.home to {}", homePath); + properties.put("python.home", homePath.toString()); + } + // Used to prevent: console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0. + properties.put("python.console.encoding", "UTF-8"); + return properties; + } + + public void terminate() throws IOException { + if (homePath != null) { + jarExtractor.deleteDirectory(homePath); + } + } +}
\ No newline at end of file diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/ToscaParser.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/ToscaParser.java new file mode 100644 index 0000000..ae595f1 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/ToscaParser.java @@ -0,0 +1,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); + } +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/ToscaParserFactory.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/ToscaParserFactory.java new file mode 100644 index 0000000..8bb1e17 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/ToscaParserFactory.java @@ -0,0 +1,48 @@ +package org.openecomp.sdc.toscaparser; + +import java.io.IOException; + +import org.openecomp.sdc.toscaparser.api.ToscaTemplateFactory; +import org.openecomp.sdc.toscaparser.utils.JarExtractor; +import org.python.util.PythonInterpreter; + +/** + * This is the entry point of the tosca-parser: a factory for creating {@code ToscaParser}s. + * <b>This class is not thread-safe.</b> Once you are done with all {@code ToscaParser}s + * created by this factory, the {@code ToscaParserFactory} itself must be closed either by + * calling its close() method explicitly or preferrably by creating it in a try-with-resources block: + * <pre> + try (ToscaParserFactory toscaParserFactory = new ToscaParserFactory()){ + ToscaParser parser = toscaParserFactory.create() + ToscaTemplate toscaTemplate = parser.parse(toscaFilePath); + ... + } + * </pre> + * @author Yaniv Nahoum + * + */ +public class ToscaParserFactory implements AutoCloseable { + + private JythonRuntime jythonRuntime; + + public ToscaParser create() throws IOException { + initRuntime(); + ToscaTemplateFactory toscaTemplateFactory = new ToscaTemplateFactory(); + PythonInterpreter pythonInterpreter = new PythonInterpreter(); + return new ToscaParser(toscaTemplateFactory, pythonInterpreter); + } + + private void initRuntime() throws IOException { + if (jythonRuntime == null) { + jythonRuntime = new JythonRuntime(new JarExtractor()); + jythonRuntime.initialize(); + } + } + + @Override + public void close() throws IOException { + if (jythonRuntime != null) { + jythonRuntime.terminate(); + } + } +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/Capability.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/Capability.java new file mode 100644 index 0000000..1359c36 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/Capability.java @@ -0,0 +1,38 @@ +package org.openecomp.sdc.toscaparser.api; + +import static com.google.common.collect.ImmutableList.toImmutableList; + +import java.util.List; +import java.util.Objects; + +import org.openecomp.sdc.toscaparser.jython.JyCapability; + +import com.google.common.base.MoreObjects; + +public class Capability { + + private final JyCapability jyCapability; + + public Capability(JyCapability jyCapability) { + this.jyCapability = Objects.requireNonNull(jyCapability); + } + + public String getName() { + return jyCapability.getJyName(); + } + + public List<Property> getProperties() { + return jyCapability.getJyProperties() + .stream() + .map(Property::new) + .collect(toImmutableList()); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("name", getName()) + .add("properties", getProperties()) + .toString(); + } +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/EntityTemplate.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/EntityTemplate.java new file mode 100644 index 0000000..a6f9868 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/EntityTemplate.java @@ -0,0 +1,70 @@ +package org.openecomp.sdc.toscaparser.api; + +import static com.google.common.collect.ImmutableList.toImmutableList; + +import java.util.List; +import java.util.Map; +import java.util.Objects; + +import org.openecomp.sdc.toscaparser.api.elements.StatefulEntityType; +import org.openecomp.sdc.toscaparser.jython.JyEntityTemplate; + +import com.google.common.base.MoreObjects; +import com.google.common.base.MoreObjects.ToStringHelper; + +public abstract class EntityTemplate { + + private final JyEntityTemplate jyEntityTemplate; + private final StatefulEntityType statefulEntityType; + + public EntityTemplate(JyEntityTemplate jyEntityTemplate) { + this.jyEntityTemplate = Objects.requireNonNull(jyEntityTemplate); + StatefulEntityTypeFactory statefulEntityTypeFactory = new StatefulEntityTypeFactory(); + statefulEntityType = statefulEntityTypeFactory.create(jyEntityTemplate.getJyTypeDefinition()); + } + + public String getName() { + return jyEntityTemplate.getJyName(); + } + + public String getDescription() { + return jyEntityTemplate.getJyDescription(); + } + + public StatefulEntityType getTypeDefinition() { + return statefulEntityType; + } + + public List<Property> getProperties() { + return jyEntityTemplate.getJyProperties() + .stream() + .map(Property::new) + .collect(toImmutableList()); + } + + public List<Capability> getCapabilities() { + return jyEntityTemplate.getJyCapabilities() + .stream() + .map(Capability::new) + .collect(toImmutableList()); + } + + public List<Map<String, Map<String, Object>>> getRequirements() { + return jyEntityTemplate.getJyRequirements(); + } + + protected ToStringHelper toStringHelper() { + return MoreObjects.toStringHelper(this) + .add("name", getName()) + .add("description", getDescription()) + .add("typeDefinition", getTypeDefinition()) + .add("properties", getProperties()) + .add("capabilities", getCapabilities()) + .add("requirements", getRequirements()); + } + + @Override + public String toString() { + return toStringHelper().toString(); + } +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/Group.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/Group.java new file mode 100644 index 0000000..65f6857 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/Group.java @@ -0,0 +1,10 @@ +package org.openecomp.sdc.toscaparser.api; + +import org.openecomp.sdc.toscaparser.jython.JyGroup; + +public class Group extends EntityTemplate { + + public Group(JyGroup jyGroup) { + super(jyGroup); + } +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/NodeTemplate.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/NodeTemplate.java new file mode 100644 index 0000000..047a421 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/NodeTemplate.java @@ -0,0 +1,10 @@ +package org.openecomp.sdc.toscaparser.api; + +import org.openecomp.sdc.toscaparser.jython.JyNodeTemplate; + +public class NodeTemplate extends EntityTemplate { + + public NodeTemplate(JyNodeTemplate jyNodeTemplate) { + super(jyNodeTemplate); + } +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/Property.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/Property.java new file mode 100644 index 0000000..c52efb6 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/Property.java @@ -0,0 +1,48 @@ +package org.openecomp.sdc.toscaparser.api; + +import java.util.Objects; + +import org.openecomp.sdc.toscaparser.jython.JyProperty; +import org.openecomp.sdc.toscaparser.utils.PythonUtils; + +import com.google.common.base.MoreObjects; + +public class Property { + + private final JyProperty jyProperty; + + public Property(JyProperty jyProperty) { + this.jyProperty = Objects.requireNonNull(jyProperty); + } + + public String getName() { + return jyProperty.getJyName(); + } + + public Object getValue() { + return PythonUtils.cast(jyProperty.getJyValue(), jyProperty.getJyValueClassName()); + } + + public String getType() { + return jyProperty.getJyType(); + } + + public boolean isRequired() { + return jyProperty.isJyRequired(); + } + + public String getDescription() { + return jyProperty.getJyDescription(); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("name", getName()) + .add("type", getType()) + .add("required", isRequired()) + .add("description", getDescription()) + .add("value", getValue()) + .toString(); + } +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/StatefulEntityTypeFactory.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/StatefulEntityTypeFactory.java new file mode 100644 index 0000000..554450a --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/StatefulEntityTypeFactory.java @@ -0,0 +1,28 @@ +package org.openecomp.sdc.toscaparser.api; + +import org.openecomp.sdc.toscaparser.api.elements.GroupType; +import org.openecomp.sdc.toscaparser.api.elements.NodeType; +import org.openecomp.sdc.toscaparser.api.elements.StatefulEntityType; +import org.openecomp.sdc.toscaparser.jython.elements.JyGroupType; +import org.openecomp.sdc.toscaparser.jython.elements.JyNodeType; +import org.openecomp.sdc.toscaparser.jython.elements.JyStatefulEntityType; + +public class StatefulEntityTypeFactory { + + public StatefulEntityType create(JyStatefulEntityType jyStatefulEntityType) { + String jyClassName = jyStatefulEntityType.getJyClassName(); + StatefulEntityType statefulEntityType; + switch (jyClassName) { + case "NodeType": + statefulEntityType = new NodeType((JyNodeType) jyStatefulEntityType); + break; + case "GroupType": + statefulEntityType = new GroupType((JyGroupType) jyStatefulEntityType); + break; + default: + throw new UnsupportedOperationException(jyClassName + " is not supported!"); + } + + return statefulEntityType; + } +}
\ No newline at end of file diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/SubstitutionMappings.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/SubstitutionMappings.java new file mode 100644 index 0000000..526a0e3 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/SubstitutionMappings.java @@ -0,0 +1,48 @@ +package org.openecomp.sdc.toscaparser.api; + +import static com.google.common.collect.ImmutableList.toImmutableList; + +import java.util.List; +import java.util.Objects; + +import org.openecomp.sdc.toscaparser.api.elements.NodeType; +import org.openecomp.sdc.toscaparser.api.parameters.Input; +import org.openecomp.sdc.toscaparser.jython.JySubstitutionMappings; + +import com.google.common.base.MoreObjects; + +public class SubstitutionMappings { + + private final JySubstitutionMappings jySubstitutionMappings; + + public SubstitutionMappings(JySubstitutionMappings jySubstitutionMappings) { + this.jySubstitutionMappings = Objects.requireNonNull(jySubstitutionMappings); + } + + public List<NodeTemplate> getNodeTemplates() { + return jySubstitutionMappings.getJyNodeTemplates() + .stream() + .map(NodeTemplate::new) + .collect(toImmutableList()); + } + + public List<Input> getInputs() { + return jySubstitutionMappings.getJyInputs() + .stream() + .map(Input::new) + .collect(toImmutableList()); + } + + public NodeType getNodeDefinition() { + return new NodeType(jySubstitutionMappings.getJyNodeDefinition()); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("nodeTemplates", getNodeTemplates()) + .add("inputs", getInputs()) + .add("nodeDefinition", getNodeDefinition()) + .toString(); + } +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/TopologyTemplate.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/TopologyTemplate.java new file mode 100644 index 0000000..1769455 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/TopologyTemplate.java @@ -0,0 +1,62 @@ +package org.openecomp.sdc.toscaparser.api; + +import static com.google.common.collect.ImmutableList.toImmutableList; + +import java.util.List; +import java.util.Objects; + +import org.openecomp.sdc.toscaparser.api.parameters.Input; +import org.openecomp.sdc.toscaparser.jython.JySubstitutionMappings; +import org.openecomp.sdc.toscaparser.jython.JyTopologyTemplate; + +import com.google.common.base.MoreObjects; + +public class TopologyTemplate { + + private final JyTopologyTemplate jyTopologyTemplate; + + public TopologyTemplate(JyTopologyTemplate jyTopologyTemplate) { + this.jyTopologyTemplate = Objects.requireNonNull(jyTopologyTemplate); + } + + public String getDescription() { + return jyTopologyTemplate.getJyDescription(); + } + + public List<NodeTemplate> getNodeTemplates() { + return jyTopologyTemplate.getJyNodeTemplates() + .stream() + .map(NodeTemplate::new) + .collect(toImmutableList()); + } + + public List<Input> getInputs() { + return jyTopologyTemplate.getJyInputs() + .stream() + .map(Input::new) + .collect(toImmutableList()); + } + + public List<Group> getGroups() { + return jyTopologyTemplate.getJyGroups() + .stream() + .map(Group::new) + .collect(toImmutableList()); + } + + public SubstitutionMappings getSubstitutionMappings() { + JySubstitutionMappings jySubstitutionMappings = jyTopologyTemplate.getJySubstitutionMappings(); + return jySubstitutionMappings != null ? new SubstitutionMappings(jySubstitutionMappings) : null; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("description", getDescription()) + .add("inputs", getInputs()) + .add("nodeTemplates", getNodeTemplates()) + .add("groups", getGroups()) + .add("substitutionMappings", getSubstitutionMappings()) + .toString(); + } +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/ToscaTemplate.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/ToscaTemplate.java new file mode 100644 index 0000000..97563fb --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/ToscaTemplate.java @@ -0,0 +1,49 @@ +package org.openecomp.sdc.toscaparser.api; + +import java.util.List; +import java.util.Objects; + +import org.openecomp.sdc.toscaparser.api.parameters.Input; +import org.openecomp.sdc.toscaparser.jython.JyToscaTemplate; + +import com.google.common.base.MoreObjects; + +public class ToscaTemplate { + + private final JyToscaTemplate jyToscaTemplate; + private final TopologyTemplate topologyTemplate; + + public ToscaTemplate(JyToscaTemplate jyToscaTemplate, TopologyTemplate topologyTemplate) { + this.jyToscaTemplate = Objects.requireNonNull(jyToscaTemplate); + this.topologyTemplate = Objects.requireNonNull(topologyTemplate); + } + + public String getVersion() { + return jyToscaTemplate.getJyVersion(); + } + + public String getDescription() { + return jyToscaTemplate.getJyDescription(); + } + + public TopologyTemplate getTopologyTemplate() { + return topologyTemplate; + } + + public List<NodeTemplate> getNodeTemplates() { + return topologyTemplate.getNodeTemplates(); + } + + public List<Input> getInputs() { + return topologyTemplate.getInputs(); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("version", getVersion()) + .add("description", getDescription()) + .add("topologyTemplate", topologyTemplate) + .toString(); + } +}
\ No newline at end of file diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/ToscaTemplateFactory.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/ToscaTemplateFactory.java new file mode 100644 index 0000000..553c56b --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/ToscaTemplateFactory.java @@ -0,0 +1,14 @@ +package org.openecomp.sdc.toscaparser.api; + +import java.util.Objects; + +import org.openecomp.sdc.toscaparser.jython.JyToscaTemplate; + +public class ToscaTemplateFactory { + + public ToscaTemplate create(JyToscaTemplate jyToscaTemplate) { + Objects.requireNonNull(jyToscaTemplate); + TopologyTemplate topologyTemplate = new TopologyTemplate(jyToscaTemplate.getJyTopologyTemplate()); + return new ToscaTemplate(jyToscaTemplate, topologyTemplate); + } +}
\ No newline at end of file diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/elements/GroupType.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/elements/GroupType.java new file mode 100644 index 0000000..8e0bedf --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/elements/GroupType.java @@ -0,0 +1,30 @@ +package org.openecomp.sdc.toscaparser.api.elements; + +import org.openecomp.sdc.toscaparser.jython.elements.JyGroupType; + +import com.google.common.base.MoreObjects.ToStringHelper; + +public class GroupType extends StatefulEntityType { + + private final JyGroupType jyGroupType; + + public GroupType(JyGroupType jyGroupType) { + super(jyGroupType); + this.jyGroupType = jyGroupType; + } + + public String getVersion() { + return jyGroupType.getJyVersion(); + } + + public String getDescription() { + return jyGroupType.getJyDescription(); + } + + @Override + protected ToStringHelper toStringHelper() { + return super.toStringHelper() + .add("version", getVersion()) + .add("description", getDescription()); + } +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/elements/NodeType.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/elements/NodeType.java new file mode 100644 index 0000000..69bd9ba --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/elements/NodeType.java @@ -0,0 +1,27 @@ +package org.openecomp.sdc.toscaparser.api.elements; + +import java.util.List; + +import org.openecomp.sdc.toscaparser.jython.elements.JyNodeType; + +import com.google.common.base.MoreObjects.ToStringHelper; + +public class NodeType extends StatefulEntityType { + + private final JyNodeType jyNodeType; + + public NodeType(JyNodeType jyNodeType) { + super(jyNodeType); + this.jyNodeType = jyNodeType; + } + + public List<?> getRequirements() { + return jyNodeType.getJyRequirements(); + } + + @Override + protected ToStringHelper toStringHelper() { + return super.toStringHelper() + .add("requirements", getRequirements()); + } +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/elements/StatefulEntityType.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/elements/StatefulEntityType.java new file mode 100644 index 0000000..32661b5 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/elements/StatefulEntityType.java @@ -0,0 +1,31 @@ +package org.openecomp.sdc.toscaparser.api.elements; + +import java.util.Objects; + +import org.openecomp.sdc.toscaparser.jython.elements.JyStatefulEntityType; + +import com.google.common.base.MoreObjects; +import com.google.common.base.MoreObjects.ToStringHelper; + +public abstract class StatefulEntityType { + + private final JyStatefulEntityType jyStatefulEntityType; + + public StatefulEntityType(JyStatefulEntityType jyStatefulEntityType) { + this.jyStatefulEntityType = Objects.requireNonNull(jyStatefulEntityType); + } + + public String getType() { + return jyStatefulEntityType.getJyType(); + } + + protected ToStringHelper toStringHelper() { + return MoreObjects.toStringHelper(this) + .add("type", getType()); + } + + @Override + public String toString() { + return toStringHelper().toString(); + } +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/parameters/Input.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/parameters/Input.java new file mode 100644 index 0000000..b700c47 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/api/parameters/Input.java @@ -0,0 +1,42 @@ +package org.openecomp.sdc.toscaparser.api.parameters; + +import java.util.Objects; + +import org.openecomp.sdc.toscaparser.jython.parameters.JyInput; + +import com.google.common.base.MoreObjects; + +public class Input { + + private final JyInput jyInput; + + public Input(JyInput jyInput) { + this.jyInput = Objects.requireNonNull(jyInput); + } + + public String getName() { + return jyInput.getJyName(); + } + + public String getType() { + return jyInput.getJyType(); + } + + public boolean isRequired() { + return jyInput.isJyRequired(); + } + + public String getDescription() { + return jyInput.getJyDescription(); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("name", getName()) + .add("type", getType()) + .add("required", isRequired()) + .add("description", getDescription()) + .toString(); + } +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyCapability.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyCapability.java new file mode 100644 index 0000000..1956932 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyCapability.java @@ -0,0 +1,9 @@ +package org.openecomp.sdc.toscaparser.jython; + +import java.util.List; + +public interface JyCapability { + + String getJyName(); + List<JyProperty> getJyProperties(); +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyEntityTemplate.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyEntityTemplate.java new file mode 100644 index 0000000..cdcb8a0 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyEntityTemplate.java @@ -0,0 +1,16 @@ +package org.openecomp.sdc.toscaparser.jython; + +import java.util.List; +import java.util.Map; + +import org.openecomp.sdc.toscaparser.jython.elements.JyStatefulEntityType; + +public interface JyEntityTemplate { + + String getJyName(); + String getJyDescription(); + JyStatefulEntityType getJyTypeDefinition(); + List<JyProperty> getJyProperties(); + List<JyCapability> getJyCapabilities(); + List<Map<String, Map<String, Object>>> getJyRequirements(); +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyGroup.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyGroup.java new file mode 100644 index 0000000..c155dc6 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyGroup.java @@ -0,0 +1,5 @@ +package org.openecomp.sdc.toscaparser.jython; + +public interface JyGroup extends JyEntityTemplate { + +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyNodeTemplate.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyNodeTemplate.java new file mode 100644 index 0000000..b04a337 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyNodeTemplate.java @@ -0,0 +1,5 @@ +package org.openecomp.sdc.toscaparser.jython; + +public interface JyNodeTemplate extends JyEntityTemplate { + +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyProperty.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyProperty.java new file mode 100644 index 0000000..4c33b92 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyProperty.java @@ -0,0 +1,11 @@ +package org.openecomp.sdc.toscaparser.jython; + +public interface JyProperty { + + String getJyName(); + Object getJyValue(); + String getJyValueClassName(); + String getJyType(); + boolean isJyRequired(); + String getJyDescription(); +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JySubstitutionMappings.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JySubstitutionMappings.java new file mode 100644 index 0000000..bd95cdc --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JySubstitutionMappings.java @@ -0,0 +1,13 @@ +package org.openecomp.sdc.toscaparser.jython; + +import java.util.List; + +import org.openecomp.sdc.toscaparser.jython.elements.JyNodeType; +import org.openecomp.sdc.toscaparser.jython.parameters.JyInput; + +public interface JySubstitutionMappings { + + List<JyNodeTemplate> getJyNodeTemplates(); + List<JyInput> getJyInputs(); + JyNodeType getJyNodeDefinition(); +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyTopologyTemplate.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyTopologyTemplate.java new file mode 100644 index 0000000..c6fe053 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyTopologyTemplate.java @@ -0,0 +1,14 @@ +package org.openecomp.sdc.toscaparser.jython; + +import java.util.List; + +import org.openecomp.sdc.toscaparser.jython.parameters.JyInput; + +public interface JyTopologyTemplate { + + String getJyDescription(); + List<JyNodeTemplate> getJyNodeTemplates(); + List<JyInput> getJyInputs(); + List<JyGroup> getJyGroups(); + JySubstitutionMappings getJySubstitutionMappings(); +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyToscaTemplate.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyToscaTemplate.java new file mode 100644 index 0000000..c58a042 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/JyToscaTemplate.java @@ -0,0 +1,11 @@ +package org.openecomp.sdc.toscaparser.jython; + +import java.util.List; + +public interface JyToscaTemplate { + + String getJyVersion(); + String getJyDescription(); + List<JyNodeTemplate> getJyNodeTemplates(); + JyTopologyTemplate getJyTopologyTemplate(); +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/elements/JyGroupType.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/elements/JyGroupType.java new file mode 100644 index 0000000..1ad2492 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/elements/JyGroupType.java @@ -0,0 +1,7 @@ +package org.openecomp.sdc.toscaparser.jython.elements; + +public interface JyGroupType extends JyStatefulEntityType { + + String getJyVersion(); + String getJyDescription(); +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/elements/JyNodeType.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/elements/JyNodeType.java new file mode 100644 index 0000000..885dcda --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/elements/JyNodeType.java @@ -0,0 +1,8 @@ +package org.openecomp.sdc.toscaparser.jython.elements; + +import java.util.List; + +public interface JyNodeType extends JyStatefulEntityType { + + List<?> getJyRequirements(); +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/elements/JyStatefulEntityType.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/elements/JyStatefulEntityType.java new file mode 100644 index 0000000..890bc6c --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/elements/JyStatefulEntityType.java @@ -0,0 +1,7 @@ +package org.openecomp.sdc.toscaparser.jython.elements; + +public interface JyStatefulEntityType { + + String getJyType(); + String getJyClassName(); +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/parameters/JyInput.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/parameters/JyInput.java new file mode 100644 index 0000000..320e91b --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/jython/parameters/JyInput.java @@ -0,0 +1,9 @@ +package org.openecomp.sdc.toscaparser.jython.parameters; + +public interface JyInput { + + String getJyName(); + String getJyType(); + boolean isJyRequired(); + String getJyDescription(); +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/JarExtractor.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/JarExtractor.java new file mode 100644 index 0000000..44feaa3 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/JarExtractor.java @@ -0,0 +1,87 @@ +package org.openecomp.sdc.toscaparser.utils; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Enumeration; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.io.MoreFiles; +import com.google.common.io.RecursiveDeleteOption; + +public class JarExtractor { + + private static final Logger LOGGER = LoggerFactory.getLogger(JarExtractor.class); + private static final String PYTHON_DEPENDENCIES_PATH = "Lib/site-packages"; + private static final String TEMP_DIR_PREFIX = "__tosca__"; + + public Path extractPyhtonPackages() throws IOException { + String codePath = getCodePath(); + if (!isRunningInJar(codePath)) { + LOGGER.info("#extractPyhtonPackages - Nothing to extract, we're not running in a jar file."); + return null; + } + + Path tempDirPath = createTempDirectory(); + String tempDirName = tempDirPath.toString(); + extractJarDirectory(codePath, tempDirName); + LOGGER.info("#extractPyhtonPackages - End. Extracted python dependencies to {}", tempDirName); + return tempDirPath; + } + + private Path createTempDirectory() throws IOException { + Path tempDir = Files.createTempDirectory(TEMP_DIR_PREFIX); + LOGGER.debug("#createTempDirectory - tempDir created: {}", tempDir); + return tempDir; + } + + private void extractJarDirectory(String jarDir, String tempDir) throws IOException { + try (JarFile jarFile = new JarFile(jarDir)) { + Enumeration<JarEntry> entries = jarFile.entries(); + while (entries.hasMoreElements()) { + JarEntry entry = entries.nextElement(); + if (shouldExtract(entry)) { + extract(jarFile, entry, tempDir); + } + } + } + } + + private void extract(JarFile jarFile, JarEntry entry, String tempDirName) throws IOException { + try (InputStream source = jarFile.getInputStream(entry)) { + Path targetPath = Paths.get(tempDirName, entry.getName()); + Files.createDirectories(targetPath.getParent()); + Files.copy(source, targetPath); + } + } + + private boolean shouldExtract(JarEntry entry) { + return !entry.isDirectory() && entry.getName() + .startsWith(PYTHON_DEPENDENCIES_PATH); + } + + private String getCodePath() { + String codePath = this.getClass() + .getProtectionDomain() + .getCodeSource() + .getLocation() + .getFile(); + LOGGER.debug("#getCodePath - codePath: {}", codePath); + return codePath; + } + + private boolean isRunningInJar(String path) { + return path.endsWith(".jar"); + } + + public void deleteDirectory(Path path) throws IOException { + MoreFiles.deleteRecursively(path, RecursiveDeleteOption.ALLOW_INSECURE); + LOGGER.info("#deleteDirectory - deleted temp directory: {}", path); + } +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/PythonType.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/PythonType.java new file mode 100644 index 0000000..f4d6939 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/PythonType.java @@ -0,0 +1,47 @@ +package org.openecomp.sdc.toscaparser.utils; + +import static java.util.stream.Collectors.toMap; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.function.Function; + +public enum PythonType { + + INT("int", Integer.class), + FLOAT("float", Double.class), + BOOL("bool", Boolean.class), + STR("str", String.class), + LIST("list", List.class), + DICT("dict", Map.class), + UNKNOWN("", Object.class); + + private static final Map<String, PythonType> NAME_TO_TYPE; + private final String typeName; + private final Class<?> javaClass; + + static { + NAME_TO_TYPE = Arrays.stream(values()) + .filter(type -> type != UNKNOWN) + .collect(toMap(PythonType::getTypeName, Function.identity())); + } + + private PythonType(String typeName, Class<?> javaClass) { + this.typeName = typeName; + this.javaClass = javaClass; + } + + public static PythonType fromName(String name) { + PythonType pythonType = NAME_TO_TYPE.get(name); + return pythonType != null ? pythonType : UNKNOWN; + } + + public String getTypeName() { + return typeName; + } + + public Class<?> getJavaClass() { + return javaClass; + } +}
\ No newline at end of file diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/PythonUtils.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/PythonUtils.java new file mode 100644 index 0000000..f73c92b --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/PythonUtils.java @@ -0,0 +1,13 @@ +package org.openecomp.sdc.toscaparser.utils; + +public final class PythonUtils { + + private PythonUtils() { + // No instances allowed + } + + public static Object cast(Object object, String pythonTypeName) { + PythonType pythonType = PythonType.fromName(pythonTypeName); + return pythonType.getJavaClass().cast(object); + } +} |