Search in sources :

Example 21 with TEntityTemplate

use of org.eclipse.winery.model.tosca.TEntityTemplate in project winery by eclipse.

the class PropertyMappingsResource method onPost.

@ApiOperation(value = "Creates or updates a property mapping with the given fields")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public // @formatter:off
Response onPost(PropertyMapping apiPropertyMapping) {
    // @formatter:on
    if (StringUtils.isEmpty(apiPropertyMapping.serviceTemplatePropertyRef)) {
        return Response.status(Status.BAD_REQUEST).entity("serviceTemplatePropertyRef must not be empty").build();
    }
    if (StringUtils.isEmpty(apiPropertyMapping.targetObjectRef)) {
        return Response.status(Status.BAD_REQUEST).entity("targetObjectRef must not be empty").build();
    }
    if (StringUtils.isEmpty(apiPropertyMapping.targetPropertyRef)) {
        return Response.status(Status.BAD_REQUEST).entity("targetPropertyRef must not be empty").build();
    }
    TEntityTemplate template = ModelUtilities.findNodeTemplateOrRequirementOfNodeTemplateOrCapabilityOfNodeTemplateOrRelationshipTemplate(this.res.getServiceTemplate().getTopologyTemplate(), apiPropertyMapping.targetObjectRef);
    if (template == null) {
        return Response.status(Status.BAD_REQUEST).entity("targetObjectRef " + apiPropertyMapping.targetObjectRef + " could not be resolved.").build();
    }
    // replace propertyMapping if it exists
    for (TPropertyMapping propertyMapping : this.propertyMappings) {
        if (propertyMapping.getServiceTemplatePropertyRef().equals(apiPropertyMapping.serviceTemplatePropertyRef)) {
            // we found a property with the same mapping
            // just update it ...
            this.updatePropertyMapping(propertyMapping, apiPropertyMapping.serviceTemplatePropertyRef, template, apiPropertyMapping.targetPropertyRef);
            // ... and finish processing
            return RestUtils.persist(this.res);
        }
    }
    // the property mapping didn't exist,
    // we create a new one
    this.propertyMappings.add(new TPropertyMapping(apiPropertyMapping.serviceTemplatePropertyRef, template, apiPropertyMapping.targetPropertyRef));
    return RestUtils.persist(this.res);
}
Also used : TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) TPropertyMapping(org.eclipse.winery.model.tosca.TPropertyMapping) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation)

Example 22 with TEntityTemplate

use of org.eclipse.winery.model.tosca.TEntityTemplate in project winery by eclipse.

the class PermutationMappingsResource method addPermutationMapping.

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public List<OTPermutationMapping> addPermutationMapping(PrmPermutationMappingApiData mapping) {
    TEntityTemplate detectorElement = this.res.getDetectorResource().getTopologyTemplate().getNodeTemplate(mapping.detectorElement);
    TEntityTemplate refinementElement = this.res.getRefinementTopologyResource().getTopologyTemplate().getNodeTemplate(mapping.refinementElement);
    return this.addMapping(mapping.createPermutationMapping(detectorElement, refinementElement));
}
Also used : TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 23 with TEntityTemplate

use of org.eclipse.winery.model.tosca.TEntityTemplate in project winery by eclipse.

the class StayMappingsResource method addPropertyMappingFromApi.

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public List<OTStayMapping> addPropertyMappingFromApi(PrmStayMappingApiData mapping) {
    TEntityTemplate detectorElement = this.res.getDetectorResource().getTopologyTemplate().getNodeTemplateOrRelationshipTemplate().stream().filter(element -> element.getId().equals(mapping.detectorElement)).findFirst().orElseThrow(IllegalArgumentException::new);
    TEntityTemplate refinementElement = this.res.getRefinementTopologyResource().getTopologyTemplate().getNodeTemplateOrRelationshipTemplate().stream().filter(element -> element.getId().equals(mapping.refinementElement)).findFirst().orElseThrow(IllegalArgumentException::new);
    return this.addMapping(mapping.createOTPrmStayMapping(detectorElement, refinementElement));
}
Also used : TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 24 with TEntityTemplate

use of org.eclipse.winery.model.tosca.TEntityTemplate in project winery by eclipse.

the class BehaviorPatternMappingsResource method addPropertyMappingFromApi.

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public List<OTBehaviorPatternMapping> addPropertyMappingFromApi(PrmBehaviorPatternMappingApiData mapping) {
    TEntityTemplate detectorElement = this.res.getDetectorResource().getTopologyTemplate().getNodeTemplateOrRelationshipTemplate().stream().filter(entityTemplate -> entityTemplate.getId().equals(mapping.detectorElement)).findFirst().orElse(null);
    TEntityTemplate refinementElement = this.res.getRefinementTopologyResource().getTopologyTemplate().getNodeTemplateOrRelationshipTemplate().stream().filter(entityTemplate -> entityTemplate.getId().equals(mapping.refinementElement)).findFirst().orElse(null);
    return this.addMapping(mapping.createBehaviorPatternMapping(detectorElement, mapping.behaviorPattern, refinementElement));
}
Also used : TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 25 with TEntityTemplate

use of org.eclipse.winery.model.tosca.TEntityTemplate in project winery by eclipse.

the class ModelUtilities method patchAnyAttributes.

/**
 * When sending JSON to the server, the content of "any" is a String and not some JSON data structure. To be able to
 * save it as XML, we have to "objectize" the content of Any
 *
 * @param templates The templates (node, relationship) to update. The content of the given collection is modified.
 * @throws IllegalStateException if DocumentBuilder could not initialize
 * @throws IOException           if something goes wrong during parsing
 */
public static void patchAnyAttributes(Collection<? extends TEntityTemplate> templates) throws IOException {
    Map<QName, String> tempConvertedOtherAttributes = new HashMap<>();
    for (TEntityTemplate template : templates) {
        // Convert the wrong QName created by the JSON serialization back to a right QName
        for (Map.Entry<QName, String> otherAttribute : template.getOtherAttributes().entrySet()) {
            QName qName;
            String localPart = otherAttribute.getKey().getLocalPart();
            if (localPart.startsWith("{")) {
                // QName is stored as plain string - this is the case when nested in "any"
                qName = QName.valueOf(localPart);
            } else {
                // sometimes, the QName is retrieved properly. So, we just keep it.
                // This is the case when directly nested in nodetemplate JSON.
                qName = new QName(otherAttribute.getKey().getNamespaceURI(), localPart);
            }
            tempConvertedOtherAttributes.put(qName, otherAttribute.getValue());
        }
        template.getOtherAttributes().clear();
        template.getOtherAttributes().putAll(tempConvertedOtherAttributes);
        tempConvertedOtherAttributes.clear();
        // Convert the String created by the JSON serialization back to an XML dom document
        TEntityTemplate.Properties properties = template.getProperties();
        if (properties instanceof TEntityTemplate.XmlProperties) {
            TEntityTemplate.XmlProperties props = (TEntityTemplate.XmlProperties) properties;
            props.setAny(patchAnyItem(props.getAny()));
        }
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) QName(javax.xml.namespace.QName) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

TEntityTemplate (org.eclipse.winery.model.tosca.TEntityTemplate)42 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)19 TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)14 QName (javax.xml.namespace.QName)13 ArrayList (java.util.ArrayList)9 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)9 Map (java.util.Map)8 HashSet (java.util.HashSet)7 List (java.util.List)7 Consumes (javax.ws.rs.Consumes)7 Produces (javax.ws.rs.Produces)7 TDeploymentArtifact (org.eclipse.winery.model.tosca.TDeploymentArtifact)7 TPolicy (org.eclipse.winery.model.tosca.TPolicy)7 PUT (javax.ws.rs.PUT)6 OTPatternRefinementModel (org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel)6 TEntityType (org.eclipse.winery.model.tosca.TEntityType)5 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 Objects (java.util.Objects)4 Collectors (java.util.stream.Collectors)4