Commit ced0bfc0 authored by Spiros Koulouzis's avatar Spiros Koulouzis

implement caller for new provisioner

parent 201f5ade
......@@ -69,7 +69,7 @@ class</p>
<dl class="dl-horizontal">
<dt>Subtypes</dt>
<dd><a href="json_DeployRequest.html">DeployRequest</a>, <a href="json_KeyPair.html">KeyPair</a>, <a href="json_ProvisionRequest.html">ProvisionRequest</a>, <a href="json_AnsibleOutput.html">AnsibleOutput</a>, <a href="json_PlanResponse.html">PlanResponse</a>, <a href="json_KeyValueHolder.html">KeyValueHolder</a>, <a href="json_SysbenchCPUBenchmark.html">SysbenchCPUBenchmark</a>, <a href="json_PlaybookRepresentation.html">PlaybookRepresentation</a>, <a href="json_ProvisionResponse.html">ProvisionResponse</a>, <a href="json_Script.html">Script</a>, <a href="json_DeployResponse.html">DeployResponse</a>, <a href="json_BenchmarkResult.html">BenchmarkResult</a>, <a href="json_ToscaRepresentation.html">ToscaRepresentation</a>, <a href="json_CloudCredentials.html">CloudCredentials</a></dd>
<dd><a href="json_DeployRequest.html">DeployRequest</a>, <a href="json_ProvisionRequest.html">ProvisionRequest</a>, <a href="json_KeyPair.html">KeyPair</a>, <a href="json_AnsibleOutput.html">AnsibleOutput</a>, <a href="json_PlanResponse.html">PlanResponse</a>, <a href="json_KeyValueHolder.html">KeyValueHolder</a>, <a href="json_SysbenchCPUBenchmark.html">SysbenchCPUBenchmark</a>, <a href="json_PlaybookRepresentation.html">PlaybookRepresentation</a>, <a href="json_ProvisionResponse.html">ProvisionResponse</a>, <a href="json_Script.html">Script</a>, <a href="json_DeployResponse.html">DeployResponse</a>, <a href="json_BenchmarkResult.html">BenchmarkResult</a>, <a href="json_ToscaRepresentation.html">ToscaRepresentation</a>, <a href="json_CloudCredentials.html">CloudCredentials</a></dd>
</dl>
<table class="table datatype-properties">
......
......@@ -22,11 +22,11 @@ import java.util.concurrent.TimeoutException;
*
* @author S. Koulouzis
*/
public class ProvisionerCaller extends DRIPCaller {
public class ProvisionerCaller0 extends DRIPCaller {
private static final String REQUEST_QUEUE_NAME = "provisioner_queue";
private static final String REQUEST_QUEUE_NAME = "provisioner_queue_v0";
public ProvisionerCaller(String messageBrokerHost) throws IOException, TimeoutException {
public ProvisionerCaller0(String messageBrokerHost) throws IOException, TimeoutException {
super(messageBrokerHost, REQUEST_QUEUE_NAME);
}
}
/*
* Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.api.rpc;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
*
* @author S. Koulouzis
*/
public class ProvisionerCaller1 extends DRIPCaller {
private static final String REQUEST_QUEUE_NAME = "provisioner_queue_v1";
public ProvisionerCaller1(String messageBrokerHost) throws IOException, TimeoutException {
super(messageBrokerHost, REQUEST_QUEUE_NAME);
}
}
......@@ -18,7 +18,6 @@ package nl.uva.sne.drip.api.service;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -51,7 +50,6 @@ import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import nl.uva.sne.drip.api.dao.KeyPairDao;
import nl.uva.sne.drip.api.exception.KeyException;
import nl.uva.sne.drip.commons.utils.MessageGenerator;
import nl.uva.sne.drip.data.v1.external.KeyPair;
import nl.uva.sne.drip.data.v1.external.ansible.AnsibleOutput;
import nl.uva.sne.drip.data.v1.external.ansible.AnsibleResult;
......
......@@ -63,7 +63,7 @@ public class PlannerService {
@Value("${message.broker.host}")
private String messageBrokerHost;
public PlanResponse getPlan(String toscaId) throws JSONException, UnsupportedEncodingException, IOException, TimeoutException, InterruptedException {
public PlanResponse getPlan(String toscaId, String clusterType) throws JSONException, UnsupportedEncodingException, IOException, TimeoutException, InterruptedException {
try (PlannerCaller planner = new PlannerCaller(messageBrokerHost)) {
Message plannerInvokationMessage = buildPlannerMessage(toscaId);
......@@ -84,7 +84,7 @@ public class PlannerService {
}
jsonArrayString.append("]");
SimplePlanContainer simplePlan = P2PConverter.convert(jsonArrayString.toString(), "vm_user", "Ubuntu 16.04", "swarm");
SimplePlanContainer simplePlan = P2PConverter.convert(jsonArrayString.toString(), "vm_user", "Ubuntu 16.04", clusterType);
PlanResponse topLevel = new PlanResponse();
topLevel.setTimestamp(System.currentTimeMillis());
topLevel.setLevel(0);
......
......@@ -17,9 +17,12 @@ package nl.uva.sne.drip.api.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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;
......@@ -35,7 +38,7 @@ 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.rpc.ProvisionerCaller0;
import nl.uva.sne.drip.api.v1.rest.ProvisionController;
import nl.uva.sne.drip.commons.utils.Converter;
import nl.uva.sne.drip.data.v1.external.CloudCredentials;
......@@ -56,6 +59,7 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import nl.uva.sne.drip.api.dao.ProvisionResponseDao;
import nl.uva.sne.drip.api.dao.KeyPairDao;
import nl.uva.sne.drip.api.rpc.ProvisionerCaller1;
import nl.uva.sne.drip.data.v1.external.KeyPair;
/**
......@@ -117,64 +121,19 @@ public class ProvisionService {
return provisionDao.findAll();
}
public ProvisionResponse provisionResources(ProvisionRequest provisionRequest) throws IOException, TimeoutException, JSONException, InterruptedException {
try (DRIPCaller provisioner = new ProvisionerCaller(messageBrokerHost);) {
Message provisionerInvokationMessage = buildProvisionerMessage(provisionRequest);
public ProvisionResponse provisionResources(ProvisionRequest provisionRequest, int provisionerVersion) throws IOException, TimeoutException, JSONException, InterruptedException {
Message response = (provisioner.call(provisionerInvokationMessage));
// Message response = MessageGenerator.generateArtificialMessage(System.getProperty("user.home")
// + File.separator + "workspace" + File.separator + "DRIP"
// + File.separator + "docs" + File.separator + "json_samples"
// + File.separator + "ec2_provisioner_provisoned3.json");
List<MessageParameter> params = response.getParameters();
ProvisionResponse provisionResponse = new ProvisionResponse();
provisionResponse.setTimestamp(System.currentTimeMillis());
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;
switch (provisionerVersion) {
case 0:
return callProvisioner0(provisionRequest);
case 1:
return callProvisioner1(provisionRequest);
}
return null;
if (!name.equals("kubernetes")) {
String value = p.getValue();
Map<String, Object> kvMap = Converter.ymlString2Map(value);
provisionResponse.setKvMap(kvMap);
provisionResponse.setPlanID(provisionRequest.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);
}
provisionResponse.setDeployParameters(deployParameters);
}
}
provisionResponse.setCloudCredentialsIDs(provisionRequest.getCloudCredentialsIDs());
provisionResponse.setKeyPairIDs(provisionRequest.getKeyPairIDs());
provisionResponse = save(provisionResponse);
return provisionResponse;
}
}
private Message buildProvisionerMessage(ProvisionRequest pReq) throws JSONException, IOException {
private Message buildProvisioner0Message(ProvisionRequest pReq) throws JSONException, IOException {
Message invokationMessage = new Message();
List<MessageParameter> parameters = new ArrayList();
CloudCredentials cred = cloudCredentialsService.findOne(pReq.getCloudCredentialsIDs().get(0));
......@@ -243,8 +202,22 @@ public class ProvisionService {
return parameters;
}
private List<MessageParameter> buildTopologyParams(String planID) throws JSONException {
PlanResponse plan = planService.getDao().findOne(planID);
private List<MessageParameter> buildTopologyParams(String planID) throws JSONException, FileNotFoundException {
PlanResponse plan = new PlanResponse(); //planService.getDao().findOne(planID);
plan.setLevel(0);
plan.setKvMap(
Converter.ymlStream2Map(
new FileInputStream(
System.getProperty("user.home") + "/workspace/DRIPProvisioningAgent/ES/standard2/zh_all_test.yml")
)
);
plan.setName("zh_all_test.yml");
plan.setTimestamp(System.currentTimeMillis());
Set<String> loweLevelPlansIDs = new HashSet<>();
loweLevelPlansIDs.add("ec2_zh_a.yml");
loweLevelPlansIDs.add("ec2_zh_b.yml");
plan.setLoweLevelPlansIDs(loweLevelPlansIDs);
if (plan == null) {
throw new PlanNotFoundException();
}
......@@ -260,7 +233,16 @@ public class ProvisionService {
Set<String> ids = plan.getLoweLevelPlanIDs();
for (String lowID : ids) {
PlanResponse lowPlan = planService.getDao().findOne(lowID);
PlanResponse lowPlan = new PlanResponse(); //planService.getDao().findOne(lowID);
lowPlan.setLevel(1);
lowPlan.setName(lowID);
lowPlan.setKvMap(Converter.ymlStream2Map(
new FileInputStream(
System.getProperty("user.home") + "/workspace/DRIPProvisioningAgent/ES/standard2/" + lowID)
)
);
lowPlan.setTimestamp(System.currentTimeMillis());
topology = new MessageParameter();
topology.setName("topology");
topology.setValue(Converter.map2YmlString(lowPlan.getKeyValue()));
......@@ -320,4 +302,83 @@ public class ProvisionService {
public void deleteAll() {
provisionDao.deleteAll();
}
private ProvisionResponse callProvisioner0(ProvisionRequest provisionRequest) throws IOException, TimeoutException, JSONException, InterruptedException {
try (DRIPCaller provisioner = new ProvisionerCaller0(messageBrokerHost);) {
Message provisionerInvokationMessage = buildProvisioner0Message(provisionRequest);
Message response = (provisioner.call(provisionerInvokationMessage));
// Message response = MessageGenerator.generateArtificialMessage(System.getProperty("user.home")
// + File.separator + "workspace" + File.separator + "DRIP"
// + File.separator + "docs" + File.separator + "json_samples"
// + File.separator + "ec2_provisioner_provisoned3.json");
List<MessageParameter> params = response.getParameters();
ProvisionResponse provisionResponse = new ProvisionResponse();
provisionResponse.setTimestamp(System.currentTimeMillis());
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);
provisionResponse.setKvMap(kvMap);
provisionResponse.setPlanID(provisionRequest.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);
}
provisionResponse.setDeployParameters(deployParameters);
}
}
provisionResponse.setCloudCredentialsIDs(provisionRequest.getCloudCredentialsIDs());
provisionResponse.setKeyPairIDs(provisionRequest.getKeyPairIDs());
provisionResponse = save(provisionResponse);
return provisionResponse;
}
}
private ProvisionResponse callProvisioner1(ProvisionRequest provisionRequest) throws IOException, TimeoutException, JSONException {
try (DRIPCaller provisioner = new ProvisionerCaller1(messageBrokerHost);) {
Message provisionerInvokationMessage = buildProvisioner1Message(provisionRequest);
}
return null;
}
private Message buildProvisioner1Message(ProvisionRequest provisionRequest) throws JSONException, FileNotFoundException {
Message invokationMessage = new Message();
List<MessageParameter> parameters = new ArrayList();
List<MessageParameter> topologies = buildTopologyParams(provisionRequest.getPlanID());
parameters.addAll(topologies);
invokationMessage.setParameters(parameters);
invokationMessage.setCreationDate((System.currentTimeMillis()));
return invokationMessage;
}
}
......@@ -65,7 +65,7 @@ public class PlannerController0 {
String yaml = plan0.file;
yaml = yaml.replaceAll("\\\\n", "\n");
String id = toscaService.saveYamlString(yaml, null);
nl.uva.sne.drip.data.v1.external.PlanResponse plan1 = plannerService.getPlan(id);
nl.uva.sne.drip.data.v1.external.PlanResponse plan1 = plannerService.getPlan(id,"swarm");
Result r = new Result();
r.info = ("INFO");
......
......@@ -128,7 +128,7 @@ public class ProvisionController0 {
try {
ProvisionRequest req = provisionService.findOne(exc.action);
req = provisionService.provisionResources(req);
req = provisionService.provisionResources(req,0);
Map<String, Object> map = req.getKeyValue();
String yaml = Converter.map2YmlString(map);
yaml = yaml.replaceAll("\n", "\\\\n");
......
......@@ -67,7 +67,7 @@ public class PlannerController {
String plan(@PathVariable("tosca_id") String toscaId) {
try {
PlanResponse plan = plannerService.getPlan(toscaId);
PlanResponse plan = plannerService.getPlan(toscaId, "swarm");
if (plan == null) {
throw new NotFoundException("Could not make plan");
}
......
......@@ -137,7 +137,7 @@ public class ProvisionController {
throw new BadRequestException();
}
try {
req = provisionService.provisionResources(req);
req = provisionService.provisionResources(req, 1);
return req.getId();
......
......@@ -61,7 +61,7 @@ public class Converter {
return (Map<String, Object>) object;
}
public static Map<String, Object> ymlString2Map(InputStream in) {
public static Map<String, Object> ymlStream2Map(InputStream in) {
Yaml yaml = new Yaml();
Map<String, Object> map = (Map<String, Object>) yaml.load(in);
return map;
......
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