Commit a5bb42ce authored by Spiros Koulouzis's avatar Spiros Koulouzis

implemented message for drip componets

implmented example planner 
parent 92988cd0
/drip-commons/target/
/drip-api/target/
\ No newline at end of file
/drip-api/target/
/drip-planner/target/
\ No newline at end of file
......@@ -15,11 +15,11 @@
*/
package nl.uva.sne.drip.api.rest;
import nl.uva.sne.drip.commons.types.FileParameter;
import com.fasterxml.jackson.databind.ObjectMapper;
import nl.uva.sne.drip.api.rpc.Planner;
import nl.uva.sne.drip.api.rpc.PlannerCaller;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
......@@ -28,25 +28,16 @@ import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import nl.uva.sne.drip.commons.types.IRequest;
import nl.uva.sne.drip.commons.types.Request;
import nl.uva.sne.drip.commons.types.Message;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import nl.uva.sne.drip.commons.types.IMessage;
/**
*
......@@ -71,22 +62,33 @@ public class UploadToscaController {
try {
String originalFileName = file.getOriginalFilename();
String name = originalFileName + System.currentTimeMillis();
File targetToscaFile = new File(inputToscaFolderPath + File.separator + name);
file.transferTo(targetToscaFile);
String name = System.currentTimeMillis() + "_" + originalFileName;
// File targetToscaFile = new File(inputToscaFolderPath + File.separator + name);
// file.transferTo(targetToscaFile);
Planner planner = new Planner(messageBrokerHost);
Message invokationMessage = new Message();
Request r = new Request();
List args = new ArrayList();
FileArgument fileArg = new FileArgument();
fileArg.setURL(targetToscaFile.toURI().toString());
args.add(targetToscaFile);
r.setArguments(args);
r.setCreationDate(new Date(System.currentTimeMillis()));
r.setStatus(IRequest.Status.SUCCESS);
List parameters = new ArrayList();
FileParameter fileArgument = new FileParameter();
byte[] bytes = file.getBytes();//Files.readAllBytes(Paths.get(targetToscaFile.getAbsolutePath()));
String charset = "UTF-8";
fileArgument.setValue(new String(bytes, charset));
fileArgument.setEncoding(charset);
fileArgument.setName(name);
parameters.add(fileArgument);
String returned = planner.plan(r);
fileArgument = new FileParameter();
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");
parameters.add(fileArgument);
invokationMessage.setParameters(parameters);
invokationMessage.setCreationDate(new Date(System.currentTimeMillis()));
PlannerCaller planner = new PlannerCaller(messageBrokerHost);
String returned = planner.plan(invokationMessage);
planner.close();
return "You successfully uploaded " + name + " into " + name + "-uploaded !";
......@@ -98,26 +100,25 @@ public class UploadToscaController {
}
@RequestMapping(value = "/args", method = RequestMethod.GET)
public Request args() {
public Message args() {
try {
Request r = new Request();
Message r = new Message();
List args = new ArrayList();
args.add(1);
args.add("str");
FileArgument targetToscaFile = new FileArgument();
FileParameter targetToscaFile = new FileParameter();
byte[] bytes = Files.readAllBytes(Paths.get("/home/alogo/Downloads/planner_output_all.yml"));
targetToscaFile.setContents(new String(bytes, "UTF-8"));
targetToscaFile.setValue(new String(bytes, "UTF-8"));
targetToscaFile.setName("planner_output_all.yml");
targetToscaFile.setEncoding("UTF-8");
args.add(targetToscaFile);
r.setArguments(args);
r.setParameters(args);
r.setCreationDate(new Date(System.currentTimeMillis()));
r.setStatus(IRequest.Status.SUCCESS);
ObjectMapper mapper = new ObjectMapper();
String jsonInString = mapper.writeValueAsString(r);
System.err.println(jsonInString);
// 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);
......
......@@ -19,9 +19,9 @@ package nl.uva.sne.drip.api.rpc;
*
* @author S. Koulouzis.
*/
class DRIPComponent {
class DRIPComponentCaller {
public DRIPComponent(){
public DRIPComponentCaller(){
}
......
......@@ -16,7 +16,6 @@
package nl.uva.sne.drip.api.rpc;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import com.rabbitmq.client.AMQP;
......@@ -25,27 +24,24 @@ import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.RpcClient;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeoutException;
import nl.uva.sne.drip.commons.types.Request;
import nl.uva.sne.drip.commons.types.Message;
/**
*
* @author S. Koulouzis.
*/
public class Planner {
public class PlannerCaller {
private static final String REQUEST_QUEUE_NAME = "planner_queue";
private final Connection connection;
private final Channel channel;
private final String replyQueueName;
public Planner(String messageBrokerHost) throws IOException, TimeoutException {
public PlannerCaller(String messageBrokerHost) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(messageBrokerHost);
......@@ -59,7 +55,7 @@ public class Planner {
replyQueueName = channel.queueDeclare().getQueue();
}
public String plan(Request r) throws IOException, TimeoutException, InterruptedException {
public String plan(Message r) throws IOException, TimeoutException, InterruptedException {
ObjectMapper mapper = new ObjectMapper();
String jsonInString = mapper.writeValueAsString(r);
......
......@@ -13,35 +13,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.api.rest;
package nl.uva.sne.drip.commons.types;
import java.io.Serializable;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
*
* @author S. Koulouzis.
*/
class FileArgument implements IFileArgument, Serializable {
public class FileParameter implements IFileParameter, IParameter, Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String id;
public static final String NAME = "name";
public static final String URL = "url";
public static final String VALUE = "value";
public static final String ENCODING = "encoding";
private String url;
private String encoding;
private String contents;
@Override
public void setID(String id) {
this.id = id;
}
@Override
public String getID() {
return this.id;
}
private String value;
private String name;
@Override
public String getURL() {
......@@ -64,13 +54,23 @@ class FileArgument implements IFileArgument, Serializable {
}
@Override
public String getContents() {
return this.contents;
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
@Override
public String getValue() {
return this.value;
}
@Override
public void setContents(String contents) {
this.contents = contents;
public void setValue(String value) {
this.value = value;
}
}
......@@ -13,17 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.api.rest;
package nl.uva.sne.drip.commons.types;
/**
*
* @author S. Koulouzis.
*/
interface IFileArgument {
public void setID(String id);
public String getID();
public interface IFileParameter {
public String getURL();
......@@ -33,8 +29,8 @@ interface IFileArgument {
public void setEncoding(String encoding);
public String getContents();
public void setName(String name);
public void setContents(String contents);
public String getName();
}
......@@ -22,27 +22,14 @@ import java.util.List;
*
* @author S. Koulouzis
*/
public interface IRequest {
public enum Status {
FAILED,
SUCCESS;
}
public void setID(String id);
public String getID();
public Date getCreationDate();
public interface IMessage {
public void setCreationDate(Date creationDate);
public Status getStatus();
public Date getCreationDate();
public void setStatus(Status status);
public void setCreationDate(Date creationDate);
public void setArguments(List args);
public void setParameters(List args);
public List getArguments();
public List getParameters();
}
/*
* 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;
/**
*
* @author S. Koulouzis
*/
public interface IParameter {
public void setName(String name);
public String getName();
public String getValue();
public void setValue(String value);
}
......@@ -34,55 +34,28 @@ import java.util.List;
* @author S. Koulouzis.
*/
//@Entity
public class Request implements IRequest, Serializable {
// @Id
// @GeneratedValue(strategy = GenerationType.AUTO)
private String id;
public class Message implements IMessage, Serializable {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.S")
// @Temporal(TemporalType.DATE)
private Date creationDate;
// @Column
private Status status;
// @Column
private List arguments;
@Override
public void setID(String id) {
this.id = id;
}
@Override
public String getID() {
return this.id;
}
private List<IParameter> parameters;
@Override
public Date getCreationDate() {
return this.creationDate;
}
@Override
public Status getStatus() {
return this.status;
}
@Override
public void setStatus(Status status) {
status = this.status;
}
@Override
public void setArguments(List args) {
this.arguments = args;
public void setParameters(List args) {
this.parameters = args;
}
@Override
public List getArguments() {
return this.arguments;
public List getParameters() {
return this.parameters;
}
@Override
......
......@@ -14,10 +14,28 @@
</properties>
<dependencies>
<dependency>
<groupId>nl.uva.sne.drip</groupId>
<artifactId>drip-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
<type>jar</type>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -15,11 +15,32 @@
*/
package nl.uva.sne.drip.drip.planner;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import nl.uva.sne.drip.commons.types.FileParameter;
import nl.uva.sne.drip.commons.types.Message;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import nl.uva.sne.drip.commons.types.IFileParameter;
/**
*
......@@ -27,13 +48,13 @@ import java.io.IOException;
*/
public class Consumer extends DefaultConsumer {
private static final String RPC_QUEUE_NAME = "planner_queue";
private static final String HOST = "172.17.0.2";
private final Channel channel;
private final Planner panner;
public Consumer(Channel channel) {
super(channel);
this.channel = channel;
this.panner = new Planner();
}
@Override
......@@ -41,22 +62,119 @@ public class Consumer extends DefaultConsumer {
AMQP.BasicProperties replyProps = new AMQP.BasicProperties.Builder()
.correlationId(properties.getCorrelationId())
.build();
String response = "";
try {
String message = new String(body, "UTF-8");
System.out.println(" [.] fib(" + message + ")");
File[] inputFiles;
inputFiles = jacksonUnmarshalExample(message);
panner.plan(inputFiles[0].getAbsolutePath(), inputFiles[1].getAbsolutePath(), "/tmp/out");
inputFiles = simpleJsonUnmarshalExample(message);
List<File> files = panner.plan(inputFiles[0].getAbsolutePath(), inputFiles[1].getAbsolutePath(), "/tmp/out");
response = jacksonMarshalExample(files);
System.err.println(response);
response = simpleJsonMarshalExample(files);
System.err.println(response);
response += message;
} catch (RuntimeException e) {
System.out.println(" [.] " + e.toString());
} catch (RuntimeException | JSONException ex) {
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
} finally {
channel.basicPublish("", properties.getReplyTo(), replyProps, response.getBytes("UTF-8"));
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
private File[] jacksonUnmarshalExample(String message) throws IOException {
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(FileParameter.NAME);
//If not null should be able to use it to download file
String url = fileArg.get(FileParameter.URL);
if (url != null) {
//download
}
String encoding = fileArg.get(FileParameter.ENCODING);
String value = fileArg.get(FileParameter.VALUE);
try (PrintWriter out = new PrintWriter(fileName)) {
out.print(value);
}
files[i] = new File(fileName);
}
return files;
}
private File[] simpleJsonUnmarshalExample(String message) throws JSONException, FileNotFoundException {
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(FileParameter.NAME);
//If not null should be able to use it to download file
if (!arg.isNull("url")) {
String url = (String) arg.get("url");
//download
}
//Otherwise get contents as string
String value = (String) arg.get(FileParameter.VALUE);
String encoding = (String) arg.get(FileParameter.ENCODING);
try (PrintWriter out = new PrintWriter(fileName)) {
out.print(value);
}
files[i] = new File(fileName);
}
return files;
}
private String jacksonMarshalExample(List<File> files) throws UnsupportedEncodingException, IOException {
Message responseMessage = new Message();
List parameters = new ArrayList();
String charset = "UTF-8";
for (File f : files) {
FileParameter fileParam = new FileParameter();
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(new Date(System.currentTimeMillis()));
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(responseMessage);
}
private String simpleJsonMarshalExample(List<File> files) throws JSONException, IOException {
JSONObject jo = new JSONObject();
jo.put("creationDate", new Date(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);
return jo.toString();
}
}
......@@ -16,97 +16,101 @@
package nl.uva.sne.drip.drip.planner;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
*
* @author S. Koulouzis
*/
public class Panner {
public class Planner {
public void plan(String input, String example, String outputDirPath) {
public List<File> plan(String input, String example, String outputDirPath) throws FileNotFoundException, IOException {
// TODO Auto-generated method stub
BufferedReader in;
try {
in = new BufferedReader(new FileReader(input));
String line = null;
boolean check = false;
ArrayList<String> dockerNames = new ArrayList<>();
while ((line = in.readLine()) != null) {
if (line.contains("topology_template")) {
check = true;
}
if (check) {
if (line.contains("file")) {
String content = line.trim().replace('\"', ' ');
String[] cs = content.split(":");
String docker_name = cs[1].trim();
dockerNames.add(docker_name);
}
}
if (line.contains("tosca_definitions_version")
|| line.contains("description")
|| line.contains("repositories")
|| line.contains("artifact_types") || line.contains("data_types")
|| line.contains("node_types")) {
check = false;
in = new BufferedReader(new FileReader(input));
String line;
boolean check = false;
ArrayList<String> dockerNames = new ArrayList<>();
while ((line = in.readLine()) != null) {
if (line.contains("topology_template")) {
check = true;
}
if (check) {
if (line.contains("file")) {
String content = line.trim().replace('\"', ' ');
String[] cs = content.split(":");
String docker_name = cs[1].trim();
dockerNames.add(docker_name);
}
}
in.close();
if (line.contains("tosca_definitions_version")
|| line.contains("description")
|| line.contains("repositories")
|| line.contains("artifact_types") || line.contains("data_types")
|| line.contains("node_types")) {
check = false;
}
}
in.close();
in = new BufferedReader(new FileReader(example));
in = new BufferedReader(new FileReader(example));
String block = "";
String head = "";
String block = "";
String head = "";
boolean block_b = false;
while ((line = in.readLine()) != null) {
if (line.contains("components")) {
block_b = true;
continue;
}
if (block_b) {
block += line + "\n";
}
if (!block_b) {
head += line + "\n";
}
boolean block_b = false;
while ((line = in.readLine()) != null) {
if (line.contains("components")) {
block_b = true;
continue;
}
if (block_b) {
block += line + "\n";
}
if (!block_b) {
head += line + "\n";
}
}
in.close();
UUID fuuid = UUID.randomUUID();
String file_guid = fuuid.toString();
in.close();
UUID fuuid = UUID.randomUUID();
String file_guid = fuuid.toString();
String outfPath = outputDirPath + "/" + file_guid + ".yml";
FileWriter outputf = new FileWriter(outfPath);
outputf.write(head);
outputf.write("components:\n");
for (int i = 0; i < dockerNames.size(); i++) {
UUID uuid = UUID.randomUUID();
String name_guid = uuid.toString();
String privateAddress = "192.168.10." + (i + 10);
if (i == 0) {
outputf.write(generateVM(block, name_guid, dockerNames.get(i), privateAddress, "master"));
} else {
outputf.write(generateVM(block, name_guid, dockerNames.get(i), privateAddress, "slave"));
}
String outfPath = outputDirPath + "/" + file_guid + ".yml";
FileWriter outputf = new FileWriter(outfPath);
outputf.write(head);
outputf.write("components:\n");
for (int i = 0; i < dockerNames.size(); i++) {
UUID uuid = UUID.randomUUID();
String name_guid = uuid.toString();
String privateAddress = "192.168.10." + (i + 10);
if (i == 0) {
outputf.write(generateVM(block, name_guid, dockerNames.get(i), privateAddress, "master"));
} else {
outputf.write(generateVM(block, name_guid, dockerNames.get(i), privateAddress, "slave"));
}
outputf.close();
String allFilePath = outputDirPath + "/" + "planner_output_all.yml";
outputf = new FileWriter(allFilePath);
outputf.write("topologies:\n");
outputf.write(" - topology: " + file_guid + "\n");
outputf.write(" cloudProvider: EC2\n");
outputf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
outputf.close();
String allFilePath = outputDirPath + "/" + "planner_output_all.yml";
outputf = new FileWriter(allFilePath);
outputf.write("topologies:\n");
outputf.write(" - topology: " + file_guid + "\n");
outputf.write(" cloudProvider: EC2\n");
outputf.close();
List<File> outputFiles = new ArrayList<>();
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) {
......
/*
* 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.drip.planner;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author S. Koulouzis
*/
public class RPCServer {
private static final String RPC_QUEUE_NAME = "planner_queue";
private static final String HOST = "172.17.0.2";
public static void main(String[] argv) {
start();
}
private static void start() {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(HOST);
try (Connection connection = factory.newConnection()) {
Channel channel = connection.createChannel();
channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
Consumer c = new Consumer(channel);
channel.basicConsume(RPC_QUEUE_NAME, false, c);
//Block so we don't close the channel
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException _ignore) {
}
}
} catch (IOException | TimeoutException ex) {
Logger.getLogger(RPCServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
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