Commit 9f6a8fda authored by Spiros Koulouzis's avatar Spiros Koulouzis

Sloved json format bugs

Added more exceptions 
parent 09026e0d
/*
* 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.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
*
* @author S. Koulouzis
*/
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Cloud credentials not found")
public class CloudCredentialsNotFoundException extends RuntimeException {
public CloudCredentialsNotFoundException(String string) {
super(string);
}
public CloudCredentialsNotFoundException() {
super();
}
}
/*
* 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.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
*
* @author S. Koulouzis
*/
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public class InternalServerErrorException extends RuntimeException {
public InternalServerErrorException(String massage) {
super(massage);
}
public InternalServerErrorException() {
super();
}
}
/*
* 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.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
*
* @author S. Koulouzis
*/
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Plan not found")
public class PlanNotFoundException extends RuntimeException {
public PlanNotFoundException(String string) {
super(string);
}
public PlanNotFoundException() {
super();
}
}
...@@ -28,6 +28,7 @@ import java.util.logging.Logger; ...@@ -28,6 +28,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.api.exception.BadRequestException; import nl.uva.sne.drip.api.exception.BadRequestException;
import nl.uva.sne.drip.api.exception.NotFoundException;
import nl.uva.sne.drip.commons.types.Message; import nl.uva.sne.drip.commons.types.Message;
import nl.uva.sne.drip.commons.types.MessageParameter; import nl.uva.sne.drip.commons.types.MessageParameter;
import org.json.JSONException; import org.json.JSONException;
...@@ -107,6 +108,30 @@ public class DeployController { ...@@ -107,6 +108,30 @@ public class DeployController {
return clusterCredentialService.getDao().findOne(id); return clusterCredentialService.getDao().findOne(id);
} }
@RequestMapping(value = "/ids", method = RequestMethod.GET)
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
List<String> getIds() {
List<ClusterCredentials> all = clusterCredentialService.getDao().findAll();
List<String> ids = new ArrayList<>(all.size());
for (ClusterCredentials pi : all) {
ids.add(pi.getId());
}
return ids;
}
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
String delete(@PathVariable("id") String id) {
ClusterCredentials cred = clusterCredentialService.getDao().findOne(id);
if (cred != null) {
provisionService.getDao().delete(id);
return "Deleted : " + id;
}
throw new NotFoundException();
}
private Message buildDeployerMessage(String provisionID, String clusterType) { private Message buildDeployerMessage(String provisionID, String clusterType) {
ProvisionInfo pro = provisionService.getDao().findOne(provisionID); ProvisionInfo pro = provisionService.getDao().findOne(provisionID);
String cloudConfID = pro.getCloudConfID(); String cloudConfID = pro.getCloudConfID();
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
*/ */
package nl.uva.sne.drip.api.rest; package nl.uva.sne.drip.api.rest;
import nl.uva.sne.drip.api.service.SimplePlannerService;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -47,8 +46,7 @@ import org.springframework.web.bind.annotation.RequestParam; ...@@ -47,8 +46,7 @@ import org.springframework.web.bind.annotation.RequestParam;
public class PlannerController { public class PlannerController {
// @Autowired // @Autowired
private SimplePlannerService simplePlannerService; // private SimplePlannerService simplePlannerService;
@Autowired @Autowired
private PlannerService plannerService; private PlannerService plannerService;
...@@ -86,22 +84,22 @@ public class PlannerController { ...@@ -86,22 +84,22 @@ public class PlannerController {
@RolesAllowed({UserService.USER, UserService.ADMIN}) @RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody public @ResponseBody
String getToscaID(@PathVariable("id") String id) { String getToscaID(@PathVariable("id") String id) {
return simplePlannerService.getToscaID(id); return plannerService.getToscaID(id);
} }
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE) @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@RolesAllowed({UserService.USER, UserService.ADMIN}) @RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody public @ResponseBody
String delete(@PathVariable("id") String id) { String delete(@PathVariable("id") String id) {
simplePlannerService.getDao().delete(id); plannerService.getDao().delete(id);
return "Deleted tosca :" + id; return "Deleted : " + id;
} }
@RequestMapping(value = "/ids") @RequestMapping(value = "/ids")
@RolesAllowed({UserService.USER, UserService.ADMIN}) @RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody public @ResponseBody
List<String> getIds() { List<String> getIds() {
List<Plan> all = simplePlannerService.findAll(); List<Plan> all = plannerService.findAll();
List<String> ids = new ArrayList<>(); List<String> ids = new ArrayList<>();
for (Plan tr : all) { for (Plan tr : all) {
ids.add(tr.getId()); ids.add(tr.getId());
......
...@@ -44,7 +44,10 @@ import org.springframework.web.bind.annotation.RequestMethod; ...@@ -44,7 +44,10 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import nl.uva.sne.drip.api.exception.BadRequestException; import nl.uva.sne.drip.api.exception.BadRequestException;
import nl.uva.sne.drip.api.exception.CloudCredentialsNotFoundException;
import nl.uva.sne.drip.api.exception.InternalServerErrorException;
import nl.uva.sne.drip.api.exception.NotFoundException; 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.DRIPCaller;
import nl.uva.sne.drip.api.rpc.ProvisionerCaller; import nl.uva.sne.drip.api.rpc.ProvisionerCaller;
import nl.uva.sne.drip.api.service.ProvisionService; import nl.uva.sne.drip.api.service.ProvisionService;
...@@ -95,6 +98,30 @@ public class ProvisionController { ...@@ -95,6 +98,30 @@ public class ProvisionController {
return provisionService.getDao().findOne(id); return provisionService.getDao().findOne(id);
} }
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
String delete(@PathVariable("id") String id) {
ProvisionInfo provPlan = provisionService.getDao().findOne(id);
if (provPlan != null) {
provisionService.getDao().delete(id);
return "Deleted : " + id;
}
throw new NotFoundException();
}
@RequestMapping(value = "/ids", method = RequestMethod.GET)
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
List<String> getIds() {
List<ProvisionInfo> all = provisionService.getDao().findAll();
List<String> ids = new ArrayList<>(all.size());
for (ProvisionInfo pi : all) {
ids.add(pi.getId());
}
return ids;
}
@RequestMapping(value = "/provision", method = RequestMethod.POST) @RequestMapping(value = "/provision", method = RequestMethod.POST)
@RolesAllowed({UserService.USER, UserService.ADMIN}) @RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody public @ResponseBody
...@@ -108,6 +135,9 @@ public class ProvisionController { ...@@ -108,6 +135,9 @@ public class ProvisionController {
for (MessageParameter p : params) { for (MessageParameter p : params) {
String name = p.getName(); String name = p.getName();
if (name.toLowerCase().contains("exception")) {
throw new InternalServerErrorException(name + ". " + p.getValue());
}
if (!name.equals("kubernetes")) { if (!name.equals("kubernetes")) {
String value = p.getValue(); String value = p.getValue();
Map<String, Object> kvMap = Converter.ymlString2Map(value); Map<String, Object> kvMap = Converter.ymlString2Map(value);
...@@ -150,7 +180,7 @@ public class ProvisionController { ...@@ -150,7 +180,7 @@ public class ProvisionController {
List<MessageParameter> 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 CloudCredentialsNotFoundException();
} }
MessageParameter conf = buildCloudConfParam(cred); MessageParameter conf = buildCloudConfParam(cred);
parameters.add(conf); parameters.add(conf);
...@@ -217,7 +247,7 @@ public class ProvisionController { ...@@ -217,7 +247,7 @@ public class ProvisionController {
private List<MessageParameter> 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 PlanNotFoundException();
} }
List<MessageParameter> parameters = new ArrayList(); List<MessageParameter> parameters = new ArrayList();
MessageParameter topology = new MessageParameter(); MessageParameter topology = new MessageParameter();
......
...@@ -82,7 +82,7 @@ public class ToscaController { ...@@ -82,7 +82,7 @@ public class ToscaController {
public @ResponseBody public @ResponseBody
String delete(@PathVariable("id") String id) { String delete(@PathVariable("id") String id) {
toscaService.delete(id); toscaService.delete(id);
return "Deleted tosca :" + id; return "Deleted : " + id;
} }
// http://localhost:8080/drip-api/tosca/ids // http://localhost:8080/drip-api/tosca/ids
......
...@@ -117,7 +117,7 @@ public class UserController { ...@@ -117,7 +117,7 @@ public class UserController {
throw new UserNotFoundException("User " + id + " not found"); throw new UserNotFoundException("User " + id + " not found");
} }
service.getDao().delete(user); service.getDao().delete(user);
return "Deleted used :" + id; return "Deleted : " + id;
} catch (Exception ex) { } catch (Exception ex) {
Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
} }
......
...@@ -154,4 +154,23 @@ public class PlannerService { ...@@ -154,4 +154,23 @@ public class PlannerService {
return ymlStr; return ymlStr;
} }
public String getToscaID(String id) {
return planDao.findOne(id).getToscaID();
}
public PlanDao getDao() {
return this.planDao;
}
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;
}
} }
...@@ -60,7 +60,7 @@ public class P2PConverter { ...@@ -60,7 +60,7 @@ public class P2PConverter {
curVM.clusterType = clusterType; curVM.clusterType = clusterType;
curVM.dockers = cmp.getDocker(); curVM.dockers = cmp.getDocker();
curVM.public_address = cmp.getName(); curVM.public_address = cmp.getName();
curVM.nodeType = "t2" + cmp.getSize().toLowerCase(); curVM.nodeType = "t2." + cmp.getSize().toLowerCase();
Eth eth = new Eth(); Eth eth = new Eth();
eth.name = "p1"; eth.name = "p1";
......
...@@ -397,26 +397,26 @@ public class Consumer extends DefaultConsumer { ...@@ -397,26 +397,26 @@ public class Consumer extends DefaultConsumer {
if (name.equals("topology")) { if (name.equals("topology")) {
JSONObject attributes = param.getJSONObject("attributes"); JSONObject attributes = param.getJSONObject("attributes");
int fileLevel = Integer.valueOf((String) attributes.get("level")); int fileLevel = Integer.valueOf((String) attributes.get("level"));
String[] parts = ((String) attributes.get("filename")).split("_");
String fileName = "";
String prefix = "";
//Clear date part form file name
for (int j = 1; j < parts.length; j++) {
fileName += prefix + parts[j];
prefix = "_";
}
if (fileLevel == level) { if (fileLevel == level) {
String originalFilename = (String) attributes.get("filename");
String fileName = "";
// String[] parts = originalFilename.split("_");
// String prefix = "";
// //Clear date part form file name
// if (isNumeric(parts[0])) {
// for (int j = 1; j < parts.length; j++) {
// fileName += prefix + parts[j];
// prefix = "_";
// }
// } else {
fileName = originalFilename;
// }
File topologyFile = new File(tempInputDirPath + File.separator + fileName); File topologyFile = new File(tempInputDirPath + File.separator + fileName);
if (topologyFile.createNewFile()) { topologyFile.createNewFile();
String val = (String) param.get(MessageParameter.VALUE); String val = (String) param.get(MessageParameter.VALUE);
//Replace '{' with indentation otherwise we get 'End of document execption'
// val = val.replaceAll("- \\{", " - ").replaceAll(", ", "\n ").replaceAll("}", "");
writeValueToFile(val, topologyFile); writeValueToFile(val, topologyFile);
return topologyFile; return topologyFile;
} else {
return null;
}
} }
} }
} }
...@@ -484,4 +484,8 @@ public class Consumer extends DefaultConsumer { ...@@ -484,4 +484,8 @@ public class Consumer extends DefaultConsumer {
return null; return null;
} }
private static boolean isNumeric(String str) {
return str.matches("-?\\d+(\\.\\d+)?");
}
} }
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