Commit 3e7ef0cf authored by Spiros Koulouzis's avatar Spiros Koulouzis

Renamed moddule

Added comments 
parent 5000c2b1
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.hint.licensePath>${project.basedir}/../licenseheader.txt</netbeans.hint.licensePath>
<org-netbeans-modules-whitelist.whitelist-oracle>false</org-netbeans-modules-whitelist.whitelist-oracle>
</properties>
</project-shared-configuration>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>nl.uva.sne.drip</groupId>
<artifactId>drip</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>drip-simple_planner</artifactId>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</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
/*
* 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.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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import nl.uva.sne.drip.commons.types.Parameter;
import nl.uva.sne.drip.commons.types.Message;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* This is an example of a Message consumer
*
*
* @author S. Koulouzis
*/
public class Consumer extends DefaultConsumer {
private final Channel channel;
private final Planner panner;
public Consumer(Channel channel) {
super(channel);
this.channel = channel;
this.panner = new Planner();
}
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//Create the reply properties which tells us where to reply, and which id to use.
//No need to change anything here
AMQP.BasicProperties replyProps = new AMQP.BasicProperties.Builder()
.correlationId(properties.getCorrelationId())
.build();
String response = "";
try {
//The queue only moves bytes so we need to convert them to stting
String message = new String(body, "UTF-8");
//Create a tmp folder to save the output.
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());
}
//We need to extact the call parameters form the json message.
inputFiles = jacksonUnmarshalExample(message);
//Call the method with the extracted parameters
List<File> files = panner.plan(inputFiles[0].getAbsolutePath(), inputFiles[1].getAbsolutePath(), tempDir.getAbsolutePath());
//Here we do the same as above with a different API
inputFiles = simpleJsonUnmarshalExample(message);
//Call the method with the extracted parameters
files = panner.plan(inputFiles[0].getAbsolutePath(), inputFiles[1].getAbsolutePath(), tempDir.getAbsolutePath());
//Now we need to put the result of the call to a message and respond
//Example 1
response = jacksonMarshalExample(files);
//Example 2
response = simpleJsonMarshalExample(files);
} catch (IOException | JSONException ex) {
response = ex.getMessage();
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
} finally {
//We send the response back. No need to change anything here
channel.basicPublish("", properties.getReplyTo(), replyProps, response.getBytes("UTF-8"));
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
private File[] jacksonUnmarshalExample(String message) throws IOException {
//Use the Jackson API to convert json to Object
File[] files = new File[2];
ObjectMapper mapper = new ObjectMapper();
Message request = mapper.readValue(message, Message.class);
List<Parameter> params = request.getParameters();
//Create tmp input files
File inputFile = File.createTempFile("input-", Long.toString(System.nanoTime()));
File exampleFile = File.createTempFile("example-", Long.toString(System.nanoTime()));
//loop through the parameters in a message to find the input files
for (Parameter param : params) {
if (param.getName().equals("input")) {
try (PrintWriter out = new PrintWriter(inputFile)) {
out.print(param.getValue());
}
files[0] = inputFile;
}
if (param.getName().equals("example")) {
try (PrintWriter out = new PrintWriter(exampleFile)) {
out.print(param.getValue());
}
files[1] = exampleFile;
}
}
//Return the array with input files
return files;
}
private File[] simpleJsonUnmarshalExample(String message) throws JSONException, FileNotFoundException, IOException {
//Use the JSONObject API to convert json to Object (Message)
File[] files = new File[2];
JSONObject jo = new JSONObject(message);
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));
}
files[0] = inputFile;
}
if (name.equals("example")) {
try (PrintWriter out = new PrintWriter(exampleFile)) {
out.print(param.get(Parameter.VALUE));
}
files[1] = exampleFile;
}
}
return files;
}
private String jacksonMarshalExample(List<File> files) throws UnsupportedEncodingException, IOException {
//Use the jackson API to convert Object (Message) to json
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);
//The creationDate is the only filed that has to be there
responseMessage.setCreationDate((System.currentTimeMillis()));
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(responseMessage);
}
private String simpleJsonMarshalExample(List<File> files) throws JSONException, IOException {
//Use the JSONObject API to convert Object (Message) to json
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);
return jo.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.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 Planner {
public List<File> plan(String input, String example, String outputDirPath) throws FileNotFoundException, IOException {
// TODO Auto-generated method stub
BufferedReader in;
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);
}
}
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));
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";
}
}
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"));
}
}
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) {
block = block.replaceAll("nodeA", nodeName);
block = block.replaceAll("DOCKER", "\"" + dockerName + "\"");
block = block.replaceAll("192.168.10.10", privateAddress);
block = block.replaceAll("ROLE", role);
return block;
}
}
/*
* 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.AMQP;
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.3";
public static void main(String[] argv) {
start();
}
private static void start() {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(HOST);
factory.setPassword("guest");
factory.setUsername("guest");
factory.setPort(AMQP.PROTOCOL.PORT);
try (Connection connection = factory.newConnection()) {
Channel channel = connection.createChannel();
//We define the queue name
channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
//Set our own customized consummer
Consumer c = new Consumer(channel);
//Start listening for messages
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