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;
}
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());
}
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());
}
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;
}
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;
}
Aggregations