Commit 67a1a59c authored by Spiros Koulouzis's avatar Spiros Koulouzis

Added to parent project

Casted objects to strings
parent cdf3304f
...@@ -2,4 +2,5 @@ ...@@ -2,4 +2,5 @@
/drip-api/target/ /drip-api/target/
/drip-planner/target/ /drip-planner/target/
/drip-commons/nbproject/ /drip-commons/nbproject/
/drip-simple_planner/target/ /drip-simple_planner/target/
\ No newline at end of file /drip-provisioner/target/
\ No newline at end of file
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<artifactId>drip</artifactId> <artifactId>drip</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</parent> </parent>
<artifactId>drip-simple_provisioner</artifactId> <artifactId>drip-provisioner</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>
......
...@@ -13,19 +13,19 @@ ...@@ -13,19 +13,19 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package nl.uva.sne.drip.drip.planner; package nl.uva.sne.drip.drip.provisioner;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.AMQP; import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel; import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope; import com.rabbitmq.client.Envelope;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.InputStreamReader;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -35,7 +35,6 @@ import java.util.Map; ...@@ -35,7 +35,6 @@ import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import nl.uva.sne.drip.commons.types.Parameter; import nl.uva.sne.drip.commons.types.Parameter;
import nl.uva.sne.drip.commons.types.Message;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -50,10 +49,10 @@ import org.json.JSONObject; ...@@ -50,10 +49,10 @@ import org.json.JSONObject;
public class Consumer extends DefaultConsumer { public class Consumer extends DefaultConsumer {
private final Channel channel; private final Channel channel;
private final Planner panner;
private final String jarFilePath = "/root/SWITCH/bin/ProvisioningCore.jar"; private final String jarFilePath = "/root/SWITCH/bin/ProvisioningCore.jar";
public class topologyElement{ public class topologyElement {
String topologyName = ""; String topologyName = "";
String outputFilePath = ""; String outputFilePath = "";
} }
...@@ -61,7 +60,6 @@ public class Consumer extends DefaultConsumer { ...@@ -61,7 +60,6 @@ public class Consumer extends DefaultConsumer {
public Consumer(Channel channel) { public Consumer(Channel channel) {
super(channel); super(channel);
this.channel = channel; this.channel = channel;
this.panner = new Planner();
} }
@Override @Override
...@@ -83,8 +81,8 @@ public class Consumer extends DefaultConsumer { ...@@ -83,8 +81,8 @@ public class Consumer extends DefaultConsumer {
if (!(tempInputDir.mkdirs())) { if (!(tempInputDir.mkdirs())) {
throw new FileNotFoundException("Could not create input directory: " + tempInputDir.getAbsolutePath()); throw new FileNotFoundException("Could not create input directory: " + tempInputDir.getAbsolutePath());
} }
ArrayList topologyInfoArray = null; ArrayList topologyInfoArray;
topologyInfoArray = invokeProvisioner(message, tempInputDirPath); topologyInfoArray = invokeProvisioner(message, tempInputDirPath);
response = generateResponse(topologyInfoArray); response = generateResponse(topologyInfoArray);
...@@ -102,243 +100,246 @@ public class Consumer extends DefaultConsumer { ...@@ -102,243 +100,246 @@ public class Consumer extends DefaultConsumer {
////If the provisioner jar file is successfully invoked, the returned value should be a set of output file paths which are expected. ////If the provisioner jar file is successfully invoked, the returned value should be a set of output file paths which are expected.
////If there are some errors or some information missing with this message, the returned value will be null. ////If there are some errors or some information missing with this message, the returned value will be null.
////The input dir path contains '/' ////The input dir path contains '/'
private ArrayList<topologyElement> invokeProvisioner(String message, String tempInputDirPath) throws IOException { private ArrayList<topologyElement> invokeProvisioner(String message, String tempInputDirPath) throws IOException, JSONException {
//Use the Jackson API to convert json to Object //Use the Jackson API to convert json to Object
JSONObject jo = new JSONObject(message); JSONObject jo = new JSONObject(message);
JSONArray parameters = jo.getJSONArray("parameters"); JSONArray parameters = jo.getJSONArray("parameters");
//Create tmp input files //Create tmp input files
File ec2ConfFile = null; File ec2ConfFile = null;
File geniConfFile = null; File geniConfFile = null;
//loop through the parameters in a message to find the input files //loop through the parameters in a message to find the input files
String logDir = "null", mainTopologyPath = "null", sshKeyFilePath = "null", scriptPath = "null"; String logDir = "null", mainTopologyPath = "null", sshKeyFilePath = "null", scriptPath = "null";
ArrayList<topologyElement> topologyInfoArray = new ArrayList<topologyElement>(); ArrayList<topologyElement> topologyInfoArray = new ArrayList();
for (int i = 0; i < parameters.length(); i++) { for (int i = 0; i < parameters.length(); i++) {
JSONObject param = (JSONObject) parameters.get(i); JSONObject param = (JSONObject) parameters.get(i);
String name = (String) param.get(Parameter.NAME); String name = (String) param.get(Parameter.NAME);
if (name.equals("ec2.conf")) { if (name.equals("ec2.conf")) {
try { try {
ec2ConfFile = new File(tempInputDirPath+"ec2.Conf"); ec2ConfFile = new File(tempInputDirPath + "ec2.Conf");
if (ec2ConfFile.createNewFile()){ if (ec2ConfFile.createNewFile()) {
PrintWriter out = new PrintWriter(ec2ConfFile); PrintWriter out = new PrintWriter(ec2ConfFile);
out.print(param.get(Parameter.VALUE)); out.print(param.get(Parameter.VALUE));
}else } else {
return null; return null;
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, e);
return null; return null;
} }
}else if (name.equals("geni.conf")){ } else if (name.equals("geni.conf")) {
try { try {
geniConfFile = new File(tempInputDirPath+"geni.Conf"); geniConfFile = new File(tempInputDirPath + "geni.Conf");
if (geniConfFile.createNewFile()){ if (geniConfFile.createNewFile()) {
PrintWriter out = new PrintWriter(geniConfFile); PrintWriter out = new PrintWriter(geniConfFile);
out.print(param.get(Parameter.VALUE)); out.print(param.get(Parameter.VALUE));
}else } else {
return null; return null;
} catch (IOException e) { }
e.printStackTrace(); } catch (IOException ex) {
return null; Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
return null;
} }
}else if (name.equals("topology")){ } else if (name.equals("topology")) {
JSONObject attribute_level = param.getJSONObject("attributes"); JSONObject attribute_level = param.getJSONObject("attributes");
int fileLevel = Integer.valueOf(attribute_level.get("level")); int fileLevel = Integer.valueOf((String) attribute_level.get("level"));
if(fileLevel == 0) /////if the file level is 0, it means that this is the top level description if (fileLevel == 0) /////if the file level is 0, it means that this is the top level description
{ {
try { try {
File topologyFile = new File(tempInputDirPath+"topology_main"); File topologyFile = new File(tempInputDirPath + "topology_main");
if (topologyFile.createNewFile()){ if (topologyFile.createNewFile()) {
PrintWriter out = new PrintWriter(geniConfFile); PrintWriter out = new PrintWriter(geniConfFile);
out.print(param.get(Parameter.VALUE)); out.print(param.get(Parameter.VALUE));
mainTopologyPath = topologyFile.getAbsolutePath(); mainTopologyPath = topologyFile.getAbsolutePath();
}else } else {
return null; return null;
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, e);
return null; return null;
} }
}else if (fileLevel == 1){ ////this means that this file is the low level detailed description } else if (fileLevel == 1) { ////this means that this file is the low level detailed description
String fileName = attribute_level.get("filename"); ////This file name does not contain suffix of '.yml' for example String fileName = (String) attribute_level.get("filename"); ////This file name does not contain suffix of '.yml' for example
try { try {
File topologyFile = new File(tempInputDirPath+fileName+".yml"); File topologyFile = new File(tempInputDirPath + fileName + ".yml");
String outputFilePath = tempInputDirPath+fileName+"_provisioned.yml"; String outputFilePath = tempInputDirPath + fileName + "_provisioned.yml";
if (topologyFile.createNewFile()){ if (topologyFile.createNewFile()) {
PrintWriter out = new PrintWriter(geniConfFile); PrintWriter out = new PrintWriter(geniConfFile);
out.print(param.get(Parameter.VALUE)); out.print(param.get(Parameter.VALUE));
topologyElement x = new topologyElement(); topologyElement x = new topologyElement();
x.topologyName = fileName; x.topologyName = fileName;
x.outputFilePath = outputFilePath; x.outputFilePath = outputFilePath;
topologyInfoArray.add(x); topologyInfoArray.add(x);
}else } else {
return null; return null;
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, e);
return null; return null;
} }
} }
}else if (name.equals("logdir")){ } else if (name.equals("logdir")) {
logDir = param.get(Parameter.VALUE); logDir = (String) param.get(Parameter.VALUE);
}else if (name.equals("sshkey")){ } else if (name.equals("sshkey")) {
sshKeyFilePath = param.get(Parameter.VALUE); sshKeyFilePath = (String) param.get(Parameter.VALUE);
}else if (name.equals("guiscript")){ } else if (name.equals("guiscript")) {
scriptPath = param.get(Parameter.VALUE); scriptPath = (String) param.get(Parameter.VALUE);
} } else {
else{
return null; return null;
} }
} }
File curDir = new File(tempInputDirPath); File curDir = new File(tempInputDirPath);
String[] ls = curDir.list(); String[] ls = curDir.list();
for(int i = 0 ; i<ls.length ; i++){ for (int i = 0; i < ls.length; i++) {
if(ls[i].contains(".")){ if (ls[i].contains(".")) {
String [] fileTypes = ls[i].split("\\."); String[] fileTypes = ls[i].split("\\.");
if(fileTypes.length > 0){ if (fileTypes.length > 0) {
int lastIndex = fileTypes.length-1; int lastIndex = fileTypes.length - 1;
String fileType = fileTypes[lastIndex]; String fileType = fileTypes[lastIndex];
if(fileType.equals("yml")){ if (fileType.equals("yml")) {
String toscaFile = curDir+ls[i]; String toscaFile = curDir + ls[i];
if(!sshKeyFilePath.equals("null")) if (!sshKeyFilePath.equals("null")) {
changeKeyFilePath(toscaFile, sshKeyFilePath); changeKeyFilePath(toscaFile, sshKeyFilePath);
}
if(!scriptPath.equals("null"))
if (!scriptPath.equals("null")) {
changeGUIScriptFilePath(toscaFile, scriptPath); changeGUIScriptFilePath(toscaFile, scriptPath);
}
} }
} }
} }
} }
if(ec2ConfFile == null && geniConfFile == null) if (ec2ConfFile == null && geniConfFile == null) {
return null; return null;
if(mainTopologyPath.equals("null")) }
if (mainTopologyPath.equals("null")) {
return null; return null;
}
String ec2ConfFilePath = "null"; String ec2ConfFilePath = "null";
String geniConfFilePath = "null"; String geniConfFilePath = "null";
if(ec2ConfFile != null) if (ec2ConfFile != null) {
ec2ConfFilePath = ec2ConfFile.getAbsolutePath(); ec2ConfFilePath = ec2ConfFile.getAbsolutePath();
if(geniConfFile != null) }
if (geniConfFile != null) {
geniConfFilePath = geniConfFile.getAbsolutePath(); geniConfFilePath = geniConfFile.getAbsolutePath();
if(logDir.equals("null")) }
if (logDir.equals("null")) {
logDir = "/tmp/"; logDir = "/tmp/";
}
String cmd = "java -jar "+jarFilePath+" ec2="+ec2ConfFilePath+" exogeni="+geniConfFilePath+" logDir="+logDir+" topology="+mainTopologyPath; String cmd = "java -jar " + jarFilePath + " ec2=" + ec2ConfFilePath + " exogeni=" + geniConfFilePath + " logDir=" + logDir + " topology=" + mainTopologyPath;
try { try {
Process p = Runtime.getRuntime().exec(cmd); Process p = Runtime.getRuntime().exec(cmd);
p.waitFor(); p.waitFor();
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, e);
} }
topologyElement x = new topologyElement(); topologyElement x = new topologyElement();
x.topologyName = "kubernetes"; x.topologyName = "kubernetes";
x.outputFilePath = tempInputDirPath+"file_kubernetes"; x.outputFilePath = tempInputDirPath + "file_kubernetes";
topologyInfoArray.add(); topologyInfoArray.add(x);
return topologyInfoArray; return topologyInfoArray;
} }
////Change the key file path in the tosca file. ////Change the key file path in the tosca file.
////Because the user needs to upload their public key file into the server file system. ////Because the user needs to upload their public key file into the server file system.
private void changeKeyFilePath(String toscaFilePath, String newKeyFilePath){ private void changeKeyFilePath(String toscaFilePath, String newKeyFilePath) {
File toscaFile = new File(toscaFilePath); File toscaFile = new File(toscaFilePath);
String fileContent = ""; String fileContent = "";
try { try {
BufferedReader in = new BufferedReader(new FileReader(toscaFile)); BufferedReader in = new BufferedReader(new FileReader(toscaFile));
String line = ""; String line = "";
while((line = in.readLine()) != null){ while ((line = in.readLine()) != null) {
if(line.contains("publicKeyPath")) if (line.contains("publicKeyPath")) {
fileContent += ("publicKeyPath: "+newKeyFilePath+"\n"); fileContent += ("publicKeyPath: " + newKeyFilePath + "\n");
else } else {
fileContent += (line+"\n"); fileContent += (line + "\n");
}
} }
in.close(); in.close();
FileWriter fw = new FileWriter(toscaFilePath, false); FileWriter fw = new FileWriter(toscaFilePath, false);
fw.write(fileContent); fw.write(fileContent);
fw.close(); fw.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, e);
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, e);
} }
} }
private void changeGUIScriptFilePath(String toscaFilePath, String newScriptPath) {
private void changeGUIScriptFilePath(String toscaFilePath, String newScriptPath){
File toscaFile = new File(toscaFilePath); File toscaFile = new File(toscaFilePath);
String fileContent = ""; String fileContent = "";
try { try {
BufferedReader in = new BufferedReader(new FileReader(toscaFile)); BufferedReader in = new BufferedReader(new FileReader(toscaFile));
String line = ""; String line;
while((line = in.readLine()) != null){ while ((line = in.readLine()) != null) {
if(line.contains("script")){ if (line.contains("script")) {
int index = line.indexOf("script:"); int index = line.indexOf("script:");
String prefix = line.substring(0, index+7); String prefix = line.substring(0, index + 7);
fileContent += (prefix+" "+newScriptPath+"\n"); fileContent += (prefix + " " + newScriptPath + "\n");
} else {
fileContent += (line + "\n");
} }
else
fileContent += (line+"\n");
} }
in.close(); in.close();
FileWriter fw = new FileWriter(toscaFilePath, false); FileWriter fw = new FileWriter(toscaFilePath, false);
fw.write(fileContent); fw.write(fileContent);
fw.close(); fw.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, e);
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, e);
} }
} }
private String generateResponse(ArrayList<topologyElement> outputs) throws JSONException, IOException { private String generateResponse(ArrayList<topologyElement> outputs) throws JSONException, IOException {
//Use the JSONObject API to convert Object (Message) to json //Use the JSONObject API to convert Object (Message) to json
JSONObject jo = new JSONObject(); JSONObject jo = new JSONObject();
jo.put("creationDate", (System.currentTimeMillis())); jo.put("creationDate", (System.currentTimeMillis()));
List parameters = new ArrayList(); List parameters = new ArrayList();
String charset = "UTF-8"; String charset = "UTF-8";
if(outputs == null){ if (outputs == null) {
Map<String, String> fileArguments = new HashMap<>(); Map<String, String> fileArguments = new HashMap<>();
fileArguments.put("encoding", charset); fileArguments.put("encoding", charset);
fileArguments.put("name", "ERROR"); fileArguments.put("name", "ERROR");
fileArguments.put("value", "Some error with input messages!"); fileArguments.put("value", "Some error with input messages!");
parameters.add(fileArguments); parameters.add(fileArguments);
}else{ } else {
for(int i = 0 ; i<outputs.size() ; i++){ for (int i = 0; i < outputs.size(); i++) {
Map<String, String> fileArguments = new HashMap<>(); Map<String, String> fileArguments = new HashMap<>();
fileArguments.put("encoding", charset); fileArguments.put("encoding", charset);
File f = new File(outputs.get(i).outputFilePath); File f = new File(outputs.get(i).outputFilePath);
if(f.exists()){ if (f.exists()) {
fileArguments.put("name", outputs.get(i).topologyName); fileArguments.put("name", outputs.get(i).topologyName);
byte[] bytes = Files.readAllBytes(Paths.get(f.getAbsolutePath())); byte[] bytes = Files.readAllBytes(Paths.get(f.getAbsolutePath()));
fileArguments.put("value", new String(bytes, charset)); fileArguments.put("value", new String(bytes, charset));
parameters.add(fileArguments); parameters.add(fileArguments);
}else{ } else {
fileArguments.put("name", outputs.get(i).topologyName); fileArguments.put("name", outputs.get(i).topologyName);
fileArguments.put("value", "ERROR::There is no output for topology "+outputs.get(i).topologyName); fileArguments.put("value", "ERROR::There is no output for topology " + outputs.get(i).topologyName);
parameters.add(fileArguments); parameters.add(fileArguments);
} }
} }
} }
jo.put("parameters", parameters); jo.put("parameters", parameters);
return jo.toString(); return jo.toString();
} }
} }
...@@ -8,9 +8,10 @@ ...@@ -8,9 +8,10 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<modules> <modules>
<module>drip-api</module> <module>drip-api</module>
<module>drip-commons</module> <module>drip-commons</module>
<module>drip-simple_planner</module> <module>drip-simple_planner</module>
</modules> <module>drip-provisioner</module>
</modules>
</project> </project>
\ No newline at end of file
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