Search in sources :

Example 1 with ArtifactType

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

the class ToscaParserSimpleProfileAlien130Test method testArtifactType.

@SuppressWarnings("unchecked")
@Test
public void testArtifactType() throws FileNotFoundException, ParsingException {
    ArtifactType mockedResult = Mockito.mock(ArtifactType.class);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(ArtifactType.class), Mockito.eq("tosca.artifact.Root"), Mockito.any(Set.class))).thenReturn(mockedResult);
    List<String> derivedFromSet = Lists.newArrayList();
    Mockito.when(mockedResult.getDerivedFrom()).thenReturn(derivedFromSet);
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-artifact-type.yml"));
    assertNoBlocker(parsingResult);
    ArchiveRoot archiveRoot = parsingResult.getResult();
    assertNotNull(archiveRoot.getArchive());
    Assert.assertEquals(getToscaVersion(), archiveRoot.getArchive().getToscaDefinitionsVersion());
    Assert.assertEquals(1, archiveRoot.getArtifactTypes().size());
    Entry<String, ArtifactType> entry = archiveRoot.getArtifactTypes().entrySet().iterator().next();
    Assert.assertEquals("my_artifact_type", entry.getKey());
    ArtifactType artifact = entry.getValue();
    Assert.assertEquals(Lists.newArrayList("tosca.artifact.Root"), artifact.getDerivedFrom());
    Assert.assertEquals("Java Archive artifact type", artifact.getDescription());
    Assert.assertEquals("application/java-archive", artifact.getMimeType());
    Assert.assertEquals(Lists.newArrayList("jar"), artifact.getFileExt());
}
Also used : Set(java.util.Set) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) ArtifactType(org.alien4cloud.tosca.model.types.ArtifactType) Test(org.junit.Test)

Example 2 with ArtifactType

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

the class AbstractArtifactPostProcessor method getArtifactTypeByExtension.

private String getArtifactTypeByExtension(String artifactReference, Node node, ArchiveRoot archiveRoot) {
    int dotIndex = artifactReference.lastIndexOf('.');
    String extension = (dotIndex == -1) ? "" : artifactReference.substring(dotIndex + 1);
    String type = null;
    ArtifactType indexedType = getFromArchiveRootWithExtension(archiveRoot, extension);
    if (indexedType == null) {
        ArtifactType artifactType = repositorySearchService.getElementInDependencies(ArtifactType.class, archiveRoot.getArchive().getDependencies(), "fileExt", extension);
        if (artifactType != null) {
            type = artifactType.getElementId();
        }
        if (type == null) {
            ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.TYPE_NOT_FOUND, "Implementation artifact", node.getStartMark(), "No artifact type in the repository references the artifact's extension", node.getEndMark(), extension));
            type = "unknown";
        }
    } else {
        type = indexedType.getElementId();
    }
    return type;
}
Also used : ParsingError(alien4cloud.tosca.parser.ParsingError) ArtifactType(org.alien4cloud.tosca.model.types.ArtifactType)

Example 3 with ArtifactType

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

the class LocationMatchNodesArtifactsElector method isEligible.

private boolean isEligible(AbstractInstantiableTemplate template, LocationMatchNodeFilter.NodeMatchContext matchContext) {
    if (template == null) {
        return true;
    }
    ArtifactSupport artifactSupport = matchContext.getArtifactSupport();
    // if no supported artifact defined, then return true
    if (artifactSupport == null || ArrayUtils.isEmpty(artifactSupport.getTypes())) {
        return true;
    }
    String[] supportedArtifacts = artifactSupport.getTypes();
    AbstractInstantiableToscaType indexedArtifactToscaElement = matchContext.getElement(AbstractInstantiableToscaType.class, template.getType());
    if (MapUtils.isNotEmpty(indexedArtifactToscaElement.getInterfaces())) {
        for (Interface interfaz : indexedArtifactToscaElement.getInterfaces().values()) {
            for (Operation operation : interfaz.getOperations().values()) {
                if (operation.getImplementationArtifact() != null) {
                    String artifactTypeString = operation.getImplementationArtifact().getArtifactType();
                    ArtifactType artifactType = matchContext.getElement(ArtifactType.class, artifactTypeString);
                    // stop the checking once one artifactType is not supported
                    if (!isFromOneOfTypes(supportedArtifacts, artifactType)) {
                        return false;
                    }
                }
            }
        }
    }
    return true;
}
Also used : ArtifactType(org.alien4cloud.tosca.model.types.ArtifactType) AbstractInstantiableToscaType(org.alien4cloud.tosca.model.types.AbstractInstantiableToscaType) Operation(org.alien4cloud.tosca.model.definitions.Operation) Interface(org.alien4cloud.tosca.model.definitions.Interface) ArtifactSupport(alien4cloud.model.orchestrators.ArtifactSupport)

Example 4 with ArtifactType

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

the class ParserTestUtil method mockNormativeTypes.

/**
 * Utility method to mock the acess to some normative types nodes and capabilities.
 *
 * @param repositorySearchService The repositorySearchService to mock.
 */
public static void mockNormativeTypes(ICSARRepositorySearchService repositorySearchService) {
    Csar csar = new Csar("tosca-normative-types", "1.0.0-ALIEN12");
    Mockito.when(repositorySearchService.getArchive(csar.getName(), csar.getVersion())).thenReturn(csar);
    NodeType mockedNodeRoot = new NodeType();
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq(NormativeTypesConstant.ROOT_NODE_TYPE), Mockito.any(Set.class))).thenReturn(mockedNodeRoot);
    RelationshipType mockedRelationshipRoot = new RelationshipType();
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(RelationshipType.class), Mockito.eq(NormativeTypesConstant.ROOT_RELATIONSHIP_TYPE), Mockito.any(Set.class))).thenReturn(mockedRelationshipRoot);
    CapabilityType mockedCapabilityType = new CapabilityType();
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq(NormativeCapabilityTypes.ROOT), Mockito.any(Set.class))).thenReturn(mockedCapabilityType);
    DataType mockedDataType = new DataType();
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(DataType.class), Mockito.eq(NormativeTypesConstant.ROOT_DATA_TYPE), Mockito.any(Set.class))).thenReturn(mockedDataType);
    ArtifactType mockedArtifactType = new ArtifactType();
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(ArtifactType.class), Mockito.eq(NormativeTypesConstant.ROOT_ARTIFACT_TYPE), Mockito.any(Set.class))).thenReturn(mockedArtifactType);
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Set(java.util.Set) ArtifactType(org.alien4cloud.tosca.model.types.ArtifactType) NodeType(org.alien4cloud.tosca.model.types.NodeType) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) DataType(org.alien4cloud.tosca.model.types.DataType)

Example 5 with ArtifactType

use of org.alien4cloud.tosca.model.types.ArtifactType 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)

Aggregations

ArtifactType (org.alien4cloud.tosca.model.types.ArtifactType)5 ParsingError (alien4cloud.tosca.parser.ParsingError)2 Set (java.util.Set)2 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)2 DataType (org.alien4cloud.tosca.model.types.DataType)2 NodeType (org.alien4cloud.tosca.model.types.NodeType)2 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)2 ArtifactSupport (alien4cloud.model.orchestrators.ArtifactSupport)1 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)1 Csar (org.alien4cloud.tosca.model.Csar)1 Interface (org.alien4cloud.tosca.model.definitions.Interface)1 Operation (org.alien4cloud.tosca.model.definitions.Operation)1 AbstractInheritableToscaType (org.alien4cloud.tosca.model.types.AbstractInheritableToscaType)1 AbstractInstantiableToscaType (org.alien4cloud.tosca.model.types.AbstractInstantiableToscaType)1 PrimitiveDataType (org.alien4cloud.tosca.model.types.PrimitiveDataType)1 Test (org.junit.Test)1 Node (org.yaml.snakeyaml.nodes.Node)1