Commit 142275f1 authored by Spiros Koulouzis's avatar Spiros Koulouzis

Fix hardcoded date on planner

Implemented the planner 
parent c4438b5f
......@@ -106,7 +106,8 @@ public class CloudConfigurationController {
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@RolesAllowed({UserService.USER, UserService.ADMIN})
public CloudCredentials get(@PathVariable("id") String id) {
public @ResponseBody
CloudCredentials get(@PathVariable("id") String id) {
CloudCredentials cc = cloudCredentialsDao.findOne(id);
if (cc == null) {
throw new NotFoundException();
......@@ -114,6 +115,14 @@ public class CloudConfigurationController {
return cc;
}
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
String delete(@PathVariable("id") String id) {
cloudCredentialsDao.delete(id);
return "Deleted :" + id;
}
@RequestMapping(value = "/ids")
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
......
......@@ -15,35 +15,29 @@
*/
package nl.uva.sne.drip.api.rest;
import nl.uva.sne.drip.api.service.SimplePlannerService;
import nl.uva.sne.drip.commons.types.ToscaRepresentation;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
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.rpc.PlannerCaller;
import nl.uva.sne.drip.commons.types.Message;
import nl.uva.sne.drip.commons.types.Parameter;
import nl.uva.sne.drip.commons.utils.Converter;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
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.rpc.DRIPCaller;
import nl.uva.sne.drip.api.service.PlannerService;
import nl.uva.sne.drip.api.service.ToscaService;
import nl.uva.sne.drip.api.service.UserService;
import org.springframework.web.bind.annotation.RequestParam;
......@@ -57,68 +51,27 @@ import org.springframework.web.bind.annotation.RequestParam;
@Component
public class PlannerController {
@Value("${message.broker.host}")
private String messageBrokerHost;
@Autowired
private ToscaService toscaService;
@Autowired
private SimplePlannerService simplePlannerService;
@Autowired
private PlannerService plannerService;
@RequestMapping(value = "/plan/{tosca_id}", method = RequestMethod.GET)
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
String plan(@PathVariable("tosca_id") String toscaId) {
DRIPCaller planner;
List<DRIPCaller> dripComponetens = new ArrayList<>();
try {
Message plannerInvokationMessage = buildPlannerMessage(toscaId);
planner = new PlannerCaller(messageBrokerHost);
dripComponetens.add(planner);
Message plannerReturnedMessage = planner.call(plannerInvokationMessage);
List<Parameter> toscaFiles = plannerReturnedMessage.getParameters();
ToscaRepresentation topLevel = new ToscaRepresentation();
ToscaRepresentation tr = null;
for (Parameter p : toscaFiles) {
//Should have levels in attributes
Map<String, String> attributess = p.getAttributes();
String originalFileName = p.getName();
String name = System.currentTimeMillis() + "_" + originalFileName;
if (originalFileName.equals("planner_output_all.yml")) {
topLevel.setName(name);
topLevel.setLevel(0);
topLevel.setKvMap(Converter.ymlString2Map(p.getValue()));
} else {
tr = new ToscaRepresentation();
tr.setName(name);
tr.setKvMap(Converter.ymlString2Map(p.getValue()));
tr.setLevel(1);
}
tr.setType(ToscaRepresentation.Type.PLAN);
toscaService.getDao().save(tr);
Set<String> ids = topLevel.getLowerLevelIDs();
if (ids == null) {
ids = new HashSet<>();
}
ids.add(tr.getId());
topLevel.setLowerLevelIDs(ids);
}
topLevel.setType(ToscaRepresentation.Type.PLAN);
toscaService.getDao().save(topLevel);
return topLevel.getId();
try {
// ToscaRepresentation plan = simplePlannerService.getPlan(toscaId);
// return plan.getId();
ToscaRepresentation plan = plannerService.getPlan(toscaId);
return plan.getId();
} catch (JSONException | IOException | TimeoutException | InterruptedException ex) {
Logger.getLogger(PlannerController.class.getName()).log(Level.SEVERE, null, ex);
} finally {
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;
}
......@@ -156,32 +109,4 @@ public class PlannerController {
return ids;
}
private Message buildPlannerMessage(String toscaId) throws JSONException, UnsupportedEncodingException, IOException {
ToscaRepresentation t2 = toscaService.getDao().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;
}
}
package nl.uva.sne.drip.api.rpc;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
......@@ -87,6 +88,7 @@ public abstract class DRIPCaller {
public Message call(Message r) throws IOException, TimeoutException, InterruptedException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
String jsonInString = mapper.writeValueAsString(r);
//Build a correlation ID to distinguish responds
......@@ -109,6 +111,7 @@ public abstract class DRIPCaller {
}
});
String strResponse = response.take();
strResponse = strResponse.replaceAll("'null'", "null").replaceAll("\'", "\"").replaceAll(" ", "");
// System.err.println(strResponse);
return mapper.readValue(strResponse, Message.class);
}
......
......@@ -15,8 +15,20 @@
*/
package nl.uva.sne.drip.api.service;
import nl.uva.sne.drip.api.dao.ToscaDao;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import nl.uva.sne.drip.api.rpc.PlannerCaller;
import nl.uva.sne.drip.commons.types.Message;
import nl.uva.sne.drip.commons.types.Parameter;
import nl.uva.sne.drip.commons.types.ToscaRepresentation;
import nl.uva.sne.drip.commons.utils.Converter;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
......@@ -25,9 +37,40 @@ import org.springframework.stereotype.Service;
*/
@Service
public class PlannerService {
@Autowired
private ToscaService toscaService;
@Value("${message.broker.host}")
private String messageBrokerHost;
public ToscaRepresentation getPlan(String toscaId) throws JSONException, UnsupportedEncodingException, IOException, TimeoutException, InterruptedException {
Message plannerInvokationMessage = buildPlannerMessage(toscaId);
PlannerCaller planner = new PlannerCaller(messageBrokerHost);
Message plannerReturnedMessage = planner.call(plannerInvokationMessage);
return null;
}
private Message buildPlannerMessage(String toscaId) throws JSONException, UnsupportedEncodingException {
ToscaRepresentation t2 = toscaService.getDao().findOne(toscaId);
Map<String, Object> map = t2.getKvMap();
String json = Converter.map2JsonString(map);
json = json.replaceAll("\\uff0E", "\\.");
byte[] bytes = json.getBytes();
Message invokationMessage = new Message();
List parameters = new ArrayList();
Parameter jsonArgument = new Parameter();
String charset = "UTF-8";
jsonArgument.setValue(new String(bytes, charset));
jsonArgument.setEncoding(charset);
jsonArgument.setName("input");
parameters.add(jsonArgument);
invokationMessage.setParameters(parameters);
invokationMessage.setCreationDate((System.currentTimeMillis()));
return invokationMessage;
}
}
/*
* 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.api.service;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeoutException;
import nl.uva.sne.drip.api.rpc.PlannerCaller;
import nl.uva.sne.drip.commons.types.Message;
import nl.uva.sne.drip.commons.types.Parameter;
import nl.uva.sne.drip.commons.types.ToscaRepresentation;
import nl.uva.sne.drip.commons.utils.Converter;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
*
* @author S. Koulouzis
*/
@Service
public class SimplePlannerService {
@Value("${message.broker.host}")
private String messageBrokerHost;
@Autowired
private ToscaService toscaService;
public ToscaRepresentation getPlan(String toscaId) throws JSONException, IOException, TimeoutException, InterruptedException {
Message plannerInvokationMessage = buildSimplePlannerMessage(toscaId);
PlannerCaller planner = new PlannerCaller(messageBrokerHost);
Message plannerReturnedMessage = planner.call(plannerInvokationMessage);
List<Parameter> toscaFiles = plannerReturnedMessage.getParameters();
ToscaRepresentation topLevel = new ToscaRepresentation();
ToscaRepresentation tr = null;
for (Parameter p : toscaFiles) {
//Should have levels in attributes
Map<String, String> attributess = p.getAttributes();
String originalFileName = p.getName();
String name = System.currentTimeMillis() + "_" + originalFileName;
if (originalFileName.equals("planner_output_all.yml")) {
topLevel.setName(name);
topLevel.setLevel(0);
topLevel.setKvMap(Converter.ymlString2Map(p.getValue()));
} else {
tr = new ToscaRepresentation();
tr.setName(name);
tr.setKvMap(Converter.ymlString2Map(p.getValue()));
tr.setLevel(1);
}
tr.setType(ToscaRepresentation.Type.PLAN);
toscaService.getDao().save(tr);
Set<String> ids = topLevel.getLowerLevelIDs();
if (ids == null) {
ids = new HashSet<>();
}
ids.add(tr.getId());
topLevel.setLowerLevelIDs(ids);
}
topLevel.setType(ToscaRepresentation.Type.PLAN);
toscaService.getDao().save(topLevel);
planner.close();
return topLevel;
}
private Message buildSimplePlannerMessage(String toscaId) throws JSONException, UnsupportedEncodingException, IOException {
ToscaRepresentation t2 = toscaService.getDao().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;
}
}
......@@ -14,30 +14,23 @@ from ICPCP import Workflow
import random
import time
import json
import time
connection = pika.BlockingConnection(pika.ConnectionParameters(host='172.17.0.2'))
connection = pika.BlockingConnection(pika.ConnectionParameters(host='172.17.0.3'))
channel = connection.channel()
channel.queue_declare(queue='planner_queue')
def handleDelivery(message):
parsed_json = json.loads(message)
params = parsed_json["parameters"]
for param in params:
name = param["name"]
value = param["value"]
def on_request(ch, method, props, body):
handleDelivery(body)
print(" Message %s" % body)
response = "AAAAAAAAAAAAAAAAAAAAAA"
json1 = response.get('parameters')[0].get('value').get('topology_template').get('node_templates')
parsed_message = json.loads(body)
value = parsed_message.get('parameters')[0].get('value')
parsed_value = json.loads(value)
json1 = parsed_value.get('topology_template').get('node_templates')
deadline = 0
for j in json1:
......@@ -101,22 +94,22 @@ def on_request(ch, method, props, body):
#print content['workflow']
#return
res = wf.generateJSON()
end = time.time()
print (end - start)
# generate the json files in the corresponding format as the
outcontent = {}
outcontent["creationDate"] = 1487002029722
outcontent["creationDate"] = time.time()
outcontent["parameters"] = []
par1 = {}
par1["url"] = "null"
par1["url"] = 'null'
par1["encoding"] = "UTF-8"
par1["value"] = res
par1["attributes"] = "null"
par1["value"] = str(res)
par1["attributes"] = 'null'
outcontent["parameters"].append(par1)
response = outcontent
print(response)
ch.basic_publish(exchange='',
routing_key=props.reply_to,
properties=pika.BasicProperties(correlation_id = \
......
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