use of org.eclipse.winery.model.ids.definitions.ServiceTemplateId in project winery by eclipse.
the class Allocation method save.
private List<ServiceTemplateId> save(ServiceTemplateId originalId, List<TopologyWrapper> allocatedTopologies, String suffix) throws IOException, AllocationException {
if (allocatedTopologies.isEmpty()) {
throw new AllocationException("No topologies were created");
}
List<ServiceTemplateId> allocatedIds = new ArrayList<>();
for (int i = 0; i < allocatedTopologies.size(); i++) {
// generate id
ServiceTemplateId allocatedId = new ServiceTemplateId(originalId.getNamespace().getDecoded(), originalId.getXmlId().getDecoded() + "-" + suffix + (i + 1), false);
repository.forceDelete(allocatedId);
repository.flagAsExisting(allocatedId);
// generate and save service template
TServiceTemplate allocated = createServiceTemplate(allocatedTopologies.get(i));
repository.setElement(allocatedId, allocated);
allocatedIds.add(allocatedId);
}
LOGGER.debug("Saved " + allocatedIds.size() + " allocated topologies");
return allocatedIds;
}
use of org.eclipse.winery.model.ids.definitions.ServiceTemplateId in project winery by eclipse.
the class Splitting method composeServiceTemplates.
/**
*/
public ServiceTemplateId composeServiceTemplates(String composedSolutionServiceTemplateID, List<ServiceTemplateId> serviceTemplateIds) throws IOException, SplittingException {
IRepository repository = RepositoryFactory.getRepository();
String solutionNamespace = "http://www.opentosca.org/solutions/";
// create composed service template
ServiceTemplateId composedServiceTemplateId = new ServiceTemplateId(solutionNamespace, composedSolutionServiceTemplateID, false);
repository.forceDelete(composedServiceTemplateId);
repository.flagAsExisting(composedServiceTemplateId);
TServiceTemplate composedServiceTemplate = new TServiceTemplate();
composedServiceTemplate.setName(composedServiceTemplateId.getXmlId().getDecoded());
composedServiceTemplate.setId(composedServiceTemplate.getName());
composedServiceTemplate.setTargetNamespace(solutionNamespace);
TTopologyTemplate composedTopologyTemplate = new TTopologyTemplate();
composedServiceTemplate.setTopologyTemplate(composedTopologyTemplate);
repository.setElement(composedServiceTemplateId, composedServiceTemplate);
// add all node and relationship templates from the solution fragements to the composed topology template
for (ServiceTemplateId id : serviceTemplateIds) {
BackendUtils.mergeTopologyTemplateAinTopologyTemplateB(id, composedServiceTemplateId, repository);
}
composedServiceTemplate = repository.getElement(composedServiceTemplateId);
composedTopologyTemplate = composedServiceTemplate.getTopologyTemplate();
List<TRequirement> openRequirements = getOpenRequirements(composedTopologyTemplate);
for (TRequirement requirement : openRequirements) {
QName requiredCapabilityTypeQName = getRequiredCapabilityTypeQNameOfRequirement(requirement);
TNodeTemplate nodeWithOpenCapability = composedTopologyTemplate.getNodeTemplates().stream().filter(nt -> nt.getCapabilities() != null).filter(nt -> nt.getCapabilities().stream().anyMatch(c -> c.getType().equals(requiredCapabilityTypeQName))).findFirst().orElse(null);
if (nodeWithOpenCapability != null) {
TCapability matchingCapability = nodeWithOpenCapability.getCapabilities().stream().filter(c -> c.getType().equals(requiredCapabilityTypeQName)).findFirst().get();
TRelationshipType matchingRelationshipType = getMatchingRelationshipType(requirement, matchingCapability);
if (matchingRelationshipType != null) {
addMatchingRelationshipTemplateToTopologyTemplate(composedTopologyTemplate, matchingRelationshipType, requirement, matchingCapability);
} else {
throw new SplittingException("No suitable relationship type found for matching");
}
}
}
LOGGER.debug("Persisting...");
repository.setElement(composedServiceTemplateId, composedServiceTemplate);
LOGGER.debug("Persisted.");
return composedServiceTemplateId;
}
use of org.eclipse.winery.model.ids.definitions.ServiceTemplateId in project winery by eclipse.
the class RestUtils method cloneServiceTemplate.
public static ServiceTemplateId cloneServiceTemplate(ServiceTemplateId serviceTemplate, String newName, String artifactName) throws JAXBException, IllegalArgumentException, IOException {
ServiceTemplateId newServiceTemplateId = new ServiceTemplateId(serviceTemplate.getNamespace().getDecoded(), newName, false);
RepositoryFileReference fileRef = new RepositoryFileReference(newServiceTemplateId, "ServiceTemplate.tosca");
TDefinitions defs = new ServiceTemplateResource(serviceTemplate).getDefinitions();
defs.setId(newName + "Definitions");
defs.setName(newName + "Definitions generated from Artifact " + artifactName);
TServiceTemplate oldSTModel = null;
for (TExtensibleElements el : defs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation()) {
if (el instanceof TServiceTemplate) {
oldSTModel = (TServiceTemplate) el;
}
}
if (oldSTModel != null) {
oldSTModel.setId(newName);
oldSTModel.setName(newName + " generated from Artifact " + artifactName);
// remove xaaspackager tags
Collection<TTag> toRemove = new ArrayList<>();
for (TTag tag : oldSTModel.getTags()) {
switch(tag.getName()) {
case "xaasPackageNode":
case "xaasPackageArtifactType":
case "xaasPackageDeploymentArtifact":
toRemove.add(tag);
break;
default:
break;
}
}
oldSTModel.getTags().removeAll(toRemove);
}
JAXBContext context = JAXBContext.newInstance(TDefinitions.class);
Marshaller m = context.createMarshaller();
StringWriter sw = new StringWriter();
m.marshal(defs, sw);
String xmlString = sw.toString();
RepositoryFactory.getRepository().putContentToFile(fileRef, xmlString, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
return newServiceTemplateId;
}
use of org.eclipse.winery.model.ids.definitions.ServiceTemplateId 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.model.ids.definitions.ServiceTemplateId in project winery by eclipse.
the class APIResource method getAllArtifactTemplatesOfContainedDeploymentArtifacts.
@GET
@Path("getallartifacttemplatesofcontaineddeploymentartifacts")
@Produces(MediaType.APPLICATION_JSON)
public Response getAllArtifactTemplatesOfContainedDeploymentArtifacts(@QueryParam("servicetemplate") String serviceTemplateQNameString, @QueryParam("nodetemplateid") String nodeTemplateId) {
if (StringUtils.isEmpty(serviceTemplateQNameString)) {
return Response.status(Status.BAD_REQUEST).entity("servicetemplate has be given as query parameter").build();
}
QName serviceTemplateQName = QName.valueOf(serviceTemplateQNameString);
ServiceTemplateId serviceTemplateId = new ServiceTemplateId(serviceTemplateQName);
final IRepository repository = RepositoryFactory.getRepository();
if (!repository.exists(serviceTemplateId)) {
return Response.status(Status.BAD_REQUEST).entity("service template does not exist").build();
}
ServiceTemplateResource serviceTemplateResource = new ServiceTemplateResource(serviceTemplateId);
Collection<QName> artifactTemplates = new ArrayList<>();
List<TNodeTemplate> allNestedNodeTemplates = BackendUtils.getAllNestedNodeTemplates(serviceTemplateResource.getServiceTemplate());
for (TNodeTemplate nodeTemplate : allNestedNodeTemplates) {
if (StringUtils.isEmpty(nodeTemplateId) || nodeTemplate.getId().equals(nodeTemplateId)) {
Collection<QName> ats = BackendUtils.getArtifactTemplatesOfReferencedDeploymentArtifacts(nodeTemplate, repository);
artifactTemplates.addAll(ats);
}
}
// convert QName list to select2 data
Select2DataWithOptGroups res = new Select2DataWithOptGroups();
for (QName qName : artifactTemplates) {
res.add(qName.getNamespaceURI(), qName.toString(), qName.getLocalPart());
}
return Response.ok().entity(res.asSortedSet()).build();
}
Aggregations