Commit 416a104a authored by Spiros Koulouzis's avatar Spiros Koulouzis

Implmented provisioner for v0.0

parent bda62e5e
......@@ -28,7 +28,7 @@ public class WebAppInitializer implements WebApplicationInitializer {
ctx.register(MultipartConfig.class);
ctx.register(MongoConfig.class);
ctx.register(SecurityConfig.class);
ctx.register(MethodSecurityConfig.class);
// ctx.register(MethodSecurityConfig.class);
ctx.setServletContext(servletContext);
......
......@@ -22,6 +22,6 @@ import org.springframework.data.mongodb.repository.MongoRepository;
*
* @author S. Koulouzis
*/
public interface UserScriptDao extends MongoRepository<Script, String> {
public interface ScriptDao extends MongoRepository<Script, String> {
}
......@@ -18,10 +18,12 @@ package nl.uva.sne.drip.api.service;
import java.util.List;
import nl.uva.sne.drip.api.dao.CloudCredentialsDao;
import nl.uva.sne.drip.commons.v1.types.CloudCredentials;
import nl.uva.sne.drip.commons.v1.types.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreFilter;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
/**
......@@ -29,33 +31,35 @@ import org.springframework.stereotype.Service;
* @author S. Koulouzis
*/
@Service
@PreAuthorize("isAuthenticated()")
public class CloudCredentialsService {
@Autowired
private CloudCredentialsDao dao;
// @PreFilter("(filterObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public CloudCredentials save(CloudCredentials cloudCredentials) {
// String owner = user.getUsername();
// cloudCredentials.setOwner(owner);
System.err.println(cloudCredentials.getOwner());
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String owner = user.getUsername();
cloudCredentials.setOwner(owner);
return dao.save(cloudCredentials);
}
// @PreAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
@PreAuthorize("hasPermission(#returnObject, 'read')")
@PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public CloudCredentials findOne(String id) {
CloudCredentials creds = dao.findOne(id);
return creds;
}
public void delete(String id) {
dao.delete(id);
@PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public CloudCredentials delete(String id) {
CloudCredentials creds = dao.findOne(id);
dao.delete(creds);
return creds;
}
// @PreAuthorize(" (hasRole('ROLE_ADMIN')) or (hasRole('ROLE_USER'))")
// @PostFilter("(filterObject.owner == authentication.name)")
@PostFilter("(filterObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
// @PostFilter("hasPermission(filterObject, 'read') or hasPermission(filterObject, 'admin')")
public List<CloudCredentials> findAll() {
return dao.findAll();
}
......
......@@ -34,11 +34,14 @@ import nl.uva.sne.drip.commons.v1.types.MessageParameter;
import nl.uva.sne.drip.commons.v1.types.Plan;
import nl.uva.sne.drip.commons.v1.types.ToscaRepresentation;
import nl.uva.sne.drip.commons.utils.Converter;
import nl.uva.sne.drip.commons.v1.types.User;
import nl.uva.sne.drip.drip.converter.P2PConverter;
import nl.uva.sne.drip.drip.converter.SimplePlanContainer;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
/**
......@@ -46,6 +49,7 @@ import org.springframework.stereotype.Service;
* @author S. Koulouzis
*/
@Service
@PreAuthorize("isAuthenticated()")
public class PlannerService {
@Autowired
......@@ -92,18 +96,18 @@ public class PlannerService {
lowLevelPlan.setToscaID(toscaId);
lowLevelPlan.setName(lowLevelNames);
lowLevelPlan.setKvMap(Converter.ymlString2Map(map.get(lowLevelNames)));
planDao.save(lowLevelPlan);
save(lowLevelPlan);
loweLevelPlansIDs.add(lowLevelPlan.getId());
}
topLevel.setLoweLevelPlansIDs(loweLevelPlansIDs);
planDao.save(topLevel);
save(topLevel);
return topLevel;
}
}
private Message buildPlannerMessage(String toscaId) throws JSONException, UnsupportedEncodingException {
ToscaRepresentation t2 = toscaService.getDao().findOne(toscaId);
ToscaRepresentation t2 = toscaService.findOne(toscaId);
if (t2 == null) {
throw new BadRequestException();
}
......@@ -136,8 +140,10 @@ public class PlannerService {
Set<String> ids = plan.getLoweLevelPlanIDs();
for (String lowID : ids) {
Map<String, Object> lowLevelMap = planDao.findOne(lowID).getKeyValue();
if (lowLevelMap != null) {
map.putAll(lowLevelMap);
}
}
if (fromat != null && fromat.equals("yml")) {
String ymlStr = Converter.map2YmlString(map);
......@@ -158,10 +164,6 @@ public class PlannerService {
return planDao.findOne(id).getToscaID();
}
public PlanDao getDao() {
return this.planDao;
}
public List<Plan> findAll() {
List<Plan> all = planDao.findAll();
List<Plan> topLevel = new ArrayList<>();
......@@ -173,4 +175,21 @@ public class PlannerService {
return topLevel;
}
public Plan save(Plan plan) {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String owner = user.getUsername();
plan.setOwner(owner);
return planDao.save(plan);
}
public Plan findOne(String lowiID) {
return planDao.findOne(lowiID);
}
public Plan delete(String id) {
Plan plan = planDao.findOne(id);
planDao.delete(plan);
return plan;
}
}
......@@ -15,22 +15,319 @@
*/
package nl.uva.sne.drip.api.service;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import nl.uva.sne.drip.api.dao.ProvisionInfoDao;
import nl.uva.sne.drip.api.exception.BadRequestException;
import nl.uva.sne.drip.api.exception.CloudCredentialsNotFoundException;
import nl.uva.sne.drip.api.exception.ExceptionHandler;
import nl.uva.sne.drip.api.exception.PlanNotFoundException;
import nl.uva.sne.drip.api.rpc.DRIPCaller;
import nl.uva.sne.drip.api.rpc.ProvisionerCaller;
import nl.uva.sne.drip.api.v1.rest.ProvisionController;
import nl.uva.sne.drip.commons.utils.Converter;
import nl.uva.sne.drip.commons.v1.types.CloudCredentials;
import nl.uva.sne.drip.commons.v1.types.DeployParameter;
import nl.uva.sne.drip.commons.v1.types.LoginKey;
import nl.uva.sne.drip.commons.v1.types.Message;
import nl.uva.sne.drip.commons.v1.types.MessageParameter;
import nl.uva.sne.drip.commons.v1.types.Plan;
import nl.uva.sne.drip.commons.v1.types.ProvisionInfo;
import nl.uva.sne.drip.commons.v1.types.Script;
import nl.uva.sne.drip.commons.v1.types.User;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
/**
*
* @author S. Koulouzis
*/
@Service
@PreAuthorize("isAuthenticated()")
public class ProvisionService {
@Autowired
private ProvisionInfoDao dao;
public ProvisionInfoDao getDao() {
return dao;
@Autowired
private CloudCredentialsService cloudCredentialsService;
@Autowired
private SimplePlannerService planService;
@Autowired
private UserScriptService userScriptService;
@Autowired
private UserKeyService userKeysService;
@Value("${message.broker.host}")
private String messageBrokerHost;
public ProvisionInfo save(ProvisionInfo provisionInfo) {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String owner = user.getUsername();
provisionInfo.setOwner(owner);
return dao.save(provisionInfo);
}
@PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public ProvisionInfo findOne(String id) {
ProvisionInfo provisionInfo = dao.findOne(id);
return provisionInfo;
}
@PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public ProvisionInfo delete(String id) {
ProvisionInfo provisionInfo = dao.findOne(id);
dao.delete(provisionInfo);
return provisionInfo;
}
// @PreAuthorize(" (hasRole('ROLE_ADMIN')) or (hasRole('ROLE_USER'))")
@PostFilter("(filterObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
// @PostFilter("hasPermission(filterObject, 'read') or hasPermission(filterObject, 'admin')")
public List<ProvisionInfo> findAll() {
return dao.findAll();
}
public ProvisionInfo provisionResources(ProvisionInfo req) throws IOException, TimeoutException, JSONException, InterruptedException {
try (DRIPCaller provisioner = new ProvisionerCaller(messageBrokerHost);) {
Message provisionerInvokationMessage = buildProvisionerMessage(req);
Message response = (provisioner.call(provisionerInvokationMessage));
// Message response = generateFakeResponse(System.getProperty("user.home")
// + File.separator + "workspace" + File.separator + "DRIP"
// + File.separator + "doc" + File.separator + "json_samples" +
// File.separator + "ec2_provisioner_provisoned2.json");
List<MessageParameter> params = response.getParameters();
for (MessageParameter p : params) {
String name = p.getName();
if (name.toLowerCase().contains("exception")) {
RuntimeException ex = ExceptionHandler.generateException(name, p.getValue());
Logger.getLogger(ProvisionController.class.getName()).log(Level.SEVERE, null, ex);
throw ex;
}
if (!name.equals("kubernetes")) {
String value = p.getValue();
Map<String, Object> kvMap = Converter.ymlString2Map(value);
req.setKvMap(kvMap);
req.setPlanID(req.getPlanID());
} else {
String value = p.getValue();
String[] lines = value.split("\n");
List<DeployParameter> deployParameters = new ArrayList<>();
for (String line : lines) {
DeployParameter deployParam = new DeployParameter();
String[] parts = line.split(" ");
String deployIP = parts[0];
String deployUser = parts[1];
String deployCertPath = parts[2];
String cloudCertificateName = FilenameUtils.removeExtension(FilenameUtils.getBaseName(deployCertPath));
String deployRole = parts[3];
deployParam.setIP(deployIP);
deployParam.setRole(deployRole);
deployParam.setUser(deployUser);
deployParam.setCloudCertificateName(cloudCertificateName);
deployParameters.add(deployParam);
}
req.setDeployParameters(deployParameters);
}
}
req = save(req);
return req;
}
}
private Message buildProvisionerMessage(ProvisionInfo pReq) throws JSONException, IOException {
Message invokationMessage = new Message();
List<MessageParameter> parameters = new ArrayList();
CloudCredentials cred = cloudCredentialsService.findOne(pReq.getCloudcloudCredentialsID());
if (cred == null) {
throw new CloudCredentialsNotFoundException();
}
MessageParameter conf = buildCloudConfParam(cred);
parameters.add(conf);
List<MessageParameter> certs = buildCertificatesParam(cred);
parameters.addAll(certs);
List<MessageParameter> topologies = buildTopologyParams(pReq.getPlanID());
parameters.addAll(topologies);
String scriptID = pReq.getscriptID();
if (scriptID != null) {
List<MessageParameter> userScripts = buildScriptParams(scriptID);
parameters.addAll(userScripts);
}
String userKeyID = pReq.getUserKeyID();
if (userKeyID != null) {
List<MessageParameter> userKeys = buildKeysParams(userKeyID);
parameters.addAll(userKeys);
}
invokationMessage.setParameters(parameters);
invokationMessage.setCreationDate((System.currentTimeMillis()));
return invokationMessage;
}
private MessageParameter buildCloudConfParam(CloudCredentials cred) throws JsonProcessingException, JSONException, IOException {
MessageParameter conf = null;
String provider = cred.getCloudProviderName();
if (provider == null) {
throw new BadRequestException("Provider name can't be null. Check the cloud credentials: " + cred.getId());
}
switch (cred.getCloudProviderName().toLowerCase()) {
case "ec2":
conf = buildEC2Conf(cred);
break;
}
return conf;
}
private List<MessageParameter> buildCertificatesParam(CloudCredentials cred) {
List<LoginKey> loginKeys = cred.getLoginKeys();
if (loginKeys == null || loginKeys.isEmpty()) {
throw new BadRequestException("Log in keys can't be empty");
}
List<MessageParameter> parameters = new ArrayList<>();
for (LoginKey lk : loginKeys) {
String domainName = lk.getAttributes().get("domain_name");
if (domainName == null) {
domainName = lk.getAttributes().get("domain_name ");
}
MessageParameter cert = new MessageParameter();
cert.setName("certificate");
cert.setValue(lk.getKey());
Map<String, String> attributes = new HashMap<>();
attributes.put("filename", domainName);
cert.setAttributes(attributes);
parameters.add(cert);
}
return parameters;
}
private List<MessageParameter> buildTopologyParams(String planID) throws JSONException {
Plan plan = planService.getDao().findOne(planID);
if (plan == null) {
throw new PlanNotFoundException();
}
List<MessageParameter> parameters = new ArrayList();
MessageParameter topology = new MessageParameter();
topology.setName("topology");
topology.setValue(Converter.map2YmlString(plan.getKeyValue()));
Map<String, String> attributes = new HashMap<>();
attributes.put("level", String.valueOf(plan.getLevel()));
attributes.put("filename", FilenameUtils.removeExtension(plan.getName()));
topology.setAttributes(attributes);
parameters.add(topology);
Set<String> ids = plan.getLoweLevelPlanIDs();
for (String lowID : ids) {
Plan lowPlan = planService.getDao().findOne(lowID);
topology = new MessageParameter();
topology.setName("topology");
topology.setValue(Converter.map2YmlString(lowPlan.getKeyValue()));
attributes = new HashMap<>();
attributes.put("level", String.valueOf(lowPlan.getLevel()));
attributes.put("filename", FilenameUtils.removeExtension(lowPlan.getName()));
topology.setAttributes(attributes);
parameters.add(topology);
}
return parameters;
}
private MessageParameter buildEC2Conf(CloudCredentials cred) throws JsonProcessingException, JSONException, IOException {
Properties prop = Converter.getEC2Properties(cred);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
prop.store(baos, null);
byte[] bytes = baos.toByteArray();
MessageParameter conf = new MessageParameter();
conf.setName("ec2.conf");
String charset = "UTF-8";
conf.setValue(new String(bytes, charset));
return conf;
}
private List<MessageParameter> buildScriptParams(String userScriptID) {
Script script = userScriptService.findOne(userScriptID);
if (script == null) {
throw new BadRequestException("User script: " + userScriptID + " was not found");
}
List<MessageParameter> parameters = new ArrayList();
MessageParameter scriptParameter = new MessageParameter();
scriptParameter.setName("guiscript");
scriptParameter.setValue(script.getContents());
scriptParameter.setEncoding("UTF-8");
parameters.add(scriptParameter);
return parameters;
}
private List<MessageParameter> buildKeysParams(String userKeyID) {
LoginKey key = userKeysService.get(userKeyID, LoginKey.Type.PUBLIC);
if (key == null) {
throw new BadRequestException("User key: " + userKeyID + " was not found");
}
List<MessageParameter> parameters = new ArrayList();
MessageParameter keyParameter = new MessageParameter();
keyParameter.setName("sshkey");
keyParameter.setValue(key.getKey());
keyParameter.setEncoding("UTF-8");
parameters.add(keyParameter);
return parameters;
}
private Message generateFakeResponse(String path) throws IOException, TimeoutException, InterruptedException, JSONException {
// String strResponse = "{\"creationDate\":1488368936945,\"parameters\":["
// + "{\"name\":\"f293ff03-4b82-49e2-871a-899aadf821ce\","
// + "\"encoding\":\"UTF-8\",\"value\":"
// + "\"publicKeyPath: /tmp/Input-4007028381500/user.pem\\nuserName: "
// + "zh9314\\nsubnets:\\n- {name: s1, subnet: 192.168.10.0, "
// + "netmask: 255.255.255.0}\\ncomponents:\\n- "
// + "name: faab6756-61b6-4800-bffa-ae9d859a9d6c\\n "
// + "type: Switch.nodes.Compute\\n nodetype: t2.medium\\n "
// + "OStype: Ubuntu 16.04\\n domain: ec2.us-east-1.amazonaws.com\\n "
// + "script: /tmp/Input-4007028381500/guiscipt.sh\\n "
// + "installation: null\\n role: master\\n "
// + "dockers: mogswitch/InputDistributor\\n "
// + "public_address: 54.144.0.91\\n instanceId: i-0e78cbf853328b820\\n "
// + "ethernet_port:\\n - {name: p1, subnet_name: s1, "
// + "address: 192.168.10.10}\\n- name: 1c75eedf-8497-46fe-aeb8-dab6a62154cb\\n "
// + "type: Switch.nodes.Compute\\n nodetype: t2.medium\\n OStype: Ubuntu 16.04\\n domain: ec2.us-east-1.amazonaws.com\\n script: /tmp/Input-4007028381500/guiscipt.sh\\n installation: null\\n role: slave\\n dockers: mogswitch/ProxyTranscoder\\n public_address: 34.207.254.160\\n instanceId: i-0a99ea18fcc77ed7a\\n ethernet_port:\\n - {name: p1, subnet_name: s1, address: 192.168.10.11}\\n\"},{\"name\":\"kubernetes\",\"encoding\":\"UTF-8\",\"value\":\"54.144.0.91 ubuntu /tmp/Input-4007028381500/Virginia.pem master\\n34.207.254.160 ubuntu /tmp/Input-4007028381500/Virginia.pem slave\\n\"}]}";
// String strResponse = "{\"creationDate\":1488805337447,\"parameters\":[{\"name\":\"2e5dafb6-5a1c-4a66-9dca-5841f99ea735\",\"encoding\":\"UTF-8\",\"value\":\"publicKeyPath: /tmp/Input-11594765342486/user.pem\\nuserName: zh9314\\nsubnets:\\n- {name: s1, subnet: 192.168.10.0, netmask: 255.255.255.0}\\ncomponents:\\n- name: 8fcc1788d9ee462c826572c79fdb2a6a\\n type: Switch.nodes.Compute\\n nodeType: t2.medium\\n OStype: Ubuntu 16.04\\n script: /tmp/Input-11594765342486/guiscipt.sh\\n domain: ec2.us-east-1.amazonaws.com\\n installation: null\\n clusterType: swarm\\n role: master\\n dockers: mogswitch/ProxyTranscoder:1.0\\n public_address: 34.207.73.18\\n instanceId: i-0e82b5624a0df99b1\\n ethernet_port:\\n - {name: p1, subnet_name: s1, address: 192.168.10.10}\\n- name: 8fcc1788d9ee462c826572c79fdb2a6a\\n type: Switch.nodes.Compute\\n nodeType: t2.medium\\n OStype: Ubuntu 16.04\\n script: /tmp/Input-11594765342486/guiscipt.sh\\n domain: ec2.us-east-1.amazonaws.com\\n installation: null\\n clusterType: swarm\\n role: slave\\n dockers: mogswitch/ProxyTranscoder:1.0\\n public_address: 34.207.73.18\\n instanceId: i-0e82b5624a0df99b1\\n ethernet_port:\\n - {name: p1, subnet_name: s1, address: 192.168.10.11}\\n\"},{\"name\":\"kubernetes\",\"encoding\":\"UTF-8\",\"value\":\"34.207.73.18 ubuntu /tmp/Input-11594765342486/Virginia.pem master\\n34.207.73.18 ubuntu /tmp/Input-11594765342486/Virginia.pem slave\\n\"}]}";
String strResponse = FileUtils.readFileToString(new File(path));
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
return mapper.readValue(strResponse, Message.class);
}
}
......@@ -56,7 +56,7 @@ public class SimplePlannerService {
private PlanDao planDao;
public Plan getPlan(String toscaId) throws JSONException, IOException, TimeoutException, InterruptedException {
ToscaRepresentation tosca = toscaService.getDao().findOne(toscaId);
ToscaRepresentation tosca = toscaService.findOne(toscaId);
Message plannerInvokationMessage = buildSimplePlannerMessage(tosca);
Plan topLevel;
......
......@@ -22,8 +22,13 @@ import nl.uva.sne.drip.api.dao.ToscaDao;
import nl.uva.sne.drip.api.exception.NotFoundException;
import nl.uva.sne.drip.commons.v1.types.ToscaRepresentation;
import nl.uva.sne.drip.commons.utils.Converter;
import nl.uva.sne.drip.commons.v1.types.User;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
......@@ -32,13 +37,14 @@ import org.springframework.web.multipart.MultipartFile;
* @author S. Koulouzis
*/
@Service
@PreAuthorize("isAuthenticated()")
public class ToscaService {
@Autowired
private ToscaDao dao;
public String get(String id, String fromat) throws JSONException {
ToscaRepresentation tosca = dao.findOne(id);
ToscaRepresentation tosca = findOne(id);
if (tosca == null) {
throw new NotFoundException();
}
......@@ -60,7 +66,7 @@ public class ToscaService {
return ymlStr;
}
public String save(MultipartFile file) throws IOException {
public String saveFile(MultipartFile file) throws IOException {
String originalFileName = file.getOriginalFilename();
String name = System.currentTimeMillis() + "_" + originalFileName;
byte[] bytes = file.getBytes();
......@@ -71,11 +77,11 @@ public class ToscaService {
ToscaRepresentation t = new ToscaRepresentation();
t.setName(name);
t.setKvMap(map);
dao.save(t);
save(t);
return t.getId();
}
public String save(String yamlString, String name) throws IOException {
public String saveYamlString(String yamlString, String name) throws IOException {
if (name == null) {
name = System.currentTimeMillis() + "_" + "tosca.yaml";
}
......@@ -84,27 +90,31 @@ public class ToscaService {
ToscaRepresentation t = new ToscaRepresentation();
t.setName(name);
t.setKvMap(map);
dao.save(t);
save(t);
return t.getId();
}
public void delete(String id) {
dao.delete(id);
@PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public ToscaRepresentation delete(String id) {
ToscaRepresentation tr = dao.findOne(id);
dao.delete(tr);
return tr;
}
@PostFilter("(filterObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public List<ToscaRepresentation> findAll() {
return dao.findAll();
}
public ToscaDao getDao() {
return dao;
@PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public ToscaRepresentation findOne(String id) {
return dao.findOne(id);
}
public ToscaRepresentation get(String planID) {
ToscaRepresentation tosca = dao.findOne(planID);
if (tosca == null) {
throw new NotFoundException();
}
return tosca;
private ToscaRepresentation save(ToscaRepresentation t) {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String owner = user.getUsername();
t.setOwner(owner);
return dao.save(t);
}
}
......@@ -19,7 +19,11 @@ import java.util.ArrayList;
import java.util.List;
import nl.uva.sne.drip.api.dao.UserKeyDao;
import nl.uva.sne.drip.commons.v1.types.LoginKey;
import nl.uva.sne.drip.commons.v1.types.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
/**
......@@ -32,12 +36,8 @@ public class UserKeyService {
@Autowired
UserKeyDao dao;
public UserKeyDao getDao() {
return dao;
}
public List<LoginKey> getAll(LoginKey.Type type) {
List<LoginKey> all = getDao().findAll();
List<LoginKey> all = findAll();
if (all != null) {
List<LoginKey> allPublic = new ArrayList<>();
for (LoginKey tr : all) {
......@@ -51,7 +51,7 @@ public class UserKeyService {
}
public LoginKey get(String id, LoginKey.Type type) {
LoginKey key = getDao().findOne(id);
LoginKey key = findOne(id);
if (key.getType().equals(type)) {
return key;
}
......@@ -61,7 +61,31 @@ public class UserKeyService {
public void delete(String id, LoginKey.Type type) {
LoginKey k = get(id, type);
if (k != null) {
getDao().delete(k);
delete(k);
}
}
@PostFilter("(filterObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public List<LoginKey> findAll() {
return dao.findAll();
}
@PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public LoginKey findOne(String id) {
return dao.findOne(id);
}
@PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public LoginKey delete(LoginKey k) {
k = dao.findOne(k.getId());
dao.delete(k);
return k;
}
public LoginKey save(LoginKey upk) {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String owner = user.getUsername();
upk.setOwner(owner);
return dao.save(upk);
}
}
......@@ -15,21 +15,52 @@
*/
package nl.uva.sne.drip.api.service;
import nl.uva.sne.drip.api.dao.UserScriptDao;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import nl.uva.sne.drip.api.dao.ScriptDao;
import nl.uva.sne.drip.commons.v1.types.Script;
import nl.uva.sne.drip.commons.v1.types.User;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.core.context.SecurityContextHolder;
/**
*
* @author S. Koulouzis
*/
@Service
@PreAuthorize("isAuthenticated()")
public class UserScriptService {
@Autowired
UserScriptDao dao;
ScriptDao dao;
public UserScriptDao getDao() {
return dao;
public Script save(Script script) {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String owner = user.getUsername();
script.setOwner(owner);
return dao.save(script);
}
@PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public Script findOne(String id) {
Script script = dao.findOne(id);
return script;
}
@PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public Script delete(String id) {
Script script = dao.findOne(id);
dao.delete(script);
return script;
}
// @PreAuthorize(" (hasRole('ROLE_ADMIN')) or (hasRole('ROLE_USER'))")
@PostFilter("(filterObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
// @PostFilter("hasPermission(filterObject, 'read') or hasPermission(filterObject, 'admin')")
public List<Script> findAll() {
return dao.findAll();
}
}
......@@ -30,6 +30,7 @@ import org.springframework.web.bind.annotation.RestController;
import nl.uva.sne.drip.api.dao.CloudCredentialsDao;
import nl.uva.sne.drip.api.exception.NullKeyException;
import nl.uva.sne.drip.api.exception.NullKeyIDException;
import nl.uva.sne.drip.api.service.CloudCredentialsService;
import nl.uva.sne.drip.api.service.UserService;
import nl.uva.sne.drip.commons.v0.types.Configure;
import nl.uva.sne.drip.commons.v1.types.LoginKey;
......@@ -49,7 +50,7 @@ import org.springframework.web.bind.annotation.RequestBody;
public class CloudConfigurationController0 {
@Autowired
private CloudCredentialsDao cloudCredentialsDao;
private CloudCredentialsService cloudCredentialsDao;
@RequestMapping(value = "/ec2", method = RequestMethod.POST, consumes = MediaType.TEXT_XML_VALUE)
@RolesAllowed({UserService.USER, UserService.ADMIN})
......
......@@ -35,7 +35,6 @@ import nl.uva.sne.drip.api.service.UserService;
import nl.uva.sne.drip.commons.utils.Converter;
import nl.uva.sne.drip.commons.v0.types.File;
import nl.uva.sne.drip.commons.v0.types.Plan;
import org.apache.commons.io.FilenameUtils;
import org.json.JSONException;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
......@@ -65,44 +64,26 @@ public class PlannerController0 {
try {
String yaml = plan0.file;
yaml = yaml.replaceAll("\\\\n", "\n");
String id = toscaService.save(yaml, null);
String id = toscaService.saveYamlString(yaml, null);
nl.uva.sne.drip.commons.v1.types.Plan plan1 = plannerService.getPlan(id);
Result r = new Result();
r.info = ("INFO");
r.status = ("Success");
List<File> files = new ArrayList<>();
File e = new File();
e.level = String.valueOf(plan1.getLevel());
String p1Name = FilenameUtils.getBaseName(plan1.getName());
if (p1Name == null) {
p1Name = "Planned_tosca_file_" + plan1.getLevel();
plan1.setName(p1Name);
plannerService.getDao().save(plan1);
}
File e = Converter.plan1toFile(plan1);
e.name = p1Name;
e.content = Converter.map2YmlString(plan1.getKeyValue()).replaceAll("\n", "\\n");
files.add(e);
for (String lowiID : plan1.getLoweLevelPlanIDs()) {
nl.uva.sne.drip.commons.v1.types.Plan lowPlan1 = plannerService.getDao().findOne(lowiID);
e = new File();
e.level = String.valueOf(lowPlan1.getLevel());
p1Name = lowPlan1.getName();
if (p1Name == null) {
p1Name = "Planned_tosca_file_" + lowPlan1.getLevel();
plan1.setName(p1Name);
plannerService.getDao().save(lowPlan1);
}
e.name = p1Name;
e.content = Converter.map2YmlString(lowPlan1.getKeyValue()).replaceAll("\n", "\\n");;
nl.uva.sne.drip.commons.v1.types.Plan lowPlan1 = plannerService.findOne(lowiID);
e = Converter.plan1toFile(lowPlan1);
files.add(e);
//Don't save them cause they will be re-uploaded in the provision step
plannerService.delete(lowPlan1.getId());
}
r.file = files;
plannerService.delete(plan1.getId());
return r;
} catch (IOException | JSONException | TimeoutException | InterruptedException ex) {
Logger.getLogger(PlannerController0.class.getName()).log(Level.SEVERE, null, ex);
......
......@@ -15,24 +15,16 @@
*/
package nl.uva.sne.drip.api.v0.rest;
import com.fasterxml.jackson.core.JsonParser;
import nl.uva.sne.drip.commons.v1.types.ProvisionInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
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.dao.CloudCredentialsDao;
import nl.uva.sne.drip.commons.v1.types.Message;
import nl.uva.sne.drip.commons.v1.types.MessageParameter;
import nl.uva.sne.drip.commons.utils.Converter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
......@@ -41,21 +33,18 @@ 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.exception.BadRequestException;
import nl.uva.sne.drip.api.exception.CloudCredentialsNotFoundException;
import nl.uva.sne.drip.api.exception.PlanNotFoundException;
import nl.uva.sne.drip.api.service.CloudCredentialsService;
import nl.uva.sne.drip.api.service.PlannerService;
import nl.uva.sne.drip.api.service.ProvisionService;
import nl.uva.sne.drip.api.service.SimplePlannerService;
import nl.uva.sne.drip.api.service.UserKeyService;
import nl.uva.sne.drip.api.service.UserScriptService;
import nl.uva.sne.drip.api.service.UserService;
import nl.uva.sne.drip.commons.v0.types.File;
import nl.uva.sne.drip.commons.v0.types.Upload;
import nl.uva.sne.drip.commons.v1.types.CloudCredentials;
import nl.uva.sne.drip.commons.v1.types.LoginKey;
import nl.uva.sne.drip.commons.v1.types.Plan;
import nl.uva.sne.drip.commons.v1.types.Script;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.json.JSONException;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
......@@ -71,9 +60,6 @@ import org.springframework.web.bind.annotation.RequestBody;
@Component
public class ProvisionController0 {
@Value("${message.broker.host}")
private String messageBrokerHost;
@Autowired
private UserScriptService userScriptService;
......@@ -81,13 +67,34 @@ public class ProvisionController0 {
private UserKeyService userKeysService;
@Autowired
private CloudCredentialsDao cloudCredentialsDao;
private CloudCredentialsService cloudCredentialsService;
@Autowired
private ProvisionService provisionService;
@Autowired
private SimplePlannerService planService;
private PlannerService planService;
@RequestMapping(value = "/get", method = RequestMethod.GET, produces = MediaType.TEXT_XML_VALUE)
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
Upload provision() {
try {
Upload up = new Upload();
up.user = "user";
up.pwd = "123";
List<File> files = new ArrayList<>();
Plan plan1 = planService.findAll().get(0);
File f = Converter.plan1toFile(plan1);
files.add(f);
up.file = files;
return up;
} catch (JSONException ex) {
Logger.getLogger(ProvisionController0.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.TEXT_XML_VALUE)
@RolesAllowed({UserService.USER, UserService.ADMIN})
......@@ -95,185 +102,38 @@ public class ProvisionController0 {
String provision(@RequestBody Upload upload) {
ProvisionInfo provInfo = new ProvisionInfo();
String cloudCredID = null;
CloudCredentials cloudCred = cloudCredentialsService.findAll().get(0);
String cloudCredID = cloudCred.getId();
provInfo.setCloudcloudCredentialsID(cloudCredID);
String planID = null;
List<nl.uva.sne.drip.commons.v0.types.File> plans = upload.file;
nl.uva.sne.drip.commons.v1.types.Plan topLevelPlan = null;
Set<String> loweLevelPlansIDs = new HashSet<>();
for (nl.uva.sne.drip.commons.v0.types.File p : plans) {
nl.uva.sne.drip.commons.v1.types.Plan plan1 = Converter.File2Plan1(p);
if (plan1.getLevel() == 0) {
topLevelPlan = plan1;
} else {
loweLevelPlansIDs.add(plan1.getId());
planService.save(plan1);
}
}
topLevelPlan.setLoweLevelPlansIDs(loweLevelPlansIDs);
topLevelPlan = planService.save(topLevelPlan);
String planID = topLevelPlan.getId();
provInfo.setPlanID(planID);
String userKeyID = null;
List<LoginKey> allKeys = userKeysService.findAll();
if (allKeys != null && !allKeys.isEmpty()) {
String userKeyID = allKeys.get(0).getId();
provInfo.setUserKeyID(userKeyID);
String scriptID = null;
}
List<Script> allScripts = userScriptService.findAll();
if (allScripts != null && !allScripts.isEmpty()) {
String scriptID = allScripts.get(0).getId();
provInfo.setScriptID(scriptID);
provisionService.getDao().save(provInfo);
}
provInfo = provisionService.save(provInfo);
return "Success: Infrastructure files are uploaded! Action number: "
+ provInfo.getId();
}
private Message buildProvisionerMessage(ProvisionInfo pReq) throws JSONException, IOException {
Message invokationMessage = new Message();
List<MessageParameter> parameters = new ArrayList();
CloudCredentials cred = cloudCredentialsDao.findOne(pReq.getCloudcloudCredentialsID());
if (cred == null) {
throw new CloudCredentialsNotFoundException();
}
MessageParameter conf = buildCloudConfParam(cred);
parameters.add(conf);
List<MessageParameter> certs = buildCertificatesParam(cred);
parameters.addAll(certs);
List<MessageParameter> topologies = buildTopologyParams(pReq.getPlanID());
parameters.addAll(topologies);
String scriptID = pReq.getscriptID();
if (scriptID != null) {
List<MessageParameter> userScripts = buildScriptParams(scriptID);
parameters.addAll(userScripts);
}
String userKeyID = pReq.getUserKeyID();
if (userKeyID != null) {
List<MessageParameter> userKeys = buildKeysParams(userKeyID);
parameters.addAll(userKeys);
}
invokationMessage.setParameters(parameters);
invokationMessage.setCreationDate((System.currentTimeMillis()));
return invokationMessage;
}
private MessageParameter buildCloudConfParam(CloudCredentials cred) throws JsonProcessingException, JSONException, IOException {
MessageParameter conf = null;
String provider = cred.getCloudProviderName();
if (provider == null) {
throw new BadRequestException("Provider name can't be null. Check the cloud credentials: " + cred.getId());
}
switch (cred.getCloudProviderName().toLowerCase()) {
case "ec2":
conf = buildEC2Conf(cred);
break;
}
return conf;
}
private List<MessageParameter> buildCertificatesParam(CloudCredentials cred) {
List<LoginKey> loginKeys = cred.getLoginKeys();
if (loginKeys == null || loginKeys.isEmpty()) {
throw new BadRequestException("Log in keys can't be empty");
}
List<MessageParameter> parameters = new ArrayList<>();
for (LoginKey lk : loginKeys) {
String domainName = lk.getAttributes().get("domain_name");
if (domainName == null) {
domainName = lk.getAttributes().get("domain_name ");
}
MessageParameter cert = new MessageParameter();
cert.setName("certificate");
cert.setValue(lk.getKey());
Map<String, String> attributes = new HashMap<>();
attributes.put("filename", domainName);
cert.setAttributes(attributes);
parameters.add(cert);
}
return parameters;
}
private List<MessageParameter> buildTopologyParams(String planID) throws JSONException {
Plan plan = planService.getDao().findOne(planID);
if (plan == null) {
throw new PlanNotFoundException();
}
List<MessageParameter> parameters = new ArrayList();
MessageParameter topology = new MessageParameter();
topology.setName("topology");
topology.setValue(Converter.map2YmlString(plan.getKeyValue()));
Map<String, String> attributes = new HashMap<>();
attributes.put("level", String.valueOf(plan.getLevel()));
attributes.put("filename", FilenameUtils.removeExtension(plan.getName()));
topology.setAttributes(attributes);
parameters.add(topology);
Set<String> ids = plan.getLoweLevelPlanIDs();
for (String lowID : ids) {
Plan lowPlan = planService.getDao().findOne(lowID);
topology = new MessageParameter();
topology.setName("topology");
topology.setValue(Converter.map2YmlString(lowPlan.getKeyValue()));
attributes = new HashMap<>();
attributes.put("level", String.valueOf(lowPlan.getLevel()));
attributes.put("filename", FilenameUtils.removeExtension(lowPlan.getName()));
topology.setAttributes(attributes);
parameters.add(topology);
}
return parameters;
}
private MessageParameter buildEC2Conf(CloudCredentials cred) throws JsonProcessingException, JSONException, IOException {
Properties prop = Converter.getEC2Properties(cred);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
prop.store(baos, null);
byte[] bytes = baos.toByteArray();
MessageParameter conf = new MessageParameter();
conf.setName("ec2.conf");
String charset = "UTF-8";
conf.setValue(new String(bytes, charset));
return conf;
}
private List<MessageParameter> buildScriptParams(String userScriptID) {
Script script = userScriptService.getDao().findOne(userScriptID);
if (script == null) {
throw new BadRequestException("User script: " + userScriptID + " was not found");
}
List<MessageParameter> parameters = new ArrayList();
MessageParameter scriptParameter = new MessageParameter();
scriptParameter.setName("guiscript");
scriptParameter.setValue(script.getContents());
scriptParameter.setEncoding("UTF-8");
parameters.add(scriptParameter);
return parameters;
}
private List<MessageParameter> buildKeysParams(String userKeyID) {
LoginKey key = userKeysService.get(userKeyID, LoginKey.Type.PUBLIC);
if (key == null) {
throw new BadRequestException("User key: " + userKeyID + " was not found");
}
List<MessageParameter> parameters = new ArrayList();
MessageParameter keyParameter = new MessageParameter();
keyParameter.setName("sshkey");
keyParameter.setValue(key.getKey());
keyParameter.setEncoding("UTF-8");
parameters.add(keyParameter);
return parameters;
}
private Message generateFakeResponse(String path) throws IOException, TimeoutException, InterruptedException, JSONException {
// String strResponse = "{\"creationDate\":1488368936945,\"parameters\":["
// + "{\"name\":\"f293ff03-4b82-49e2-871a-899aadf821ce\","
// + "\"encoding\":\"UTF-8\",\"value\":"
// + "\"publicKeyPath: /tmp/Input-4007028381500/user.pem\\nuserName: "
// + "zh9314\\nsubnets:\\n- {name: s1, subnet: 192.168.10.0, "
// + "netmask: 255.255.255.0}\\ncomponents:\\n- "
// + "name: faab6756-61b6-4800-bffa-ae9d859a9d6c\\n "
// + "type: Switch.nodes.Compute\\n nodetype: t2.medium\\n "
// + "OStype: Ubuntu 16.04\\n domain: ec2.us-east-1.amazonaws.com\\n "
// + "script: /tmp/Input-4007028381500/guiscipt.sh\\n "
// + "installation: null\\n role: master\\n "
// + "dockers: mogswitch/InputDistributor\\n "
// + "public_address: 54.144.0.91\\n instanceId: i-0e78cbf853328b820\\n "
// + "ethernet_port:\\n - {name: p1, subnet_name: s1, "
// + "address: 192.168.10.10}\\n- name: 1c75eedf-8497-46fe-aeb8-dab6a62154cb\\n "
// + "type: Switch.nodes.Compute\\n nodetype: t2.medium\\n OStype: Ubuntu 16.04\\n domain: ec2.us-east-1.amazonaws.com\\n script: /tmp/Input-4007028381500/guiscipt.sh\\n installation: null\\n role: slave\\n dockers: mogswitch/ProxyTranscoder\\n public_address: 34.207.254.160\\n instanceId: i-0a99ea18fcc77ed7a\\n ethernet_port:\\n - {name: p1, subnet_name: s1, address: 192.168.10.11}\\n\"},{\"name\":\"kubernetes\",\"encoding\":\"UTF-8\",\"value\":\"54.144.0.91 ubuntu /tmp/Input-4007028381500/Virginia.pem master\\n34.207.254.160 ubuntu /tmp/Input-4007028381500/Virginia.pem slave\\n\"}]}";
// String strResponse = "{\"creationDate\":1488805337447,\"parameters\":[{\"name\":\"2e5dafb6-5a1c-4a66-9dca-5841f99ea735\",\"encoding\":\"UTF-8\",\"value\":\"publicKeyPath: /tmp/Input-11594765342486/user.pem\\nuserName: zh9314\\nsubnets:\\n- {name: s1, subnet: 192.168.10.0, netmask: 255.255.255.0}\\ncomponents:\\n- name: 8fcc1788d9ee462c826572c79fdb2a6a\\n type: Switch.nodes.Compute\\n nodeType: t2.medium\\n OStype: Ubuntu 16.04\\n script: /tmp/Input-11594765342486/guiscipt.sh\\n domain: ec2.us-east-1.amazonaws.com\\n installation: null\\n clusterType: swarm\\n role: master\\n dockers: mogswitch/ProxyTranscoder:1.0\\n public_address: 34.207.73.18\\n instanceId: i-0e82b5624a0df99b1\\n ethernet_port:\\n - {name: p1, subnet_name: s1, address: 192.168.10.10}\\n- name: 8fcc1788d9ee462c826572c79fdb2a6a\\n type: Switch.nodes.Compute\\n nodeType: t2.medium\\n OStype: Ubuntu 16.04\\n script: /tmp/Input-11594765342486/guiscipt.sh\\n domain: ec2.us-east-1.amazonaws.com\\n installation: null\\n clusterType: swarm\\n role: slave\\n dockers: mogswitch/ProxyTranscoder:1.0\\n public_address: 34.207.73.18\\n instanceId: i-0e82b5624a0df99b1\\n ethernet_port:\\n - {name: p1, subnet_name: s1, address: 192.168.10.11}\\n\"},{\"name\":\"kubernetes\",\"encoding\":\"UTF-8\",\"value\":\"34.207.73.18 ubuntu /tmp/Input-11594765342486/Virginia.pem master\\n34.207.73.18 ubuntu /tmp/Input-11594765342486/Virginia.pem slave\\n\"}]}";
String strResponse = FileUtils.readFileToString(new File(path));
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
return mapper.readValue(strResponse, Message.class);
}
}
......@@ -39,9 +39,7 @@ import nl.uva.sne.drip.api.exception.NullKeyIDException;
import nl.uva.sne.drip.api.service.CloudCredentialsService;
import nl.uva.sne.drip.api.service.UserService;
import nl.uva.sne.drip.commons.v1.types.LoginKey;
import nl.uva.sne.drip.commons.v1.types.User;
import org.apache.commons.io.FilenameUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
......
......@@ -26,7 +26,6 @@ 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.exception.BadRequestException;
import nl.uva.sne.drip.api.exception.NotFoundException;
import nl.uva.sne.drip.commons.v1.types.Message;
......@@ -34,13 +33,13 @@ import nl.uva.sne.drip.commons.v1.types.MessageParameter;
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.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.rpc.DeployerCaller;
import nl.uva.sne.drip.api.service.CloudCredentialsService;
import nl.uva.sne.drip.api.service.ClusterCredentialService;
import nl.uva.sne.drip.api.service.ProvisionService;
import nl.uva.sne.drip.api.service.UserService;
......@@ -67,7 +66,7 @@ public class DeployController {
private String messageBrokerHost;
@Autowired
private CloudCredentialsDao cloudCredentialsDao;
private CloudCredentialsService cloudCredentialsService;
@Autowired
private ProvisionService provisionService;
......@@ -152,19 +151,19 @@ public class DeployController {
String delete(@PathVariable("id") String id) {
ClusterCredentials cred = clusterCredentialService.getDao().findOne(id);
if (cred != null) {
provisionService.getDao().delete(id);
provisionService.delete(id);
return "Deleted : " + id;
}
throw new NotFoundException();
}
private Message buildDeployerMessage(String provisionID, String clusterType) {
ProvisionInfo pro = provisionService.getDao().findOne(provisionID);
ProvisionInfo pro = provisionService.findOne(provisionID);
if (pro == null) {
throw new NotFoundException();
}
String cloudConfID = pro.getCloudcloudCredentialsID();
CloudCredentials cCred = cloudCredentialsDao.findOne(cloudConfID);
CloudCredentials cCred = cloudCredentialsService.findOne(cloudConfID);
List<LoginKey> loginKeys = cCred.getLoginKeys();
List<DeployParameter> deployParams = pro.getDeployParameters();
List<MessageParameter> parameters = new ArrayList<>();
......
......@@ -25,7 +25,6 @@ import javax.annotation.security.RolesAllowed;
import nl.uva.sne.drip.api.exception.NotFoundException;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
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;
......@@ -117,7 +116,7 @@ public class PlannerController {
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
String delete(@PathVariable("id") String id) {
plannerService.getDao().delete(id);
plannerService.delete(id);
return "Deleted : " + id;
}
......
......@@ -15,53 +15,23 @@
*/
package nl.uva.sne.drip.api.v1.rest;
import com.fasterxml.jackson.core.JsonParser;
import nl.uva.sne.drip.commons.v1.types.ProvisionInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
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.dao.CloudCredentialsDao;
import nl.uva.sne.drip.commons.v1.types.Message;
import nl.uva.sne.drip.commons.v1.types.MessageParameter;
import nl.uva.sne.drip.commons.utils.Converter;
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.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.exception.BadRequestException;
import nl.uva.sne.drip.api.exception.CloudCredentialsNotFoundException;
import nl.uva.sne.drip.api.exception.ExceptionHandler;
import nl.uva.sne.drip.api.exception.NotFoundException;
import nl.uva.sne.drip.api.exception.PlanNotFoundException;
import nl.uva.sne.drip.api.rpc.DRIPCaller;
import nl.uva.sne.drip.api.rpc.ProvisionerCaller;
import nl.uva.sne.drip.api.service.ProvisionService;
import nl.uva.sne.drip.api.service.SimplePlannerService;
import nl.uva.sne.drip.api.service.UserKeyService;
import nl.uva.sne.drip.api.service.UserScriptService;
import nl.uva.sne.drip.api.service.UserService;
import nl.uva.sne.drip.commons.v1.types.CloudCredentials;
import nl.uva.sne.drip.commons.v1.types.DeployParameter;
import nl.uva.sne.drip.commons.v1.types.LoginKey;
import nl.uva.sne.drip.commons.v1.types.Plan;
import nl.uva.sne.drip.commons.v1.types.Script;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.json.JSONException;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
......@@ -69,6 +39,7 @@ import org.springframework.web.bind.annotation.RequestBody;
/**
* This controller is responsible for obtaining resources from cloud providers
* based the plan generated by the planner
*
* @author S. Koulouzis
*/
@RestController
......@@ -76,26 +47,16 @@ import org.springframework.web.bind.annotation.RequestBody;
@Component
public class ProvisionController {
@Value("${message.broker.host}")
private String messageBrokerHost;
@Autowired
private UserScriptService userScriptService;
@Autowired
private UserKeyService userKeysService;
@Autowired
private CloudCredentialsDao cloudCredentialsDao;
@Autowired
private ProvisionService provisionService;
@Autowired
private SimplePlannerService planService;
/**
* Gets the ProvisionInfo
*
* @param id. The id of the ProvisionInfo
* @return the requested ProvisionInfo
*/
......@@ -103,11 +64,12 @@ public class ProvisionController {
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
ProvisionInfo get(@PathVariable("id") String id) {
return provisionService.getDao().findOne(id);
return provisionService.findOne(id);
}
/**
* Deletes the ProvisionInfo
*
* @param id. The ID of the ProvisionInfo to be deleted
* @return the ID of the deleted ProvisionInfo
*/
......@@ -115,24 +77,24 @@ public class ProvisionController {
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
String delete(@PathVariable("id") String id) {
ProvisionInfo provPlan = provisionService.getDao().findOne(id);
ProvisionInfo provPlan = provisionService.findOne(id);
if (provPlan != null) {
provisionService.getDao().delete(id);
provisionService.delete(id);
return "Deleted : " + id;
}
throw new NotFoundException();
}
/**
* Gets the IDs of all the stored ProvisionInfo
*
* @return a list of IDs
*/
@RequestMapping(value = "/ids", method = RequestMethod.GET)
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
List<String> getIds() {
List<ProvisionInfo> all = provisionService.getDao().findAll();
List<ProvisionInfo> all = provisionService.findAll();
List<String> ids = new ArrayList<>(all.size());
for (ProvisionInfo pi : all) {
ids.add(pi.getId());
......@@ -142,232 +104,25 @@ public class ProvisionController {
/**
* Provison the resources specified by a plan.
* @param req. The ProvisionInfo. This is a container the plan ID,
* cloud credent ID, etc.
*
* @param req. The ProvisionInfo. This is a container the plan ID, cloud
* credent ID, etc.
* @return The ID of the provisioned ProvisionInfo
*/
@RequestMapping(value = "/provision", method = RequestMethod.POST)
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
String provision(@RequestBody ProvisionInfo req) {
try (DRIPCaller provisioner = new ProvisionerCaller(messageBrokerHost);) {
Message provisionerInvokationMessage = buildProvisionerMessage(req);
Message response = (provisioner.call(provisionerInvokationMessage));
// Message response = generateFakeResponse(System.getProperty("user.home")
// + File.separator + "workspace" + File.separator + "DRIP"
// + File.separator + "doc" + File.separator + "json_samples" +
// File.separator + "ec2_provisioner_provisoned2.json");
List<MessageParameter> params = response.getParameters();
for (MessageParameter p : params) {
String name = p.getName();
if (name.toLowerCase().contains("exception")) {
RuntimeException ex = ExceptionHandler.generateException(name, p.getValue());
Logger.getLogger(ProvisionController.class.getName()).log(Level.SEVERE, null, ex);
throw ex;
}
if (!name.equals("kubernetes")) {
String value = p.getValue();
Map<String, Object> kvMap = Converter.ymlString2Map(value);
req.setKvMap(kvMap);
req.setPlanID(req.getPlanID());
} else {
String value = p.getValue();
String[] lines = value.split("\n");
List<DeployParameter> deployParameters = new ArrayList<>();
for (String line : lines) {
DeployParameter deployParam = new DeployParameter();
String[] parts = line.split(" ");
String deployIP = parts[0];
String deployUser = parts[1];
try {
req = provisionService.provisionResources(req);
String deployCertPath = parts[2];
String cloudCertificateName = FilenameUtils.removeExtension(FilenameUtils.getBaseName(deployCertPath));
String deployRole = parts[3];
deployParam.setIP(deployIP);
deployParam.setRole(deployRole);
deployParam.setUser(deployUser);
deployParam.setCloudCertificateName(cloudCertificateName);
deployParameters.add(deployParam);
}
req.setDeployParameters(deployParameters);
}
}
provisionService.getDao().save(req);
return req.getId();
} catch (IOException | TimeoutException | JSONException | InterruptedException ex) {
Logger.getLogger(ProvisionController.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
private Message buildProvisionerMessage(ProvisionInfo pReq) throws JSONException, IOException {
Message invokationMessage = new Message();
List<MessageParameter> parameters = new ArrayList();
CloudCredentials cred = cloudCredentialsDao.findOne(pReq.getCloudcloudCredentialsID());
if (cred == null) {
throw new CloudCredentialsNotFoundException();
}
MessageParameter conf = buildCloudConfParam(cred);
parameters.add(conf);
List<MessageParameter> certs = buildCertificatesParam(cred);
parameters.addAll(certs);
List<MessageParameter> topologies = buildTopologyParams(pReq.getPlanID());
parameters.addAll(topologies);
String scriptID = pReq.getscriptID();
if (scriptID != null) {
List<MessageParameter> userScripts = buildScriptParams(scriptID);
parameters.addAll(userScripts);
}
String userKeyID = pReq.getUserKeyID();
if (userKeyID != null) {
List<MessageParameter> userKeys = buildKeysParams(userKeyID);
parameters.addAll(userKeys);
}
invokationMessage.setParameters(parameters);
invokationMessage.setCreationDate((System.currentTimeMillis()));
return invokationMessage;
}
private MessageParameter buildCloudConfParam(CloudCredentials cred) throws JsonProcessingException, JSONException, IOException {
MessageParameter conf = null;
String provider = cred.getCloudProviderName();
if (provider == null) {
throw new BadRequestException("Provider name can't be null. Check the cloud credentials: " + cred.getId());
}
switch (cred.getCloudProviderName().toLowerCase()) {
case "ec2":
conf = buildEC2Conf(cred);
break;
}
return conf;
}
private List<MessageParameter> buildCertificatesParam(CloudCredentials cred) {
List<LoginKey> loginKeys = cred.getLoginKeys();
if (loginKeys == null || loginKeys.isEmpty()) {
throw new BadRequestException("Log in keys can't be empty");
}
List<MessageParameter> parameters = new ArrayList<>();
for (LoginKey lk : loginKeys) {
String domainName = lk.getAttributes().get("domain_name");
if (domainName == null) {
domainName = lk.getAttributes().get("domain_name ");
}
MessageParameter cert = new MessageParameter();
cert.setName("certificate");
cert.setValue(lk.getKey());
Map<String, String> attributes = new HashMap<>();
attributes.put("filename", domainName);
cert.setAttributes(attributes);
parameters.add(cert);
}
return parameters;
}
private List<MessageParameter> buildTopologyParams(String planID) throws JSONException {
Plan plan = planService.getDao().findOne(planID);
if (plan == null) {
throw new PlanNotFoundException();
}
List<MessageParameter> parameters = new ArrayList();
MessageParameter topology = new MessageParameter();
topology.setName("topology");
topology.setValue(Converter.map2YmlString(plan.getKeyValue()));
Map<String, String> attributes = new HashMap<>();
attributes.put("level", String.valueOf(plan.getLevel()));
attributes.put("filename", FilenameUtils.removeExtension(plan.getName()));
topology.setAttributes(attributes);
parameters.add(topology);
Set<String> ids = plan.getLoweLevelPlanIDs();
for (String lowID : ids) {
Plan lowPlan = planService.getDao().findOne(lowID);
topology = new MessageParameter();
topology.setName("topology");
topology.setValue(Converter.map2YmlString(lowPlan.getKeyValue()));
attributes = new HashMap<>();
attributes.put("level", String.valueOf(lowPlan.getLevel()));
attributes.put("filename", FilenameUtils.removeExtension(lowPlan.getName()));
topology.setAttributes(attributes);
parameters.add(topology);
}
return parameters;
}
private MessageParameter buildEC2Conf(CloudCredentials cred) throws JsonProcessingException, JSONException, IOException {
Properties prop = Converter.getEC2Properties(cred);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
prop.store(baos, null);
byte[] bytes = baos.toByteArray();
MessageParameter conf = new MessageParameter();
conf.setName("ec2.conf");
String charset = "UTF-8";
conf.setValue(new String(bytes, charset));
return conf;
}
private List<MessageParameter> buildScriptParams(String userScriptID) {
Script script = userScriptService.getDao().findOne(userScriptID);
if (script == null) {
throw new BadRequestException("User script: " + userScriptID + " was not found");
}
List<MessageParameter> parameters = new ArrayList();
MessageParameter scriptParameter = new MessageParameter();
scriptParameter.setName("guiscript");
scriptParameter.setValue(script.getContents());
scriptParameter.setEncoding("UTF-8");
parameters.add(scriptParameter);
return parameters;
}
private List<MessageParameter> buildKeysParams(String userKeyID) {
LoginKey key = userKeysService.get(userKeyID, LoginKey.Type.PUBLIC);
if (key == null) {
throw new BadRequestException("User key: " + userKeyID + " was not found");
}
List<MessageParameter> parameters = new ArrayList();
MessageParameter keyParameter = new MessageParameter();
keyParameter.setName("sshkey");
keyParameter.setValue(key.getKey());
keyParameter.setEncoding("UTF-8");
parameters.add(keyParameter);
return parameters;
}
private Message generateFakeResponse(String path) throws IOException, TimeoutException, InterruptedException, JSONException {
// String strResponse = "{\"creationDate\":1488368936945,\"parameters\":["
// + "{\"name\":\"f293ff03-4b82-49e2-871a-899aadf821ce\","
// + "\"encoding\":\"UTF-8\",\"value\":"
// + "\"publicKeyPath: /tmp/Input-4007028381500/user.pem\\nuserName: "
// + "zh9314\\nsubnets:\\n- {name: s1, subnet: 192.168.10.0, "
// + "netmask: 255.255.255.0}\\ncomponents:\\n- "
// + "name: faab6756-61b6-4800-bffa-ae9d859a9d6c\\n "
// + "type: Switch.nodes.Compute\\n nodetype: t2.medium\\n "
// + "OStype: Ubuntu 16.04\\n domain: ec2.us-east-1.amazonaws.com\\n "
// + "script: /tmp/Input-4007028381500/guiscipt.sh\\n "
// + "installation: null\\n role: master\\n "
// + "dockers: mogswitch/InputDistributor\\n "
// + "public_address: 54.144.0.91\\n instanceId: i-0e78cbf853328b820\\n "
// + "ethernet_port:\\n - {name: p1, subnet_name: s1, "
// + "address: 192.168.10.10}\\n- name: 1c75eedf-8497-46fe-aeb8-dab6a62154cb\\n "
// + "type: Switch.nodes.Compute\\n nodetype: t2.medium\\n OStype: Ubuntu 16.04\\n domain: ec2.us-east-1.amazonaws.com\\n script: /tmp/Input-4007028381500/guiscipt.sh\\n installation: null\\n role: slave\\n dockers: mogswitch/ProxyTranscoder\\n public_address: 34.207.254.160\\n instanceId: i-0a99ea18fcc77ed7a\\n ethernet_port:\\n - {name: p1, subnet_name: s1, address: 192.168.10.11}\\n\"},{\"name\":\"kubernetes\",\"encoding\":\"UTF-8\",\"value\":\"54.144.0.91 ubuntu /tmp/Input-4007028381500/Virginia.pem master\\n34.207.254.160 ubuntu /tmp/Input-4007028381500/Virginia.pem slave\\n\"}]}";
// String strResponse = "{\"creationDate\":1488805337447,\"parameters\":[{\"name\":\"2e5dafb6-5a1c-4a66-9dca-5841f99ea735\",\"encoding\":\"UTF-8\",\"value\":\"publicKeyPath: /tmp/Input-11594765342486/user.pem\\nuserName: zh9314\\nsubnets:\\n- {name: s1, subnet: 192.168.10.0, netmask: 255.255.255.0}\\ncomponents:\\n- name: 8fcc1788d9ee462c826572c79fdb2a6a\\n type: Switch.nodes.Compute\\n nodeType: t2.medium\\n OStype: Ubuntu 16.04\\n script: /tmp/Input-11594765342486/guiscipt.sh\\n domain: ec2.us-east-1.amazonaws.com\\n installation: null\\n clusterType: swarm\\n role: master\\n dockers: mogswitch/ProxyTranscoder:1.0\\n public_address: 34.207.73.18\\n instanceId: i-0e82b5624a0df99b1\\n ethernet_port:\\n - {name: p1, subnet_name: s1, address: 192.168.10.10}\\n- name: 8fcc1788d9ee462c826572c79fdb2a6a\\n type: Switch.nodes.Compute\\n nodeType: t2.medium\\n OStype: Ubuntu 16.04\\n script: /tmp/Input-11594765342486/guiscipt.sh\\n domain: ec2.us-east-1.amazonaws.com\\n installation: null\\n clusterType: swarm\\n role: slave\\n dockers: mogswitch/ProxyTranscoder:1.0\\n public_address: 34.207.73.18\\n instanceId: i-0e82b5624a0df99b1\\n ethernet_port:\\n - {name: p1, subnet_name: s1, address: 192.168.10.11}\\n\"},{\"name\":\"kubernetes\",\"encoding\":\"UTF-8\",\"value\":\"34.207.73.18 ubuntu /tmp/Input-11594765342486/Virginia.pem master\\n34.207.73.18 ubuntu /tmp/Input-11594765342486/Virginia.pem slave\\n\"}]}";
String strResponse = FileUtils.readFileToString(new File(path));
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
return mapper.readValue(strResponse, Message.class);
}
}
......@@ -62,7 +62,7 @@ public class ToscaController {
throw new BadRequestException("Must uplaod a file");
}
try {
return toscaService.save(file);
return toscaService.saveFile(file);
} catch (IOException | IllegalStateException ex) {
Logger.getLogger(ToscaController.class.getName()).log(Level.SEVERE, null, ex);
}
......
......@@ -77,7 +77,7 @@ public class UserPublicKeysController {
upk.setName(name);
upk.setType(LoginKey.Type.PUBLIC);
service.getDao().save(upk);
service.save(upk);
return upk.getId();
} catch (IOException | IllegalStateException ex) {
......@@ -119,7 +119,7 @@ public class UserPublicKeysController {
key.setName(name);
key.setType(LoginKey.Type.PUBLIC);
service.getDao().save(key);
service.save(key);
return key.getId();
}
......
......@@ -28,10 +28,10 @@ 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.multipart.MultipartFile;
import nl.uva.sne.drip.api.dao.UserScriptDao;
import nl.uva.sne.drip.api.exception.NotFoundException;
import nl.uva.sne.drip.commons.v1.types.Script;
import org.springframework.web.bind.annotation.PathVariable;
import nl.uva.sne.drip.api.dao.ScriptDao;
/**
* This controller is responsible for handling user scripts. These user can be
......@@ -45,7 +45,7 @@ import org.springframework.web.bind.annotation.PathVariable;
public class UserScriptController {
@Autowired
private UserScriptDao dao;
private ScriptDao dao;
// curl -v -X POST -F "file=@script.sh" localhost:8080/drip-api/rest/user_script/upload
/**
......
......@@ -26,9 +26,12 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import nl.uva.sne.drip.commons.v0.types.File;
import nl.uva.sne.drip.commons.v1.types.CloudCredentials;
import nl.uva.sne.drip.commons.v1.types.Message;
import nl.uva.sne.drip.commons.v1.types.MessageParameter;
import nl.uva.sne.drip.commons.v1.types.Plan;
import org.apache.commons.io.FilenameUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -174,4 +177,29 @@ public class Converter {
return mess;
}
public static File plan1toFile(Plan plan1) throws JSONException {
File e = new File();
e.level = String.valueOf(plan1.getLevel());
String p1Name = FilenameUtils.getBaseName(plan1.getName());
if (p1Name == null) {
p1Name = "Planned_tosca_file_" + plan1.getLevel();
plan1.setName(p1Name);
}
e.name = p1Name;
String ymlString = Converter.map2YmlString(plan1.getKeyValue());
e.content = ymlString.replaceAll("\n", "\\\\n");
return e;
}
public static Plan File2Plan1(File p0) {
Plan p1 = new Plan();
p1.setLevel(Integer.valueOf(p0.level));
p1.setName(p0.name);
String yaml = p0.content.replaceAll("\\\\n", "\n");
p1.setKvMap(ymlString2Map(yaml));
return p1;
}
}
......@@ -16,16 +16,18 @@
package nl.uva.sne.drip.commons.v0.types;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author S. Koulouzis
*/
@XmlRootElement
public class Upload {
public String user;
public String pwd;
List<File> file;
public List<File> file;
}
......@@ -24,7 +24,7 @@ import org.springframework.data.mongodb.core.mapping.Document;
* @author S. Koulouzis
*/
@Document
public class KeyValueHolder {
public class KeyValueHolder extends OwnedObject{
@Id
private String id;
......
......@@ -28,7 +28,7 @@ import org.springframework.data.mongodb.core.mapping.Document;
* @author S. Koulouzis
*/
@Document
public class LoginKey {
public class LoginKey extends OwnedObject {
@Id
private String id;
......
......@@ -25,7 +25,7 @@ import org.springframework.data.mongodb.core.mapping.Document;
* @author S. Koulouzis
*/
@Document
public class Script {
public class Script extends OwnedObject{
@Id
private String id;
......
......@@ -15,8 +15,6 @@
*/
package nl.uva.sne.drip.commons.v1.types;
import java.util.Map;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
/**
......
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