diff --git a/gef-integration.target/gef-integration.target b/gef-integration.target/gef-integration.target index 7db6c9a..c64c0d6 100644 --- a/gef-integration.target/gef-integration.target +++ b/gef-integration.target/gef-integration.target @@ -1,5 +1,6 @@ - + + diff --git a/org.sofproject.alsa.topo/.classpath b/org.sofproject.alsa.topo/.classpath index eca7bdb..2bce911 100644 --- a/org.sofproject.alsa.topo/.classpath +++ b/org.sofproject.alsa.topo/.classpath @@ -1,6 +1,10 @@ - + + + + + diff --git a/org.sofproject.alsa.topo/META-INF/MANIFEST.MF b/org.sofproject.alsa.topo/META-INF/MANIFEST.MF index 99e2537..836ccbf 100644 --- a/org.sofproject.alsa.topo/META-INF/MANIFEST.MF +++ b/org.sofproject.alsa.topo/META-INF/MANIFEST.MF @@ -10,3 +10,4 @@ Export-Package: org.sofproject.alsa.topo.conf, Require-Bundle: org.sofproject.core, org.sofproject.topo.ui Bundle-Vendor: Intel Corporation +Import-Package: org.sofproject.topo.ui.json diff --git a/org.sofproject.alsa.topo/src/org/sofproject/alsa/topo/model/AlsaTopoGraph.java b/org.sofproject.alsa.topo/src/org/sofproject/alsa/topo/model/AlsaTopoGraph.java index 6bd194a..8dd1706 100644 --- a/org.sofproject.alsa.topo/src/org/sofproject/alsa/topo/model/AlsaTopoGraph.java +++ b/org.sofproject.alsa.topo/src/org/sofproject/alsa/topo/model/AlsaTopoGraph.java @@ -536,7 +536,56 @@ public void serialize() throws CoreException, IOException { } outputFile.getParent().refreshLocal(1, null); } + + @Override + public String getPipelineString() { + try { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + Writer writer = new BufferedWriter(new OutputStreamWriter(os)); + // tlv-s + tlvs.serialize(writer); + + // vendor tokens + vTokensIndex.serialize(writer); + + // vendor tuples + vTuplesIndex.serialize(writer); + + // data + dataIndex.serialize(writer); + + // control bytes + controlBytes.serialize(writer); + + // control mixers + controlMixers.serialize(writer); + + // pcm capabilities + pcmCapsIndex.serialize(writer); + // pcm-s + pcms.serialize(writer); + + // be-s + beIndex.serialize(writer); + + // hw-configs + hwConfigs.serialize(writer); + + // pipelines (widgets + graphs) + pipelines.serialize(writer); + interConnections.serialize(writer); + + writer.close(); + os.close(); + + return os.toString(); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + @Override public IRemoteOpsProvider getRemoteOpsProvider() { return null; // no extra ops diff --git a/org.sofproject.alsa.topo/src/org/sofproject/alsa/topo/model/AlsaTopoPipeline.java b/org.sofproject.alsa.topo/src/org/sofproject/alsa/topo/model/AlsaTopoPipeline.java index f7b2067..bfd0ece 100644 --- a/org.sofproject.alsa.topo/src/org/sofproject/alsa/topo/model/AlsaTopoPipeline.java +++ b/org.sofproject.alsa.topo/src/org/sofproject/alsa/topo/model/AlsaTopoPipeline.java @@ -29,7 +29,10 @@ package org.sofproject.alsa.topo.model; +import java.io.BufferedWriter; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.OutputStreamWriter; import java.io.Writer; import java.util.Collection; import java.util.HashMap; diff --git a/org.sofproject.fw.ui/.classpath b/org.sofproject.fw.ui/.classpath index eca7bdb..2bce911 100644 --- a/org.sofproject.fw.ui/.classpath +++ b/org.sofproject.fw.ui/.classpath @@ -1,6 +1,10 @@ - + + + + + diff --git a/org.sofproject.gst.topo/.classpath b/org.sofproject.gst.topo/.classpath index eca7bdb..2bce911 100644 --- a/org.sofproject.gst.topo/.classpath +++ b/org.sofproject.gst.topo/.classpath @@ -1,6 +1,10 @@ - + + + + + diff --git a/org.sofproject.gst.topo/META-INF/MANIFEST.MF b/org.sofproject.gst.topo/META-INF/MANIFEST.MF index dd4cd5d..447c607 100644 --- a/org.sofproject.gst.topo/META-INF/MANIFEST.MF +++ b/org.sofproject.gst.topo/META-INF/MANIFEST.MF @@ -10,3 +10,5 @@ Require-Bundle: org.sofproject.core, org.sofproject.ui, org.sofproject.topo.ui, com.jcraft.jsch +Import-Package: org.eclipse.ui +Export-Package: org.sofproject.gst.topo.ops diff --git a/org.sofproject.gst.topo/build.properties b/org.sofproject.gst.topo/build.properties index e9863e2..950c215 100644 --- a/org.sofproject.gst.topo/build.properties +++ b/org.sofproject.gst.topo/build.properties @@ -2,4 +2,7 @@ source.. = src/ output.. = bin/ bin.includes = META-INF/,\ .,\ - plugin.xml + plugin.xml,\ + lib/jackson-annotations-2.10.1.jar,\ + lib/jackson-core-2.10.1.jar,\ + lib/jackson-databind-2.10.1.jar diff --git a/org.sofproject.gst.topo/src/org/sofproject/gst/topo/model/GstTopoGraph.java b/org.sofproject.gst.topo/src/org/sofproject/gst/topo/model/GstTopoGraph.java index 761fff4..5979483 100644 --- a/org.sofproject.gst.topo/src/org/sofproject/gst/topo/model/GstTopoGraph.java +++ b/org.sofproject.gst.topo/src/org/sofproject/gst/topo/model/GstTopoGraph.java @@ -34,8 +34,11 @@ import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileWriter; import java.io.IOException; import java.io.OutputStreamWriter; +import java.io.PrintWriter; import java.io.Writer; import java.util.ArrayList; import java.util.Arrays; @@ -192,6 +195,7 @@ public void serialize() throws CoreException, IOException { fileInput.setContents(new ByteArrayInputStream(os.toByteArray()), true, false, null); } + @Override public String getPipelineString() { try { ByteArrayOutputStream os = new ByteArrayOutputStream(); diff --git a/org.sofproject.gst.topo/src/org/sofproject/gst/topo/ops/GstDockerOperation.java b/org.sofproject.gst.topo/src/org/sofproject/gst/topo/ops/GstDockerOperation.java new file mode 100644 index 0000000..2c66005 --- /dev/null +++ b/org.sofproject.gst.topo/src/org/sofproject/gst/topo/ops/GstDockerOperation.java @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2019, Intel Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Intel Corporation nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +package org.sofproject.gst.topo.ops; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.nio.file.FileSystem; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.sofproject.core.AudioDevNodeProject; +import org.sofproject.core.connection.AudioDevNodeConnection; +import org.sofproject.core.ops.SimpleRemoteOp; +import org.sofproject.topo.ui.json.JsonProperty; +import org.sofproject.topo.ui.json.JsonUtils; + +import com.jcraft.jsch.ChannelExec; +import com.jcraft.jsch.JSchException; +import com.jcraft.jsch.Session; + +public class GstDockerOperation extends SimpleRemoteOp { + + private JsonUtils jsonUtils; + + public GstDockerOperation(AudioDevNodeConnection conn, JsonUtils jsonUtils) { + super(conn); + this.jsonUtils = jsonUtils; + } + + @Override + public boolean isCancelable() { + return true; + } + + @Override + public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { + monitor.beginTask("Sending json to docker", 100); + + try { + if (!conn.isConnected()) { + throw new InvocationTargetException(new IllegalStateException("Node not connected")); + } + + JsonProperty jsonProperty = jsonUtils.getJsonProperty(); + + AudioDevNodeProject proj = conn.getProject(); + Session session = conn.getSession(); + ChannelExec channel = (ChannelExec) session.openChannel("exec"); + channel.setInputStream(null); + + String jsonFileName = "pipeline.json"; + String projectPath = proj.getProject().getLocation().toString(); + Path partPathToJson = Paths.get(jsonProperty.getType().toLowerCase(), jsonProperty.getName(), jsonProperty.getVersion()); + Path fullPathToJson = Paths.get(projectPath, partPathToJson.toString(), jsonFileName); + String linuxPartPathToJson = partPathToJson.toString(); + linuxPartPathToJson = linuxPartPathToJson.replace("\\", "/"); + + BufferedReader reader = new BufferedReader(new FileReader(fullPathToJson.toString())); + String currentLine = reader.readLine(); + reader.close(); + + channel.setPty(true); // for ctrl+c sending + String command = String.format( + "docker exec -t video_analytics_serving_gstreamer /bin/bash -c \"cd pipelines; mkdir -p %s; cd %s; touch %s; echo \'%s\' > %s\"", + linuxPartPathToJson, linuxPartPathToJson, jsonFileName, currentLine.replace("\"", "\\\""), jsonFileName); + channel.setCommand(command); + + channel.connect(); + + while (!channel.isEOF()) + ; + channel.disconnect(); + monitor.beginTask("Sending json to docker", 1000); + + } catch (JSchException | IOException e) { + throw new InvocationTargetException(e); + } + } + +} diff --git a/org.sofproject.gst.topo/src/org/sofproject/gst/topo/ops/GstDockerOpsProvider.java b/org.sofproject.gst.topo/src/org/sofproject/gst/topo/ops/GstDockerOpsProvider.java new file mode 100644 index 0000000..6450aa2 --- /dev/null +++ b/org.sofproject.gst.topo/src/org/sofproject/gst/topo/ops/GstDockerOpsProvider.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2019, Intel Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Intel Corporation nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +package org.sofproject.gst.topo.ops; + +import org.sofproject.core.connection.AudioDevNodeConnection; +import org.sofproject.core.ops.IRemoteOp; +import org.sofproject.core.ops.IRemoteOpsProvider; +import org.sofproject.topo.ui.json.JsonUtils; + +public class GstDockerOpsProvider implements IRemoteOpsProvider { + + public static final String SEND_DOCKER = "org.sofproject.gst.topo.ops.senddocker"; + + public static final String[] OPS = { SEND_DOCKER }; + + JsonUtils jsonUtils; + + public GstDockerOpsProvider(JsonUtils jsonUtils) { + this.jsonUtils = jsonUtils; + } + + @Override + public String[] getRemoteOpsIds() { + return OPS; + } + + @Override + public String getRemoteOpDisplayName(String opId) { + switch (opId) { + case SEND_DOCKER: + return "Serialize Topology to Json & send to Docker"; + default: + return null; + } + } + + @Override + public IRemoteOp createRemoteOp(String opId, AudioDevNodeConnection conn) { + switch (opId) { + case SEND_DOCKER: + return new GstDockerOperation(conn, jsonUtils); + default: + return null; + } + } + +} diff --git a/org.sofproject.gst.topo/src/org/sofproject/gst/topo/ops/GstGraphOpsProvider.java b/org.sofproject.gst.topo/src/org/sofproject/gst/topo/ops/GstGraphOpsProvider.java index 77efe70..e1bab57 100644 --- a/org.sofproject.gst.topo/src/org/sofproject/gst/topo/ops/GstGraphOpsProvider.java +++ b/org.sofproject.gst.topo/src/org/sofproject/gst/topo/ops/GstGraphOpsProvider.java @@ -67,7 +67,7 @@ public IRemoteOp createRemoteOp(String opId, AudioDevNodeConnection conn) { switch (opId) { case TEST_TOPO: if (conn == null) { - conn = AudioDevNodeConnectionManager.getInstance().getConnection(graph.getFileInput().getProject());; + conn = AudioDevNodeConnectionManager.getInstance().getConnection(graph.getFileInput().getProject()); } return new GstTopoTestOperation(graph, conn); default: diff --git a/org.sofproject.topo.ui/.classpath b/org.sofproject.topo.ui/.classpath index eca7bdb..6a489de 100644 --- a/org.sofproject.topo.ui/.classpath +++ b/org.sofproject.topo.ui/.classpath @@ -1,7 +1,15 @@ - + + + + + + + + + diff --git a/org.sofproject.topo.ui/META-INF/MANIFEST.MF b/org.sofproject.topo.ui/META-INF/MANIFEST.MF index e524341..9b090ad 100644 --- a/org.sofproject.topo.ui/META-INF/MANIFEST.MF +++ b/org.sofproject.topo.ui/META-INF/MANIFEST.MF @@ -16,7 +16,8 @@ Require-Bundle: org.eclipse.core.runtime;visibility:=reexport, org.eclipse.gef.zest.fx;bundle-version="5.0.1", org.eclipse.gef.zest.fx.ui;bundle-version="5.0.2", org.sofproject.core, - org.sofproject.ui + org.sofproject.ui, + org.sofproject.gst.topo Import-Package: com.google.common.collect;version="21.0.0", com.google.common.reflect;version="21.0.0", com.google.inject;version="1.3.0", @@ -25,4 +26,9 @@ Import-Package: com.google.common.collect;version="21.0.0", com.google.inject.util;version="1.3.0" Bundle-Vendor: Intel Corporation Export-Package: org.sofproject.topo.ui.editor, - org.sofproject.topo.ui.graph + org.sofproject.topo.ui.graph, + org.sofproject.topo.ui.json +Bundle-ClassPath: lib/jackson-annotations-2.10.1.jar, + lib/jackson-core-2.10.1.jar, + lib/jackson-databind-2.10.1.jar, + . diff --git a/org.sofproject.topo.ui/build.properties b/org.sofproject.topo.ui/build.properties index 2dcc4bd..fe6aae5 100644 --- a/org.sofproject.topo.ui/build.properties +++ b/org.sofproject.topo.ui/build.properties @@ -3,4 +3,7 @@ output.. = bin/ bin.includes = META-INF/,\ .,\ plugin.xml,\ - bin/ + bin/,\ + lib/jackson-annotations-2.10.1.jar,\ + lib/jackson-core-2.10.1.jar,\ + lib/jackson-databind-2.10.1.jar diff --git a/org.sofproject.topo.ui/lib/jackson-annotations-2.10.1.jar b/org.sofproject.topo.ui/lib/jackson-annotations-2.10.1.jar new file mode 100644 index 0000000..a10399c Binary files /dev/null and b/org.sofproject.topo.ui/lib/jackson-annotations-2.10.1.jar differ diff --git a/org.sofproject.topo.ui/lib/jackson-core-2.10.1.jar b/org.sofproject.topo.ui/lib/jackson-core-2.10.1.jar new file mode 100644 index 0000000..1640a4e Binary files /dev/null and b/org.sofproject.topo.ui/lib/jackson-core-2.10.1.jar differ diff --git a/org.sofproject.topo.ui/lib/jackson-databind-2.10.1.jar b/org.sofproject.topo.ui/lib/jackson-databind-2.10.1.jar new file mode 100644 index 0000000..abe3748 Binary files /dev/null and b/org.sofproject.topo.ui/lib/jackson-databind-2.10.1.jar differ diff --git a/org.sofproject.topo.ui/lib/javax-inject.jar b/org.sofproject.topo.ui/lib/javax-inject.jar new file mode 100644 index 0000000..4c86c52 Binary files /dev/null and b/org.sofproject.topo.ui/lib/javax-inject.jar differ diff --git a/org.sofproject.topo.ui/src/org/sofproject/topo/ui/editor/policies/TopoEditorOnClickHandler.java b/org.sofproject.topo.ui/src/org/sofproject/topo/ui/editor/policies/TopoEditorOnClickHandler.java index ac9b9de..14f303a 100644 --- a/org.sofproject.topo.ui/src/org/sofproject/topo/ui/editor/policies/TopoEditorOnClickHandler.java +++ b/org.sofproject.topo.ui/src/org/sofproject/topo/ui/editor/policies/TopoEditorOnClickHandler.java @@ -31,6 +31,8 @@ import java.io.IOException; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.gef.mvc.fx.handlers.AbstractHandler; import org.eclipse.gef.mvc.fx.handlers.IOnClickHandler; @@ -41,11 +43,17 @@ import org.eclipse.gef.mvc.fx.viewer.IViewer; import org.eclipse.gef.mvc.fx.viewer.InfiniteCanvasViewer; import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.IFileEditorInput; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.PlatformUI; +import org.sofproject.core.connection.AudioDevNodeConnectionManager; import org.sofproject.core.ops.IRemoteOpsProvider; import org.sofproject.topo.ui.graph.GefTopoNode; import org.sofproject.topo.ui.graph.ITopoGraph; import org.sofproject.topo.ui.graph.ITopoNode; import org.sofproject.topo.ui.graph.TopoZestGraphBuilder; +import org.sofproject.topo.ui.json.JsonCustomOptionPane; +import org.sofproject.topo.ui.json.JsonUtils; import org.sofproject.topo.ui.models.TopoItemCreationModel; import org.sofproject.topo.ui.parts.TopoGraphPart; import org.sofproject.ui.ops.AudioDevNodeOpRunner; @@ -124,16 +132,67 @@ public void handle(ActionEvent event) { try { getGraphFromHost().serialize(); } catch (CoreException | IOException e) { - e.printStackTrace(); //TODO: + e.printStackTrace(); // TODO: } } }); menu.getItems().add(miSerialize); + + menu.getItems().add(new SeparatorMenuItem()); + + MenuItem miSerializeJson = new MenuItem("Serialize Topology to Json"); + miSerializeJson.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent event) { + menu.hide(); + new JsonCustomOptionPane(Display.getCurrent(), getGraphFromHost(), new JsonUtils()); + } + }); + menu.getItems().add(miSerializeJson); + + MenuItem miSendJson = new MenuItem("Serialize Topology to Json & send to Docker"); + JsonUtils jsonUtils = new JsonUtils(); + IRemoteOpsProvider dockerOpsProv = jsonUtils.getDockerOpsProvider(); + + if (dockerOpsProv != null) { + for (String opId : dockerOpsProv.getRemoteOpsIds()) { + miSendJson.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent event) { + menu.hide(); + new JsonCustomOptionPane(Display.getCurrent(), getGraphFromHost(), jsonUtils); + + Display.getDefault().asyncExec(new Runnable() { + + @Override + public void run() { + AudioDevNodeOpRunner + .runOp(dockerOpsProv.createRemoteOp(opId, AudioDevNodeConnectionManager + .getInstance().getConnection(getProject()))); + } + }); + } + }); + } + } + menu.getItems().add(miSendJson); + menu.show(((InfiniteCanvasViewer) viewer).getScene().getWindow(), e.getScreenX(), e.getScreenY()); } } } + private IProject getProject() { + IProject activeProject = null; + IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + if (page.getActiveEditor().getEditorInput() instanceof IFileEditorInput) { + IFileEditorInput input = (IFileEditorInput) page.getActiveEditor().getEditorInput(); + IFile file = input.getFile(); + activeProject = file.getProject(); + } + return activeProject; + } + private ITopoGraph getGraphFromHost() { IVisualPart part = getHost().getRoot().getChildrenUnmodifiable().get(0); if (part instanceof TopoGraphPart) { diff --git a/org.sofproject.topo.ui/src/org/sofproject/topo/ui/graph/ITopoGraph.java b/org.sofproject.topo.ui/src/org/sofproject/topo/ui/graph/ITopoGraph.java index 34b896f..08cee8d 100644 --- a/org.sofproject.topo.ui/src/org/sofproject/topo/ui/graph/ITopoGraph.java +++ b/org.sofproject.topo.ui/src/org/sofproject/topo/ui/graph/ITopoGraph.java @@ -36,6 +36,7 @@ import org.eclipse.core.runtime.CoreException; import org.sofproject.core.binfile.BinFile; import org.sofproject.core.ops.IRemoteOpsProvider; +import org.sofproject.topo.ui.json.JsonProperty; /** * Topology graph, implemented by a specific topology binding. @@ -69,6 +70,8 @@ public interface ITopoGraph { public BinFile getBinTopology(); public void serialize() throws CoreException, IOException; - + + public String getPipelineString(); + public IRemoteOpsProvider getRemoteOpsProvider(); } diff --git a/org.sofproject.topo.ui/src/org/sofproject/topo/ui/json/JsonCustomOptionPane.java b/org.sofproject.topo.ui/src/org/sofproject/topo/ui/json/JsonCustomOptionPane.java new file mode 100644 index 0000000..6e8c5d0 --- /dev/null +++ b/org.sofproject.topo.ui/src/org/sofproject/topo/ui/json/JsonCustomOptionPane.java @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2019, Intel Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Intel Corporation nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +package org.sofproject.topo.ui.json; + +import java.io.IOException; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Combo; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.MessageBox; +import org.eclipse.swt.widgets.Monitor; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; +import org.sofproject.topo.ui.graph.ITopoGraph; +import org.sofproject.topo.ui.json.JsonUtils; + +public class JsonCustomOptionPane { + + ITopoGraph graph; + + private static boolean isInteger(String s) { + try { + Integer.parseInt(s); + } catch (NumberFormatException e) { + return false; + } catch (NullPointerException e) { + return false; + } + return true; + } + + public JsonCustomOptionPane(Display display, ITopoGraph graph, JsonUtils jsonUtils) { + this.graph = graph; + Shell shell = new Shell(display, SWT.CLOSE | SWT.TITLE | SWT.MIN); + + shell.setText("Serialize JSON"); + + GridLayout gridLayout = new GridLayout(4, false); + gridLayout.verticalSpacing = 8; + shell.setLayout( gridLayout); + + new Label(shell, SWT.NULL).setText("Name:"); + Text nameText = new Text(shell, SWT.SINGLE | SWT.BORDER); + GridData nameGridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); + nameGridData.horizontalSpan = 3; + nameText.setLayoutData(nameGridData); + + new Label(shell, SWT.NULL).setText("Version:"); + Text versionText = new Text(shell, SWT.BORDER); + GridData versionGridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); + versionGridData.horizontalSpan = 3; + versionText.setLayoutData(versionGridData); + + new Label(shell, SWT.NULL).setText("Description:"); + Text descriptionText = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL); + GridData descriptionGridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); + descriptionGridData.horizontalSpan = 3; + descriptionGridData.heightHint = 100; + descriptionGridData.widthHint = 200; + descriptionText.setLayoutData(descriptionGridData); + + new Label(shell, SWT.NULL).setText("Type:"); + Combo typeCombo = new Combo(shell, SWT.READ_ONLY); + typeCombo.setBounds(50, 50, 200, 65); + String items[] = { "GStreamer", "Ffmpeg" }; + typeCombo.setItems(items); + typeCombo.setText(items[0]); + GridData typeGridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); + typeGridData.horizontalSpan = 3; + typeCombo.setLayoutData(typeGridData); + + Button okButton = new Button(shell, SWT.PUSH); + okButton.setText("Ok"); + GridData buttonGridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); + okButton.setLayoutData(buttonGridData); + okButton.addSelectionListener(new SelectionAdapter() { + + @Override + public void widgetSelected(SelectionEvent e) { + if (nameText.getText().isEmpty()) { + MessageBox messageBox = new MessageBox(shell, SWT.ERROR); + messageBox.setMessage("Name cannot be empty!"); + messageBox.open(); + } else if (!isInteger(versionText.getText())) { + MessageBox messageBox = new MessageBox(shell, SWT.ERROR); + messageBox.setMessage("Version number should be an integer!"); + messageBox.open(); + } else { + try { + JsonProperty jsonProperty = new JsonProperty(nameText.getText(), descriptionText.getText(), + versionText.getText(), typeCombo.getItem(typeCombo.getSelectionIndex())); + jsonUtils.serializeJson(jsonProperty, graph.getPipelineString()); + shell.close(); + } catch (CoreException | IOException error) { + error.printStackTrace(); // TODO: + } + } + } + }); + + Button cancelButton = new Button(shell, SWT.PUSH); + cancelButton.setText("Cancel"); + GridData cancelbuttonGridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); + cancelButton.setLayoutData(cancelbuttonGridData); + cancelButton.addSelectionListener(new SelectionAdapter() { + + @Override + public void widgetSelected(SelectionEvent e) { + shell.close(); + } + }); + + Monitor primary = display.getPrimaryMonitor(); + Rectangle bounds = primary.getBounds(); + Rectangle rect = shell.getBounds(); + int x = bounds.x + (bounds.width - rect.width) / 2; + int y = bounds.y + (bounds.height - rect.height) / 2; + shell.setLocation(x, y); + + shell.pack(); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + } + +} diff --git a/org.sofproject.topo.ui/src/org/sofproject/topo/ui/json/JsonProperty.java b/org.sofproject.topo.ui/src/org/sofproject/topo/ui/json/JsonProperty.java new file mode 100644 index 0000000..02236a7 --- /dev/null +++ b/org.sofproject.topo.ui/src/org/sofproject/topo/ui/json/JsonProperty.java @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2019, Intel Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Intel Corporation nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +package org.sofproject.topo.ui.json; + +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +@JsonSerialize +public class JsonProperty { + + private String name; + private String description; + private String version; + private String type; + private String template; + private Parameters parameters; + + public JsonProperty(String name, String description, String version, String type) { + this.name = name; + this.description = description; + this.version = version; + this.type = type; + + // set default parameters + this.parameters = new Parameters("object", new Properties()); + } + + public String getName() { + return name; + } + + public String getDescription() { + return description; + } + + public String getVersion() { + return version; + } + + public String getType() { + return type; + } + + public String getTemplate() { + return template; + } + + public Parameters getParameters() { + return parameters; + } + + public void setTemplate(String newTemplate) { + this.template = newTemplate; + } + + public void setParameters(Parameters parameters) { + this.parameters = parameters; + } +} + +@JsonSerialize +class Parameters { + private String type; + private Properties properties; + + public Parameters(String type, Properties properties) { + this.type = type; + this.properties = properties; + } + + public String getType() { + return type; + } + + public Properties getProperties() { + return properties; + } +} + +@JsonSerialize +class Properties { + public Properties() { + } +} diff --git a/org.sofproject.topo.ui/src/org/sofproject/topo/ui/json/JsonUtils.java b/org.sofproject.topo.ui/src/org/sofproject/topo/ui/json/JsonUtils.java new file mode 100644 index 0000000..263688e --- /dev/null +++ b/org.sofproject.topo.ui/src/org/sofproject/topo/ui/json/JsonUtils.java @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2019, Intel Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Intel Corporation nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +package org.sofproject.topo.ui.json; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Paths; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.PlatformUI; +import org.sofproject.core.ops.IRemoteOpsProvider; +import org.sofproject.gst.topo.ops.GstDockerOpsProvider; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class JsonUtils { + + JsonProperty jsonProperty_; + + public void serializeJson(JsonProperty jsonProperty, String pipelineString) throws CoreException, IOException { + try { + String projectPath = getProjectPath(); + File file; + if (projectPath != null) { + String path = Paths.get(projectPath, jsonProperty.getType().toLowerCase(), jsonProperty.getName(), + jsonProperty.getVersion()).toString(); + new File(path).mkdirs(); + file = new File(Paths.get(path, "pipeline.json").toString()); + } else { + file = new File(jsonProperty.getName() + ".json"); + } + jsonProperty.setTemplate(pipelineString); + jsonProperty_ = jsonProperty; + BufferedWriter writer = new BufferedWriter(new FileWriter(file)); + ObjectMapper obj = new ObjectMapper(); + obj.writeValue(writer, jsonProperty); + writer.close(); + + } catch (Exception e) { + System.out.println(e.toString()); + } + } + + public String getProjectPath() { + IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); + IWorkbenchPage activePage = window.getActivePage(); + + IEditorPart activeEditor = activePage.getActiveEditor(); + + if (activeEditor != null) { + IEditorInput input = activeEditor.getEditorInput(); + + IProject project = input.getAdapter(IProject.class); + if (project == null) { + IResource resource = input.getAdapter(IResource.class); + if (resource != null) { + project = resource.getProject(); + IPath path = project.getLocation(); + return path.toString(); + } + } + } + + return null; + } + + public IRemoteOpsProvider getDockerOpsProvider() { + return new GstDockerOpsProvider(this); + } + + public JsonProperty getJsonProperty() { + return jsonProperty_; + } + +}