Commit 446c8c13 authored by Spiros Koulouzis's avatar Spiros Koulouzis

add credentials to TOSCA

parent 03b20b8c
...@@ -23,9 +23,11 @@ import java.io.FileNotFoundException; ...@@ -23,9 +23,11 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
...@@ -133,64 +135,7 @@ public class ProvisionService { ...@@ -133,64 +135,7 @@ public class ProvisionService {
return callProvisioner1(provisionRequest); return callProvisioner1(provisionRequest);
} }
// private Message buildProvisioner0Message(ProvisionRequest pReq) throws JSONException, IOException { private List<MessageParameter> buildTopologyParams(PlanResponse plan) throws JSONException, FileNotFoundException {
// Message invokationMessage = new Message();
// List<MessageParameter> parameters = new ArrayList();
// CloudCredentials cred = cloudCredentialsService.findOne(pReq.getCloudCredentialsIDs().get(0));
// if (cred == null) {
// throw new CloudCredentialsNotFoundException();
// }
// MessageParameter conf = buildCloudCredentialParam(cred, 0).get(0);
// parameters.add(conf);
//
// List<MessageParameter> certs = buildCertificatesParam(cred);
// parameters.addAll(certs);
//
// List<MessageParameter> topologies = buildTopologyParams(pReq.getPlanID());
// parameters.addAll(topologies);
//
// List<String> userKeyIDs = pReq.getUserKeyPairIDs();
// if (userKeyIDs != null) {
// List<MessageParameter> userKeys = buildUserKeysParams(userKeyIDs.get(0), 0);
// parameters.addAll(userKeys);
// }
//
// invokationMessage.setParameters(parameters);
// invokationMessage.setCreationDate((System.currentTimeMillis()));
// return invokationMessage;
// }
private List<MessageParameter> buildCloudCredentialParam(CloudCredentials cred, int version) throws JsonProcessingException, JSONException, IOException {
List<MessageParameter> cloudCredentialParams = new ArrayList<>();
MessageParameter cloudCred = new MessageParameter();
cloudCred.setName("cloud_credential");
cloudCred.setEncoding("UTF-8");
List<KeyPair> keyPairs = new ArrayList<>();
// List<String> keyPairIds = cred.getkeyPairIDs();
// if (keyPairIds != null) {
// for (String id : cred.getkeyPairIDs()) {
// KeyPair pair = keyDao.findOne(id);
// keyPairs.add(pair);
// }
// cred.setKeyPairs(keyPairs);
// }
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
String jsonInString = mapper.writeValueAsString(cred);
cloudCred.setValue("\"" + jsonInString + "\"");
cloudCredentialParams.add(cloudCred);
return cloudCredentialParams;
}
private List<MessageParameter> buildTopologyParams(String planID) throws JSONException, FileNotFoundException {
PlanResponse plan = simplePlanService.getDao().findOne(planID);
if (plan == null) { if (plan == null) {
throw new PlanNotFoundException(); throw new PlanNotFoundException();
} }
...@@ -291,99 +236,11 @@ public class ProvisionService { ...@@ -291,99 +236,11 @@ public class ProvisionService {
return parameters; return parameters;
} }
private List<MessageParameter> buildCloudKeyParams(ProvisionResponse provisionInfo) {
List<MessageParameter> parameters = new ArrayList();
List<String> ids = provisionInfo.getCloudKeyPairIDs();
if (ids != null) {
for (String id : ids) {
KeyPair pair = keyPairService.findOne(id);
MessageParameter param = new MessageParameter();
param.setName("private_cloud_key");
param.setValue(pair.getPrivateKey().getKey());
HashMap<String, String> attributes = new HashMap<>();
attributes.putAll(pair.getPrivateKey().getAttributes());
attributes.put("name", pair.getPrivateKey().getName());
attributes.put("key_pair_id", pair.getKeyPairId());
param.setAttributes(attributes);
parameters.add(param);
param = new MessageParameter();
param.setName("public_cloud_key");
param.setValue(pair.getPublicKey().getKey());
attributes = new HashMap<>();
attributes.putAll(pair.getPrivateKey().getAttributes());
attributes.put("name", pair.getPublicKey().getName());
attributes.put("key_pair_id", pair.getKeyPairId());
param.setAttributes(attributes);
parameters.add(param);
}
}
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> buildUserKeysParams(String userKeyID, int version) {
KeyPair key = keyPairService.findOne(userKeyID);
if (key == null) {
throw new BadRequestException("User key: " + userKeyID + " was not found");
}
List<MessageParameter> parameters = new ArrayList();
MessageParameter keyParameter = new MessageParameter();
if (version == 0) {
keyParameter.setName("sshkey");
}
if (version == 1) {
keyParameter.setName("user_ssh_key");
}
keyParameter.setValue(key.getPublicKey().getKey());
keyParameter.setEncoding("UTF-8");
parameters.add(keyParameter);
return parameters;
}
@PostFilter("(hasRole('ROLE_ADMIN'))") @PostFilter("(hasRole('ROLE_ADMIN'))")
public void deleteAll() { public void deleteAll() {
provisionDao.deleteAll(); provisionDao.deleteAll();
} }
// private ProvisionResponse callProvisioner0(ProvisionRequest provisionRequest) throws IOException, TimeoutException, JSONException, InterruptedException, Exception {
// try (DRIPCaller provisioner = new ProvisionerCaller0(messageBrokerHost);) {
// Message provisionerInvokationMessage = buildProvisioner0Message(provisionRequest);
// provisionerInvokationMessage.setOwner(((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
// logger.info("Calling provisioner");
// Message response = (provisioner.call(provisionerInvokationMessage));
// logger.info("Got provisioner response");
// return parseCreateResourcesResponse(response.getParameters(), provisionRequest, null, true, true);
// }
// }
private ProvisionResponse callProvisioner1(ProvisionRequest provisionRequest) throws IOException, TimeoutException, JSONException, InterruptedException, Exception { private ProvisionResponse callProvisioner1(ProvisionRequest provisionRequest) throws IOException, TimeoutException, JSONException, InterruptedException, Exception {
try (DRIPCaller provisioner = new ProvisionerCaller1(messageBrokerHost);) { try (DRIPCaller provisioner = new ProvisionerCaller1(messageBrokerHost);) {
Message provisionerInvokationMessage = buildProvisioner1Message(provisionRequest); Message provisionerInvokationMessage = buildProvisioner1Message(provisionRequest);
...@@ -485,100 +342,76 @@ public class ProvisionService { ...@@ -485,100 +342,76 @@ public class ProvisionService {
action.setValue("start_topology"); action.setValue("start_topology");
parameters.add(action); parameters.add(action);
List<MessageParameter> topologies = buildTopologyParams(provisionRequest.getPlanID()); PlanResponse plan = addCloudCredentialsOnTOSCAPlan(provisionRequest);
parameters.addAll(topologies);
List<String> userKeyIDs = provisionRequest.getUserKeyPairIDs();
if (userKeyIDs != null) {
for (String keyID : userKeyIDs) {
List<MessageParameter> userKeys = buildUserKeysParams(keyID, 1);
parameters.addAll(userKeys);
}
}
List<String> deployerKeys = provisionRequest.getDeployerKeyPairIDs();
if (userKeyIDs != null) {
for (String keyID : deployerKeys) {
List<MessageParameter> deplyMessageKey = buildDeployKeysParams(keyID);
parameters.addAll(deplyMessageKey);
}
}
for (String id : provisionRequest.getCloudCredentialsIDs()) {
CloudCredentials cred = cloudCredentialsService.findOne(id);
if (cred == null) {
throw new CloudCredentialsNotFoundException();
}
List<MessageParameter> cloudCredentialParams = buildCloudCredentialParam(cred, 1);
parameters.addAll(cloudCredentialParams);
}
List<MessageParameter> topologies = buildTopologyParams(plan);
parameters.addAll(topologies);
invokationMessage.setParameters(parameters); invokationMessage.setParameters(parameters);
invokationMessage.setCreationDate((System.currentTimeMillis())); invokationMessage.setCreationDate((System.currentTimeMillis()));
return invokationMessage; return invokationMessage;
} }
private Message buildTopoplogyModificationMessage(ProvisionResponse provisionInfo, String actionName, Map<String, String> extraAttributes) throws JSONException, IOException { private Message buildTopoplogyModificationMessage(ProvisionResponse provisionInfo, String actionName, Map<String, String> extraAttributes) throws JSONException, IOException {
Message invokationMessage = new Message(); // Message invokationMessage = new Message();
List<MessageParameter> parameters = new ArrayList(); // List<MessageParameter> parameters = new ArrayList();
//
MessageParameter action = new MessageParameter(); // MessageParameter action = new MessageParameter();
action.setName("action"); // action.setName("action");
action.setValue(actionName); // action.setValue(actionName);
parameters.add(action); // parameters.add(action);
//
List<MessageParameter> topologies = buildProvisionedTopologyParams(provisionInfo); // List<MessageParameter> topologies = buildProvisionedTopologyParams(provisionInfo);
parameters.addAll(topologies); // parameters.addAll(topologies);
//
List<MessageParameter> clusterKeys = buildClusterKeyParams(provisionInfo); // List<MessageParameter> clusterKeys = buildClusterKeyParams(provisionInfo);
parameters.addAll(clusterKeys); // parameters.addAll(clusterKeys);
//
List<MessageParameter> userKeys = buildUserKeyParams(provisionInfo); // List<MessageParameter> userKeys = buildUserKeyParams(provisionInfo);
parameters.addAll(userKeys); // parameters.addAll(userKeys);
//
List<MessageParameter> cloudKeys = buildCloudKeyParams(provisionInfo); // List<MessageParameter> cloudKeys = buildCloudKeyParams(provisionInfo);
parameters.addAll(cloudKeys); // parameters.addAll(cloudKeys);
//
for (String id : provisionInfo.getCloudCredentialsIDs()) { //
CloudCredentials cred = cloudCredentialsService.findOne(id); // for (String id : provisionInfo.getCloudCredentialsIDs()) {
if (cred == null) { // CloudCredentials cred = cloudCredentialsService.findOne(id);
throw new CloudCredentialsNotFoundException(); // if (cred == null) {
} // throw new CloudCredentialsNotFoundException();
List<MessageParameter> cloudCredentialParams = buildCloudCredentialParam(cred, 1); // }
parameters.addAll(cloudCredentialParams); //// List<MessageParameter> cloudCredentialParams = buildCloudCredentialParam(cred, 1);
} //// parameters.addAll(cloudCredentialParams);
if (extraAttributes != null && extraAttributes.containsKey("scale_topology_name")) { // }
MessageParameter scale = new MessageParameter(); // if (extraAttributes != null && extraAttributes.containsKey("scale_topology_name")) {
scale.setName("scale_topology_name"); // MessageParameter scale = new MessageParameter();
scale.setValue(extraAttributes.get("scale_topology_name")); // scale.setName("scale_topology_name");
Map<String, String> attributes = new HashMap<>(); // scale.setValue(extraAttributes.get("scale_topology_name"));
attributes.put("number_of_instances", extraAttributes.get("number_of_instances")); // Map<String, String> attributes = new HashMap<>();
attributes.put("cloud_provider", extraAttributes.get("cloud_provider")); // attributes.put("number_of_instances", extraAttributes.get("number_of_instances"));
attributes.put("domain", extraAttributes.get("domain")); // attributes.put("cloud_provider", extraAttributes.get("cloud_provider"));
scale.setAttributes(attributes); // attributes.put("domain", extraAttributes.get("domain"));
parameters.add(scale); // scale.setAttributes(attributes);
} // parameters.add(scale);
invokationMessage.setParameters(parameters); // }
invokationMessage.setCreationDate(System.currentTimeMillis()); // invokationMessage.setParameters(parameters);
return invokationMessage; // invokationMessage.setCreationDate(System.currentTimeMillis());
} return null;
private List<MessageParameter> buildDeployKeysParams(String keyID) {
KeyPair key = keyPairService.findOne(keyID);
if (key == null) {
throw new BadRequestException("User key: " + keyID + " was not found");
}
List<MessageParameter> parameters = new ArrayList();
MessageParameter keyParameter = new MessageParameter();
keyParameter.setName("deployer_ssh_key");
keyParameter.setValue(key.getPublicKey().getKey());
keyParameter.setEncoding("UTF-8");
parameters.add(keyParameter);
return parameters;
} }
// private List<MessageParameter> buildDeployKeysParams(String keyID) {
// KeyPair key = keyPairService.findOne(keyID);
// if (key == null) {
// throw new BadRequestException("User key: " + keyID + " was not found");
// }
// List<MessageParameter> parameters = new ArrayList();
// MessageParameter keyParameter = new MessageParameter();
//
// keyParameter.setName("deployer_ssh_key");
//
// keyParameter.setValue(key.getPublicKey().getKey());
// keyParameter.setEncoding("UTF-8");
// parameters.add(keyParameter);
// return parameters;
// }
private ProvisionResponse parseCreateResourcesResponse(List<MessageParameter> parameters, private ProvisionResponse parseCreateResourcesResponse(List<MessageParameter> parameters,
ProvisionRequest provisionRequest, ProvisionResponse provisionResponse, boolean saveUserKeys, boolean saveDeployerKeyI) throws Exception { ProvisionRequest provisionRequest, ProvisionResponse provisionResponse, boolean saveUserKeys, boolean saveDeployerKeyI) throws Exception {
if (provisionResponse == null) { if (provisionResponse == null) {
...@@ -683,7 +516,7 @@ public class ProvisionService { ...@@ -683,7 +516,7 @@ public class ProvisionService {
if (provisionRequest != null) { if (provisionRequest != null) {
userKeyIds = provisionRequest.getUserKeyPairIDs(); userKeyIds = provisionRequest.getUserKeyPairIDs();
} else { } else {
userKeyIds = provisionResponse.getUserKeyPairIDs(); // userKeyIds = provisionResponse.getUserKeyPairIDs();
} }
if (saveUserKeys) { if (saveUserKeys) {
...@@ -729,26 +562,26 @@ public class ProvisionService { ...@@ -729,26 +562,26 @@ public class ProvisionService {
} }
} }
ArrayList<String> existingCloudKeyPairIDs = provisionResponse.getCloudKeyPairIDs(); // ArrayList<String> existingCloudKeyPairIDs = provisionResponse.getCloudKeyPairIDs();
if (existingCloudKeyPairIDs != null) { // if (existingCloudKeyPairIDs != null) {
existingCloudKeyPairIDs.addAll(cloudKeyPairIDs); // existingCloudKeyPairIDs.addAll(cloudKeyPairIDs);
} else { // } else {
existingCloudKeyPairIDs = cloudKeyPairIDs; // existingCloudKeyPairIDs = cloudKeyPairIDs;
} // }
provisionResponse.setCloudKeyPairIDs(existingCloudKeyPairIDs); // provisionResponse.setCloudKeyPairIDs(existingCloudKeyPairIDs);
provisionResponse.setDeployParameters(deployParameters); // provisionResponse.setDeployParameters(deployParameters);
provisionResponse.setKvMap(kvMap); provisionResponse.setKvMap(kvMap);
if (provisionRequest != null) { if (provisionRequest != null) {
provisionResponse.setCloudCredentialsIDs(provisionRequest.getCloudCredentialsIDs()); // provisionResponse.setCloudCredentialsIDs(provisionRequest.getCloudCredentialsIDs());
provisionResponse.setPlanID(provisionRequest.getPlanID()); provisionResponse.setPlanID(provisionRequest.getPlanID());
} }
if (userKeyIds != null) { if (userKeyIds != null) {
provisionResponse.setUserKeyPairIDs(userKeyIds); // provisionResponse.setUserKeyPairIDs(userKeyIds);
} }
if (deployerKeyIds != null) { if (deployerKeyIds != null) {
provisionResponse.setDeployerKeyPairIDs(deployerKeyIds); // provisionResponse.setDeployerKeyPairIDs(deployerKeyIds);
} }
provisionResponse = save(provisionResponse); provisionResponse = save(provisionResponse);
...@@ -757,4 +590,38 @@ public class ProvisionService { ...@@ -757,4 +590,38 @@ public class ProvisionService {
private void parseDeleteResourcesResponse(List<MessageParameter> parameters, ProvisionResponse provisionInfo) { private void parseDeleteResourcesResponse(List<MessageParameter> parameters, ProvisionResponse provisionInfo) {
} }
private PlanResponse addCloudCredentialsOnTOSCAPlan(ProvisionRequest provisionRequest) {
List<Map<String, Object>> credentials = new ArrayList<>();
for (String id : provisionRequest.getCloudCredentialsIDs()) {
CloudCredentials cred = cloudCredentialsService.findOne(id);
if (cred == null) {
throw new CloudCredentialsNotFoundException();
}
Map<String, Object> toscaCredential = new HashMap<>();
Map<String, Object> toscaCredentialProperties = new HashMap<>();
toscaCredentialProperties.put("token_type", "secretKey");
toscaCredentialProperties.put("token", cred.getSecretKey());
toscaCredentialProperties.put("accessKeyId", cred.getAccessKeyId());
toscaCredentialProperties.put("cloud_provider_name", cred.getCloudProviderName());
toscaCredentialProperties.put("attributes", cred.getAttributes());
toscaCredential.put("properties", toscaCredentialProperties);
credentials.add(toscaCredential);
}
PlanResponse plan = simplePlanService.getDao().findOne(provisionRequest.getPlanID());
Map<String, Object> toscaPlan = plan.getKeyValue();
Map<String, Object> nodeTemplates = (Map<String, Object>) ((Map<String, Object>) toscaPlan.get("topology_template")).get("node_templates");
Iterator it = nodeTemplates.entrySet().iterator();
while (it.hasNext()) {
Map.Entry node = (Map.Entry) it.next();
Map<String, Object> value = (Map<String, Object>) node.getValue();
String type = (String) value.get("type");
if (type.equals("tosca.nodes.ARTICONF.VM.topology")) {
Map<String, Object> properties = (Map<String, Object>) value.get("properties");
properties.put("credentials", credentials);
}
}
return plan;
}
} }
...@@ -153,7 +153,7 @@ public class ProvisionController { ...@@ -153,7 +153,7 @@ public class ProvisionController {
List<String> getIds() { List<String> getIds() {
List<ProvisionResponse> all = provisionService.findAll(); List<ProvisionResponse> all = provisionService.findAll();
List<String> ids = new ArrayList<>(all.size()); List<String> ids = new ArrayList<>(all.size());
for (ProvisionRequest pi : all) { for (ProvisionResponse pi : all) {
ids.add(pi.getId()); ids.add(pi.getId());
} }
return ids; return ids;
...@@ -181,9 +181,9 @@ public class ProvisionController { ...@@ -181,9 +181,9 @@ public class ProvisionController {
throw new BadRequestException(); throw new BadRequestException();
} }
try { try {
req = provisionService.provisionResources(req, 1); ProvisionResponse resp = provisionService.provisionResources(req, 1);
return req.getId(); return resp.getId();
} catch (Exception ex) { } catch (Exception ex) {
if (ex instanceof nl.uva.sne.drip.api.exception.PlanNotFoundException if (ex instanceof nl.uva.sne.drip.api.exception.PlanNotFoundException
...@@ -210,16 +210,16 @@ public class ProvisionController { ...@@ -210,16 +210,16 @@ public class ProvisionController {
}) })
public @ResponseBody public @ResponseBody
String postProvisionResponse(@RequestBody ProvisionResponse resp) { String postProvisionResponse(@RequestBody ProvisionResponse resp) {
if (resp.getCloudCredentialsIDs() == null || resp.getCloudCredentialsIDs().get(0) == null // if (resp.getCloudCredentialsIDs() == null || resp.getCloudCredentialsIDs().get(0) == null
|| resp.getCloudCredentialsIDs().get(0).length() < 2) { // || resp.getCloudCredentialsIDs().get(0).length() < 2) {
throw new BadRequestException(); // throw new BadRequestException();
} // }
if (resp.getPlanID() == null || resp.getPlanID().length() < 2) { // if (resp.getPlanID() == null || resp.getPlanID().length() < 2) {
throw new BadRequestException(); // throw new BadRequestException();
} // }
if (resp.getDeployParameters() == null || resp.getDeployParameters().get(0) == null) { // if (resp.getDeployParameters() == null || resp.getDeployParameters().get(0) == null) {
throw new BadRequestException(); // throw new BadRequestException();
} // }
resp = provisionService.save(resp); resp = provisionService.save(resp);
return resp.getId(); return resp.getId();
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package nl.uva.sne.drip.drip.commons.data.v1.external; package nl.uva.sne.drip.drip.commons.data.v1.external;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.webcohesion.enunciate.metadata.DocumentationExample;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -25,36 +26,55 @@ import java.util.List; ...@@ -25,36 +26,55 @@ import java.util.List;
* @author S. Koulouzis * @author S. Koulouzis
*/ */
@JsonInclude(JsonInclude.Include.NON_NULL) @JsonInclude(JsonInclude.Include.NON_NULL)
public class ProvisionResponse extends ProvisionRequest { public class ProvisionResponse extends KeyValueHolder {
private List<DeployParameter> deployParameters; private String planID;
private ArrayList<String> cloudKeyPairIDs;
/**
/** * The ID of the plan <code>PlanResponse</code> to provision for.
* The deploy parameters.
* *
* @return the deployParameters * @return the planID
*/ */
public List<DeployParameter> getDeployParameters() { @DocumentationExample("ASedsfd46b4fDFd83ba1q")
return deployParameters; public String getPlanID() {
return planID;
} }
/** /**
* @param deployParameters the deployParameters to set * @param planID the planID to set
*/ */
public void setDeployParameters(List<DeployParameter> deployParameters) { public void setPlanID(String planID) {
this.deployParameters = deployParameters; this.planID = planID;
}
public void setCloudKeyPairIDs(ArrayList<String> cloudKeyPairIDs) {
this.cloudKeyPairIDs = cloudKeyPairIDs;
} }
/** // private List<DeployParameter> deployParameters;
* @return the cloudKeyPairIDs // private ArrayList<String> cloudKeyPairIDs;
*/ //
public ArrayList<String> getCloudKeyPairIDs() { // /**
return cloudKeyPairIDs; // * The deploy parameters.
} // *
// * @return the deployParameters
// */
// public List<DeployParameter> getDeployParameters() {
// return deployParameters;
// }
//
// /**
// * @param deployParameters the deployParameters to set
// */
// public void setDeployParameters(List<DeployParameter> deployParameters) {
// this.deployParameters = deployParameters;
// }
//
// public void setCloudKeyPairIDs(ArrayList<String> cloudKeyPairIDs) {
// this.cloudKeyPairIDs = cloudKeyPairIDs;
// }
//
// /**
// * @return the cloudKeyPairIDs
// */
// public ArrayList<String> getCloudKeyPairIDs() {
// return cloudKeyPairIDs;
// }
} }
...@@ -4,7 +4,6 @@ import java.io.IOException; ...@@ -4,7 +4,6 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
...@@ -25,24 +24,28 @@ import org.json.JSONException; ...@@ -25,24 +24,28 @@ import org.json.JSONException;
public class P2PConverter { public class P2PConverter {
public static SimplePlanContainer transfer( Map<String, Object> toscaPlanMap, public static SimplePlanContainer transfer(Map<String, Object> toscaPlanMap,
String userName, String domainName, String cloudProvider) throws JsonParseException, JsonMappingException, IOException, JSONException { String userName, String domainName, String cloudProvider) throws JsonParseException, JsonMappingException, IOException, JSONException {
if (cloudProvider != null) { if (cloudProvider != null) {
cloudProvider = cloudProvider.toUpperCase(); cloudProvider = cloudProvider.toUpperCase();
} }
Map<String, Object> topologyTemplate = (Map<String, Object>) ((Map<String, Object>) toscaPlanMap.get("topology_template")).get("node_templates"); Map<String, Object> topologyTemplate = (Map<String, Object>) ((Map<String, Object>) toscaPlanMap.get("topology_template")).get("node_templates");
//Get the domain provider and vm list //Get the domain provider and vm list
Map<String, Object> topologyNode = null;
String topologyNodeName = null;
List<String> vmNames = new ArrayList<>(); List<String> vmNames = new ArrayList<>();
for (Map.Entry<String, Object> entry : topologyTemplate.entrySet()) { for (Map.Entry<String, Object> entry : topologyTemplate.entrySet()) {
Map<String, Object> node = (Map<String, Object>) entry.getValue(); Map<String, Object> node = (Map<String, Object>) entry.getValue();
String type = (String) node.get("type"); String type = (String) node.get("type");
if (type.equals("tosca.nodes.ARTICONF.VM.topology")) { if (type.equals("tosca.nodes.ARTICONF.VM.topology")) {
Map<String, Object> properties = (Map<String, Object>) node.get("properties"); topologyNode = node;
topologyNodeName = entry.getKey();
Map<String, Object> properties = (Map<String, Object>) topologyNode.get("properties");
domainName = (String) properties.get("domain"); domainName = (String) properties.get("domain");
cloudProvider = (String) properties.get("provider"); cloudProvider = (String) properties.get("provider");
List<Object> requirements = (List<Object>) node.get("requirements"); List<Object> requirements = (List<Object>) topologyNode.get("requirements");
for (Object requirement : requirements) { for (Object requirement : requirements) {
Map<String, Object> requirementMap = (Map<String, Object>) requirement; Map<String, Object> requirementMap = (Map<String, Object>) requirement;
...@@ -51,13 +54,16 @@ public class P2PConverter { ...@@ -51,13 +54,16 @@ public class P2PConverter {
String nodeName = (String) ((Map<String, Object>) requirementMap.get(requirementEntry.getKey())).get("node"); String nodeName = (String) ((Map<String, Object>) requirementMap.get(requirementEntry.getKey())).get("node");
vmNames.add(nodeName); vmNames.add(nodeName);
} }
break;
} }
} }
List<Object> vmList = new ArrayList<>(); List<Object> vmList = new ArrayList<>();
for (String vmName : vmNames) { for (String vmName : vmNames) {
Map<String, Object> vm = (Map<String, Object>) topologyTemplate.get(vmName); Map<String, Object> vm = (Map<String, Object>) topologyTemplate.get(vmName);
userName = (String) ((Map<String, Object>)vm.get("properties")).get("user_name"); Map<String, Object> properties = (Map<String, Object>) vm.get("properties");
vm.put("name", vmName); userName = (String) properties.get("user_name");
String hostName = (String) properties.get("host_name");
vm.put("name", hostName);
vmList.add(vm); vmList.add(vm);
} }
...@@ -71,23 +77,24 @@ public class P2PConverter { ...@@ -71,23 +77,24 @@ public class P2PConverter {
String provisionerScalingMode = "fixed"; String provisionerScalingMode = "fixed";
SubTopology subTopology = createSubTopology(cloudProvider); SubTopology subTopology = createSubTopology(cloudProvider);
subTopologyInfo.cloudProvider = cloudProvider; subTopologyInfo.cloudProvider = cloudProvider;
subTopologyInfo.topology = UUID.randomUUID().toString(); subTopologyInfo.topology = topologyNodeName.replaceAll("_", "");
subTopologyInfo.domain = domainName; subTopologyInfo.domain = domainName;
subTopologyInfo.status = "fresh"; subTopologyInfo.status = "fresh";
subTopologyInfo.statusInfo = null; subTopologyInfo.statusInfo = null;
subTopologyInfo.tag = provisionerScalingMode; subTopologyInfo.tag = provisionerScalingMode;
Map<String, SubTopologyInfo> subTopologyInfos = new HashMap<>(); Map<String, SubTopologyInfo> subTopologyInfos = new HashMap<>();
int counter = 0;
for (Object element : vmList) { for (Object element : vmList) {
VM vm = createVM(element, cloudProvider, firstVM); VM vm = createVM(element, cloudProvider, firstVM);
firstVM = false; firstVM = false;
counter++;
if (isScalable(element)) { if (isScalable(element)) {
subTopologyInfo = new SubTopologyInfo(); subTopologyInfo = new SubTopologyInfo();
subTopology = createSubTopology(cloudProvider); subTopology = createSubTopology(cloudProvider);
provisionerScalingMode = "scaling"; provisionerScalingMode = "scaling";
subTopologyInfo.cloudProvider = cloudProvider; subTopologyInfo.cloudProvider = cloudProvider;
subTopologyInfo.topology = UUID.randomUUID().toString(); subTopologyInfo.topology = "subTopology"+counter;
subTopologyInfo.domain = domainName; subTopologyInfo.domain = domainName;
subTopologyInfo.status = "fresh"; subTopologyInfo.status = "fresh";
subTopologyInfo.tag = provisionerScalingMode; subTopologyInfo.tag = provisionerScalingMode;
......
...@@ -28,6 +28,7 @@ import java.util.ArrayList; ...@@ -28,6 +28,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Base64; import java.util.Base64;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
...@@ -56,6 +57,7 @@ import provisioning.credential.ExoGENICredential; ...@@ -56,6 +57,7 @@ import provisioning.credential.ExoGENICredential;
* @author S. Koulouzis * @author S. Koulouzis
*/ */
public class MessageParsing { public class MessageParsing {
private static SimplePlanContainer simplePlan; private static SimplePlanContainer simplePlan;
public static List<File> getTopologies(JSONArray parameters, String tempInputDirPath, int level) throws JSONException, IOException { public static List<File> getTopologies(JSONArray parameters, String tempInputDirPath, int level) throws JSONException, IOException {
...@@ -66,8 +68,7 @@ public class MessageParsing { ...@@ -66,8 +68,7 @@ public class MessageParsing {
if (name.equals("topology")) { if (name.equals("topology")) {
String toscaPlan = new String(Base64.getDecoder().decode((String) param.get("value"))); String toscaPlan = new String(Base64.getDecoder().decode((String) param.get("value")));
Map<String, Object> toscaPlanMap = Converter.ymlString2Map(toscaPlan); Map<String, Object> toscaPlanMap = Converter.ymlString2Map(toscaPlan);
if (level == 0) { if (level == 0) {
simplePlan = P2PConverter.transfer(toscaPlanMap, null, null, null); simplePlan = P2PConverter.transfer(toscaPlanMap, null, null, null);
String fileName = "planner_output_all.yml"; String fileName = "planner_output_all.yml";
...@@ -178,79 +179,82 @@ public class MessageParsing { ...@@ -178,79 +179,82 @@ public class MessageParsing {
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
MessageParameter messageParam = mapper.readValue(param.toString(), MessageParameter.class); MessageParameter messageParam = mapper.readValue(param.toString(), MessageParameter.class);
String name = messageParam.getName(); String name = messageParam.getName();
String value = messageParam.getValue();
if (name.equals("cloud_credential")) { if (name.equals("topology")) {
Credential credential = null; Credential credential = null;
value = value.substring(1, value.length() - 1); String toscaPlan = new String(Base64.getDecoder().decode((String) param.get("value")));
CloudCredentials cred = mapper.readValue(value, CloudCredentials.class); Map<String, Object> toscaPlanMap = Converter.ymlString2Map(toscaPlan);
if (cred.getCloudProviderName().toLowerCase().equals("ec2")) { Map<String, Object> nodeTemplates = (Map<String, Object>) ((Map<String, Object>) toscaPlanMap.get("topology_template")).get("node_templates");
EC2Credential ec2 = new EC2Credential(); Iterator it = nodeTemplates.entrySet().iterator();
ec2.accessKey = cred.getAccessKeyId(); List<Map<String, Object>> toscaCredentialsList = new ArrayList<>();
ec2.secretKey = cred.getSecretKey(); while (it.hasNext()) {
credential = ec2; Map.Entry node = (Map.Entry) it.next();
} Map<String, Object> nodeValue = (Map<String, Object>) node.getValue();
if (cred.getCloudProviderName().toLowerCase().equals("egi")) { String type = (String) nodeValue.get("type");
EGICredential egi = new EGICredential(); if (type.equals("tosca.nodes.ARTICONF.VM.topology")) {
Map<String, Object> att = cred.getAttributes(); Map<String, Object> properties = (Map<String, Object>) nodeValue.get("properties");
String trustedCertificatesURL = null; toscaCredentialsList = (List<Map<String, Object>>) properties.get("credentials");
if (att != null && att.containsKey("trustedCertificatesURL")) { break;
trustedCertificatesURL = (String) att.get("trustedCertificatesURL");
}
if (trustedCertificatesURL != null) {
downloadCACertificates(new URL(trustedCertificatesURL), PropertyValues.TRUSTED_CERTIFICATE_FOLDER);
} else {
downloadCACertificates(PropertyValues.CA_BUNDLE_URL, PropertyValues.TRUSTED_CERTIFICATE_FOLDER);
} }
String myProxyEndpoint = null; }
if (att != null && att.containsKey("myProxyEndpoint")) { for (Map<String, Object> toscaCredential : toscaCredentialsList) {
myProxyEndpoint = (String) att.get("myProxyEndpoint");
Map<String, Object> toscaCredentialProperties = (Map<String, Object>) toscaCredential.get("properties");
String cloudProviderName = (String) toscaCredentialProperties.get("cloud_provider_name");
if (cloudProviderName.toLowerCase().equals("ec2")) {
EC2Credential ec2 = new EC2Credential();
ec2.accessKey = (String) toscaCredentialProperties.get("accessKeyId");
ec2.secretKey = (String) toscaCredentialProperties.get("token");
credential = ec2;
} }
if (myProxyEndpoint != null) { if (cloudProviderName.toLowerCase().equals("egi")) {
String[] myVOs = null; EGICredential egi = new EGICredential();
List voNames = null; Map<String, Object> att = (Map<String, Object>) toscaCredentialProperties.get("attributes");
if (att != null && att.containsKey("voms")) { String trustedCertificatesURL = null;
myVOs = ((String) att.get("voms")).split(","); if (att != null && att.containsKey("trustedCertificatesURL")) {
voNames = (List) Arrays.asList(myVOs); trustedCertificatesURL = (String) att.get("trustedCertificatesURL");
}
if (trustedCertificatesURL != null) {
downloadCACertificates(new URL(trustedCertificatesURL), PropertyValues.TRUSTED_CERTIFICATE_FOLDER);
} else {
downloadCACertificates(PropertyValues.CA_BUNDLE_URL, PropertyValues.TRUSTED_CERTIFICATE_FOLDER);
}
String myProxyEndpoint = null;
if (att != null && att.containsKey("myProxyEndpoint")) {
myProxyEndpoint = (String) att.get("myProxyEndpoint");
}
if (myProxyEndpoint != null) {
String[] myVOs = null;
List voNames = null;
if (att != null && att.containsKey("voms")) {
myVOs = ((String) att.get("voms")).split(",");
voNames = (List) Arrays.asList(myVOs);
}
egi.proxyFilePath = AAUtils.generateProxy((String) toscaCredentialProperties.get("accessKeyId"), (String) toscaCredentialProperties.get("token"), SOURCE.MY_PROXY, myProxyEndpoint, voNames);
} else {
egi.proxyFilePath = AAUtils.generateProxy((String) toscaCredentialProperties.get("accessKeyId"), (String) toscaCredentialProperties.get("token"), SOURCE.PROXY_FILE, myProxyEndpoint, null);
} }
egi.proxyFilePath = AAUtils.generateProxy(cred.getAccessKeyId(), cred.getSecretKey(), SOURCE.MY_PROXY, myProxyEndpoint, voNames); egi.trustedCertPath = PropertyValues.TRUSTED_CERTIFICATE_FOLDER;
} else { credential = egi;
egi.proxyFilePath = AAUtils.generateProxy(cred.getAccessKeyId(), cred.getSecretKey(), SOURCE.PROXY_FILE, myProxyEndpoint, null);
} }
egi.trustedCertPath = PropertyValues.TRUSTED_CERTIFICATE_FOLDER; if (cloudProviderName.toLowerCase().equals("exogeni")) {
credential = egi; ExoGENICredential exoGeniCredential = new ExoGENICredential();
} exoGeniCredential.keyAlias = (String) toscaCredentialProperties.get("accessKeyId");
if (cred.getCloudProviderName().toLowerCase().equals("exogeni")) { exoGeniCredential.keyPassword = (String) toscaCredentialProperties.get("token");
ExoGENICredential exoGeniCredential = new ExoGENICredential(); Map<String, Object> att = (Map<String, Object>) toscaCredentialProperties.get("attributes");
exoGeniCredential.keyAlias = cred.getAccessKeyId(); if (att != null && att.containsKey("keystore")) {
exoGeniCredential.keyPassword = cred.getSecretKey(); String javaKeyStoreEncoded = (String) att.get("keystore");
Map<String, Object> att = cred.getAttributes(); byte[] decoded = Base64.getDecoder().decode(javaKeyStoreEncoded);
if (att != null && att.containsKey("keystore")) { File keyStoreFile = new File(tempInputDirPath + File.separator + "user.jks");
String javaKeyStoreEncoded = (String) att.get("keystore"); FileUtils.writeByteArrayToFile(keyStoreFile, decoded);
byte[] decoded = Base64.getDecoder().decode(javaKeyStoreEncoded); exoGeniCredential.userKeyPath = keyStoreFile.getAbsolutePath();
File keyStoreFile = new File(tempInputDirPath + File.separator + "user.jks"); }
FileUtils.writeByteArrayToFile(keyStoreFile, decoded); credential = exoGeniCredential;
exoGeniCredential.userKeyPath = keyStoreFile.getAbsolutePath();
} }
credential = exoGeniCredential;
} }
// for (KeyPair pair : cred.getKeyPairs()) {
// if (pair != null) {
// File dir = new File(tempInputDirPath + File.separator + pair.getId());
// dir.mkdir();
// Key privateKey = pair.getPrivateKey();
// if (privateKey != null) {
// writeValueToFile(privateKey.getKey(), new File(dir.getAbsolutePath() + File.separator + privateKey.getName()));
// }
// Key publicKey = pair.getPublicKey();
// if (publicKey != null) {
// writeValueToFile(publicKey.getKey(), new File(dir.getAbsolutePath() + File.separator + publicKey.getName()));
// }
// }
// }
credentials.add(credential); credentials.add(credential);
} }
} }
......
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