Search in sources :

Example 6 with AbstractInheritableToscaType

use of org.alien4cloud.tosca.model.types.AbstractInheritableToscaType in project alien4cloud by alien4cloud.

the class TopologyAbstractRelationshipValidationService method getTaskListFromMapArray.

/**
 * Constructs a TopologyTask list given a Map (node template name => component) and the code
 */
private <T extends AbstractInheritableToscaType> List<AbstractRelationshipTask> getTaskListFromMapArray(Map<String, T[]> components, TaskCode taskCode) {
    List<AbstractRelationshipTask> taskList = Lists.newArrayList();
    for (Map.Entry<String, T[]> entry : components.entrySet()) {
        for (AbstractInheritableToscaType compo : entry.getValue()) {
            AbstractRelationshipTask task = new AbstractRelationshipTask();
            task.setNodeTemplateName(entry.getKey());
            task.setComponent(compo);
            task.setCode(taskCode);
            taskList.add(task);
        }
    }
    if (taskList.isEmpty()) {
        return null;
    } else {
        return taskList;
    }
}
Also used : AbstractInheritableToscaType(org.alien4cloud.tosca.model.types.AbstractInheritableToscaType) AbstractRelationshipTask(alien4cloud.topology.task.AbstractRelationshipTask) Map(java.util.Map)

Example 7 with AbstractInheritableToscaType

use of org.alien4cloud.tosca.model.types.AbstractInheritableToscaType in project alien4cloud by alien4cloud.

the class ToscaTypeIndexerService method indexInheritableElement.

@Override
@ToscaContextual
public void indexInheritableElement(String archiveName, String archiveVersion, AbstractInheritableToscaType element, Collection<CSARDependency> dependencies) {
    if (CollectionUtils.isNotEmpty(element.getDerivedFrom())) {
        boolean deriveFromSimpleType = false;
        String parentId = element.getDerivedFrom().get(0);
        if (element.getDerivedFrom().size() == 1 && ToscaTypes.isSimple(parentId)) {
            deriveFromSimpleType = true;
        }
        if (!deriveFromSimpleType) {
            AbstractInheritableToscaType superElement = ToscaContext.getOrFail(element.getClass(), parentId);
            IndexedModelUtils.mergeInheritableIndex(superElement, element);
        }
    }
    alienDAO.save(element);
    refreshIndexForSearching();
}
Also used : AbstractInheritableToscaType(org.alien4cloud.tosca.model.types.AbstractInheritableToscaType) ToscaContextual(alien4cloud.tosca.context.ToscaContextual)

Example 8 with AbstractInheritableToscaType

use of org.alien4cloud.tosca.model.types.AbstractInheritableToscaType in project alien4cloud by alien4cloud.

the class IndexedModelUtils method orderByDerivedFromHierarchy.

/**
 * This utility method returns an ordered {@link AbstractInheritableToscaType} collection. The parent elements will be before
 * the children elements
 * This utility method returns an ordered {@link AbstractInheritableToscaType} collection. The parent elements will be before the children elements
 *
 * @param elementsByIdMap map of {@link AbstractInheritableToscaType} by id
 * @return
 */
public static <T extends AbstractInheritableToscaType> List<T> orderByDerivedFromHierarchy(final Map<String, T> elementsByIdMap) {
    if (elementsByIdMap == null) {
        return null;
    }
    List<T> orderedElements = new ArrayList<T>(elementsByIdMap.values());
    final Map<String, Integer> elementsLevelMap = Maps.newHashMap();
    for (AbstractInheritableToscaType element : orderedElements) {
        AbstractInheritableToscaType parent = element;
        int levelCount = 0;
        while (true) {
            if (parent.getDerivedFrom() == null || parent.getDerivedFrom().isEmpty()) {
                break;
            }
            AbstractInheritableToscaType oldParent = parent;
            parent = elementsByIdMap.get(parent.getDerivedFrom().get(0));
            if (parent == null) {
                break;
            }
            if (oldParent.equals(parent)) {
                // This error must have been normally detected in the validation phase and so here it means that it's a bug in our code of validation
                throw new IndexingServiceException(parent.getElementId() + " is parent of it-self, bug in csar validation service");
            }
            levelCount++;
        }
        elementsLevelMap.put(element.getElementId(), levelCount);
    }
    Collections.sort(orderedElements, (left, right) -> elementsLevelMap.get(left.getElementId()).compareTo(elementsLevelMap.get(right.getElementId())));
    return orderedElements;
}
Also used : IndexingServiceException(alien4cloud.exception.IndexingServiceException) ArrayList(java.util.ArrayList) AbstractInheritableToscaType(org.alien4cloud.tosca.model.types.AbstractInheritableToscaType)

Example 9 with AbstractInheritableToscaType

use of org.alien4cloud.tosca.model.types.AbstractInheritableToscaType in project alien4cloud by alien4cloud.

the class DerivedFromPostProcessor method process.

private void process(Map<AbstractInheritableToscaType, String> processed, Map<AbstractInheritableToscaType, String> processing, AbstractInheritableToscaType instance, Map<String, ? extends AbstractInheritableToscaType> instances) {
    if (processed.containsKey(instance)) {
        // Already processed
        return;
    }
    if (processing.containsKey(instance)) {
        // Cyclic dependency as parent is currently being processed...
        Node node = ParsingContextExecution.getObjectToNodeMap().get(instance);
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.CYCLIC_DERIVED_FROM, "Cyclic derived from has been detected", node.getStartMark(), "The type specified as parent or one of it's parent type refers the current type as parent, invalid cycle detected.", node.getEndMark(), instance.getElementId()));
        processing.remove(instance);
        return;
    }
    List<String> derivedFrom = instance.getDerivedFrom();
    if (derivedFrom != null && derivedFrom.size() > 1) {
        // The type has been already processed.
        return;
    }
    if (derivedFrom == null || derivedFrom.isEmpty()) {
        // If the user forgot to derive from Root, automatically do it but make an alert
        String defaultDerivedFrom = null;
        if (instance instanceof NodeType && !NormativeTypesConstant.ROOT_NODE_TYPE.equals(instance.getElementId())) {
            defaultDerivedFrom = NormativeTypesConstant.ROOT_NODE_TYPE;
        } else if (instance instanceof RelationshipType && !NormativeTypesConstant.ROOT_RELATIONSHIP_TYPE.equals(instance.getElementId())) {
            defaultDerivedFrom = NormativeTypesConstant.ROOT_RELATIONSHIP_TYPE;
        } else if (instance instanceof DataType && !NormativeTypesConstant.ROOT_DATA_TYPE.equals(instance.getElementId())) {
            defaultDerivedFrom = NormativeTypesConstant.ROOT_DATA_TYPE;
        } else if (instance instanceof CapabilityType && !NormativeCapabilityTypes.ROOT.equals(instance.getElementId())) {
            defaultDerivedFrom = NormativeCapabilityTypes.ROOT;
        } else if (instance instanceof ArtifactType && !NormativeTypesConstant.ROOT_ARTIFACT_TYPE.equals(instance.getElementId())) {
            defaultDerivedFrom = NormativeTypesConstant.ROOT_ARTIFACT_TYPE;
        }
        if (defaultDerivedFrom != null) {
            derivedFrom = new ArrayList<>();
            derivedFrom.add(defaultDerivedFrom);
            instance.setDerivedFrom(derivedFrom);
            Node node = ParsingContextExecution.getObjectToNodeMap().get(instance);
            ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.DERIVED_FROM_NOTHING, defaultDerivedFrom, node.getStartMark(), "The " + instance.getClass().getSimpleName() + " " + instance.getElementId() + " derives from nothing, default " + defaultDerivedFrom + " will be set as parent type.", node.getEndMark(), instance.getElementId()));
        } else {
            // Non managed default parent type then returns
            return;
        }
    }
    String parentElementType = derivedFrom.get(0);
    // Merge the type with it's parent except for primitive data types.
    if (instance instanceof DataType && ToscaTypes.isSimple(parentElementType)) {
        if (instance instanceof PrimitiveDataType) {
            log.debug("Do not merge data type instance with parent as it extends from a primitive type.");
        } else {
            Node node = ParsingContextExecution.getObjectToNodeMap().get(instance);
            // type has not been parsed as primitive because it has some properties
            ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.SYNTAX_ERROR, "Primitive types cannot define properties.", node.getStartMark(), "The defined type inherit from a primitive type but defines some properties.", node.getEndMark(), parentElementType));
        }
        return;
    }
    AbstractInheritableToscaType parent = instances.get(parentElementType);
    if (parent == null) {
        parent = ToscaContext.get(instance.getClass(), parentElementType);
    } else {
        // first process the parent type
        processing.put(instance, null);
        process(processed, processing, parent, instances);
        processing.remove(instance);
    }
    if (parent == null) {
        Node node = ParsingContextExecution.getObjectToNodeMap().get(instance);
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.TYPE_NOT_FOUND, "Derived_from type not found", node.getStartMark(), "The type specified as parent is not found neither in the archive or its dependencies.", node.getEndMark(), parentElementType));
        return;
    }
    // Merge with parent type
    IndexedModelUtils.mergeInheritableIndex(parent, instance);
    processed.put(instance, null);
}
Also used : CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) ParsingError(alien4cloud.tosca.parser.ParsingError) PrimitiveDataType(org.alien4cloud.tosca.model.types.PrimitiveDataType) ArtifactType(org.alien4cloud.tosca.model.types.ArtifactType) Node(org.yaml.snakeyaml.nodes.Node) NodeType(org.alien4cloud.tosca.model.types.NodeType) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) DataType(org.alien4cloud.tosca.model.types.DataType) PrimitiveDataType(org.alien4cloud.tosca.model.types.PrimitiveDataType) AbstractInheritableToscaType(org.alien4cloud.tosca.model.types.AbstractInheritableToscaType)

Example 10 with AbstractInheritableToscaType

use of org.alien4cloud.tosca.model.types.AbstractInheritableToscaType in project alien4cloud by alien4cloud.

the class DerivedFromPostProcessor method process.

@Override
public void process(Map<String, ? extends AbstractInheritableToscaType> instances) {
    // Detect cyclic derived from
    Map<AbstractInheritableToscaType, String> processed = new IdentityHashMap<>();
    Map<AbstractInheritableToscaType, String> processing = new IdentityHashMap<>();
    // Then process to get the list of derived from and merge instances
    for (AbstractInheritableToscaType instance : safe(instances).values()) {
        process(processed, processing, instance, instances);
    }
}
Also used : IdentityHashMap(java.util.IdentityHashMap) AbstractInheritableToscaType(org.alien4cloud.tosca.model.types.AbstractInheritableToscaType)

Aggregations

AbstractInheritableToscaType (org.alien4cloud.tosca.model.types.AbstractInheritableToscaType)10 NotFoundException (alien4cloud.exception.NotFoundException)2 IndexingServiceException (alien4cloud.exception.IndexingServiceException)1 InvalidArgumentException (alien4cloud.exception.InvalidArgumentException)1 AbstractLocationResourceTemplate (alien4cloud.model.orchestrators.locations.AbstractLocationResourceTemplate)1 Location (alien4cloud.model.orchestrators.locations.Location)1 AbstractRelationshipTask (alien4cloud.topology.task.AbstractRelationshipTask)1 ToscaContextual (alien4cloud.tosca.context.ToscaContextual)1 ParsingError (alien4cloud.tosca.parser.ParsingError)1 PendingException (cucumber.api.PendingException)1 Given (cucumber.api.java.en.Given)1 ArrayList (java.util.ArrayList)1 IdentityHashMap (java.util.IdentityHashMap)1 Map (java.util.Map)1 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)1 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)1 EqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)1 ValidValuesConstraint (org.alien4cloud.tosca.model.definitions.constraints.ValidValuesConstraint)1 AbstractToscaType (org.alien4cloud.tosca.model.types.AbstractToscaType)1 ArtifactType (org.alien4cloud.tosca.model.types.ArtifactType)1