Skip to content

Commit

Permalink
Small improvements of the splitting functionality (#60)
Browse files Browse the repository at this point in the history
Improving the splitting functionality by fixing that by calling injectNodeTemplates by a http call the injected topology is not renaming, because attached plans can't deal with renaming.

Adding to the injected new hosts the provider repository name as target label to ease the identification of the new hosts removing the case sensitive handling of target labels and by adding the target label to a cloned Node Template if it is present.

Improve the communication with clients with exposing headers like `Location` and `Access-Control-Allow-Origin`

Signed-off-by: Karoline Saatkamp <karoline.saatkamp@iaas.uni-stuttgart.de>
  • Loading branch information
saatkamp authored and koppor committed Apr 13, 2017
1 parent 820722a commit 3f5b557
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
Expand Up @@ -727,6 +727,10 @@ public static TNodeTemplate clone(TNodeTemplate nodeTemplate) {
nodeTemplateClone.setProperties(nodeTemplate.getProperties());
nodeTemplateClone.setPropertyConstraints(nodeTemplate.getPropertyConstraints());

if (ModelUtilities.getTargetLabel(nodeTemplate).isPresent()) {
ModelUtilities.setTargetLabel(nodeTemplateClone, ModelUtilities.getTargetLabel(nodeTemplate).get());
}

return nodeTemplateClone;
}

Expand Down
Expand Up @@ -15,6 +15,7 @@

import java.io.IOException;
import java.io.StringReader;
import java.net.URI;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -30,9 +31,11 @@
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand All @@ -58,7 +61,6 @@
import org.eclipse.winery.repository.backend.BackendUtils;
import org.eclipse.winery.repository.backend.Repository;
import org.eclipse.winery.repository.resources.AbstractComponentInstanceWithReferencesResource;
import org.eclipse.winery.repository.resources.AbstractComponentsResource;
import org.eclipse.winery.repository.resources.IHasName;
import org.eclipse.winery.repository.resources._support.dataadapter.InjectorReplaceData;
import org.eclipse.winery.repository.resources._support.dataadapter.InjectorReplaceOptions;
Expand Down Expand Up @@ -196,7 +198,7 @@ public Response getInjectorOptions() {
@Path("injector/replace")
@Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON})
public Response injectNodeTemplates(InjectorReplaceData injectorReplaceData) throws IOException, ParserConfigurationException, SAXException {
public Response injectNodeTemplates(InjectorReplaceData injectorReplaceData, @Context UriInfo uriInfo) throws IOException, ParserConfigurationException, SAXException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = dbf.newDocumentBuilder();

Expand Down Expand Up @@ -227,16 +229,15 @@ public Response injectNodeTemplates(InjectorReplaceData injectorReplaceData) thr
}
TTopologyTemplate tTopologyTemplate = splitting.injectNodeTemplates(this.getServiceTemplate().getTopologyTemplate(), injectorReplaceData.injections);
this.getServiceTemplate().setTopologyTemplate(tTopologyTemplate);
ServiceTemplateId injectedServiceTemplateId = new ServiceTemplateId(id.getNamespace().getDecoded(), id.getXmlId().getDecoded() + "-injected", false);
Repository.INSTANCE.forceDelete(injectedServiceTemplateId);
Repository.INSTANCE.flagAsExisting(injectedServiceTemplateId);
ServiceTemplateResource splitServiceTempateResource = (ServiceTemplateResource) AbstractComponentsResource.getComponentInstaceResource(injectedServiceTemplateId);

splitServiceTempateResource.getServiceTemplate().setTopologyTemplate(tTopologyTemplate);
LOGGER.debug("Persisting...");
splitServiceTempateResource.persist();
this.persist();
LOGGER.debug("Persisted.");
return Utils.getXML(TTopologyTemplate.class, tTopologyTemplate);

//No renaming of the Service Template allowed because of the plans

URI url = uriInfo.getBaseUri().resolve(Utils.getAbsoluteURL(id));
LOGGER.debug("URI of the old and new service template {}", url.toString());
return Response.created(url).build();
}

/**
Expand Down
Expand Up @@ -17,6 +17,7 @@

import javax.xml.namespace.QName;

import org.eclipse.winery.common.ModelUtilities;
import org.eclipse.winery.common.ids.definitions.RequirementTypeId;
import org.eclipse.winery.common.ids.definitions.ServiceTemplateId;
import org.eclipse.winery.model.tosca.TNodeTemplate;
Expand Down Expand Up @@ -55,9 +56,15 @@ public List<TNodeTemplate> getAllNodeTemplatesForLocation(String targetLocation)
// get all contained node templates
.flatMap(id -> {
ServiceTemplateResource serviceTemplateResource = (ServiceTemplateResource) AbstractComponentsResource.getComponentInstaceResource(id);
return serviceTemplateResource.getServiceTemplate().getTopologyTemplate().getNodeTemplateOrRelationshipTemplate().stream()
List<TNodeTemplate> matchedNodeTemplates = serviceTemplateResource.getServiceTemplate().getTopologyTemplate().getNodeTemplateOrRelationshipTemplate().stream()
.filter(t -> t instanceof TNodeTemplate)
.map(TNodeTemplate.class::cast);
.map(TNodeTemplate.class::cast)
.collect(Collectors.toList());

matchedNodeTemplates.stream().forEach(t -> ModelUtilities.setTargetLabel(t, id.getNamespace().getDecoded().replace(NS_NAME_START, "")));

return matchedNodeTemplates.stream();

})
.collect(Collectors.toList());
}
Expand Down
Expand Up @@ -124,7 +124,7 @@ public boolean checkValidTopology (TTopologyTemplate topologyTemplate) {
if (!transitiveAndDirectSuccessors.get(node).isEmpty()) {
for (TNodeTemplate successor : transitiveAndDirectSuccessors.get(node)) {
if (ModelUtilities.getTargetLabel(successor).isPresent() && ModelUtilities.getTargetLabel(node).isPresent()
&& !ModelUtilities.getTargetLabel(node).equals(ModelUtilities.getTargetLabel(successor))) {
&& !ModelUtilities.getTargetLabel(node).get().equalsIgnoreCase(ModelUtilities.getTargetLabel(successor).get())) {
return false;
}
}
Expand Down Expand Up @@ -169,7 +169,7 @@ public TTopologyTemplate split(TTopologyTemplate topologyTemplate) throws Splitt
}

//noinspection OptionalGetWithoutIsPresent
predecessorsTargetLabel.add(targetLabel.get());
predecessorsTargetLabel.add(targetLabel.get().toLowerCase());
}
// If all predecessors have the same target label assign this label to the considered node
if (predecessorsTargetLabel.size() == 1) {
Expand Down Expand Up @@ -200,8 +200,8 @@ public TTopologyTemplate split(TTopologyTemplate topologyTemplate) throws Splitt
* The origin relationships are duplicated
*/
if (sourceElementIncommingRel instanceof TNodeTemplate
&& ((ModelUtilities.getTargetLabel((TNodeTemplate) sourceElementIncommingRel)
.equals(ModelUtilities.getTargetLabel(duplicatedNode))
&& ((ModelUtilities.getTargetLabel((TNodeTemplate) sourceElementIncommingRel).get()
.equalsIgnoreCase(ModelUtilities.getTargetLabel(duplicatedNode).get())
&& incomingRelationship.getType().getLocalPart().toLowerCase().contains("hostedon"))
|| !predecessors.contains(sourceElementIncommingRel))) {

Expand Down Expand Up @@ -294,7 +294,6 @@ public Map<String, List<TNodeTemplate>> getInjectionOptions(TTopologyTemplate to

//Add compatible nodes to the injectionOptions to host the considered lowest level node
if (!compatibleNodeTemplates.isEmpty()) {
compatibleNodeTemplates.stream().forEach(n -> ModelUtilities.setTargetLabel(n,targetLabel));
injectionOptions.put(needHostNode.getId(), compatibleNodeTemplates);
nodesForWhichHostsFound.add(needHostNode);
}
Expand Down Expand Up @@ -326,7 +325,6 @@ public Map<String, List<TNodeTemplate>> getInjectionOptions(TTopologyTemplate to
.getAllNodeTemplatesForLocationAndOfferingCapability(targetLabel, predecessor.getRequirements().getRequirement());
//Add compatible nodes to the injectionOptions to host the considered lowest level node
if (!compatibleNodeTemplates.isEmpty()) {
compatibleNodeTemplates.stream().forEach(n -> ModelUtilities.setTargetLabel(n, targetLabel));
injectionOptions.put(predecessor.getId(), compatibleNodeTemplates);
nodesForWhichHostsFound.add(predecessor);
}
Expand Down Expand Up @@ -419,16 +417,24 @@ public TTopologyTemplate injectNodeTemplates (TTopologyTemplate topologyTemplate
originHostSuccessors.clear();
originHostSuccessors = getHostedOnSuccessorsOfNodeTemplate(topologyTemplate, predecessorOfNewHost);
TNodeTemplate newMatchingNodeTemplate;
TNodeTemplate newHostNodeTemplate = injectNodes.get(predecessorOfNewHostId);

boolean matchingFound = matching.stream()
.anyMatch(nt -> ModelUtilities.getTargetLabel(nt).get().toLowerCase()
.equals(ModelUtilities.getTargetLabel(newHostNodeTemplate).get().toLowerCase())
&& nt.getId().equals(Util.makeNCName(newHostNodeTemplate.getId() + "-" + ModelUtilities.getTargetLabel(newHostNodeTemplate).get())));

//Check if the chosen replace node is already in the matching
if (!matching.contains(injectNodes.get(predecessorOfNewHostId))) {
if (!matchingFound) {
newMatchingNodeTemplate = injectNodes.get(predecessorOfNewHostId);
newMatchingNodeTemplate.setId(Util.makeNCName(newMatchingNodeTemplate.getId() + "-" + ModelUtilities.getTargetLabel(predecessorOfNewHost).get()));
newMatchingNodeTemplate.setName(Util.makeNCName(newMatchingNodeTemplate.getId() + "-" + ModelUtilities.getTargetLabel(predecessorOfNewHost).get()));
newMatchingNodeTemplate.setId(Util.makeNCName(newMatchingNodeTemplate.getId() + "-" + ModelUtilities.getTargetLabel(newMatchingNodeTemplate).get()));
newMatchingNodeTemplate.setName(Util.makeNCName(newMatchingNodeTemplate.getName() + "-" + ModelUtilities.getTargetLabel(newMatchingNodeTemplate).get()));
topologyTemplate.getNodeTemplateOrRelationshipTemplate().add(newMatchingNodeTemplate);
matching.add(newMatchingNodeTemplate);
} else {
newMatchingNodeTemplate = matching.stream().filter(nt -> injectNodes.get(predecessorOfNewHostId).equals(nt)).findAny().get();
newMatchingNodeTemplate = matching.stream().filter(nt -> ModelUtilities.getTargetLabel(nt).get().toLowerCase()
.equals(ModelUtilities.getTargetLabel(newHostNodeTemplate).get().toLowerCase())
&& nt.getId().equals(Util.makeNCName(newHostNodeTemplate.getId() + "-" + ModelUtilities.getTargetLabel(newHostNodeTemplate).get()))).findAny().get();
}

//In case the predecessor was a lowest node a new hostedOn relationship has to be added
Expand Down
4 changes: 4 additions & 0 deletions org.eclipse.winery.repository/src/main/webapp/WEB-INF/web.xml
Expand Up @@ -26,6 +26,10 @@
<param-name>cors.allowed.methods</param-name>
<param-value>GET,PUT,POST,DELETE,HEAD,OPTIONS</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Location, Access-Control-Allow-Origin</param-value>
</init-param>
<init-param>
<param-name>cors.preflight.maxage</param-name>
<param-value>10</param-value>
Expand Down

0 comments on commit 3f5b557

Please sign in to comment.