Commit acb0dfb1 authored by Spiros Koulouzis's avatar Spiros Koulouzis

Added utils

Remved IParameter to simplify unmarshalling with jackson
Added tests in commons for types  
parent e1e33d95
/drip-commons/target/
/drip-api/target/
/drip-planner/target/
/drip-commons/nbproject/
\ No newline at end of file
......@@ -15,6 +15,7 @@
*/
package nl.uva.sne.drip.api.rest;
import com.fasterxml.jackson.databind.ObjectMapper;
import nl.uva.sne.drip.commons.types.Parameter;
import nl.uva.sne.drip.api.rpc.PlannerCaller;
import java.io.IOException;
......@@ -26,7 +27,6 @@ import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import nl.uva.sne.drip.commons.types.IParameter;
import nl.uva.sne.drip.commons.types.Message;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
......@@ -56,81 +56,55 @@ public class UploadToscaController {
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody
String toscaUpload(@RequestParam("file") MultipartFile file) {
PlannerCaller planner = null;
if (!file.isEmpty()) {
try {
String originalFileName = file.getOriginalFilename();
String name = System.currentTimeMillis() + "_" + originalFileName;
// File targetToscaFile = new File(inputToscaFolderPath + File.separator + name);
// file.transferTo(targetToscaFile);
Message invokationMessage = new Message();
List parameters = new ArrayList();
Parameter fileArgument = new Parameter();
byte[] bytes = file.getBytes();//Files.readAllBytes(Paths.get(targetToscaFile.getAbsolutePath()));
byte[] bytes = file.getBytes();
String charset = "UTF-8";
fileArgument.setValue(new String(bytes, charset));
fileArgument.setEncoding(charset);
fileArgument.setName(name);
fileArgument.setName("input");
parameters.add(fileArgument);
fileArgument = new Parameter();
bytes = Files.readAllBytes(Paths.get("/home/alogo/Downloads/DRIP/example_a.yml"));
fileArgument.setValue(new String(bytes, charset));
fileArgument.setEncoding(charset);
fileArgument.setName("example_a.yml");
fileArgument.setName("example");
parameters.add(fileArgument);
invokationMessage.setParameters(parameters);
invokationMessage.setCreationDate(new Date(System.currentTimeMillis()));
invokationMessage.setCreationDate((System.currentTimeMillis()));
PlannerCaller planner = new PlannerCaller(messageBrokerHost);
planner = new PlannerCaller(messageBrokerHost);
String returned = planner.plan(invokationMessage);
ObjectMapper mapper = new ObjectMapper();
Message request = mapper.readValue(returned, Message.class);
System.err.println(returned);
System.err.println(request.getCreationDate());
planner.close();
return "You successfully uploaded " + name + " into " + name + "-uploaded !";
} catch (IOException | IllegalStateException | TimeoutException | InterruptedException ex) {
Logger.getLogger(UploadToscaController.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (planner != null) {
try {
planner.close();
} catch (IOException | TimeoutException ex) {
Logger.getLogger(UploadToscaController.class.getName()).log(Level.WARNING, null, ex);
}
}
return "Upload failed. 'file' was empty.";
}
@RequestMapping(value = "/args", method = RequestMethod.GET)
public Message args() {
try {
Message r = new Message();
List<IParameter> args = new ArrayList();
IParameter intParam = new Parameter();
intParam.setValue("1");
args.add(intParam);
Parameter strParam = new Parameter();
strParam.setValue("string");
args.add(strParam);
IParameter targetToscaFile = new Parameter();
byte[] bytes = Files.readAllBytes(Paths.get("/home/alogo/Downloads/planner_output_all.yml"));
targetToscaFile.setValue(new String(bytes, "UTF-8"));
targetToscaFile.setName("planner_output_all.yml");
targetToscaFile.setEncoding("UTF-8");
args.add(targetToscaFile);
IParameter file = new Parameter();
file.setName("Dockerfile");
file.setURL("https://github.com/QCAPI-DRIP/DRIP-integradation/releases/download/valpha/Dockerfile");
args.add(file);
r.setParameters(args);
r.setCreationDate(new Date(System.currentTimeMillis()));
// ObjectMapper mapper = new ObjectMapper();
// String jsonInString = mapper.writeValueAsString(r);
// System.err.println(jsonInString);
return r;
} catch (IOException ex) {
Logger.getLogger(UploadToscaController.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
return "Upload failed. 'file' was empty.";
}
}
......@@ -15,6 +15,33 @@
<version>2.8.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
<type>jar</type>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
......
......@@ -24,11 +24,13 @@ import java.util.List;
*/
public interface IMessage {
public Date getCreationDate();
public static final String CREATION_DATE = "creationDate";
public void setCreationDate(Date creationDate);
public Long getCreationDate();
public void setParameters(List<IParameter> params);
public void setCreationDate(Long creationDate);
public void setParameters(List<Parameter> params);
public List getParameters();
......
......@@ -19,6 +19,7 @@ import com.fasterxml.jackson.annotation.JsonFormat;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;
//import javax.persistence.Column;
//import javax.persistence.Entity;
//import javax.persistence.GeneratedValue;
......@@ -36,30 +37,29 @@ import java.util.List;
//@Entity
public class Message implements IMessage, Serializable {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.S")
// @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.S")
// @Temporal(TemporalType.DATE)
private Date creationDate;
private Long creationDate;
// @Column
private List<IParameter> parameters;
private List<Parameter> parameters;
@Override
public Date getCreationDate() {
public Long getCreationDate() {
return this.creationDate;
}
@Override
public void setParameters(List<IParameter> args) {
this.parameters = args;
public void setParameters(List<Parameter> parameters) {
this.parameters = parameters;
}
@Override
public List getParameters() {
public List<Parameter> getParameters() {
return this.parameters;
}
@Override
public void setCreationDate(Date creationDate) {
public void setCreationDate(Long creationDate) {
this.creationDate = creationDate;
}
......
......@@ -21,49 +21,55 @@ import java.io.Serializable;
*
* @author S. Koulouzis.
*/
public class Parameter implements IParameter, Serializable {
public class Parameter implements Serializable {
private String url;
private String encoding;
private String value;
private String name;
@Override
public static final String NAME = "name";
public static final String URL = "url";
public static final String VALUE = "value";
public static final String ENCODING = "encoding";
// @Override
public String getURL() {
return this.url;
}
@Override
// @Override
public void setURL(String url) {
this.url = url;
}
@Override
// @Override
public String getEncoding() {
return this.encoding;
}
@Override
// @Override
public void setEncoding(String encoding) {
this.encoding = encoding;
}
@Override
// @Override
public void setName(String name) {
this.name = name;
}
@Override
// @Override
public String getName() {
return this.name;
}
//
// @Override
@Override
public String getValue() {
return this.value;
}
@Override
// @Override
public void setValue(String value) {
this.value = value;
}
......
......@@ -13,33 +13,44 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.commons.types;
package nl.uva.sne.drip.commons.utils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
*
* @author S. Koulouzis
*/
public interface IParameter {
public static final String NAME = "name";
public static final String URL = "url";
public static final String VALUE = "value";
public static final String ENCODING = "encoding";
public void setName(String name);
public String getName();
public String getValue();
public void setValue(String value);
public String getURL();
public void setURL(String url);
public String getEncoding();
public void setEncoding(String encoding);
public class FileHash {
/**
* Code from: http://www.mkyong.com/java/java-sha-hashing-example/
*
* @param filePath
* @return
* @throws NoSuchAlgorithmException
* @throws FileNotFoundException
* @throws IOException
*/
public static String getSHA256(String filePath) throws NoSuchAlgorithmException, FileNotFoundException, IOException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
FileInputStream fis = new FileInputStream(filePath);
byte[] dataBytes = new byte[1024];
int nread;
while ((nread = fis.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
}
byte[] mdbytes = md.digest();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mdbytes.length; i++) {
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
}
/*
* Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.commons.types;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import nl.uva.sne.drip.commons.utils.FileHash;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
*
* @author alogo
*/
public class TypesJUnitTest {
private static File tempFile1;
private static String tempFile1Hash;
private static Message message1;
private static String fileParamContentsName;
private static File tempFile2;
private static String tempFile2Hash;
private static List<File> files;
@BeforeClass
public static void setUpClass() throws IOException, NoSuchAlgorithmException {
files = new ArrayList<>();
initTmpRandomFiles();
initMessages();
}
@AfterClass
public static void tearDownClass() {
if (tempFile1 != null) {
tempFile1.delete();
}
}
private static void initTmpRandomFiles() throws IOException, NoSuchAlgorithmException {
tempFile1 = File.createTempFile("temp-", Long.toString(System.nanoTime()));
try (PrintWriter writer = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(tempFile1), "UTF-8")), false)) {
Random random = new Random();
String sep = "";
for (int i = 0; i < 100; i++) {
int number = random.nextInt(1000) + 1;
writer.print(sep);
writer.print(number / 1e3);
sep = " ";
writer.println();
}
writer.println();
}
tempFile1Hash = FileHash.getSHA256(tempFile1.getAbsolutePath());
files.add(tempFile1);
tempFile2 = File.createTempFile("temp-", Long.toString(System.nanoTime()));
try (PrintWriter writer = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(tempFile2), "UTF-8")), false)) {
Random random = new Random();
String sep = "";
for (int i = 0; i < 100; i++) {
int number = random.nextInt(1000) + 1;
writer.print(sep);
writer.print(number / 1e3);
sep = " ";
writer.println();
}
writer.println();
}
tempFile2Hash = FileHash.getSHA256(tempFile2.getAbsolutePath());
files.add(tempFile2);
}
private static void initMessages() throws IOException {
message1 = new Message();
message1.setCreationDate((System.currentTimeMillis()));
List<Parameter> parameters = new ArrayList();
Parameter numParam = new Parameter();
String numParamName = "input";
numParam.setName(numParamName);
numParam.setValue("33000");
parameters.add(numParam);
Parameter fileParamContents = new Parameter();
fileParamContentsName = "someInputFile";
fileParamContents.setName(fileParamContentsName);
byte[] bytes = Files.readAllBytes(Paths.get(tempFile1.getAbsolutePath()));
String charset = "UTF-8";
fileParamContents.setValue(new String(bytes, charset));
fileParamContents.setEncoding(charset);
parameters.add(fileParamContents);
Parameter fileParamRef = new Parameter();
fileParamRef.setName("theNameOfTheParamater");
fileParamRef.setURL("http://www.gutenberg.org/cache/epub/3160/pg3160.txt");
parameters.add(fileParamRef);
message1.setParameters(parameters);
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
@Test
public void testMarshallUnmarshalJackson() throws IOException, NoSuchAlgorithmException {
File fileParam = null;
try {
ObjectMapper mapper = new ObjectMapper();
String jsonInString = mapper.writeValueAsString(message1);
// System.err.println(jsonInString);
assertTrue("JSON should contain " + IMessage.CREATION_DATE, jsonInString.contains(IMessage.CREATION_DATE));
assertTrue("JSON should contain parameters", jsonInString.contains("parameters"));
// System.err.println(jsonInString);
Message request = mapper.readValue(jsonInString, Message.class);
List<Parameter> params = request.getParameters();
assertEquals(message1.getParameters().size(), params.size());
fileParam = new File(fileParamContentsName);
for (Parameter param : params) {
if (param.getName().equals(fileParamContentsName)) {
String value = param.getValue();
try (PrintWriter out = new PrintWriter(fileParam)) {
out.print(value);
}
break;
}
}
assertNotNull(fileParam);
assertEquals(tempFile1.length(), fileParam.length());
String fileParamHash = FileHash.getSHA256(fileParam.getAbsolutePath());
assertEquals(tempFile1Hash, fileParamHash);
} finally {
if (fileParam != null) {
fileParam.delete();
}
}
}
@Test
public void testMarshallUnmarshalSimpleJson() throws IOException, NoSuchAlgorithmException, JSONException {
File fileParam = null;
try {
ObjectMapper mapper = new ObjectMapper();
String jsonInString = mapper.writeValueAsString(message1);
JSONObject jo = new JSONObject(jsonInString);
JSONArray parameters = jo.getJSONArray("parameters");
fileParam = new File(fileParamContentsName);
for (int i = 0; i < parameters.length(); i++) {
JSONObject param = (JSONObject) parameters.get(i);
String name = (String) param.get(Parameter.NAME);
if (name.equals(fileParamContentsName)) {
String value = (String) param.get(Parameter.VALUE);
try (PrintWriter out = new PrintWriter(fileParam)) {
out.print(value);
}
break;
}
}
assertNotNull(fileParam);
assertEquals(tempFile1.length(), fileParam.length());
String fileParamHash = FileHash.getSHA256(fileParam.getAbsolutePath());
assertEquals(tempFile1Hash, fileParamHash);
} finally {
if (fileParam != null) {
fileParam.delete();
}
}
}
@Test
public void testJacksonMarshal() throws UnsupportedEncodingException, IOException {
Message responseMessage = new Message();
List parameters = new ArrayList();
String charset = "UTF-8";
for (File f : files) {
Parameter fileParam = new Parameter();
byte[] bytes = Files.readAllBytes(Paths.get(f.getAbsolutePath()));
fileParam.setValue(new String(bytes, charset));
fileParam.setEncoding(charset);
fileParam.setName(f.getName());
parameters.add(fileParam);
}
responseMessage.setParameters(parameters);
responseMessage.setCreationDate((System.currentTimeMillis()));
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(responseMessage);
}
@Test
public void testsimpleJsonMarshal() throws JSONException, IOException {
JSONObject jo = new JSONObject();
jo.put("creationDate", (System.currentTimeMillis()));
List parameters = new ArrayList();
String charset = "UTF-8";
for (File f : files) {
Map<String, String> fileArguments = new HashMap<>();
fileArguments.put("encoding", charset);
fileArguments.put("name", f.getName());
byte[] bytes = Files.readAllBytes(Paths.get(f.getAbsolutePath()));
fileArguments.put("value", new String(bytes, charset));
parameters.add(fileArguments);
}
jo.put("parameters", parameters);
ObjectMapper mapper = new ObjectMapper();
Message request = mapper.readValue(jo.toString(), Message.class);
Date cDate = new Date(request.getCreationDate());
}
}
......@@ -65,18 +65,24 @@ public class Consumer extends DefaultConsumer {
try {
String message = new String(body, "UTF-8");
File[] inputFiles;
File tempDir = new File(System.getProperty("java.io.tmpdir") + File.separator + this.getClass().getSimpleName() + "-" + Long.toString(System.nanoTime()));
if (!(tempDir.mkdirs())) {
throw new FileNotFoundException("Could not create output directory: " + tempDir.getAbsolutePath());
}
inputFiles = jacksonUnmarshalExample(message);
panner.plan(inputFiles[0].getAbsolutePath(), inputFiles[1].getAbsolutePath(), "/tmp/out");
panner.plan(inputFiles[0].getAbsolutePath(), inputFiles[1].getAbsolutePath(), tempDir.getAbsolutePath());
inputFiles = simpleJsonUnmarshalExample(message);
List<File> files = panner.plan(inputFiles[0].getAbsolutePath(), inputFiles[1].getAbsolutePath(), "/tmp/out");
List<File> files = panner.plan(inputFiles[0].getAbsolutePath(), inputFiles[1].getAbsolutePath(), tempDir.getAbsolutePath());
response = jacksonMarshalExample(files);
System.err.println(response);
response = simpleJsonMarshalExample(files);
System.err.println(response);
} catch (RuntimeException | JSONException ex) {
} catch (JSONException | FileNotFoundException ex) {
response = ex.getMessage();
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
} finally {
channel.basicPublish("", properties.getReplyTo(), replyProps, response.getBytes("UTF-8"));
......@@ -89,50 +95,48 @@ public class Consumer extends DefaultConsumer {
File[] files = new File[2];
ObjectMapper mapper = new ObjectMapper();
Message request = mapper.readValue(message, Message.class);
//We know that in parameters 0-1 we should have files
for (int i = 0; i < 2; i++) {
Map<String, String> fileArg = (java.util.LinkedHashMap) request.getParameters().get(i);
String fileName = fileArg.get(Parameter.NAME);
//If not null should be able to use it to download file
String url = fileArg.get(Parameter.URL);
if (url != null) {
//download
List<Parameter> params = request.getParameters();
File inputFile = File.createTempFile("input-", Long.toString(System.nanoTime()));
File exampleFile = File.createTempFile("example-", Long.toString(System.nanoTime()));
for (Parameter param : params) {
if (param.getName().equals("input")) {
try (PrintWriter out = new PrintWriter(inputFile)) {
out.print(param.getValue());
}
String encoding = fileArg.get(Parameter.ENCODING);
String value = fileArg.get(Parameter.VALUE);
try (PrintWriter out = new PrintWriter(fileName)) {
out.print(value);
files[0] = inputFile;
}
if (param.getName().equals("example")) {
try (PrintWriter out = new PrintWriter(exampleFile)) {
out.print(param.getValue());
}
files[1] = exampleFile;
}
files[i] = new File(fileName);
}
return files;
}
private File[] simpleJsonUnmarshalExample(String message) throws JSONException, FileNotFoundException {
private File[] simpleJsonUnmarshalExample(String message) throws JSONException, FileNotFoundException, IOException {
File[] files = new File[2];
JSONObject jo = new JSONObject(message);
JSONArray args = jo.getJSONArray("parameters");
//We know that in parameters 0-2 we should have files
for (int i = 0; i < 2; i++) {
JSONObject arg = (JSONObject) args.get(i);
String fileName = (String) arg.get(Parameter.NAME);
//If not null should be able to use it to download file
if (!arg.isNull("url")) {
String url = (String) arg.get("url");
//download
JSONArray parameters = jo.getJSONArray("parameters");
File inputFile = File.createTempFile("input-", Long.toString(System.nanoTime()));
File exampleFile = File.createTempFile("example-", Long.toString(System.nanoTime()));
for (int i = 0; i < parameters.length(); i++) {
JSONObject param = (JSONObject) parameters.get(i);
String name = (String) param.get(Parameter.NAME);
if (name.equals("input")) {
try (PrintWriter out = new PrintWriter(inputFile)) {
out.print(param.get(Parameter.VALUE));
}
//Otherwise get contents as string
String value = (String) arg.get(Parameter.VALUE);
String encoding = (String) arg.get(Parameter.ENCODING);
try (PrintWriter out = new PrintWriter(fileName)) {
out.print(value);
files[0] = inputFile;
}
if (name.equals("example")) {
try (PrintWriter out = new PrintWriter(exampleFile)) {
out.print(param.get(Parameter.VALUE));
}
files[1] = exampleFile;
}
files[i] = new File(fileName);
}
return files;
}
......@@ -149,10 +153,9 @@ public class Consumer extends DefaultConsumer {
fileParam.setEncoding(charset);
fileParam.setName(f.getName());
parameters.add(fileParam);
}
responseMessage.setParameters(parameters);
responseMessage.setCreationDate(new Date(System.currentTimeMillis()));
responseMessage.setCreationDate((System.currentTimeMillis()));
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(responseMessage);
......@@ -160,7 +163,7 @@ public class Consumer extends DefaultConsumer {
private String simpleJsonMarshalExample(List<File> files) throws JSONException, IOException {
JSONObject jo = new JSONObject();
jo.put("creationDate", new Date(System.currentTimeMillis()));
jo.put("creationDate", (System.currentTimeMillis()));
List parameters = new ArrayList();
String charset = "UTF-8";
for (File f : files) {
......
......@@ -111,6 +111,7 @@ public class Planner {
outputFiles.add(new File(outfPath));
outputFiles.add(new File(allFilePath));
return outputFiles;
}
private String generateVM(String block, String nodeName, String dockerName, String privateAddress, String role) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment