Search in sources :

Example 11 with Operation

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

the class IndexedModelTest method testMergeInterfaceOperationsKeepTo.

@Test
public void testMergeInterfaceOperationsKeepTo() {
    // TO keeps it's own operation
    Map<String, Interface> from = Maps.newHashMap();
    Interface i1 = new Interface();
    Map<String, Operation> ios1 = Maps.newHashMap();
    Operation o1 = new Operation();
    ios1.put("o1", o1);
    i1.setOperations(ios1);
    from.put("i1", i1);
    Map<String, Interface> to = Maps.newHashMap();
    Interface i2 = new Interface();
    Map<String, Operation> ios2 = Maps.newHashMap();
    Operation o2 = new Operation();
    ios2.put("o1", o2);
    i2.setOperations(ios2);
    to.put("i1", i2);
    Map<String, Interface> merged = IndexedModelUtils.mergeInterfaces(from, to);
    assertEquals(1, merged.size());
    assertSame(merged.get("i1").getOperations().get("o1"), o2);
}
Also used : Operation(org.alien4cloud.tosca.model.definitions.Operation) Interface(org.alien4cloud.tosca.model.definitions.Interface) Test(org.junit.Test)

Example 12 with Operation

use of org.alien4cloud.tosca.model.definitions.Operation 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 13 with Operation

use of org.alien4cloud.tosca.model.definitions.Operation 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 14 with Operation

use of org.alien4cloud.tosca.model.definitions.Operation 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 15 with Operation

use of org.alien4cloud.tosca.model.definitions.Operation 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

Operation (org.alien4cloud.tosca.model.definitions.Operation)19 Interface (org.alien4cloud.tosca.model.definitions.Interface)16 Test (org.junit.Test)5 NodeType (org.alien4cloud.tosca.model.types.NodeType)4 Map (java.util.Map)3 IValue (org.alien4cloud.tosca.model.definitions.IValue)3 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ImplementationArtifact (org.alien4cloud.tosca.model.definitions.ImplementationArtifact)2 ServiceNodeTemplate (org.alien4cloud.tosca.model.templates.ServiceNodeTemplate)2 AbstractInstantiableToscaType (org.alien4cloud.tosca.model.types.AbstractInstantiableToscaType)2 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)2 NotFoundException (alien4cloud.exception.NotFoundException)1 ArtifactSupport (alien4cloud.model.orchestrators.ArtifactSupport)1 IllegalOperationWarning (alien4cloud.topology.warning.IllegalOperationWarning)1 ToscaContext (alien4cloud.tosca.context.ToscaContext)1 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)1 ParsingContextExecution (alien4cloud.tosca.parser.ParsingContextExecution)1 ParsingError (alien4cloud.tosca.parser.ParsingError)1 ParsingErrorLevel (alien4cloud.tosca.parser.ParsingErrorLevel)1