Search in sources :

Example 1 with NonNull

use of org.eclipse.jdt.annotation.NonNull in project winery by eclipse.

the class PropertiesResource method getProperties.

/**
 * Gets the defined properties. If no properties are defined, an empty JSON object is returned. If k/v properties
 * are defined, then a JSON is returned. Otherwise an XML is returned.
 *
 * @return Key/Value map in the case of Winery WPD mode - else instance of XML Element in case of non-key/value
 * properties
 */
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
@NonNull
public Response getProperties() {
    TEntityType tempType = RepositoryFactory.getRepository().getTypeForTemplate(this.template);
    WinerysPropertiesDefinition wpd = tempType.getWinerysPropertiesDefinition();
    TEntityTemplate.Properties props = this.template.getProperties();
    if (wpd == null) {
        // These can be null resulting in 200 No Content at the caller
        if (props == null) {
            return Response.ok().entity("{}").type(MediaType.APPLICATION_JSON).build();
        } else {
            @Nullable final Object any = props.getAny();
            if (any == null) {
                LOGGER.debug("XML properties expected, but none found. Returning empty JSON.");
                return Response.ok().entity("{}").type(MediaType.APPLICATION_JSON).build();
            }
            try {
                @ADR(6) String xmlAsString = BackendUtils.getXMLAsString(TEntityTemplate.Properties.class, props, true);
                return Response.ok().entity(xmlAsString).type(MediaType.TEXT_XML).build();
            } catch (Exception e) {
                throw new WebApplicationException(e);
            }
        }
    } else {
        Map<String, String> kvProperties = this.template.getProperties().getKVProperties();
        return Response.ok().entity(kvProperties).type(MediaType.APPLICATION_JSON).build();
    }
}
Also used : TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) TEntityType(org.eclipse.winery.model.tosca.TEntityType) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition) Nullable(org.eclipse.jdt.annotation.Nullable) ADR(io.github.adr.embedded.ADR) NonNull(org.eclipse.jdt.annotation.NonNull)

Example 2 with NonNull

use of org.eclipse.jdt.annotation.NonNull in project winery by eclipse.

the class ReflectionUtil method getFieldName.

/**
 * Checks a field for XmlElement or XmlAttribute annotations
 *
 * @return a pair of name and field
 */
@NonNull
private Pair<String, Field> getFieldName(Field field) {
    XmlAttribute xmlAttribute = field.getAnnotation(XmlAttribute.class);
    XmlElement xmlElement = field.getAnnotation(XmlElement.class);
    String name = field.getName();
    if (Objects.nonNull(xmlAttribute) && !xmlAttribute.name().equals("##default")) {
        name = xmlAttribute.name();
    } else if (Objects.nonNull(xmlElement) && !xmlElement.name().equals("##default")) {
        name = xmlElement.name();
    }
    return Tuples.pair(name, field);
}
Also used : XmlAttribute(javax.xml.bind.annotation.XmlAttribute) XmlElement(javax.xml.bind.annotation.XmlElement) NonNull(org.eclipse.jdt.annotation.NonNull)

Example 3 with NonNull

use of org.eclipse.jdt.annotation.NonNull in project winery by eclipse.

the class Splitting method resolveTopologyTemplate.

/**
 * @param serviceTemplateId
 * @throws SplittingException
 */
public void resolveTopologyTemplate(ServiceTemplateId serviceTemplateId) throws SplittingException, IOException {
    IRepository repository = RepositoryFactory.getRepository();
    @NonNull TServiceTemplate serviceTemplate = repository.getElement(serviceTemplateId);
    @NonNull TTopologyTemplate topologyTemplate = serviceTemplate.getTopologyTemplate();
    List<TRequirement> openRequirements = getOpenRequirements(topologyTemplate);
    for (TRequirement requirement : openRequirements) {
        QName requiredCapTypeQName = getRequiredCapabilityTypeQNameOfRequirement(requirement);
        List<TNodeTemplate> nodesWithMatchingCapability = topologyTemplate.getNodeTemplates().stream().filter(nt -> nt.getCapabilities() != null).filter(nt -> nt.getCapabilities().getCapability().stream().anyMatch(c -> c.getType().equals(requiredCapTypeQName))).collect(Collectors.toList());
        if (!nodesWithMatchingCapability.isEmpty() && nodesWithMatchingCapability.size() == 1) {
            TCapability matchingCapability = nodesWithMatchingCapability.get(0).getCapabilities().getCapability().stream().filter(c -> c.getType().equals(requiredCapTypeQName)).findFirst().get();
            TRelationshipType matchingRelationshipType = getMatchingRelationshipType(requirement, matchingCapability);
            if (matchingRelationshipType != null) {
                addMatchingRelationshipTemplateToTopologyTemplate(topologyTemplate, matchingRelationshipType, requirement, matchingCapability);
            } else {
                throw new SplittingException("No suitable relationship type found for matching");
            }
        }
    }
    repository.setElement(serviceTemplateId, serviceTemplate);
}
Also used : java.util(java.util) RequirementTypeId(org.eclipse.winery.common.ids.definitions.RequirementTypeId) ServiceTemplateId(org.eclipse.winery.common.ids.definitions.ServiceTemplateId) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) CapabilityTypeId(org.eclipse.winery.common.ids.definitions.CapabilityTypeId) Util(org.eclipse.winery.common.Util) Collectors(java.util.stream.Collectors) RepositoryFactory(org.eclipse.winery.repository.backend.RepositoryFactory) RelationshipTypeId(org.eclipse.winery.common.ids.definitions.RelationshipTypeId) org.eclipse.winery.model.tosca(org.eclipse.winery.model.tosca) DASpecification(org.eclipse.winery.repository.driverspecificationandinjection.DASpecification) BackendUtils(org.eclipse.winery.repository.backend.BackendUtils) IRepository(org.eclipse.winery.repository.backend.IRepository) ModelUtilities(org.eclipse.winery.model.tosca.utils.ModelUtilities) DriverInjection(org.eclipse.winery.repository.driverspecificationandinjection.DriverInjection) QName(javax.xml.namespace.QName) NonNull(org.eclipse.jdt.annotation.NonNull) QName(javax.xml.namespace.QName) NonNull(org.eclipse.jdt.annotation.NonNull) IRepository(org.eclipse.winery.repository.backend.IRepository)

Example 4 with NonNull

use of org.eclipse.jdt.annotation.NonNull in project winery by eclipse.

the class Splitting method matchTopologyOfServiceTemplate.

/**
 * Splits the topology template of the given service template. Creates a new service template with "-split" suffix
 * as id. Any existing "-split" service template will be deleted. Matches the split topology template to the cloud
 * providers according to the target labels. Creates a new service template with "-matched" suffix as id. Any
 * existing "-matched" service template will be deleted.
 *
 * @param id of the ServiceTemplate switch should be split and matched to cloud providers
 * @return id of the ServiceTemplate which contains the matched topology
 */
public ServiceTemplateId matchTopologyOfServiceTemplate(ServiceTemplateId id) throws Exception {
    long start = System.currentTimeMillis();
    IRepository repository = RepositoryFactory.getRepository();
    @NonNull TServiceTemplate serviceTemplate = repository.getElement(id);
    @NonNull TTopologyTemplate topologyTemplate = serviceTemplate.getTopologyTemplate();
    /*
        Get all open requirements and the basis type of the required capability type
		Two different basis types are distinguished:
			"Container" which means a hostedOn injection is required
			"Endpoint" which means a connectsTo injection is required
		 */
    Map<TRequirement, String> requirementsAndMatchingBasisCapabilityTypes = getOpenRequirementsAndMatchingBasisCapabilityTypeNames(topologyTemplate);
    // Output check
    for (TRequirement req : requirementsAndMatchingBasisCapabilityTypes.keySet()) {
        System.out.println("open Requirement: " + req.getId());
        System.out.println("matchingbasisType: " + requirementsAndMatchingBasisCapabilityTypes.get(req));
    }
    TTopologyTemplate matchedConnectedTopologyTemplate;
    if (requirementsAndMatchingBasisCapabilityTypes.containsValue("Container")) {
        @NonNull TTopologyTemplate matchedHostsTopologyTemplate = hostMatchingWithDefaultLabelingAndHostSelection(topologyTemplate);
        if (requirementsAndMatchingBasisCapabilityTypes.containsValue("Endpoint")) {
            matchedConnectedTopologyTemplate = connectionMatchingWithDefaultConnectorSelection(matchedHostsTopologyTemplate);
        } else {
            matchedConnectedTopologyTemplate = matchedHostsTopologyTemplate;
        }
    } else if (requirementsAndMatchingBasisCapabilityTypes.containsValue("Endpoint")) {
        matchedConnectedTopologyTemplate = connectionMatchingWithDefaultConnectorSelection(topologyTemplate);
    } else {
        throw new SplittingException("No open Requirements which can be matched");
    }
    TTopologyTemplate daSpecifiedTopology = matchedConnectedTopologyTemplate;
    // Start additional functionality Driver Injection
    if (!DASpecification.getNodeTemplatesWithAbstractDAs(matchedConnectedTopologyTemplate).isEmpty() && DASpecification.getNodeTemplatesWithAbstractDAs(matchedConnectedTopologyTemplate) != null) {
        daSpecifiedTopology = DriverInjection.injectDriver(matchedConnectedTopologyTemplate);
    }
    // End additional functionality Driver Injection
    // create wrapper service template
    ServiceTemplateId matchedServiceTemplateId = new ServiceTemplateId(id.getNamespace().getDecoded(), id.getXmlId().getDecoded() + "-matched", false);
    RepositoryFactory.getRepository().forceDelete(matchedServiceTemplateId);
    RepositoryFactory.getRepository().flagAsExisting(matchedServiceTemplateId);
    repository.flagAsExisting(matchedServiceTemplateId);
    TServiceTemplate matchedServiceTemplate = new TServiceTemplate();
    matchedServiceTemplate.setName(matchedServiceTemplateId.getXmlId().getDecoded());
    matchedServiceTemplate.setId(matchedServiceTemplate.getName());
    matchedServiceTemplate.setTargetNamespace(id.getNamespace().getDecoded());
    matchedServiceTemplate.setTopologyTemplate(daSpecifiedTopology);
    LOGGER.debug("Persisting...");
    repository.setElement(matchedServiceTemplateId, matchedServiceTemplate);
    LOGGER.debug("Persisted.");
    long duration = System.currentTimeMillis() - start;
    LOGGER.debug("Execution Time in millisec: " + duration + "ms");
    return matchedServiceTemplateId;
}
Also used : NonNull(org.eclipse.jdt.annotation.NonNull) IRepository(org.eclipse.winery.repository.backend.IRepository) ServiceTemplateId(org.eclipse.winery.common.ids.definitions.ServiceTemplateId)

Example 5 with NonNull

use of org.eclipse.jdt.annotation.NonNull in project winery by eclipse.

the class GenericImportResource method getFile.

@GET
@Path("{filename}")
public Response getFile(@PathParam("filename") @NonNull final String encodedFileName) {
    Objects.requireNonNull(encodedFileName);
    final Optional<TImport> theImport = ImportUtils.getTheImport((GenericImportId) id);
    if (!theImport.isPresent()) {
        return Response.status(Status.NOT_FOUND).build();
    }
    @Nullable final String location = theImport.get().getLocation();
    @NonNull String fileName = Util.URLdecode(encodedFileName);
    if (!fileName.equals(location)) {
        LOGGER.debug("Filename mismatch %s vs %s", fileName, location);
        return Response.status(Status.NOT_FOUND).build();
    }
    RepositoryFileReference ref = new RepositoryFileReference(this.id, location);
    return RestUtils.returnRepoPath(ref, null);
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) NonNull(org.eclipse.jdt.annotation.NonNull) TImport(org.eclipse.winery.model.tosca.TImport) Nullable(org.eclipse.jdt.annotation.Nullable) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

NonNull (org.eclipse.jdt.annotation.NonNull)11 IOException (java.io.IOException)4 QName (javax.xml.namespace.QName)4 IRepository (org.eclipse.winery.repository.backend.IRepository)3 Nullable (org.eclipse.jdt.annotation.Nullable)2 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)2 ServiceTemplateId (org.eclipse.winery.common.ids.definitions.ServiceTemplateId)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ADR (io.github.adr.embedded.ADR)1 ApiOperation (io.swagger.annotations.ApiOperation)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 MalformedURLException (java.net.MalformedURLException)1 Path (java.nio.file.Path)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Collectors (java.util.stream.Collectors)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1