use of org.eclipse.winery.repository.rest.resources._support.ResourceResult in project winery by eclipse.
the class ServiceTemplateResource method createParticipantsVersion.
@POST()
@Path("createparticipantsversion")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public List<Response> createParticipantsVersion() throws IOException {
IRepository repo = RepositoryFactory.getRepository();
// create list of responses because we create multiple resources at once
List<Response> listOfResponses = new ArrayList<>();
LOGGER.debug("Creating new participants version of Service Template {}...", this.getId());
ServiceTemplateId id = (ServiceTemplateId) this.getId();
WineryVersion version = VersionUtils.getVersion(id.getXmlId().getDecoded());
TTopologyTemplate topologyTemplate = this.getTopology();
List<TTag> tags = new ArrayList<>();
Splitting splitting = new Splitting();
if (topologyTemplate.getParticipants() != null) {
for (OTParticipant participant : topologyTemplate.getParticipants()) {
// check if tag with partner in service template
WineryVersion newVersion = new WineryVersion(participant.getName() + "-" + version.toString().replace("gdm", "ldm"), 1, 1);
List<OTParticipant> newParticipantList = new ArrayList<>(topologyTemplate.getParticipants());
// new tag to define participant of service template
tags.add(new TTag.Builder("participant", participant.getName()).build());
String choreoValue = splitting.calculateChoreographyTag(this.getServiceTemplate().getTopologyTemplate().getNodeTemplates(), participant.getName());
tags.add(new TTag.Builder("choreography", choreoValue).build());
ServiceTemplateId newId = new ServiceTemplateId(id.getNamespace().getDecoded(), VersionUtils.getNameWithoutVersion(id.getXmlId().getDecoded()) + WineryVersion.WINERY_NAME_FROM_VERSION_SEPARATOR + newVersion.toString(), false);
if (repo.exists(newId)) {
repo.forceDelete(newId);
}
ResourceResult response = RestUtils.duplicate(id, newId);
if (response.getStatus() == Status.CREATED) {
response.setUri(null);
response.setMessage(new QNameApiData(newId));
}
TServiceTemplate tempServiceTempl = repo.getElement(newId);
tempServiceTempl.setTags(tags);
tempServiceTempl.getTopologyTemplate().setParticipants(newParticipantList);
listOfResponses.add(response.getResponse());
// set element to propagate changed tags
repo.setElement(newId, tempServiceTempl);
}
}
return listOfResponses;
}
use of org.eclipse.winery.repository.rest.resources._support.ResourceResult in project winery by eclipse.
the class ServiceTemplateResource method createPlaceholderSubstituteVersion.
@Path("placeholdersubstitution")
@POST()
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response createPlaceholderSubstituteVersion() throws IOException, SplittingException {
TTopologyTemplate originTopologyTemplate = this.getServiceTemplate().getTopologyTemplate();
if (originTopologyTemplate == null) {
return Response.notModified().build();
}
List<TTag> tagsOfServiceTemplate = this.getServiceTemplate().getTags();
List<OTParticipant> participants = originTopologyTemplate.getParticipants();
String participantId = "";
List<TTag> newTagList = new ArrayList<>();
for (TTag tagOfServiceTemplate : tagsOfServiceTemplate) {
if (tagOfServiceTemplate.getName().equals("participant")) {
participantId = tagOfServiceTemplate.getValue();
newTagList.add(tagOfServiceTemplate);
} else if (!tagOfServiceTemplate.getName().equals("choreography")) {
newTagList.add(tagOfServiceTemplate);
}
}
final String finalParticipantId = participantId;
List<String> nodeTemplatesWithNewHost = new ArrayList<>();
for (TNodeTemplate tNodeTemplate : originTopologyTemplate.getNodeTemplates()) {
// Multiple participants can be annotated on one node template
Optional<String> nodeOwners = ModelUtilities.getParticipant(tNodeTemplate);
if (nodeOwners.isPresent() && nodeOwners.get().contains(finalParticipantId)) {
for (TRelationshipTemplate tRelationshipTemplate : ModelUtilities.getIncomingRelationshipTemplates(originTopologyTemplate, tNodeTemplate)) {
nodeTemplatesWithNewHost.add(ModelUtilities.getSourceNodeTemplateOfRelationshipTemplate(originTopologyTemplate, tRelationshipTemplate).getId());
}
}
}
ServiceTemplateId id = (ServiceTemplateId) this.getId();
WineryVersion version = VersionUtils.getVersion(id.getXmlId().getDecoded());
WineryVersion newVersion = new WineryVersion("_substituted_" + version.toString(), 1, 1);
ServiceTemplateId newId = new ServiceTemplateId(id.getNamespace().getDecoded(), VersionUtils.getNameWithoutVersion(id.getXmlId().getDecoded()) + WineryVersion.WINERY_NAME_FROM_VERSION_SEPARATOR + newVersion.toString(), false);
IRepository repo = RepositoryFactory.getRepository();
if (repo.exists(newId)) {
repo.forceDelete(newId);
}
ResourceResult response = RestUtils.duplicate(id, newId);
TServiceTemplate newServiceTemplate = repo.getElement(newId);
newServiceTemplate.setTopologyTemplate(BackendUtils.clone(originTopologyTemplate));
newServiceTemplate.getTopologyTemplate().setParticipants(participants);
Splitting splitting = new Splitting();
Map<String, List<TTopologyTemplate>> resultList = splitting.getHostingInjectionOptions(BackendUtils.clone(newServiceTemplate.getTopologyTemplate()));
for (Map.Entry<String, List<TTopologyTemplate>> entry : resultList.entrySet()) {
Optional<String> nodeOwners = ModelUtilities.getParticipant(newServiceTemplate.getTopologyTemplate().getNodeTemplate(entry.getKey()));
if (nodeOwners.isPresent() && nodeOwners.get().contains(finalParticipantId)) {
if (nodeTemplatesWithNewHost.contains(entry.getKey()) && !resultList.get(entry.getKey()).isEmpty()) {
Map<String, TTopologyTemplate> choiceTopologyTemplate = new LinkedHashMap<>();
choiceTopologyTemplate.put(entry.getKey(), entry.getValue().get(0));
splitting.injectNodeTemplates(newServiceTemplate.getTopologyTemplate(), choiceTopologyTemplate, InjectRemoval.REMOVE_REPLACED);
for (TNodeTemplate injectNodeTemplate : choiceTopologyTemplate.get(entry.getKey()).getNodeTemplates()) {
injectNodeTemplate.getOtherAttributes().put(QNAME_PARTICIPANT, finalParticipantId);
}
}
}
}
String choreoValue = splitting.calculateChoreographyTag(newServiceTemplate.getTopologyTemplate().getNodeTemplates(), participantId);
TTag choreoTag = new TTag();
choreoTag.setName("choreography");
choreoTag.setValue(choreoValue);
newTagList.add(choreoTag);
newServiceTemplate.setTags(newTagList);
repo.setElement(newId, newServiceTemplate);
if (response.getStatus() == Status.CREATED) {
response.setUri(null);
response.setMessage(new QNameApiData(newId));
}
return response.getResponse();
}
use of org.eclipse.winery.repository.rest.resources._support.ResourceResult in project winery by eclipse.
the class RestUtils method freezeVersion.
public static ResourceResult freezeVersion(DefinitionsChildId componentToCommit) {
ResourceResult result = new ResourceResult();
try {
BackendUtils.commit(componentToCommit, "Freeze", RepositoryFactory.getRepository());
result.setStatus(Status.OK);
} catch (Exception e) {
LOGGER.error("Error freezing component", e);
result.setStatus(Status.INTERNAL_SERVER_ERROR);
}
return result;
}
use of org.eclipse.winery.repository.rest.resources._support.ResourceResult in project winery by eclipse.
the class RestUtils method create.
/**
* Generates given TOSCA element and returns appropriate response code <br />
* <p>
* In the case of an existing resource, the other possible return code is 302. This code has no Status constant,
* therefore we use Status.CONFLICT, which is also possible.
*
* @return <ul> <li> <ul> <li>Status.CREATED (201) if the resource has been created,</li> <li>Status.CONFLICT if the
* resource already exists,</li> <li>Status.INTERNAL_SERVER_ERROR (500) if something went wrong</li> </ul> </li>
* <li>URI: the absolute URI of the newly created resource</li> </ul>
*/
public static ResourceResult create(GenericId id, String name) {
ResourceResult res = new ResourceResult();
if (RepositoryFactory.getRepository().exists(id)) {
// res.setStatus(302);
res.setStatus(Status.CONFLICT);
} else {
if (RepositoryFactory.getRepository().flagAsExisting(id)) {
res.setStatus(Status.CREATED);
// @formatter:off
// This method is a generic method
// We cannot return an "absolute" URL as the URL is always
// relative to the caller
// Does not work: String path = Environment.getUrlConfiguration().getRepositoryApiUrl()
// + "/" +
// Utils.getUrlPathForPathInsideRepo(id.getPathInsideRepo());
// We distinguish between two cases: DefinitionsChildId and
// TOSCAelementId
// @formatter:on
String path;
if (id instanceof DefinitionsChildId) {
// here, we return namespace + id, as it is only possible to
// post on the definition child*s* resource to create an
// instance of a definition child
DefinitionsChildId tcId = (DefinitionsChildId) id;
path = tcId.getNamespace().getEncoded() + "/" + tcId.getXmlId().getEncoded() + "/";
// in case the resource additionally supports a name attribute, we set the original name
if ((tcId instanceof ServiceTemplateId) || (tcId instanceof ArtifactTemplateId) || (tcId instanceof PolicyTemplateId)) {
// these three types have an additional name (instead of a pure id)
// we store the name
IHasName resource = (IHasName) AbstractComponentsResource.getComponentInstanceResource(tcId);
resource.setName(name);
}
} else {
assert (id instanceof ToscaElementId);
// We just return the id as we assume that only the parent
// of this id may create sub elements
path = id.getXmlId().getEncoded() + "/";
}
// we have to encode it twice to get correct URIs
path = Util.getUrlPath(path);
URI uri = URI.create(path);
res.setUri(uri);
res.setId(id);
} else {
res.setStatus(Status.INTERNAL_SERVER_ERROR);
}
}
return res;
}
use of org.eclipse.winery.repository.rest.resources._support.ResourceResult in project winery by eclipse.
the class RestUtils method renameAllVersionsOfOneDefinition.
public static Response renameAllVersionsOfOneDefinition(DefinitionsChildId oldId, DefinitionsChildId newId) {
SortedSet<? extends DefinitionsChildId> definitions = WineryVersionUtils.getOtherVersionDefinitionsFromDefinition(oldId, RepositoryFactory.getRepository());
Response finalResponse = null;
for (DefinitionsChildId definition : definitions) {
ResourceResult response = rename(definition, newId);
if (response.getStatus() == Status.INTERNAL_SERVER_ERROR) {
return response.getResponse();
}
if (Objects.isNull(finalResponse) || definition.equals(oldId)) {
finalResponse = response.getResponse();
}
}
return finalResponse;
}
Aggregations