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;
import javax.annotation.security.RolesAllowed;
import nl.uva.sne.drip.api.dao.CloudCredentialsDao;
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 org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -104,9 +104,9 @@ public class ProvisionController {
Message response = provisioner.call(provisionerInvokationMessage);
// 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();
if (!name.equals("kubernetes")) {
String value = p.getValue();
......@@ -147,29 +147,29 @@ public class ProvisionController {
private Message buildProvisionerMessage(ProvisionInfo pReq) throws JSONException, IOException {
Message invokationMessage = new Message();
List<Parameter> parameters = new ArrayList();
List<MessageParameter> parameters = new ArrayList();
CloudCredentials cred = cloudCredentialsDao.findOne(pReq.getCloudConfID());
if (cred == null) {
throw new NotFoundException("Cloud credentials :" + pReq.getCloudConfID() + " not found");
}
Parameter conf = buildCloudConfParam(cred);
MessageParameter conf = buildCloudConfParam(cred);
parameters.add(conf);
List<Parameter> certs = buildCertificatesParam(cred);
List<MessageParameter> certs = buildCertificatesParam(cred);
parameters.addAll(certs);
List<Parameter> topologies = buildTopologyParams(pReq.getPlanID());
List<MessageParameter> topologies = buildTopologyParams(pReq.getPlanID());
parameters.addAll(topologies);
String scriptID = pReq.getUserScriptID();
if (scriptID != null) {
List<Parameter> userScripts = buildScriptParams(scriptID);
List<MessageParameter> userScripts = buildScriptParams(scriptID);
parameters.addAll(userScripts);
}
String userKeyID = pReq.getUserKeyID();
if (userKeyID != null) {
List<Parameter> userKeys = buildKeysParams(userKeyID);
List<MessageParameter> userKeys = buildKeysParams(userKeyID);
parameters.addAll(userKeys);
}
......@@ -178,8 +178,8 @@ public class ProvisionController {
return invokationMessage;
}
private Parameter buildCloudConfParam(CloudCredentials cred) throws JsonProcessingException, JSONException, IOException {
Parameter conf = null;
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());
......@@ -192,18 +192,18 @@ public class ProvisionController {
return conf;
}
private List<Parameter> buildCertificatesParam(CloudCredentials cred) {
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<Parameter> parameters = new ArrayList<>();
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 ");
}
Parameter cert = new Parameter();
MessageParameter cert = new MessageParameter();
cert.setName("certificate");
cert.setValue(lk.getKey());
Map<String, String> attributes = new HashMap<>();
......@@ -214,13 +214,13 @@ public class ProvisionController {
return parameters;
}
private List<Parameter> buildTopologyParams(String planID) throws JSONException {
private List<MessageParameter> buildTopologyParams(String planID) throws JSONException {
Plan plan = planService.getDao().findOne(planID);
if (plan == null) {
throw new NotFoundException();
}
List<Parameter> parameters = new ArrayList();
Parameter topology = new Parameter();
List<MessageParameter> parameters = new ArrayList();
MessageParameter topology = new MessageParameter();
topology.setName("topology");
topology.setValue(Converter.map2YmlString(plan.getKvMap()));
Map<String, String> attributes = new HashMap<>();
......@@ -232,7 +232,7 @@ public class ProvisionController {
Set<String> ids = plan.getLoweLevelPlanIDs();
for (String lowID : ids) {
Plan lowPlan = planService.getDao().findOne(lowID);
topology = new Parameter();
topology = new MessageParameter();
topology.setName("topology");
topology.setValue(Converter.map2YmlString(lowPlan.getKvMap()));
attributes = new HashMap<>();
......@@ -244,13 +244,13 @@ public class ProvisionController {
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);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
prop.store(baos, null);
byte[] bytes = baos.toByteArray();
Parameter conf = new Parameter();
MessageParameter conf = new MessageParameter();
conf.setName("ec2.conf");
String charset = "UTF-8";
conf.setValue(new String(bytes, charset));
......@@ -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);
if (script == null) {
throw new BadRequestException("User script: " + userScriptID + " was not found");
}
List<Parameter> parameters = new ArrayList();
Parameter scriptParameter = new Parameter();
List<MessageParameter> parameters = new ArrayList();
MessageParameter scriptParameter = new MessageParameter();
scriptParameter.setName("guiscript");
scriptParameter.setValue(script.getContents());
scriptParameter.setEncoding("UTF-8");
......@@ -272,13 +272,13 @@ public class ProvisionController {
return parameters;
}
private List<Parameter> buildKeysParams(String userKeyID) {
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<Parameter> parameters = new ArrayList();
Parameter keyParameter = new Parameter();
List<MessageParameter> parameters = new ArrayList();
MessageParameter keyParameter = new MessageParameter();
keyParameter.setName("sshkey");
keyParameter.setValue(key.getKey());
keyParameter.setEncoding("UTF-8");
......
......@@ -16,7 +16,7 @@ import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeoutException;
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.JSONException;
import org.json.JSONObject;
......@@ -118,7 +118,8 @@ public abstract class DRIPCaller implements AutoCloseable {
}
});
String strResponse = response.take();
strResponse = strResponse.replaceAll("'null'", "null").replaceAll("\'", "\"").replaceAll(" ", "");
System.err.println(strResponse);
// return unMarshallWithSimpleJson(strResponse);
return mapper.readValue(strResponse, Message.class);
}
......@@ -130,12 +131,12 @@ public abstract class DRIPCaller implements AutoCloseable {
Message responseMessage = new Message();
responseMessage.setCreationDate((Long) jsonObj.get("creationDate"));
JSONArray jsonParams = (JSONArray) jsonObj.get("parameters");
List<Parameter> parameters = new ArrayList<>();
List<MessageParameter> parameters = new ArrayList<>();
for (int i = 0; i < jsonParams.length(); i++) {
JSONObject jsonParam = (JSONObject) jsonParams.get(i);
Parameter parameter = new Parameter();
MessageParameter parameter = new MessageParameter();
parameter.setName(jsonParam.getString("name"));
parameter.setValue(jsonParam.getString("value"));
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;
import nl.uva.sne.drip.api.exception.BadRequestException;
import nl.uva.sne.drip.api.rpc.PlannerCaller;
import nl.uva.sne.drip.commons.types.Message;
import nl.uva.sne.drip.commons.types.Parameter;
import nl.uva.sne.drip.commons.types.MessageParameter;
import nl.uva.sne.drip.commons.types.ToscaRepresentation;
import nl.uva.sne.drip.commons.utils.Converter;
import org.json.JSONException;
......@@ -51,8 +51,8 @@ public class PlannerService {
Message plannerInvokationMessage = buildPlannerMessage(toscaId);
Message plannerReturnedMessage = planner.call(plannerInvokationMessage);
List<Parameter> parameters = plannerReturnedMessage.getParameters();
for (Parameter param : parameters) {
List<MessageParameter> parameters = plannerReturnedMessage.getParameters();
for (MessageParameter param : parameters) {
}
ToscaRepresentation tr = new ToscaRepresentation();
......@@ -75,7 +75,7 @@ public class PlannerService {
Message invokationMessage = new Message();
List parameters = new ArrayList();
Parameter jsonArgument = new Parameter();
MessageParameter jsonArgument = new MessageParameter();
String charset = "UTF-8";
jsonArgument.setValue(new String(bytes, charset));
jsonArgument.setEncoding(charset);
......
......@@ -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.rpc.PlannerCaller;
import nl.uva.sne.drip.commons.types.Message;
import nl.uva.sne.drip.commons.types.Parameter;
import nl.uva.sne.drip.commons.types.MessageParameter;
import nl.uva.sne.drip.commons.types.Plan;
import nl.uva.sne.drip.commons.types.ToscaRepresentation;
import nl.uva.sne.drip.commons.utils.Converter;
......@@ -62,14 +62,14 @@ public class SimplePlannerService {
Plan topLevel;
try (PlannerCaller planner = new PlannerCaller(messageBrokerHost)) {
Message plannerReturnedMessage = planner.call(plannerInvokationMessage);
List<Parameter> planFiles = plannerReturnedMessage.getParameters();
List<MessageParameter> planFiles = plannerReturnedMessage.getParameters();
topLevel = new Plan();
Set<String> ids = topLevel.getLoweLevelPlanIDs();
if (ids == null) {
ids = new HashSet<>();
}
Plan lowerLevelPlan = null;
for (Parameter p : planFiles) {
for (MessageParameter p : planFiles) {
//Should have levels in attributes
Map<String, String> attributess = p.getAttributes();
if (attributess != null) {
......@@ -108,7 +108,7 @@ public class SimplePlannerService {
Message invokationMessage = new Message();
List parameters = new ArrayList();
Parameter fileArgument = new Parameter();
MessageParameter fileArgument = new MessageParameter();
String charset = "UTF-8";
fileArgument.setValue(new String(bytes, charset));
......@@ -116,7 +116,7 @@ public class SimplePlannerService {
fileArgument.setName("input");
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"));
fileArgument.setValue(new String(bytes, 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