Commit 798834f3 authored by Spiros Koulouzis's avatar Spiros Koulouzis

Added system prop for tmp folder

parent ab85a819
......@@ -15,8 +15,9 @@
*/
package nl.uva.sne.drip.api.rest;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.ByteArrayOutputStream;
import nl.uva.sne.drip.commons.types.ToscaRepresentation;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
......@@ -26,10 +27,12 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.security.RolesAllowed;
import nl.uva.sne.drip.api.dao.CloudCredentialsDao;
import nl.uva.sne.drip.api.rpc.PlannerCaller;
import nl.uva.sne.drip.commons.types.Message;
import nl.uva.sne.drip.commons.types.Parameter;
......@@ -44,7 +47,11 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import nl.uva.sne.drip.api.dao.ToscaDao;
import nl.uva.sne.drip.api.rpc.DRIPCaller;
import nl.uva.sne.drip.api.rpc.ProvisionerCaller;
import nl.uva.sne.drip.api.service.UserService;
import nl.uva.sne.drip.commons.types.CloudCredentials;
import org.apache.commons.io.FilenameUtils;
/**
*
......@@ -59,59 +66,43 @@ public class PlannerController {
private String messageBrokerHost;
@Autowired
private ToscaDao dao;
@Autowired
private CloudCredentialsDao cloudCredentialsDao;
@RequestMapping(value = "/plan/{tosca_id}", method = RequestMethod.POST)
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
String plann(@PathVariable("tosca_id") String toscaId) {
PlannerCaller planner = null;
DRIPCaller planner = null;
DRIPCaller provisioner = null;
List<DRIPCaller> dripComponetens = new ArrayList<>();
try {
ToscaRepresentation t2 = dao.findOne(toscaId);
Map<String, Object> map = t2.getKvMap();
String ymlStr = Converter.map2YmlString(map);
ymlStr = ymlStr.replaceAll("\\uff0E", "\\.");
byte[] bytes = ymlStr.getBytes();
Message invokationMessage = new Message();
List parameters = new ArrayList();
Parameter fileArgument = new Parameter();
String charset = "UTF-8";
fileArgument.setValue(new String(bytes, charset));
fileArgument.setEncoding(charset);
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");
parameters.add(fileArgument);
invokationMessage.setParameters(parameters);
invokationMessage.setCreationDate((System.currentTimeMillis()));
Message plannerInvokationMessage = buildPlannerMessage(toscaId);
planner = new PlannerCaller(messageBrokerHost);
String returned = planner.plan(invokationMessage);
System.err.println(returned);
dripComponetens.add(planner);
Message plannerReturnedMessage = planner.call(plannerInvokationMessage);
ObjectMapper mapper = new ObjectMapper();
Message request = mapper.readValue(returned, Message.class);
Message provisionerInvokationMessage = buildProvisionerMessage(plannerReturnedMessage, "58a1f0a963d42f004b1d63ad");
provisioner = new ProvisionerCaller(messageBrokerHost);
dripComponetens.add(provisioner);
provisioner.call(provisionerInvokationMessage);
return returned;
} catch (UnsupportedEncodingException | TimeoutException | InterruptedException ex) {
Logger.getLogger(PlannerController.class.getName()).log(Level.SEVERE, null, ex);
} catch (JSONException | IOException ex) {
return "";
} catch (JSONException | IOException | TimeoutException | InterruptedException ex) {
Logger.getLogger(PlannerController.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (planner != null) {
try {
planner.close();
} catch (IOException | TimeoutException ex) {
Logger.getLogger(PlannerController.class.getName()).log(Level.SEVERE, null, ex);
for (DRIPCaller drip : dripComponetens) {
if (drip != null) {
try {
drip.close();
} catch (IOException | TimeoutException ex) {
Logger.getLogger(PlannerController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
return null;
}
......@@ -157,4 +148,74 @@ public class PlannerController {
}
return null;
}
private Message buildPlannerMessage(String toscaId) throws JSONException, UnsupportedEncodingException, IOException {
ToscaRepresentation t2 = dao.findOne(toscaId);
Map<String, Object> map = t2.getKvMap();
String ymlStr = Converter.map2YmlString(map);
ymlStr = ymlStr.replaceAll("\\uff0E", "\\.");
byte[] bytes = ymlStr.getBytes();
Message invokationMessage = new Message();
List parameters = new ArrayList();
Parameter fileArgument = new Parameter();
String charset = "UTF-8";
fileArgument.setValue(new String(bytes, charset));
fileArgument.setEncoding(charset);
fileArgument.setName("input");
parameters.add(fileArgument);
fileArgument = new Parameter();
bytes = Files.readAllBytes(Paths.get(System.getProperty("user.home") + File.separator + "Downloads/DRIP/example_a.yml"));
fileArgument.setValue(new String(bytes, charset));
fileArgument.setEncoding(charset);
fileArgument.setName("example");
parameters.add(fileArgument);
invokationMessage.setParameters(parameters);
invokationMessage.setCreationDate((System.currentTimeMillis()));
return invokationMessage;
}
private Message buildProvisionerMessage(Message plannerReturnedMessage, String cloudConfID) throws IOException, JsonProcessingException, JSONException {
Message invokationMessage = new Message();
List<Parameter> parameters = new ArrayList();
Parameter conf = new Parameter();
String charset = "UTF-8";
CloudCredentials cred = cloudCredentialsDao.findOne(cloudConfID);
Properties prop = Converter.Object2Properties(cred);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
prop.store(baos, null);
byte[] bytes = baos.toByteArray();
conf.setName("ec2.conf");
conf.setValue(new String(bytes, charset));
parameters.add(conf);
List<Parameter> returnedParams = plannerReturnedMessage.getParameters();
for (Parameter param : returnedParams) {
Parameter topology = new Parameter();
String name = param.getName();
if (name.equals("planner_output_all.yml")) {
topology.setName("topology");
topology.setValue(param.getValue());
Map<String, String> attributes = new HashMap<>();
attributes.put("level", "0");
attributes.put("filename", FilenameUtils.removeExtension(name));
topology.setAttributes(attributes);
} else {
topology.setName("topology");
topology.setValue(param.getValue());
Map<String, String> attributes = new HashMap<>();
attributes.put("level", "1");
attributes.put("filename", FilenameUtils.removeExtension(name));
topology.setAttributes(attributes);
}
parameters.add(topology);
}
invokationMessage.setParameters(parameters);
invokationMessage.setCreationDate((System.currentTimeMillis()));
return invokationMessage;
}
}
package nl.uva.sne.drip.api.rpc;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;
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.Message;
/*
* 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.
*/
/**
*
* @author S. Koulouzis
*/
public abstract class DRIPCaller {
private final Connection connection;
private final Channel channel;
private final String replyQueueName;
private final String requestQeueName;
public DRIPCaller(String messageBrokerHost, String requestQeueName) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(messageBrokerHost);
factory.setPort(AMQP.PROTOCOL.PORT);
//factory.setUsername("guest");
//factory.setPassword("pass");
connection = factory.newConnection();
channel = connection.createChannel();
// create a single callback queue per client not per requests.
replyQueueName = channel.queueDeclare().getQueue();
this.requestQeueName = requestQeueName;
}
/**
* @return the connection
*/
public Connection getConnection() {
return connection;
}
/**
* @return the channel
*/
public Channel getChannel() {
return channel;
}
/**
* @return the replyQueueName
*/
public String getReplyQueueName() {
return replyQueueName;
}
public void close() throws IOException, TimeoutException {
if (channel != null && channel.isOpen()) {
channel.close();
}
if (connection != null && connection.isOpen()) {
connection.close();
}
}
public Message call(Message r) throws IOException, TimeoutException, InterruptedException {
ObjectMapper mapper = new ObjectMapper();
String jsonInString = mapper.writeValueAsString(r);
//Build a correlation ID to distinguish responds
final String corrId = UUID.randomUUID().toString();
AMQP.BasicProperties props = new AMQP.BasicProperties.Builder()
.correlationId(corrId)
.replyTo(getReplyQueueName())
.build();
getChannel().basicPublish("", requestQeueName, props, jsonInString.getBytes("UTF-8"));
final BlockingQueue<String> response = new ArrayBlockingQueue(1);
getChannel().basicConsume(getReplyQueueName(), true, new DefaultConsumer(getChannel()) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
if (properties.getCorrelationId().equals(corrId)) {
response.offer(new String(body, "UTF-8"));
}
}
});
String strResponse = response.take();
System.err.println(strResponse);
return mapper.readValue(strResponse, Message.class);
}
}
......@@ -15,79 +15,20 @@
*/
package nl.uva.sne.drip.api.rpc;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
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.Message;
/**
*
* @author S. Koulouzis.
*/
public class PlannerCaller {
public class PlannerCaller extends DRIPCaller {
private static final String REQUEST_QUEUE_NAME = "planner_queue";
private final Connection connection;
private final Channel channel;
private final String replyQueueName;
public PlannerCaller(String messageBrokerHost) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(messageBrokerHost);
factory.setPort(AMQP.PROTOCOL.PORT);
//factory.setUsername("guest");
//factory.setPassword("pass");
connection = factory.newConnection();
channel = connection.createChannel();
// create a single callback queue per client not per requests.
replyQueueName = channel.queueDeclare().getQueue();
}
public String plan(Message r) throws IOException, TimeoutException, InterruptedException {
ObjectMapper mapper = new ObjectMapper();
String jsonInString = mapper.writeValueAsString(r);
//Build a correlation ID to distinguish responds
final String corrId = UUID.randomUUID().toString();
AMQP.BasicProperties props = new AMQP.BasicProperties.Builder()
.correlationId(corrId)
.replyTo(replyQueueName)
.build();
channel.basicPublish("", REQUEST_QUEUE_NAME, props, jsonInString.getBytes("UTF-8"));
final BlockingQueue<String> response = new ArrayBlockingQueue(1);
channel.basicConsume(replyQueueName, true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
if (properties.getCorrelationId().equals(corrId)) {
response.offer(new String(body, "UTF-8"));
}
}
});
return response.take();
super(messageBrokerHost,REQUEST_QUEUE_NAME);
}
public void close() throws IOException, TimeoutException {
if (channel != null && channel.isOpen()) {
channel.close();
}
if (connection != null && connection.isOpen()) {
connection.close();
}
}
}
/*
* Copyright 2017 S. Koulouzis.
* 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.
......@@ -15,14 +15,18 @@
*/
package nl.uva.sne.drip.api.rpc;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
*
* @author S. Koulouzis.
* @author S. Koulouzis
*/
class DRIPComponentCaller {
public class ProvisionerCaller extends DRIPCaller {
public DRIPComponentCaller(){
}
private static final String REQUEST_QUEUE_NAME = "provisioner_queue";
public ProvisionerCaller(String messageBrokerHost) throws IOException, TimeoutException {
super(messageBrokerHost, REQUEST_QUEUE_NAME);
}
}
......@@ -33,6 +33,13 @@
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
......
......@@ -16,7 +16,6 @@
package nl.uva.sne.drip.commons.types;
import java.util.List;
import java.util.Map;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
......
......@@ -15,20 +15,19 @@
*/
package nl.uva.sne.drip.commons.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Properties;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.events.Event;
/**
*
......@@ -105,4 +104,10 @@ public class Converter {
return yaml.dump(ymlString2Map(jsonString));
}
public static Properties Object2Properties(Object obj) throws JsonProcessingException, JSONException {
ObjectMapper mapper = new ObjectMapper();
String jsonInString = mapper.writeValueAsString(obj);
return Property.toProperties(new JSONObject(jsonInString));
}
}
/*
Copyright (c) 2002 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package nl.uva.sne.drip.commons.utils;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* Code from https://github.com/stleary/JSON-java/blob/master/Property.java
*/
public class Property {
/**
* Converts a property file object into a JSONObject. The property file
* object is a table of name value pairs.
*
* @param properties java.util.Properties
* @return JSONObject
* @throws JSONException
*/
public static JSONObject toJSONObject(java.util.Properties properties) throws JSONException {
JSONObject jo = new JSONObject();
if (properties != null && !properties.isEmpty()) {
Enumeration<?> enumProperties = properties.propertyNames();
while (enumProperties.hasMoreElements()) {
String name = (String) enumProperties.nextElement();
jo.put(name, properties.getProperty(name));
}
}
return jo;
}
/**
* Converts the JSONObject into a property file object.
*
* @param jo JSONObject
* @return java.util.Properties
* @throws JSONException
*/
public static Properties toProperties(JSONObject jo) throws JSONException {
Properties properties = new Properties();
if (jo != null) {
Iterator<String> keys = jo.keys();
while (keys.hasNext()) {
String name = keys.next();
properties.put(name, jo.getString(name));
}
}
return properties;
}
}
......@@ -37,5 +37,11 @@
<version>20090211</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<type>jar</type>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -35,6 +35,7 @@ import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import nl.uva.sne.drip.commons.types.Parameter;
import org.apache.commons.io.FilenameUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -75,7 +76,7 @@ public class Consumer extends DefaultConsumer {
try {
//The queue only moves bytes so we need to convert them to stting
String message = new String(body, "UTF-8");
String tempInputDirPath = System.getProperty("java.io.tmpdir") + File.separator + "Input-" + Long.toString(System.nanoTime()) + File.separator;
File tempInputDir = new File(tempInputDirPath);
if (!(tempInputDir.mkdirs())) {
......@@ -145,39 +146,32 @@ public class Consumer extends DefaultConsumer {
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
{
try {
File topologyFile = new File(tempInputDirPath + "topology_main");
if (topologyFile.createNewFile()) {
PrintWriter out = new PrintWriter(topologyFile);
out.print(param.get(Parameter.VALUE));
mainTopologyPath = topologyFile.getAbsolutePath();
} else {
return null;
}
} catch (IOException e) {
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, e);
File topologyFile = new File(tempInputDirPath + "topology_main");
if (topologyFile.createNewFile()) {
PrintWriter out = new PrintWriter(topologyFile);
out.print(param.get(Parameter.VALUE));
mainTopologyPath = topologyFile.getAbsolutePath();
} else {
return null;
}
} else if (fileLevel == 1) { ////this means that this file is the low level detailed description
String fileName = (String) attribute_level.get("filename"); ////This file name does not contain suffix of '.yml' for example
try {
File topologyFile = new File(tempInputDirPath + fileName + ".yml");
String outputFilePath = tempInputDirPath + fileName + "_provisioned.yml";
if (topologyFile.createNewFile()) {
PrintWriter out = new PrintWriter(geniConfFile);
out.print(param.get(Parameter.VALUE));
topologyElement x = new topologyElement();
x.topologyName = fileName;
x.outputFilePath = outputFilePath;
topologyInfoArray.add(x);
} else {
return null;
}
} catch (IOException e) {
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, e);
File topologyFile = new File(tempInputDirPath + fileName + ".yml");
String outputFilePath = tempInputDirPath + fileName + "_provisioned.yml";
if (topologyFile.createNewFile()) {
PrintWriter out = new PrintWriter(outputFilePath);
out.print(param.get(Parameter.VALUE));
topologyElement x = new topologyElement();
x.topologyName = fileName;
x.outputFilePath = outputFilePath;
topologyInfoArray.add(x);
} else {
return null;
}
}
} else if (name.equals("logdir")) {
logDir = (String) param.get(Parameter.VALUE);
......@@ -194,10 +188,8 @@ public class Consumer extends DefaultConsumer {
String[] ls = curDir.list();
for (int i = 0; i < ls.length; i++) {
if (ls[i].contains(".")) {
String[] fileTypes = ls[i].split("\\.");
if (fileTypes.length > 0) {
int lastIndex = fileTypes.length - 1;
String fileType = fileTypes[lastIndex];
String fileType = FilenameUtils.getExtension(ls[i]);
if (fileType != null) {
if (fileType.equals("yml")) {
String toscaFile = curDir + ls[i];
if (!sshKeyFilePath.equals("null")) {
......@@ -228,7 +220,7 @@ public class Consumer extends DefaultConsumer {
geniConfFilePath = geniConfFile.getAbsolutePath();
}
if (logDir.equals("null")) {
logDir = "/tmp/";
logDir = System.getProperty("java.io.tmpdir");
}
String cmd = "java -jar " + jarFilePath + " ec2=" + ec2ConfFilePath + " exogeni=" + geniConfFilePath + " logDir=" + logDir + " topology=" + mainTopologyPath;
......
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