Commit 1611ae99 authored by Spiros Koulouzis's avatar Spiros Koulouzis

Refactor data types: Each agent has its own data type

parent 75da4d65
/*
* 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.Plan;
import org.springframework.data.mongodb.repository.MongoRepository;
/**
*
* @author S. Koulouzis
*/
public interface PlanDao extends MongoRepository<Plan, String> {
}
/*
* 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.Provision;
import org.springframework.data.mongodb.repository.MongoRepository;
/**
*
* @author S. Koulouzis
*/
public interface ProvisionDao extends MongoRepository<Provision, String> {
}
......@@ -16,7 +16,6 @@
package nl.uva.sne.drip.api.rest;
import nl.uva.sne.drip.api.service.SimplePlannerService;
import nl.uva.sne.drip.commons.types.ToscaRepresentation;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
......@@ -36,6 +35,7 @@ import org.springframework.web.bind.annotation.RestController;
import nl.uva.sne.drip.api.service.PlannerService;
import nl.uva.sne.drip.api.service.ToscaService;
import nl.uva.sne.drip.api.service.UserService;
import nl.uva.sne.drip.commons.types.Plan;
import org.springframework.web.bind.annotation.RequestParam;
/**
......@@ -62,8 +62,8 @@ public class PlannerController {
String plan(@PathVariable("tosca_id") String toscaId) {
try {
ToscaRepresentation plan = simplePlannerService.getPlan(toscaId);
// ToscaRepresentation plan = plannerService.getPlan(toscaId);
Plan plan = simplePlannerService.getPlan(toscaId);
// Plan plan = plannerService.getPlan(toscaId);
if (plan == null) {
throw new NotFoundException("Could not make plan");
}
......@@ -79,29 +79,35 @@ public class PlannerController {
public @ResponseBody
String get(@PathVariable("id") String id, @RequestParam(value = "format") String format) {
try {
return toscaService.get(id, format, ToscaRepresentation.Type.PLAN);
return simplePlannerService.get(id, format);
} catch (JSONException ex) {
Logger.getLogger(ToscaController.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
@RequestMapping(value = "/tosca/{id}", method = RequestMethod.GET)
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
String getToscaID(@PathVariable("id") String id) {
return simplePlannerService.getToscaID(id);
}
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
String delete(@PathVariable("id") String id) {
toscaService.delete(id, ToscaRepresentation.Type.PLAN);
simplePlannerService.getDao().delete(id);
return "Deleted tosca :" + id;
}
// http://localhost:8080/drip-api/tosca/ids
@RequestMapping(value = "/ids")
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
List<String> getIds() {
List<ToscaRepresentation> all = toscaService.findAll(ToscaRepresentation.Type.PLAN);
List<Plan> all = simplePlannerService.findAll();
List<String> ids = new ArrayList<>();
for (ToscaRepresentation tr : all) {
for (Plan tr : all) {
ids.add(tr.getId());
}
return ids;
......
......@@ -15,10 +15,11 @@
*/
package nl.uva.sne.drip.api.rest;
import nl.uva.sne.drip.commons.types.ProvisionRequest;
import com.fasterxml.jackson.core.JsonParser;
import nl.uva.sne.drip.commons.types.Provision;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.ByteArrayOutputStream;
import nl.uva.sne.drip.commons.types.ToscaRepresentation;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -46,14 +47,17 @@ import nl.uva.sne.drip.api.exception.BadRequestException;
import nl.uva.sne.drip.api.exception.NotFoundException;
import nl.uva.sne.drip.api.rpc.DRIPCaller;
import nl.uva.sne.drip.api.rpc.ProvisionerCaller;
import nl.uva.sne.drip.api.service.ToscaService;
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.types.CloudCredentials;
import nl.uva.sne.drip.commons.types.LoginKey;
import nl.uva.sne.drip.commons.types.Plan;
import nl.uva.sne.drip.commons.types.UserScript;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
/**
......@@ -68,9 +72,6 @@ public class ProvisionController {
@Value("${message.broker.host}")
private String messageBrokerHost;
@Autowired
private ToscaService toscaService;
@Autowired
private UserScriptService userScriptService;
......@@ -80,35 +81,48 @@ public class ProvisionController {
@Autowired
private CloudCredentialsDao cloudCredentialsDao;
@RequestMapping(value = "/get", method = RequestMethod.GET)
@Autowired
private ProvisionService provisionService;
@Autowired
private SimplePlannerService planService;
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
ProvisionRequest get() {
ProvisionRequest re = new ProvisionRequest();
re.setCloudConfID("58a1f0a963d42f004b1d63ad");
re.setPlanID("58ad99d578b6ba941aeb22a4");
re.setUserKeyID("58a20be263d4a5898835676e");
re.setUserScriptID("58a2112363d41754cca042b4");
return re;
Provision get(@PathVariable("id") String id) {
return provisionService.getDao().findOne(id);
}
@RequestMapping(value = "/provision", method = RequestMethod.POST)
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
String plann(@RequestBody ProvisionRequest req) {
String plann(@RequestBody Provision req) {
try (DRIPCaller provisioner = new ProvisionerCaller(messageBrokerHost);) {
Message provisionerInvokationMessage = buildProvisionerMessage(req);
Message response = provisioner.call(provisionerInvokationMessage);
return "";
// Message response = provisioner.call(provisionerInvokationMessage);
Message response = generateFakeResponse();
List<Parameter> params = response.getParameters();
for (Parameter p : params) {
String name = p.getName();
if (!name.equals("kubernetes")) {
String value = p.getValue();
Map<String, Object> kvMap = Converter.ymlString2Map(value);
req.setKvMap(kvMap);
req.setPlanID(req.getPlanID());
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(ProvisionRequest pReq) throws JSONException, IOException {
private Message buildProvisionerMessage(Provision pReq) throws JSONException, IOException {
Message invokationMessage = new Message();
List<Parameter> parameters = new ArrayList();
CloudCredentials cred = cloudCredentialsDao.findOne(pReq.getCloudConfID());
......@@ -178,7 +192,7 @@ public class ProvisionController {
}
private List<Parameter> buildTopologyParams(String planID) throws JSONException {
ToscaRepresentation plan = toscaService.get(planID, ToscaRepresentation.Type.PLAN);
Plan plan = planService.getDao().findOne(planID);
if (plan == null) {
throw new NotFoundException();
}
......@@ -192,15 +206,15 @@ public class ProvisionController {
topology.setAttributes(attributes);
parameters.add(topology);
Set<String> ids = plan.getLowerLevelIDs();
Set<String> ids = plan.getLoweLevelPlanIDs();
for (String lowID : ids) {
plan = toscaService.get(lowID, ToscaRepresentation.Type.PLAN);
Plan lowPlan = planService.getDao().findOne(lowID);
topology = new Parameter();
topology.setName("topology");
topology.setValue(Converter.map2YmlString(plan.getKvMap()));
topology.setValue(Converter.map2YmlString(lowPlan.getKvMap()));
attributes = new HashMap<>();
attributes.put("level", String.valueOf(plan.getLevel()));
attributes.put("filename", FilenameUtils.removeExtension(plan.getName()));
attributes.put("level", String.valueOf(lowPlan.getLevel()));
attributes.put("filename", FilenameUtils.removeExtension(lowPlan.getName()));
topology.setAttributes(attributes);
parameters.add(topology);
}
......@@ -249,4 +263,27 @@ public class ProvisionController {
return parameters;
}
private Message generateFakeResponse() 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\"}]}";
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
return mapper.readValue(strResponse, Message.class);
}
}
......@@ -57,11 +57,10 @@ public class ToscaController {
throw new BadRequestException("Must uplaod a file");
}
try {
return toscaService.save(file, ToscaRepresentation.Type.SIDE);
return toscaService.save(file);
} catch (IOException | IllegalStateException ex) {
Logger.getLogger(ToscaController.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
......@@ -71,7 +70,7 @@ public class ToscaController {
public @ResponseBody
String get(@PathVariable("id") String id, @RequestParam(value = "format") String format) {
try {
return toscaService.get(id, format,ToscaRepresentation.Type.SIDE);
return toscaService.get(id, format);
} catch (JSONException ex) {
Logger.getLogger(ToscaController.class.getName()).log(Level.SEVERE, null, ex);
}
......@@ -82,7 +81,7 @@ public class ToscaController {
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
String delete(@PathVariable("id") String id) {
toscaService.delete(id,ToscaRepresentation.Type.SIDE);
toscaService.delete(id);
return "Deleted tosca :" + id;
}
......@@ -91,7 +90,7 @@ public class ToscaController {
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
List<String> getIds() {
List<ToscaRepresentation> all = toscaService.findAll(ToscaRepresentation.Type.SIDE);
List<ToscaRepresentation> all = toscaService.findAll();
List<String> ids = new ArrayList<>();
for (ToscaRepresentation tr : all) {
ids.add(tr.getId());
......
......@@ -58,7 +58,6 @@ public class PlannerService {
ToscaRepresentation tr = new ToscaRepresentation();
Map<String, Object> kvMap = null;
tr.setKvMap(kvMap);
tr.setType(ToscaRepresentation.Type.PLAN);
return null;
}
......@@ -66,7 +65,7 @@ public class PlannerService {
private Message buildPlannerMessage(String toscaId) throws JSONException, UnsupportedEncodingException {
ToscaRepresentation t2 = toscaService.getDao().findOne(toscaId);
if (t2 == null || t2.getType().equals(ToscaRepresentation.Type.PLAN)) {
if (t2 == null) {
throw new BadRequestException("The description: " + toscaId + " is a plan. Cannot be used as planner input");
}
Map<String, Object> map = t2.getKvMap();
......
/*
* 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 java.io.IOException;
import java.util.List;
import java.util.Map;
import nl.uva.sne.drip.api.dao.ProvisionDao;
import nl.uva.sne.drip.api.dao.ToscaDao;
import nl.uva.sne.drip.api.exception.NotFoundException;
import nl.uva.sne.drip.commons.types.ToscaRepresentation;
import nl.uva.sne.drip.commons.utils.Converter;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
/**
*
* @author S. Koulouzis
*/
@Service
public class ProvisionService {
@Autowired
private ProvisionDao dao;
public ProvisionDao getDao() {
return dao;
}
}
......@@ -26,11 +26,12 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeoutException;
import nl.uva.sne.drip.api.exception.BadRequestException;
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.Plan;
import nl.uva.sne.drip.commons.types.ToscaRepresentation;
import nl.uva.sne.drip.commons.utils.Converter;
import org.json.JSONException;
......@@ -51,17 +52,29 @@ public class SimplePlannerService {
@Autowired
private ToscaService toscaService;
public ToscaRepresentation getPlan(String toscaId) throws JSONException, IOException, TimeoutException, InterruptedException {
Message plannerInvokationMessage = buildSimplePlannerMessage(toscaId);
ToscaRepresentation topLevel;
@Autowired
private PlanDao planDao;
public Plan getPlan(String toscaId) throws JSONException, IOException, TimeoutException, InterruptedException {
ToscaRepresentation tosca = toscaService.getDao().findOne(toscaId);
Message plannerInvokationMessage = buildSimplePlannerMessage(tosca);
Plan topLevel;
try (PlannerCaller planner = new PlannerCaller(messageBrokerHost)) {
Message plannerReturnedMessage = planner.call(plannerInvokationMessage);
List<Parameter> toscaFiles = plannerReturnedMessage.getParameters();
topLevel = new ToscaRepresentation();
ToscaRepresentation tr = null;
for (Parameter p : toscaFiles) {
List<Parameter> planFiles = plannerReturnedMessage.getParameters();
topLevel = new Plan();
Set<String> ids = topLevel.getLoweLevelPlanIDs();
if (ids == null) {
ids = new HashSet<>();
}
Plan lowerLevelPlan = null;
for (Parameter p : planFiles) {
//Should have levels in attributes
Map<String, String> attributess = p.getAttributes();
if (attributess != null) {
attributess.get("level");
}
String originalFileName = p.getName();
String name = System.currentTimeMillis() + "_" + originalFileName;
if (originalFileName.equals("planner_output_all.yml")) {
......@@ -69,30 +82,24 @@ public class SimplePlannerService {
topLevel.setLevel(0);
topLevel.setKvMap(Converter.ymlString2Map(p.getValue()));
} else {
tr = new ToscaRepresentation();
tr.setName(name);
tr.setKvMap(Converter.ymlString2Map(p.getValue()));
tr.setLevel(1);
}
tr.setType(ToscaRepresentation.Type.PLAN);
toscaService.getDao().save(tr);
Set<String> ids = topLevel.getLowerLevelIDs();
if (ids == null) {
ids = new HashSet<>();
lowerLevelPlan = new Plan();
lowerLevelPlan.setName(name);
lowerLevelPlan.setKvMap(Converter.ymlString2Map(p.getValue()));
lowerLevelPlan.setLevel(1);
planDao.save(lowerLevelPlan);
ids.add(lowerLevelPlan.getId());
}
ids.add(tr.getId());
topLevel.setLowerLevelIDs(ids);
}
topLevel.setType(ToscaRepresentation.Type.PLAN);
toscaService.getDao().save(topLevel);
topLevel.setLoweLevelPlansIDs(ids);
}
topLevel.setToscaID(toscaId);
planDao.save(topLevel);
return topLevel;
}
private Message buildSimplePlannerMessage(String toscaId) throws JSONException, UnsupportedEncodingException, IOException {
ToscaRepresentation t2 = toscaService.getDao().findOne(toscaId);
if (t2 == null || t2.getType().equals(ToscaRepresentation.Type.PLAN)) {
throw new BadRequestException("The description: " + toscaId + " is a plan. Cannot be used as planner input");
private Message buildSimplePlannerMessage(ToscaRepresentation t2) throws JSONException, UnsupportedEncodingException, IOException {
if (t2 == null) {
throw new NotFoundException();
}
Map<String, Object> map = t2.getKvMap();
String ymlStr = Converter.map2YmlString(map);
......@@ -120,4 +127,51 @@ public class SimplePlannerService {
invokationMessage.setCreationDate((System.currentTimeMillis()));
return invokationMessage;
}
public String get(String id, String fromat) throws JSONException {
Plan plan = planDao.findOne(id);
if (plan == null) {
throw new NotFoundException();
}
Map<String, Object> map = plan.getKvMap();
Set<String> ids = plan.getLoweLevelPlanIDs();
for (String lowID : ids) {
Map<String, Object> lowLevelMap = planDao.findOne(lowID).getKvMap();
map.putAll(lowLevelMap);
}
if (fromat != null && fromat.equals("yml")) {
String ymlStr = Converter.map2YmlString(map);
ymlStr = ymlStr.replaceAll("\\uff0E", "\\.");
return ymlStr;
}
if (fromat != null && fromat.equals("json")) {
String jsonStr = Converter.map2JsonString(map);
jsonStr = jsonStr.replaceAll("\\uff0E", "\\.");
return jsonStr;
}
String ymlStr = Converter.map2YmlString(map);
ymlStr = ymlStr.replaceAll("\\uff0E", "\\.");
return ymlStr;
}
public PlanDao getDao() {
return planDao;
}
public String getToscaID(String id) {
return planDao.findOne(id).getToscaID();
}
public List<Plan> findAll() {
List<Plan> all = planDao.findAll();
List<Plan> topLevel = new ArrayList<>();
for (Plan p : all) {
if (p.getLevel() == 0) {
topLevel.add(p);
}
}
return topLevel;
}
}
......@@ -16,10 +16,8 @@
package nl.uva.sne.drip.api.service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import nl.uva.sne.drip.api.dao.ToscaDao;
import nl.uva.sne.drip.api.exception.NotFoundException;
import nl.uva.sne.drip.commons.types.ToscaRepresentation;
......@@ -39,18 +37,13 @@ public class ToscaService {
@Autowired
private ToscaDao dao;
public String get(String id, String fromat, ToscaRepresentation.Type type) throws JSONException {
public String get(String id, String fromat) throws JSONException {
ToscaRepresentation tosca = dao.findOne(id);
if (tosca == null || !tosca.getType().equals(type)) {
if (tosca == null) {
throw new NotFoundException();
}
Set<String> ids = tosca.getLowerLevelIDs();
Map<String, Object> map = tosca.getKvMap();
if (ids != null) {
for (String lowID : ids) {
map.putAll(dao.findOne(lowID).getKvMap());
}
}
if (fromat != null && fromat.equals("yml")) {
String ymlStr = Converter.map2YmlString(map);
......@@ -67,7 +60,7 @@ public class ToscaService {
return ymlStr;
}
public String save(MultipartFile file, ToscaRepresentation.Type type) throws IOException {
public String save(MultipartFile file) throws IOException {
String originalFileName = file.getOriginalFilename();
String name = System.currentTimeMillis() + "_" + originalFileName;
byte[] bytes = file.getBytes();
......@@ -78,42 +71,25 @@ public class ToscaService {
ToscaRepresentation t = new ToscaRepresentation();
t.setName(name);
t.setKvMap(map);
t.setType(type);
dao.save(t);
return t.getId();
}
public void delete(String id, ToscaRepresentation.Type type) {
ToscaRepresentation tosca = dao.findOne(id);
if (!tosca.getType().equals(type)) {
throw new NotFoundException();
} else {
dao.delete(id);
}
public void delete(String id) {
dao.delete(id);
}
public List<ToscaRepresentation> findAll(ToscaRepresentation.Type type) {
List<ToscaRepresentation> all = dao.findAll();
List<ToscaRepresentation> allType = new ArrayList<>();
if (all == null) {
throw new NotFoundException();
}
for (ToscaRepresentation tr : all) {
if (tr.getType() != null && tr.getType().equals(type)) {
allType.add(tr);
}
}
return allType;
public List<ToscaRepresentation> findAll() {
return dao.findAll();
}
public ToscaDao getDao() {
return dao;
}
public ToscaRepresentation get(String planID, ToscaRepresentation.Type type) {
public ToscaRepresentation get(String planID) {
ToscaRepresentation tosca = dao.findOne(planID);
if (tosca == null || !tosca.getType().equals(type)) {
if (tosca == null) {
throw new NotFoundException();
}
return tosca;
......
/*
* 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.commons.types;
import java.util.Map;
import java.util.Set;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
/**
*
* @author S. Koulouzis
*/
@Document
public class Plan {
@Id
private String id;
private String toscaID;
private String name;
private Integer level;
private Set<String> loweLevelPlansIDs;
private Map<String, Object> kvMap;
public final String getId() {
return id;
}
public final void setId(final String id) {
this.id = id;
}
/**
* @return the kvMap
*/
public Map<String, Object> getKvMap() {
return kvMap;
}
/**
* @param kvMap the kvMap to set
*/
public void setKvMap(Map<String, Object> kvMap) {
this.kvMap = kvMap;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the toscaID
*/
public String getToscaID() {
return toscaID;
}
/**
* @param toscaID the toscaID to set
*/
public void setToscaID(String toscaID) {
this.toscaID = toscaID;
}
/**
* @return the level
*/
public Integer getLevel() {
return level;
}
/**
* @param level the level to set
*/
public void setLevel(Integer level) {
this.level = level;
}
/**
* @return the loweLevelPlansIDs
*/
public Set<String> getLoweLevelPlanIDs() {
return loweLevelPlansIDs;
}
/**
* @param loweLevelPlans the loweLevelPlansIDs to set
*/
public void setLoweLevelPlansIDs(Set<String> loweLevelPlansIDs) {
this.loweLevelPlansIDs = loweLevelPlansIDs;
}
}
......@@ -15,11 +15,17 @@
*/
package nl.uva.sne.drip.commons.types;
import java.util.Map;
import org.springframework.data.annotation.Id;
/**
*
* @author S. Koulouzis
*/
public class ProvisionRequest {
public class Provision {
@Id
private String id;
private String cloudConfID;
......@@ -29,6 +35,8 @@ public class ProvisionRequest {
private String userKeyID;
private Map<String, Object> kvMap;
/**
* @return the cloudConfID
*/
......@@ -85,4 +93,32 @@ public class ProvisionRequest {
this.userKeyID = userKeyID;
}
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the kvMap
*/
public Map<String, Object> getKvMap() {
return kvMap;
}
/**
* @param kvMap the kvMap to set
*/
public void setKvMap(Map<String, Object> kvMap) {
this.kvMap = kvMap;
}
}
......@@ -30,35 +30,10 @@ public class ToscaRepresentation {
@Id
private String id;
private Set<String> lowerLevelIDs;
private String name;
private Map<String, Object> kvMap;
private Integer level;
private Type type;
public static enum Type {
PLAN,
SIDE
}
/**
* @return the type
*/
public Type getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(Type type) {
this.type = type;
}
public final String getId() {
return id;
}
......@@ -94,33 +69,4 @@ public class ToscaRepresentation {
public void setName(String name) {
this.name = name;
}
/**
* @return the level
*/
public Integer getLevel() {
return level;
}
/**
* @param level the level to set
*/
public void setLevel(Integer level) {
this.level = level;
}
/**
* @return the lowerLevelIDs
*/
public Set<String> getLowerLevelIDs() {
return lowerLevelIDs;
}
/**
* @param lowerLevelIDs the lowerLevelIDs to set
*/
public void setLowerLevelIDs(Set<String> lowerLevelIDs) {
this.lowerLevelIDs = lowerLevelIDs;
}
}
......@@ -118,9 +118,16 @@ public class Consumer extends DefaultConsumer {
topologyInfoArray = invokeProvisioner(message, tempInputDirPath);
response = generateResponse(topologyInfoArray);
} catch (IOException | JSONException ex) {
response = ex.getMessage();
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
} catch (Throwable ex) {
try {
response = generateExeptionResponse(ex);
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
} catch (JSONException ex1) {
response = "{\"creationDate\": " + System.currentTimeMillis()
+ ",\"parameters\": [{\"url\": null,\"encoding\": UTF-8,"
+ "\"value\": \"" + ex.getMessage() + "\",\"name\": \""
+ ex.getClass().getName() + "\",\"attributes\": null}]}";
}
} finally {
//We send the response back. No need to change anything here
channel.basicPublish("", properties.getReplyTo(), replyProps, response.getBytes("UTF-8"));
......@@ -336,6 +343,22 @@ public class Consumer extends DefaultConsumer {
return jo.toString();
}
private String generateExeptionResponse(Throwable ex) throws JSONException {
JSONObject jo = new JSONObject();
jo.put("creationDate", (System.currentTimeMillis()));
List parameters = new ArrayList();
String charset = "UTF-8";
Map<String, String> errorArgument = new HashMap<>();
errorArgument.put("encoding", charset);
errorArgument.put("name", ex.getClass().getName());
errorArgument.put("value", ex.getMessage());
parameters.add(errorArgument);
jo.put("parameters", parameters);
return jo.toString();
}
private void writeValueToFile(String value, File file) throws FileNotFoundException {
try (PrintWriter out = new PrintWriter(file)) {
out.print(value);
......
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