Commit a428e436 authored by Spiros Koulouzis's avatar Spiros Koulouzis

Parse to Message POJO

parent c3886b3f
/*
* 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.dao;
import nl.uva.sne.drip.commons.types.ClusterCredentials;
import org.springframework.data.mongodb.repository.MongoRepository;
/**
*
* @author S. Koulouzis
*/
public interface ClusterCredentialsDao extends MongoRepository<ClusterCredentials, String> {
}
...@@ -33,7 +33,7 @@ import java.util.logging.Logger; ...@@ -33,7 +33,7 @@ import java.util.logging.Logger;
import javax.annotation.security.RolesAllowed; import javax.annotation.security.RolesAllowed;
import nl.uva.sne.drip.api.dao.CloudCredentialsDao; import nl.uva.sne.drip.api.dao.CloudCredentialsDao;
import nl.uva.sne.drip.commons.types.Message; import nl.uva.sne.drip.commons.types.Message;
import nl.uva.sne.drip.commons.types.Parameter; import nl.uva.sne.drip.commons.types.MessageParameter;
import nl.uva.sne.drip.commons.utils.Converter; import nl.uva.sne.drip.commons.utils.Converter;
import org.json.JSONException; import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -104,9 +104,9 @@ public class ProvisionController { ...@@ -104,9 +104,9 @@ public class ProvisionController {
Message response = provisioner.call(provisionerInvokationMessage); Message response = provisioner.call(provisionerInvokationMessage);
// Message response = generateFakeResponse(); // Message response = generateFakeResponse();
List<Parameter> params = response.getParameters(); List<MessageParameter> params = response.getParameters();
for (Parameter p : params) { for (MessageParameter p : params) {
String name = p.getName(); String name = p.getName();
if (!name.equals("kubernetes")) { if (!name.equals("kubernetes")) {
String value = p.getValue(); String value = p.getValue();
...@@ -147,29 +147,29 @@ public class ProvisionController { ...@@ -147,29 +147,29 @@ public class ProvisionController {
private Message buildProvisionerMessage(ProvisionInfo pReq) throws JSONException, IOException { private Message buildProvisionerMessage(ProvisionInfo pReq) throws JSONException, IOException {
Message invokationMessage = new Message(); Message invokationMessage = new Message();
List<Parameter> parameters = new ArrayList(); List<MessageParameter> parameters = new ArrayList();
CloudCredentials cred = cloudCredentialsDao.findOne(pReq.getCloudConfID()); CloudCredentials cred = cloudCredentialsDao.findOne(pReq.getCloudConfID());
if (cred == null) { if (cred == null) {
throw new NotFoundException("Cloud credentials :" + pReq.getCloudConfID() + " not found"); throw new NotFoundException("Cloud credentials :" + pReq.getCloudConfID() + " not found");
} }
Parameter conf = buildCloudConfParam(cred); MessageParameter conf = buildCloudConfParam(cred);
parameters.add(conf); parameters.add(conf);
List<Parameter> certs = buildCertificatesParam(cred); List<MessageParameter> certs = buildCertificatesParam(cred);
parameters.addAll(certs); parameters.addAll(certs);
List<Parameter> topologies = buildTopologyParams(pReq.getPlanID()); List<MessageParameter> topologies = buildTopologyParams(pReq.getPlanID());
parameters.addAll(topologies); parameters.addAll(topologies);
String scriptID = pReq.getUserScriptID(); String scriptID = pReq.getUserScriptID();
if (scriptID != null) { if (scriptID != null) {
List<Parameter> userScripts = buildScriptParams(scriptID); List<MessageParameter> userScripts = buildScriptParams(scriptID);
parameters.addAll(userScripts); parameters.addAll(userScripts);
} }
String userKeyID = pReq.getUserKeyID(); String userKeyID = pReq.getUserKeyID();
if (userKeyID != null) { if (userKeyID != null) {
List<Parameter> userKeys = buildKeysParams(userKeyID); List<MessageParameter> userKeys = buildKeysParams(userKeyID);
parameters.addAll(userKeys); parameters.addAll(userKeys);
} }
...@@ -178,8 +178,8 @@ public class ProvisionController { ...@@ -178,8 +178,8 @@ public class ProvisionController {
return invokationMessage; return invokationMessage;
} }
private Parameter buildCloudConfParam(CloudCredentials cred) throws JsonProcessingException, JSONException, IOException { private MessageParameter buildCloudConfParam(CloudCredentials cred) throws JsonProcessingException, JSONException, IOException {
Parameter conf = null; MessageParameter conf = null;
String provider = cred.getCloudProviderName(); String provider = cred.getCloudProviderName();
if (provider == null) { if (provider == null) {
throw new BadRequestException("Provider name can't be null. Check the cloud credentials: " + cred.getId()); throw new BadRequestException("Provider name can't be null. Check the cloud credentials: " + cred.getId());
...@@ -192,18 +192,18 @@ public class ProvisionController { ...@@ -192,18 +192,18 @@ public class ProvisionController {
return conf; return conf;
} }
private List<Parameter> buildCertificatesParam(CloudCredentials cred) { private List<MessageParameter> buildCertificatesParam(CloudCredentials cred) {
List<LoginKey> loginKeys = cred.getLoginKeys(); List<LoginKey> loginKeys = cred.getLoginKeys();
if (loginKeys == null || loginKeys.isEmpty()) { if (loginKeys == null || loginKeys.isEmpty()) {
throw new BadRequestException("Log in keys can't be empty"); throw new BadRequestException("Log in keys can't be empty");
} }
List<Parameter> parameters = new ArrayList<>(); List<MessageParameter> parameters = new ArrayList<>();
for (LoginKey lk : loginKeys) { for (LoginKey lk : loginKeys) {
String domainName = lk.getAttributes().get("domain_name"); String domainName = lk.getAttributes().get("domain_name");
if (domainName == null) { if (domainName == null) {
domainName = lk.getAttributes().get("domain_name "); domainName = lk.getAttributes().get("domain_name ");
} }
Parameter cert = new Parameter(); MessageParameter cert = new MessageParameter();
cert.setName("certificate"); cert.setName("certificate");
cert.setValue(lk.getKey()); cert.setValue(lk.getKey());
Map<String, String> attributes = new HashMap<>(); Map<String, String> attributes = new HashMap<>();
...@@ -214,13 +214,13 @@ public class ProvisionController { ...@@ -214,13 +214,13 @@ public class ProvisionController {
return parameters; return parameters;
} }
private List<Parameter> buildTopologyParams(String planID) throws JSONException { private List<MessageParameter> buildTopologyParams(String planID) throws JSONException {
Plan plan = planService.getDao().findOne(planID); Plan plan = planService.getDao().findOne(planID);
if (plan == null) { if (plan == null) {
throw new NotFoundException(); throw new NotFoundException();
} }
List<Parameter> parameters = new ArrayList(); List<MessageParameter> parameters = new ArrayList();
Parameter topology = new Parameter(); MessageParameter topology = new MessageParameter();
topology.setName("topology"); topology.setName("topology");
topology.setValue(Converter.map2YmlString(plan.getKvMap())); topology.setValue(Converter.map2YmlString(plan.getKvMap()));
Map<String, String> attributes = new HashMap<>(); Map<String, String> attributes = new HashMap<>();
...@@ -232,7 +232,7 @@ public class ProvisionController { ...@@ -232,7 +232,7 @@ public class ProvisionController {
Set<String> ids = plan.getLoweLevelPlanIDs(); Set<String> ids = plan.getLoweLevelPlanIDs();
for (String lowID : ids) { for (String lowID : ids) {
Plan lowPlan = planService.getDao().findOne(lowID); Plan lowPlan = planService.getDao().findOne(lowID);
topology = new Parameter(); topology = new MessageParameter();
topology.setName("topology"); topology.setName("topology");
topology.setValue(Converter.map2YmlString(lowPlan.getKvMap())); topology.setValue(Converter.map2YmlString(lowPlan.getKvMap()));
attributes = new HashMap<>(); attributes = new HashMap<>();
...@@ -244,13 +244,13 @@ public class ProvisionController { ...@@ -244,13 +244,13 @@ public class ProvisionController {
return parameters; return parameters;
} }
private Parameter buildEC2Conf(CloudCredentials cred) throws JsonProcessingException, JSONException, IOException { private MessageParameter buildEC2Conf(CloudCredentials cred) throws JsonProcessingException, JSONException, IOException {
Properties prop = Converter.getEC2Properties(cred); Properties prop = Converter.getEC2Properties(cred);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
prop.store(baos, null); prop.store(baos, null);
byte[] bytes = baos.toByteArray(); byte[] bytes = baos.toByteArray();
Parameter conf = new Parameter(); MessageParameter conf = new MessageParameter();
conf.setName("ec2.conf"); conf.setName("ec2.conf");
String charset = "UTF-8"; String charset = "UTF-8";
conf.setValue(new String(bytes, charset)); conf.setValue(new String(bytes, charset));
...@@ -258,13 +258,13 @@ public class ProvisionController { ...@@ -258,13 +258,13 @@ public class ProvisionController {
} }
private List<Parameter> buildScriptParams(String userScriptID) { private List<MessageParameter> buildScriptParams(String userScriptID) {
UserScript script = userScriptService.getDao().findOne(userScriptID); UserScript script = userScriptService.getDao().findOne(userScriptID);
if (script == null) { if (script == null) {
throw new BadRequestException("User script: " + userScriptID + " was not found"); throw new BadRequestException("User script: " + userScriptID + " was not found");
} }
List<Parameter> parameters = new ArrayList(); List<MessageParameter> parameters = new ArrayList();
Parameter scriptParameter = new Parameter(); MessageParameter scriptParameter = new MessageParameter();
scriptParameter.setName("guiscript"); scriptParameter.setName("guiscript");
scriptParameter.setValue(script.getContents()); scriptParameter.setValue(script.getContents());
scriptParameter.setEncoding("UTF-8"); scriptParameter.setEncoding("UTF-8");
...@@ -272,13 +272,13 @@ public class ProvisionController { ...@@ -272,13 +272,13 @@ public class ProvisionController {
return parameters; return parameters;
} }
private List<Parameter> buildKeysParams(String userKeyID) { private List<MessageParameter> buildKeysParams(String userKeyID) {
LoginKey key = userKeysService.get(userKeyID, LoginKey.Type.PUBLIC); LoginKey key = userKeysService.get(userKeyID, LoginKey.Type.PUBLIC);
if (key == null) { if (key == null) {
throw new BadRequestException("User key: " + userKeyID + " was not found"); throw new BadRequestException("User key: " + userKeyID + " was not found");
} }
List<Parameter> parameters = new ArrayList(); List<MessageParameter> parameters = new ArrayList();
Parameter keyParameter = new Parameter(); MessageParameter keyParameter = new MessageParameter();
keyParameter.setName("sshkey"); keyParameter.setName("sshkey");
keyParameter.setValue(key.getKey()); keyParameter.setValue(key.getKey());
keyParameter.setEncoding("UTF-8"); keyParameter.setEncoding("UTF-8");
......
...@@ -16,7 +16,7 @@ import java.util.concurrent.ArrayBlockingQueue; ...@@ -16,7 +16,7 @@ import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import nl.uva.sne.drip.commons.types.Message; import nl.uva.sne.drip.commons.types.Message;
import nl.uva.sne.drip.commons.types.Parameter; import nl.uva.sne.drip.commons.types.MessageParameter;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -118,7 +118,8 @@ public abstract class DRIPCaller implements AutoCloseable { ...@@ -118,7 +118,8 @@ public abstract class DRIPCaller implements AutoCloseable {
} }
}); });
String strResponse = response.take(); String strResponse = response.take();
strResponse = strResponse.replaceAll("'null'", "null").replaceAll("\'", "\"").replaceAll(" ", "");
System.err.println(strResponse);
// return unMarshallWithSimpleJson(strResponse); // return unMarshallWithSimpleJson(strResponse);
return mapper.readValue(strResponse, Message.class); return mapper.readValue(strResponse, Message.class);
} }
...@@ -130,12 +131,12 @@ public abstract class DRIPCaller implements AutoCloseable { ...@@ -130,12 +131,12 @@ public abstract class DRIPCaller implements AutoCloseable {
Message responseMessage = new Message(); Message responseMessage = new Message();
responseMessage.setCreationDate((Long) jsonObj.get("creationDate")); responseMessage.setCreationDate((Long) jsonObj.get("creationDate"));
JSONArray jsonParams = (JSONArray) jsonObj.get("parameters"); JSONArray jsonParams = (JSONArray) jsonObj.get("parameters");
List<Parameter> parameters = new ArrayList<>(); List<MessageParameter> parameters = new ArrayList<>();
for (int i = 0; i < jsonParams.length(); i++) { for (int i = 0; i < jsonParams.length(); i++) {
JSONObject jsonParam = (JSONObject) jsonParams.get(i); JSONObject jsonParam = (JSONObject) jsonParams.get(i);
Parameter parameter = new Parameter(); MessageParameter parameter = new MessageParameter();
parameter.setName(jsonParam.getString("name")); parameter.setName(jsonParam.getString("name"));
parameter.setValue(jsonParam.getString("value")); parameter.setValue(jsonParam.getString("value"));
parameters.add(parameter); parameters.add(parameter);
......
/*
* Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.api.service;
import nl.uva.sne.drip.api.dao.ClusterCredentialsDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
*
* @author S. Koulouzis
*/
@Service
public class ClusterCredentialService {
@Autowired
private ClusterCredentialsDao dao;
/**
* @return the dao
*/
public ClusterCredentialsDao getDao() {
return dao;
}
}
...@@ -24,7 +24,7 @@ import java.util.concurrent.TimeoutException; ...@@ -24,7 +24,7 @@ import java.util.concurrent.TimeoutException;
import nl.uva.sne.drip.api.exception.BadRequestException; import nl.uva.sne.drip.api.exception.BadRequestException;
import nl.uva.sne.drip.api.rpc.PlannerCaller; import nl.uva.sne.drip.api.rpc.PlannerCaller;
import nl.uva.sne.drip.commons.types.Message; import nl.uva.sne.drip.commons.types.Message;
import nl.uva.sne.drip.commons.types.Parameter; import nl.uva.sne.drip.commons.types.MessageParameter;
import nl.uva.sne.drip.commons.types.ToscaRepresentation; import nl.uva.sne.drip.commons.types.ToscaRepresentation;
import nl.uva.sne.drip.commons.utils.Converter; import nl.uva.sne.drip.commons.utils.Converter;
import org.json.JSONException; import org.json.JSONException;
...@@ -51,8 +51,8 @@ public class PlannerService { ...@@ -51,8 +51,8 @@ public class PlannerService {
Message plannerInvokationMessage = buildPlannerMessage(toscaId); Message plannerInvokationMessage = buildPlannerMessage(toscaId);
Message plannerReturnedMessage = planner.call(plannerInvokationMessage); Message plannerReturnedMessage = planner.call(plannerInvokationMessage);
List<Parameter> parameters = plannerReturnedMessage.getParameters(); List<MessageParameter> parameters = plannerReturnedMessage.getParameters();
for (Parameter param : parameters) { for (MessageParameter param : parameters) {
} }
ToscaRepresentation tr = new ToscaRepresentation(); ToscaRepresentation tr = new ToscaRepresentation();
...@@ -75,7 +75,7 @@ public class PlannerService { ...@@ -75,7 +75,7 @@ public class PlannerService {
Message invokationMessage = new Message(); Message invokationMessage = new Message();
List parameters = new ArrayList(); List parameters = new ArrayList();
Parameter jsonArgument = new Parameter(); MessageParameter jsonArgument = new MessageParameter();
String charset = "UTF-8"; String charset = "UTF-8";
jsonArgument.setValue(new String(bytes, charset)); jsonArgument.setValue(new String(bytes, charset));
jsonArgument.setEncoding(charset); jsonArgument.setEncoding(charset);
......
...@@ -30,7 +30,7 @@ import nl.uva.sne.drip.api.dao.PlanDao; ...@@ -30,7 +30,7 @@ import nl.uva.sne.drip.api.dao.PlanDao;
import nl.uva.sne.drip.api.exception.NotFoundException; import nl.uva.sne.drip.api.exception.NotFoundException;
import nl.uva.sne.drip.api.rpc.PlannerCaller; import nl.uva.sne.drip.api.rpc.PlannerCaller;
import nl.uva.sne.drip.commons.types.Message; import nl.uva.sne.drip.commons.types.Message;
import nl.uva.sne.drip.commons.types.Parameter; import nl.uva.sne.drip.commons.types.MessageParameter;
import nl.uva.sne.drip.commons.types.Plan; import nl.uva.sne.drip.commons.types.Plan;
import nl.uva.sne.drip.commons.types.ToscaRepresentation; import nl.uva.sne.drip.commons.types.ToscaRepresentation;
import nl.uva.sne.drip.commons.utils.Converter; import nl.uva.sne.drip.commons.utils.Converter;
...@@ -62,14 +62,14 @@ public class SimplePlannerService { ...@@ -62,14 +62,14 @@ public class SimplePlannerService {
Plan topLevel; Plan topLevel;
try (PlannerCaller planner = new PlannerCaller(messageBrokerHost)) { try (PlannerCaller planner = new PlannerCaller(messageBrokerHost)) {
Message plannerReturnedMessage = planner.call(plannerInvokationMessage); Message plannerReturnedMessage = planner.call(plannerInvokationMessage);
List<Parameter> planFiles = plannerReturnedMessage.getParameters(); List<MessageParameter> planFiles = plannerReturnedMessage.getParameters();
topLevel = new Plan(); topLevel = new Plan();
Set<String> ids = topLevel.getLoweLevelPlanIDs(); Set<String> ids = topLevel.getLoweLevelPlanIDs();
if (ids == null) { if (ids == null) {
ids = new HashSet<>(); ids = new HashSet<>();
} }
Plan lowerLevelPlan = null; Plan lowerLevelPlan = null;
for (Parameter p : planFiles) { for (MessageParameter p : planFiles) {
//Should have levels in attributes //Should have levels in attributes
Map<String, String> attributess = p.getAttributes(); Map<String, String> attributess = p.getAttributes();
if (attributess != null) { if (attributess != null) {
...@@ -108,7 +108,7 @@ public class SimplePlannerService { ...@@ -108,7 +108,7 @@ public class SimplePlannerService {
Message invokationMessage = new Message(); Message invokationMessage = new Message();
List parameters = new ArrayList(); List parameters = new ArrayList();
Parameter fileArgument = new Parameter(); MessageParameter fileArgument = new MessageParameter();
String charset = "UTF-8"; String charset = "UTF-8";
fileArgument.setValue(new String(bytes, charset)); fileArgument.setValue(new String(bytes, charset));
...@@ -116,7 +116,7 @@ public class SimplePlannerService { ...@@ -116,7 +116,7 @@ public class SimplePlannerService {
fileArgument.setName("input"); fileArgument.setName("input");
parameters.add(fileArgument); parameters.add(fileArgument);
fileArgument = new Parameter(); fileArgument = new MessageParameter();
bytes = Files.readAllBytes(Paths.get(System.getProperty("user.home") + File.separator + "Downloads/DRIP/example_a.yml")); bytes = Files.readAllBytes(Paths.get(System.getProperty("user.home") + File.separator + "Downloads/DRIP/example_a.yml"));
fileArgument.setValue(new String(bytes, charset)); fileArgument.setValue(new String(bytes, charset));
fileArgument.setEncoding(charset); fileArgument.setEncoding(charset);
......
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