Search in sources :

Example 16 with IRepository

use of org.eclipse.winery.repository.backend.IRepository in project winery by eclipse.

the class ModelUtilitiesTest method instantiateNodeTemplate.

// endregion
@Test
public void instantiateNodeTemplate() throws Exception {
    this.setRevisionTo("origin/plain");
    IRepository repository = RepositoryFactory.getRepository();
    TNodeType nodeType = repository.getElement(new NodeTypeId(QName.valueOf("{http://opentosca.org/add/management/to/instances/nodetypes}Ubuntu_16.04-w1")));
    TNodeTemplate nodeTemplate = ModelUtilities.instantiateNodeTemplate(nodeType);
    assertNotNull(nodeTemplate);
    assertNotNull(nodeTemplate.getProperties());
    assertEquals(8, ((TEntityTemplate.WineryKVProperties) nodeTemplate.getProperties()).getKVProperties().size());
}
Also used : NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) IRepository(org.eclipse.winery.repository.backend.IRepository) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) TNodeType(org.eclipse.winery.model.tosca.TNodeType) Test(org.junit.jupiter.api.Test)

Example 17 with IRepository

use of org.eclipse.winery.repository.backend.IRepository in project winery by eclipse.

the class RestUtils method releaseVersion.

public static Response releaseVersion(DefinitionsChildId releasableComponent) {
    ResourceResult result = new ResourceResult();
    final IRepository repository = RepositoryFactory.getRepository();
    WineryVersion version = WineryVersionUtils.getCurrentVersionWithAllFlags(releasableComponent, repository);
    if (version.isReleasable()) {
        if (repository instanceof GitBasedRepository) {
            try {
                freezeVersion(releasableComponent);
                version.setWorkInProgressVersion(0);
                String newId = releasableComponent.getNameWithoutVersion() + WineryVersion.WINERY_NAME_FROM_VERSION_SEPARATOR + version;
                DefinitionsChildId newComponent = BackendUtils.getDefinitionsChildId(releasableComponent.getClass(), releasableComponent.getNamespace().getDecoded(), newId, false);
                result = duplicate(releasableComponent, newComponent);
                BackendUtils.commit(newComponent, "Release", repository);
            } catch (GitAPIException e) {
                result.setStatus(Status.INTERNAL_SERVER_ERROR);
            }
        } else {
            result.setStatus(Status.INTERNAL_SERVER_ERROR);
        }
    } else {
        result.setStatus(Status.BAD_REQUEST);
    }
    return result.getResponse();
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) ResourceResult(org.eclipse.winery.repository.rest.resources._support.ResourceResult) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) IRepository(org.eclipse.winery.repository.backend.IRepository) GitBasedRepository(org.eclipse.winery.repository.backend.filebased.GitBasedRepository) WineryVersion(org.eclipse.winery.common.version.WineryVersion)

Example 18 with IRepository

use of org.eclipse.winery.repository.backend.IRepository in project winery by eclipse.

the class RestUtils method rename.

public static ResourceResult rename(DefinitionsChildId oldId, DefinitionsChildId newId) {
    ResourceResult result = new ResourceResult();
    IRepository repo = RepositoryFactory.getRepository();
    WineryVersion version = oldId.getVersion();
    DefinitionsChildId id = newId;
    if (version.toString().length() > 0) {
        // ensure that the version isn't changed by the user
        String componentName = newId.getNameWithoutVersion() + WineryVersion.WINERY_NAME_FROM_VERSION_SEPARATOR + version;
        id = BackendUtils.getDefinitionsChildId(oldId.getClass(), newId.getNamespace().getDecoded(), componentName, false);
    }
    // If a definition was not committed yet, it is renamed, otherwise duplicate the definition.
    if (repo.hasChangesInFile(oldId)) {
        try {
            repo.rename(oldId, id);
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
            result.setStatus(Status.INTERNAL_SERVER_ERROR);
            result.setMessage(e.getMessage());
            return result;
        }
    } else {
        result = duplicate(oldId, id);
        if (result.isSuccess()) {
            result = freezeVersion(id);
        }
    }
    URI uri = RestUtils.getAbsoluteURI(id);
    result.setUri(uri);
    result.setStatus(Status.CREATED);
    return result;
}
Also used : ResourceResult(org.eclipse.winery.repository.rest.resources._support.ResourceResult) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) IOException(java.io.IOException) IRepository(org.eclipse.winery.repository.backend.IRepository) URI(java.net.URI) WineryVersion(org.eclipse.winery.common.version.WineryVersion)

Example 19 with IRepository

use of org.eclipse.winery.repository.backend.IRepository in project winery by eclipse.

the class ServiceTemplateResource method createNewPlaceholderVersion.

@POST()
@Path("createplaceholderversion")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response createNewPlaceholderVersion() throws IOException {
    LOGGER.debug("Creating new placeholder version of Service Template {}...", this.getId());
    ServiceTemplateId id = (ServiceTemplateId) this.getId();
    WineryVersion version = VersionUtils.getVersion(id.getXmlId().getDecoded());
    WineryVersion newVersion = new WineryVersion("gdm-" + version.toString(), 1, 1);
    IRepository repository = RepositoryFactory.getRepository();
    ServiceTemplateId newId = new ServiceTemplateId(id.getNamespace().getDecoded(), VersionUtils.getNameWithoutVersion(id.getXmlId().getDecoded()) + WineryVersion.WINERY_NAME_FROM_VERSION_SEPARATOR + newVersion.toString(), false);
    if (repository.exists(newId)) {
        repository.forceDelete(newId);
    }
    ResourceResult response = RestUtils.duplicate(id, newId);
    if (response.getStatus() == Status.CREATED) {
        response.setUri(null);
        response.setMessage(new QNameApiData(newId));
    }
    LOGGER.debug("Created Service Template {}", newId.getQName());
    return response.getResponse();
}
Also used : ResourceResult(org.eclipse.winery.repository.rest.resources._support.ResourceResult) QNameApiData(org.eclipse.winery.repository.rest.resources.apiData.QNameApiData) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) IRepository(org.eclipse.winery.repository.backend.IRepository) WineryVersion(org.eclipse.winery.common.version.WineryVersion) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 20 with IRepository

use of org.eclipse.winery.repository.backend.IRepository in project winery by eclipse.

the class ServiceTemplateResource method generatePlaceholdersWithCapability.

@POST
@Path("placeholder/generator")
@Consumes({ MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
public Response generatePlaceholdersWithCapability() {
    Splitting splitting = new Splitting();
    TTopologyTemplate topologyTemplate = this.getServiceTemplate().getTopologyTemplate();
    if (topologyTemplate == null) {
        return Response.notModified().build();
    }
    try {
        // get all open requirements and the respective node templates with open requirements
        Map<TRequirement, TNodeTemplate> requirementsAndItsNodeTemplates = splitting.getOpenRequirementsAndItsNodeTemplate(topologyTemplate);
        IRepository repo = RepositoryFactory.getRepository();
        // iterate over all open requirements
        for (Map.Entry<TRequirement, TNodeTemplate> entry : requirementsAndItsNodeTemplates.entrySet()) {
            List<PropertyDefinitionKV> propertyDefinitionKVList = new ArrayList<>();
            LinkedHashMap<String, String> placeholderNodeTemplateProperties = new LinkedHashMap<>();
            // current node template with open requirements
            TNodeTemplate nodeTemplateWithOpenReq = entry.getValue();
            // get type of node template with open requirements
            NodeTypeId id = new NodeTypeId(nodeTemplateWithOpenReq.getType());
            TNodeType sourceNodeType = repo.getElement(id);
            List<TInterface> sourceNodeTypeInterfaces = sourceNodeType.getInterfaces();
            if (sourceNodeTypeInterfaces != null) {
                for (TInterface tInterface : sourceNodeTypeInterfaces) {
                    // TODO: make this more safe
                    for (TOperation tOperation : tInterface.getOperations()) {
                        List<TParameter> inputParameters = tOperation.getInputParameters();
                        if (inputParameters != null) {
                            for (TParameter inputParameter : inputParameters) {
                                generateInputParameters(propertyDefinitionKVList, placeholderNodeTemplateProperties, sourceNodeType, inputParameter);
                            }
                        }
                    }
                }
            }
            List<TRelationshipTemplate> incomingRelationshipTemplates = ModelUtilities.getIncomingRelationshipTemplates(topologyTemplate, nodeTemplateWithOpenReq);
            List<TParameter> inputParameters = splitting.getInputParamListofIncomingRelationshipTemplates(topologyTemplate, incomingRelationshipTemplates);
            for (TParameter inputParameter : inputParameters) {
                String prefixTARGET = "TARGET_";
                String prefixSOURCE = "SOURCE_";
                String inputParamName = inputParameter.getName();
                if (inputParamName.contains(prefixTARGET)) {
                    inputParamName = inputParamName.replaceAll(prefixTARGET, "");
                }
                if (inputParamName.contains(prefixSOURCE)) {
                    inputParamName = inputParamName.replaceAll(prefixSOURCE, "");
                }
                inputParameter.setName(inputParamName);
                generateInputParameters(propertyDefinitionKVList, placeholderNodeTemplateProperties, sourceNodeType, inputParameter);
            }
            // get required capability type of open requirement
            QName capabilityType = splitting.getRequiredCapabilityTypeQNameOfRequirement(entry.getKey());
            // create new placeholder node type
            TNodeType placeholderNodeType = splitting.createPlaceholderNodeType(nodeTemplateWithOpenReq.getName());
            QName placeholderQName = new QName(placeholderNodeType.getTargetNamespace(), placeholderNodeType.getName());
            WinerysPropertiesDefinition winerysPropertiesDefinition = sourceNodeType.getWinerysPropertiesDefinition();
            // add properties definition
            placeholderNodeType.setProperties(null);
            if (winerysPropertiesDefinition != null) {
                winerysPropertiesDefinition.setPropertyDefinitions(propertyDefinitionKVList);
                placeholderNodeType.setProperties(winerysPropertiesDefinition);
                String namespace = placeholderNodeType.getWinerysPropertiesDefinition().getNamespace();
                NamespaceManager namespaceManager = RepositoryFactory.getRepository().getNamespaceManager();
                if (!namespaceManager.hasPermanentProperties(namespace)) {
                    namespaceManager.addPermanentNamespace(namespace);
                }
            }
            NodeTypeId placeholderId = new NodeTypeId(placeholderQName);
            // check if placeholder node type exists
            if (repo.exists(placeholderId)) {
                // delete and create new
                RestUtils.delete(placeholderId);
            }
            repo.setElement(placeholderId, placeholderNodeType);
            // create placeholder node template
            TNodeTemplate placeholderNodeTemplate = splitting.createPlaceholderNodeTemplate(topologyTemplate, nodeTemplateWithOpenReq.getName(), placeholderQName);
            // create capability of placeholder node template
            TCapability capa = splitting.createPlaceholderCapability(topologyTemplate, capabilityType);
            ModelUtilities.setPropertiesKV(placeholderNodeTemplate, placeholderNodeTemplateProperties);
            if (placeholderNodeTemplate.getCapabilities() == null) {
                placeholderNodeTemplate.setCapabilities(new ArrayList<>());
            }
            placeholderNodeTemplate.getCapabilities().add(capa);
            for (Map.Entry<QName, String> targetLocation : nodeTemplateWithOpenReq.getOtherAttributes().entrySet()) {
                placeholderNodeTemplate.getOtherAttributes().put(targetLocation.getKey(), targetLocation.getValue());
            }
            // add placeholder to node template and connect with source node template with open requirements
            topologyTemplate.addNodeTemplate(placeholderNodeTemplate);
            ModelUtilities.createRelationshipTemplateAndAddToTopology(nodeTemplateWithOpenReq, placeholderNodeTemplate, ToscaBaseTypes.hostedOnRelationshipType, topologyTemplate);
        }
        LOGGER.debug("PERSISTING");
        RestUtils.persist(this);
        LOGGER.debug("PERSISTED");
        String responseId = this.getServiceTemplate().getId();
        return Response.ok().entity(responseId).build();
    } catch (Exception e) {
        LOGGER.error("Could not fetch requirements and capabilities", e);
        return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
    }
}
Also used : NamespaceManager(org.eclipse.winery.repository.backend.NamespaceManager) PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) TInterface(org.eclipse.winery.model.tosca.TInterface) ArrayList(java.util.ArrayList) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) LinkedHashMap(java.util.LinkedHashMap) TRequirement(org.eclipse.winery.model.tosca.TRequirement) TOperation(org.eclipse.winery.model.tosca.TOperation) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) QName(javax.xml.namespace.QName) TCapability(org.eclipse.winery.model.tosca.TCapability) SplittingException(org.eclipse.winery.repository.splitting.SplittingException) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) TNodeType(org.eclipse.winery.model.tosca.TNodeType) Splitting(org.eclipse.winery.repository.splitting.Splitting) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) IRepository(org.eclipse.winery.repository.backend.IRepository) TParameter(org.eclipse.winery.model.tosca.TParameter) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Aggregations

IRepository (org.eclipse.winery.repository.backend.IRepository)75 TNodeType (org.eclipse.winery.model.tosca.TNodeType)29 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)28 NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)27 IOException (java.io.IOException)24 QName (javax.xml.namespace.QName)24 TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)22 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)21 ArrayList (java.util.ArrayList)20 Map (java.util.Map)20 HashMap (java.util.HashMap)15 LoggerFactory (org.slf4j.LoggerFactory)15 LinkedHashMap (java.util.LinkedHashMap)14 List (java.util.List)14 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)14 RepositoryFactory (org.eclipse.winery.repository.backend.RepositoryFactory)14 Collectors (java.util.stream.Collectors)13 POST (javax.ws.rs.POST)13 Path (javax.ws.rs.Path)13 Produces (javax.ws.rs.Produces)13