/** * Manage node group members when a node name is removed or its name has changed. * * @param newName : the new name of the node or <code>null</code> if the node has been removed. */ public static void updateGroupMembers(Topology topology, NodeTemplate template, String nodeName, String newName) { Map<String, NodeGroup> topologyGroups = topology.getGroups(); if (template.getGroups() != null && !template.getGroups().isEmpty() && topologyGroups != null) { for (String groupId : template.getGroups()) { NodeGroup nodeGroup = topologyGroups.get(groupId); if (nodeGroup != null && nodeGroup.getMembers() != null) { boolean removed = nodeGroup.getMembers().remove(nodeName); if (removed && newName != null) { nodeGroup.getMembers().add(newName); } } } } }
/** * Get the location ids out of the location groups settings. * * @return map of location group to location id */ public Map<String, String> getLocationIds() { Map<String, String> locationIds = Maps.newHashMap(); for (NodeGroup group : safe(locationGroups).values()) { for (AbstractPolicy policy : safe(group.getPolicies())) { if (policy instanceof LocationPlacementPolicy) { locationIds.put(group.getName(), ((LocationPlacementPolicy) policy).getLocationId()); } } } return locationIds; }
public static int getAvailableGroupIndex(Topology topology) { if (topology == null || topology.getGroups() == null) { return 0; } Collection<NodeGroup> nodeGroups = topology.getGroups().values(); LinkedHashSet<Integer> indexSet = new LinkedHashSet<>(nodeGroups.size()); for (int i = 0; i < nodeGroups.size(); i++) { indexSet.add(i); } for (NodeGroup nodeGroup : nodeGroups) { indexSet.remove(nodeGroup.getIndex()); } if (indexSet.isEmpty()) { return nodeGroups.size(); } return indexSet.iterator().next(); }
private Map<String, List<LocationResourceTemplate>> getAvailableSubstitutions(Topology topology, Map<String, NodeGroup> locationGroups, Map<String, Location> locationByIds, String environmentId) { // Fetch all node types for templates in the topology Map<String, NodeType> nodeTypes = getNodeTypes(topology); Map<String, List<LocationResourceTemplate>> availableSubstitutions = Maps.newHashMap(); // Based on our model nodes may come from various locations actually. for (final Map.Entry<String, NodeGroup> locationGroupEntry : locationGroups.entrySet()) { String groupName = locationGroupEntry.getKey(); final NodeGroup locationNodeGroup = locationGroupEntry.getValue(); Map<String, NodeTemplate> nodesToMatch = Maps.newHashMap(); if (MapUtils.isNotEmpty(topology.getNodeTemplates())) { if (AlienConstants.GROUP_ALL.equals(groupName)) { locationNodeGroup.setMembers(topology.getNodeTemplates().keySet()); nodesToMatch = topology.getNodeTemplates(); } else { nodesToMatch = Maps.filterEntries(topology.getNodeTemplates(), input -> locationNodeGroup.getMembers().contains(input.getKey())); } } availableSubstitutions.putAll(nodeMatcherService.match(nodeTypes, nodesToMatch, locationByIds.get(groupName), environmentId)); } return availableSubstitutions; }
nodeGroup = new NodeGroup(); nodeGroup.setName(operation.getGroupName()); nodeGroup.setIndex(TopologyUtils.getAvailableGroupIndex(topology)); Set<String> members = Sets.newHashSet(); nodeGroup.setMembers(members); List<AbstractPolicy> policies = Lists.newArrayList(); policy.setName("High Availability"); policies.add(policy); nodeGroup.setPolicies(policies); groups.put(operation.getGroupName(), nodeGroup); if (nodeGroup.getMembers().contains(operation.getNodeName())) { throw new AlreadyExistException("Node <" + operation.getNodeName() + "> is already member of group <" + operation.getGroupName() + ">."); nodeGroup.getMembers().add(operation.getNodeName());
@Override public void process(NodeGroup instance) { final ArchiveRoot archiveRoot = (ArchiveRoot) ParsingContextExecution.getRoot().getWrappedInstance(); // ensure that member groups exists and add the group to the nodes. Iterator<String> groupMembers = instance.getMembers().iterator(); while (groupMembers.hasNext()) { String nodeTemplateId = groupMembers.next(); NodeTemplate nodeTemplate = archiveRoot.getTopology().getNodeTemplates().get(nodeTemplateId); if (nodeTemplate == null) { Node node = ParsingContextExecution.getObjectToNodeMap().get(instance); // add an error to the context ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.UNKOWN_GROUP_MEMBER, null, node.getStartMark(), null, node.getEndMark(), nodeTemplateId)); // and remove the member groupMembers.remove(); } else { Set<String> groups = nodeTemplate.getGroups(); if (groups == null) { groups = Sets.newHashSet(); nodeTemplate.setGroups(groups); } groups.add(instance.getName()); } } } }
locationPolicy.setName("Location policy"); Map<String, NodeGroup> groups = configuration.getLocationGroups(); NodeGroup group = new NodeGroup(); group.setName(matchEntry.getKey()); group.setPolicies(Lists.<AbstractPolicy> newArrayList()); group.getPolicies().add(locationPolicy); groups.put(matchEntry.getKey(), group);
@And("^The RestResponse should contain a group named \"([^\"]*)\" whose members are \"([^\"]*)\" and policy is \"([^\"]*)\"$") public void The_RestResponse_should_contain_a_group_named_whose_members_are_and_policy_is(String groupName, String members, String policy) throws Throwable { String topologyResponseText = Context.getInstance().getRestResponse(); RestResponse<TopologyDTO> topologyResponse = JsonUtil.read(topologyResponseText, TopologyDTO.class, Context.getJsonMapper()); Assert.assertNotNull(topologyResponse.getData().getTopology().getGroups()); NodeGroup nodeGroup = topologyResponse.getData().getTopology().getGroups().get(groupName); Set<String> expectedMembers = Sets.newHashSet(members.split(",")); Assert.assertNotNull(nodeGroup); Assert.assertEquals(nodeGroup.getMembers(), expectedMembers); Assert.assertEquals(nodeGroup.getPolicies().iterator().next().getType(), policy); for (String expectedMember : expectedMembers) { NodeTemplate nodeTemplate = topologyResponse.getData().getTopology().getNodeTemplates().get(expectedMember); Assert.assertNotNull(nodeTemplate); Assert.assertTrue(nodeTemplate.getGroups().contains(groupName)); } }
nodeGroup.setIndex(groupIndex++); groupPostProcessor.process(nodeGroup);
throw new NotFoundException("Group with name [" + operation.getGroupName() + "] does not exists and cannot be renamed."); nodeGroup.setName(operation.getNewGroupName()); Map<String, NodeTemplate> nodeTemplates = TopologyUtils.getNodeTemplates(topology); for (NodeTemplate nodeTemplate : nodeTemplates.values()) {
@Override protected void processNodeOperation(Csar csar, Topology topology, RemoveGroupMemberOperation operation, NodeTemplate nodeTemplate) { NodeGroup nodeGroup = topology.getGroups().get(operation.getGroupName()); if (nodeGroup != null && nodeGroup.getMembers() != null) { boolean removed = nodeGroup.getMembers().remove(operation.getNodeName()); if (!removed) { throw new NotFoundException("Node <" + operation.getNodeName() + "> is not part of group <" + operation.getGroupName() + ">."); } } if (nodeTemplate != null && nodeTemplate.getGroups() != null) { nodeTemplate.getGroups().remove(operation.getGroupName()); } } }
/** * Get the location on which a Deployment Topology should be deployed. * * @param deploymentTopology the deployment topology. * @return map of location group to location id */ public static Map<String, String> getLocationIds(DeploymentTopology deploymentTopology) { Map<String, String> locationIds = Maps.newHashMap(); if (MapUtils.isNotEmpty(deploymentTopology.getLocationGroups())) { for (NodeGroup group : deploymentTopology.getLocationGroups().values()) { for (AbstractPolicy policy : group.getPolicies()) { if (policy instanceof LocationPlacementPolicy) { locationIds.put(group.getName(), ((LocationPlacementPolicy) policy).getLocationId()); } } } } return locationIds; } }
Set<String> nodeGroups = Sets.newHashSet(); for (Map.Entry<String, NodeGroup> groupEntry : topology.getGroups().entrySet()) { if (groupEntry.getValue().getMembers() != null && groupEntry.getValue().getMembers().contains(templateEntry.getKey())) { nodeGroups.add(groupEntry.getKey());
Set<String> members = nodeGroup.getMembers(); if (members != null && members.remove(oldName)) { members.add(newName);