Service group validation
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/ea8fce5c Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/ea8fce5c Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/ea8fce5c Branch: refs/heads/master Commit: ea8fce5c0b77fd82b696c3af5523bb6ebd720c77 Parents: 25b09fa Author: Vishanth <[email protected]> Authored: Wed May 6 17:51:31 2015 +0530 Committer: Imesh Gunaratne <[email protected]> Committed: Mon May 11 12:19:59 2015 +0530 ---------------------------------------------------------------------- .../rest/endpoint/api/StratosApiV41.java | 9 + .../rest/endpoint/api/StratosApiV41Utils.java | 187 ++++++++++++++----- 2 files changed, 153 insertions(+), 43 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/ea8fce5c/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java index d308e6e..4578019 100644 --- a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java +++ b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java @@ -455,6 +455,15 @@ public class StratosApiV41 extends AbstractApi { if (e.getCause().getMessage().contains("already exists")) { return Response.status(Response.Status.CONFLICT).entity(new StatusResponseBean( Response.Status.CONFLICT.getStatusCode(), "Cartridge group not found")).build(); + } else if(e.getCause().getMessage().contains("duplicate cartridges")) { + return Response.status(Response.Status.BAD_REQUEST).entity(new StatusResponseBean( + Response.Status.BAD_REQUEST.getStatusCode(), "Cartridges duplicated in the group definition")).build(); + } else if(e.getCause().getMessage().contains("duplicate groups")) { + return Response.status(Response.Status.BAD_REQUEST).entity(new StatusResponseBean( + Response.Status.BAD_REQUEST.getStatusCode(), "Groups duplicated in the group definition")).build(); + } else if(e.getCause().getMessage().contains("cyclic group")) { + return Response.status(Response.Status.BAD_REQUEST).entity(new StatusResponseBean( + Response.Status.BAD_REQUEST.getStatusCode(), "Cyclic group behaviour identified in the group definition")).build(); } else { throw e; } http://git-wip-us.apache.org/repos/asf/stratos/blob/ea8fce5c/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java index 2d878bc..96f0c18 100644 --- a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java +++ b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java @@ -980,50 +980,41 @@ public class StratosApiV41Utils { List<String> groupNames; String[] cartridgeGroupNames; - // if any cartridges are specified in the group, they should be already deployed - if (serviceGroupDefinition.getCartridges() != null) { - if (log.isDebugEnabled()) { log.debug("checking cartridges in cartridge group " + serviceGroupDefinition.getName()); } findCartridgesInGroupBean(serviceGroupDefinition, cartridgeTypes); - Set<String> duplicates = findDuplicates(cartridgeTypes); - if (duplicates.size() > 0) { - StringBuilder duplicatesOutput = new StringBuilder(); - for (String dup : duplicates) { - duplicatesOutput.append(dup).append(" "); - } - if (log.isDebugEnabled()) { - log.debug("duplicate cartridges defined: " + duplicatesOutput.toString()); - } - throw new RestAPIException("Invalid Service Group definition, duplicate cartridges defined:" + - duplicatesOutput.toString()); - } + //validate the group definition to check if cartridges duplicate in any groups defined + validateCartridgeDuplicationInGroupDefinition(serviceGroupDefinition); + + //validate the group definition to check if groups duplicate in any groups and + //validate the group definition to check for cyclic group behaviour + validateGroupDuplicationInGroupDefinition(serviceGroupDefinition, new ArrayList<String>()); CloudControllerServiceClient ccServiceClient = getCloudControllerServiceClient(); - cartridgeNames = new String[cartridgeTypes.size()]; - int i = 0; - for (String cartridgeType : cartridgeTypes) { - try { - if (ccServiceClient.getCartridge(cartridgeType) == null) { - // cartridge is not deployed, can't continue - log.error("Invalid cartridge found in cartridge group " + cartridgeType); - throw new RestAPIException("No Cartridge Definition found with type " + cartridgeType); - } else { - cartridgeNames[i] = cartridgeType; - i++; - } - } catch (RemoteException e) { - throw new RestAPIException(e); - } catch (CloudControllerServiceCartridgeNotFoundExceptionException e) { - throw new RestAPIException(e); + cartridgeNames = new String[cartridgeTypes.size()]; + int j = 0; + for (String cartridgeType : cartridgeTypes) { + try { + if (ccServiceClient.getCartridge(cartridgeType) == null) { + // cartridge is not deployed, can't continue + log.error("Invalid cartridge found in cartridge group " + cartridgeType); + throw new RestAPIException("No Cartridge Definition found with type " + cartridgeType); + } else { + cartridgeNames[j] = cartridgeType; + j++; } + } catch (RemoteException e) { + throw new RestAPIException(e); + } catch (CloudControllerServiceCartridgeNotFoundExceptionException e) { + throw new RestAPIException(e); } } + // if any sub groups are specified in the group, they should be already deployed if (serviceGroupDefinition.getGroups() != null) { if (log.isDebugEnabled()) { @@ -1188,12 +1179,12 @@ public class StratosApiV41Utils { // Remove the dependent cartridges and cartridge groups from Stratos Manager cache // - done after service group has been removed - if (serviceGroup.getCartridges() != null) { - List<String> cartridgeList = new ArrayList<String>(); - findCartridgesInServiceGroup(serviceGroup, cartridgeList); - String[] cartridgeNames = cartridgeList.toArray(new String[cartridgeList.size()]); - smServiceClient.removeUsedCartridgesInCartridgeGroups(name, cartridgeNames); - } + + List<String> cartridgeList = new ArrayList<String>(); + findCartridgesInServiceGroup(serviceGroup, cartridgeList); + String[] cartridgeNames = cartridgeList.toArray(new String[cartridgeList.size()]); + smServiceClient.removeUsedCartridgesInCartridgeGroups(name, cartridgeNames); + } catch (RemoteException e) { throw new RestAPIException("Could not remove cartridge groups", e); } @@ -1213,9 +1204,11 @@ public class StratosApiV41Utils { return; } - for (String cartridge : serviceGroup.getCartridges()) { - if (!cartridges.contains(cartridge)) { - cartridges.add(cartridge); + if (serviceGroup.getCartridges() != null) { + for (String cartridge : serviceGroup.getCartridges()) { + if (!cartridges.contains(cartridge)) { + cartridges.add(cartridge); + } } } @@ -1238,9 +1231,11 @@ public class StratosApiV41Utils { return; } - for (String cartridge : groupBean.getCartridges()) { - if (!cartridges.contains(cartridge)) { - cartridges.add(cartridge); + if (groupBean.getCartridges() != null) { + for (String cartridge : groupBean.getCartridges()) { + if (!cartridges.contains(cartridge)) { + cartridges.add(cartridge); + } } } @@ -3229,4 +3224,110 @@ public class StratosApiV41Utils { return userList; } + /** + * This method is to validate the cartridge duplication in the group definition recursively for group within groups + * + * @param groupBean - cartridge group definition + * @throws RestAPIException - throws the rest api exception when the group definition is invalid + */ + private static void validateCartridgeDuplicationInGroupDefinition(GroupBean groupBean) throws RestAPIException{ + if (groupBean == null) { + return; + } + List<String> cartridges = new ArrayList<String>(); + if (groupBean.getCartridges() != null) { + if (groupBean.getCartridges().size() > 1) { + cartridges.addAll(groupBean.getCartridges()); + validateCartridgeDuplicationInGroup(cartridges); + } + } + if (groupBean.getGroups() != null) { + //Recursive because to check groups inside groups + for (GroupBean group : groupBean.getGroups()) { + validateCartridgeDuplicationInGroupDefinition(group); + } + } + } + /** + * This method is to validate the duplication of cartridges from the given list + * + * @param cartridges - list of strings which holds the cartridgeTypes values + * @throws RestAPIException - throws the rest api exception when the cartridges are duplicated + */ + private static void validateCartridgeDuplicationInGroup(List<String> cartridges) throws RestAPIException{ + List<String> checkList = new ArrayList<String>(); + for (String cartridge : cartridges) { + if (!checkList.contains(cartridge)) { + checkList.add(cartridge); + } + else { + if (log.isDebugEnabled()) { + log.debug("duplicate cartridges defined: " + cartridge); + } + throw new RestAPIException("Invalid Service Group definition, duplicate cartridges defined: " + + cartridge); + } + } + } + /** + * This method is to validate the group duplication in the group definition recursively for group within groups + * + * @param groupBean - cartridge group definition + * @param parentGroups - list of string which holds the parent group names (all parents in the hierarchy) + * @throws RestAPIException - throws the rest api exception when the group definition is invalid + */ + private static void validateGroupDuplicationInGroupDefinition(GroupBean groupBean, List<String> parentGroups) + throws RestAPIException{ + if (groupBean == null) { + return; + } + List<String> groups = new ArrayList<String>(); + parentGroups.add(groupBean.getName()); + if (groupBean.getGroups() != null) { + if (!groupBean.getGroups().isEmpty()) { + for (GroupBean g : groupBean.getGroups()) { + groups.add(g.getName()); + } + validateGroupDuplicationInGroup(groups, parentGroups); + } + } + if (groupBean.getGroups() != null) { + //Recursive because to check groups inside groups + for (GroupBean group : groupBean.getGroups()) { + validateGroupDuplicationInGroupDefinition(group, parentGroups); + parentGroups.remove(group.getName()); + } + } + } + /** + * This method is to validate the duplication of groups in the same level and to validate cyclic behaviour of groups + * + * @param groups - cartridge group definition + * @param parentGroups - list of string which holds the parent group names (all parents in the hierarchy) + * @throws RestAPIException - throws the rest api exception when group duplicate or when cyclic behaviour occurs + */ + private static void validateGroupDuplicationInGroup(List<String> groups, List<String> parentGroups) + throws RestAPIException{ + List<String> checkList = new ArrayList<String>(); + for (String group : groups) { + if (!checkList.contains(group)) { + checkList.add(group); + } + else { + if (log.isDebugEnabled()) { + log.debug("duplicate group defined: " + group); + } + throw new RestAPIException("Invalid Service Group definition, duplicate groups defined: " + + group); + } + if (parentGroups.contains(group)) { + if (log.isDebugEnabled()) { + log.debug("cyclic group behaviour identified [group-name]: " + group); + } + throw new RestAPIException("Invalid Service Group definition, cyclic group behaviour identified: " + + group); + } + } + } + }
