Search in sources :

Example 21 with Interface

use of org.alien4cloud.tosca.model.definitions.Interface in project alien4cloud by alien4cloud.

the class IndexedModelUtils method mergeInterfaces.

/**
 * Merge interface & operations: all 'from' interfaces will be merged into 'to' interfaces.
 * <p>
 * 'from' entries are added to 'to' entries if not exist (so entries in 'to' are preserved).
 */
public static Map<String, Interface> mergeInterfaces(Map<String, Interface> from, Map<String, Interface> to) {
    Map<String, Interface> target = to;
    if (target == null) {
        return from;
    }
    if (from == null) {
        return target;
    }
    for (Entry<String, Interface> fromEntry : from.entrySet()) {
        Interface toInterface = target.get(fromEntry.getKey());
        Interface fromInterface = fromEntry.getValue();
        if (toInterface == null) {
            // the target doesn't contain this key, just put it
            target.put(fromEntry.getKey(), fromEntry.getValue());
        } else {
            // the target already have this entry, so we'll compare operations in detail
            Map<String, Operation> toOperations = toInterface.getOperations();
            if (toOperations == null) {
                toInterface.setOperations(fromInterface.getOperations());
            } else if (fromInterface.getOperations() != null) {
                for (Entry<String, Operation> fromOperationEntry : fromInterface.getOperations().entrySet()) {
                    if (!toOperations.containsKey(fromOperationEntry.getKey())) {
                        toOperations.put(fromOperationEntry.getKey(), fromOperationEntry.getValue());
                    }
                }
            }
        }
    }
    return target;
}
Also used : Entry(java.util.Map.Entry) Operation(org.alien4cloud.tosca.model.definitions.Operation) Interface(org.alien4cloud.tosca.model.definitions.Interface)

Example 22 with Interface

use of org.alien4cloud.tosca.model.definitions.Interface in project alien4cloud by alien4cloud.

the class ToscaParserSimpleProfileAlien130Test method testNodeTypeWithCutomInterface.

@SuppressWarnings("unchecked")
@Test
public void testNodeTypeWithCutomInterface() throws FileNotFoundException, ParsingException {
    NodeType mockedResult = Mockito.mock(NodeType.class);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.SoftwareComponent"), Mockito.any(Set.class))).thenReturn(mockedResult);
    Mockito.when(mockedResult.getDerivedFrom()).thenReturn(Lists.newArrayList("tosca.nodes.Root"));
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Root"), Mockito.any(Set.class))).thenReturn(mockedResult);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Compute"), Mockito.any(Set.class))).thenReturn(mockedResult);
    CapabilityType mockedCapabilityResult = Mockito.mock(CapabilityType.class);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("mytypes.mycapabilities.MyCapabilityTypeName"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("tosca.capabilities.Container"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("tosca.capabilities.Endpoint"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
    RelationshipType hostedOn = new RelationshipType();
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(RelationshipType.class), Mockito.eq("tosca.relationships.HostedOn"), Mockito.any(Set.class))).thenReturn(hostedOn);
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-node-type-interface-operations.yml"));
    assertNoBlocker(parsingResult);
    ArchiveRoot archiveRoot = parsingResult.getResult();
    assertNotNull(archiveRoot.getArchive());
    Assert.assertEquals(getToscaVersion(), archiveRoot.getArchive().getToscaDefinitionsVersion());
    Assert.assertEquals(1, archiveRoot.getNodeTypes().size());
    // check node type.
    Entry<String, NodeType> entry = archiveRoot.getNodeTypes().entrySet().iterator().next();
    Assert.assertEquals("my_company.my_types.MyAppNodeType", entry.getKey());
    NodeType nodeType = entry.getValue();
    assertNotNull(nodeType.getInterfaces());
    Assert.assertEquals(2, nodeType.getInterfaces().size());
    assertNotNull(nodeType.getInterfaces().get(ToscaNodeLifecycleConstants.STANDARD));
    Interface customInterface = nodeType.getInterfaces().get("custom");
    assertNotNull(customInterface);
    Assert.assertEquals("this is a sample interface used to execute custom operations.", customInterface.getDescription());
    Assert.assertEquals(1, customInterface.getOperations().size());
    Operation operation = customInterface.getOperations().get("do_something");
    assertNotNull(operation);
    Assert.assertEquals(3, operation.getInputParameters().size());
    Assert.assertEquals(ScalarPropertyValue.class, operation.getInputParameters().get("value_input").getClass());
    Assert.assertEquals(PropertyDefinition.class, operation.getInputParameters().get("definition_input").getClass());
    Assert.assertEquals(FunctionPropertyValue.class, operation.getInputParameters().get("function_input").getClass());
}
Also used : CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Set(java.util.Set) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeType(org.alien4cloud.tosca.model.types.NodeType) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) Operation(org.alien4cloud.tosca.model.definitions.Operation) Interface(org.alien4cloud.tosca.model.definitions.Interface) Test(org.junit.Test)

Example 23 with Interface

use of org.alien4cloud.tosca.model.definitions.Interface in project alien4cloud by alien4cloud.

the class ToscaParserSimpleProfileAlien130Test method testGetOperationOutputFunction.

@Test
@SuppressWarnings("unchecked")
public void testGetOperationOutputFunction() throws Throwable {
    Csar csar = new Csar("tosca-normative-types", "1.0.0-SNAPSHOT-wd03");
    // Mockito.when(csarRepositorySearchService.getArchive(csar.getId())).thenReturn(csar);
    NodeType mockedResult = Mockito.mock(NodeType.class);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.SoftwareComponent"), Mockito.any(Set.class))).thenReturn(mockedResult);
    Mockito.when(mockedResult.getDerivedFrom()).thenReturn(Lists.newArrayList("tosca.nodes.Root"));
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Root"), Mockito.any(Set.class))).thenReturn(mockedResult);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Compute"), Mockito.any(Set.class))).thenReturn(mockedResult);
    CapabilityType mockedCapabilityResult = Mockito.mock(CapabilityType.class);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("mytypes.mycapabilities.MyCapabilityTypeName"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("tosca.capabilities.Container"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("tosca.capabilities.Endpoint"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
    RelationshipType hostedOn = new RelationshipType();
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(RelationshipType.class), Mockito.eq("tosca.relationships.HostedOn"), Mockito.any(Set.class))).thenReturn(hostedOn);
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-functions.yml"));
    Mockito.verify(csarRepositorySearchService).getArchive(csar.getName(), csar.getVersion());
    assertNoBlocker(parsingResult);
    ArchiveRoot archiveRoot = parsingResult.getResult();
    assertNotNull(archiveRoot.getArchive());
    Assert.assertEquals(getToscaVersion(), archiveRoot.getArchive().getToscaDefinitionsVersion());
    // check nodetype elements
    Entry<String, NodeType> entry = archiveRoot.getNodeTypes().entrySet().iterator().next();
    Assert.assertEquals("my_company.my_types.MyAppNodeType", entry.getKey());
    NodeType nodeType = entry.getValue();
    // on input level
    Map<String, Interface> interfaces = nodeType.getInterfaces();
    Interface customInterface = interfaces.get("custom");
    Map<String, IValue> doSomethingInputs = customInterface.getOperations().get("do_something").getInputParameters();
    assertNotNull(doSomethingInputs);
    Assert.assertFalse(doSomethingInputs.isEmpty());
    IValue operationOutput_input = doSomethingInputs.get("operationOutput_input");
    assertTrue(operationOutput_input instanceof FunctionPropertyValue);
    FunctionPropertyValue function = (FunctionPropertyValue) operationOutput_input;
    Assert.assertEquals("get_operation_output", function.getFunction());
    Assert.assertEquals(4, function.getParameters().size());
    Map<String, IValue> attributes = nodeType.getAttributes();
    IValue operationOutputAttr = attributes.get("url");
    // check attributes types
    assertTrue(operationOutputAttr instanceof FunctionPropertyValue);
    function = (FunctionPropertyValue) operationOutputAttr;
    Assert.assertEquals("get_operation_output", function.getFunction());
    Assert.assertEquals(4, function.getParameters().size());
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Set(java.util.Set) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) IValue(org.alien4cloud.tosca.model.definitions.IValue) NodeType(org.alien4cloud.tosca.model.types.NodeType) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) Interface(org.alien4cloud.tosca.model.definitions.Interface) Test(org.junit.Test)

Example 24 with Interface

use of org.alien4cloud.tosca.model.definitions.Interface in project alien4cloud by alien4cloud.

the class InterfaceUtils method getOperationIfArtifactDefined.

public static Operation getOperationIfArtifactDefined(Map<String, Interface> interfaceMap, String interfaceName, String operationName) {
    Interface interfaz = safe(interfaceMap).get(interfaceName);
    if (interfaz == null) {
        return null;
    }
    Operation operation = safe(interfaz.getOperations()).get(operationName);
    if (operation == null || operation.getImplementationArtifact() == null || StringUtils.isBlank(operation.getImplementationArtifact().getArtifactRef())) {
        return null;
    }
    return operation;
}
Also used : Operation(org.alien4cloud.tosca.model.definitions.Operation) Interface(org.alien4cloud.tosca.model.definitions.Interface)

Example 25 with Interface

use of org.alien4cloud.tosca.model.definitions.Interface in project alien4cloud by alien4cloud.

the class TopologyUtils method filterAbstractInterfaces.

/**
 * Extract interfaces that have implemented operations only.
 *
 * @param allInterfaces all interfaces
 * @return interfaces that have implemented operations
 */
public static Map<String, Interface> filterAbstractInterfaces(Map<String, Interface> allInterfaces) {
    Map<String, Interface> interfaces = Maps.newHashMap();
    for (Map.Entry<String, Interface> interfaceEntry : allInterfaces.entrySet()) {
        Map<String, Operation> operations = Maps.newHashMap();
        for (Map.Entry<String, Operation> operationEntry : interfaceEntry.getValue().getOperations().entrySet()) {
            if (operationEntry.getValue().getImplementationArtifact() == null) {
                // Don't consider operation which do not have any implementation artifact
                continue;
            }
            operations.put(operationEntry.getKey(), operationEntry.getValue());
        }
        if (!operations.isEmpty()) {
            // At least one operation fulfill the criteria
            Interface inter = new Interface();
            inter.setDescription(interfaceEntry.getValue().getDescription());
            inter.setOperations(operations);
            interfaces.put(interfaceEntry.getKey(), inter);
        }
    }
    return interfaces;
}
Also used : Operation(org.alien4cloud.tosca.model.definitions.Operation) Map(java.util.Map) Interface(org.alien4cloud.tosca.model.definitions.Interface)

Aggregations

Interface (org.alien4cloud.tosca.model.definitions.Interface)28 Operation (org.alien4cloud.tosca.model.definitions.Operation)16 Test (org.junit.Test)7 NodeType (org.alien4cloud.tosca.model.types.NodeType)5 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)5 ScalarNode (org.yaml.snakeyaml.nodes.ScalarNode)4 Map (java.util.Map)3 IValue (org.alien4cloud.tosca.model.definitions.IValue)3 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)3 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)3 ServiceNodeTemplate (org.alien4cloud.tosca.model.templates.ServiceNodeTemplate)3 AbstractInstantiableToscaType (org.alien4cloud.tosca.model.types.AbstractInstantiableToscaType)3 MappingNode (org.yaml.snakeyaml.nodes.MappingNode)3 NodeTuple (org.yaml.snakeyaml.nodes.NodeTuple)3 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)2 INodeParser (alien4cloud.tosca.parser.INodeParser)2 ParsingError (alien4cloud.tosca.parser.ParsingError)2 MapParser (alien4cloud.tosca.parser.impl.base.MapParser)2 Set (java.util.Set)2 ImplementationArtifact (org.alien4cloud.tosca.model.definitions.ImplementationArtifact)2