Search in sources :

Example 1 with IncompatibleHalfRelationshipException

use of org.alien4cloud.alm.service.exceptions.IncompatibleHalfRelationshipException in project alien4cloud by alien4cloud.

the class ServiceResourceService method validateRelationshipTypes.

private void validateRelationshipTypes(ServiceResource serviceResource, final NodeType nodeType) {
    safe(serviceResource.getCapabilitiesRelationshipTypes()).forEach((capabilityName, relationshipTypeId) -> {
        RelationshipType relationshipType = toscaTypeSearchService.findByIdOrFail(RelationshipType.class, relationshipTypeId);
        String[] validTargets = relationshipType.getValidTargets();
        if (ArrayUtils.isNotEmpty(validTargets)) {
            CapabilityDefinition capabilityDefinition = nodeType.getCapabilities().stream().filter(c -> c.getId().equals(capabilityName)).findFirst().get();
            Csar csar = toscaTypeSearchService.getArchive(nodeType.getArchiveName(), nodeType.getArchiveVersion());
            Set<CSARDependency> allDependencies = new HashSet<>(safe(csar.getDependencies()));
            allDependencies.add(new CSARDependency(csar.getName(), csar.getVersion(), csar.getHash()));
            CapabilityType capabilityType = toscaTypeSearchService.getElementInDependencies(CapabilityType.class, capabilityDefinition.getType(), allDependencies);
            Set<String> allAcceptedTypes = new HashSet<>();
            allAcceptedTypes.addAll(capabilityType.getDerivedFrom());
            allAcceptedTypes.add(capabilityType.getElementId());
            boolean isValid = false;
            for (String validTarget : validTargets) {
                if (allAcceptedTypes.contains(validTarget)) {
                    isValid = true;
                    break;
                }
            }
            if (!isValid) {
                throw new IncompatibleHalfRelationshipException("[" + relationshipType.getId() + "] is not compatible with [" + capabilityType.getId() + "]");
            }
        }
    });
    safe(serviceResource.getRequirementsRelationshipTypes()).forEach((k, v) -> {
        toscaTypeSearchService.findByIdOrFail(RelationshipType.class, v);
    });
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) CapabilityDefinition(org.alien4cloud.tosca.model.definitions.CapabilityDefinition) IncompatibleHalfRelationshipException(org.alien4cloud.alm.service.exceptions.IncompatibleHalfRelationshipException) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) HashSet(java.util.HashSet)

Aggregations

HashSet (java.util.HashSet)1 IncompatibleHalfRelationshipException (org.alien4cloud.alm.service.exceptions.IncompatibleHalfRelationshipException)1 CSARDependency (org.alien4cloud.tosca.model.CSARDependency)1 Csar (org.alien4cloud.tosca.model.Csar)1 CapabilityDefinition (org.alien4cloud.tosca.model.definitions.CapabilityDefinition)1 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)1 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)1