use of org.alien4cloud.tosca.model.types.RelationshipType in project alien4cloud by alien4cloud.
the class ToscaParserSimpleProfileAlien130Test method testParseTopologyWithWorkflows.
@Test
public void testParseTopologyWithWorkflows() throws FileNotFoundException, ParsingException {
Csar csar = new Csar("tosca-normative-types", "1.0.0.wd03-SNAPSHOT");
NodeType mockedCompute = getMockedCompute();
NodeType mockedSoftware = getMockedSoftwareComponent();
CapabilityType mockedContainer = Mockito.mock(CapabilityType.class);
Mockito.when(mockedContainer.getElementId()).thenReturn("tosca.capabilities.Container");
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.SoftwareComponent"), Mockito.any(Set.class))).thenReturn(mockedSoftware);
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("tosca.capabilities.Container"), Mockito.any(Set.class))).thenReturn(mockedContainer);
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Compute"), Mockito.any(Set.class))).thenReturn(mockedCompute);
RelationshipType hostedOn = Mockito.mock(RelationshipType.class);
Mockito.when(hostedOn.getElementId()).thenReturn("tosca.relationships.HostedOn");
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(RelationshipType.class), Mockito.eq("tosca.relationships.HostedOn"), Mockito.any(Set.class))).thenReturn(hostedOn);
Mockito.when(csarRepositorySearchService.getArchive(csar.getName(), csar.getVersion())).thenReturn(csar);
ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-topology-template-workflows.yml"));
Mockito.verify(csarRepositorySearchService).getArchive(csar.getName(), csar.getVersion());
assertNoBlocker(parsingResult);
Assert.assertEquals(3, parsingResult.getContext().getParsingErrors().size());
assertNotNull(parsingResult.getResult().getTopology());
Assert.assertEquals(5, parsingResult.getResult().getTopology().getWorkflows().size());
// check invalid names were renamed
assertTrue(parsingResult.getResult().getTopology().getWorkflows().containsKey("invalidName_"));
assertTrue(parsingResult.getResult().getTopology().getWorkflows().containsKey("invalid_Name"));
assertTrue(parsingResult.getResult().getTopology().getWorkflows().containsKey("invalid_Name_1"));
}
use of org.alien4cloud.tosca.model.types.RelationshipType in project alien4cloud by alien4cloud.
the class RelationshipPostProcessor method process.
public void process(NodeType nodeTemplateType, Map.Entry<String, RelationshipTemplate> instance) {
RelationshipTemplate relationshipTemplate = instance.getValue();
if (relationshipTemplate.getTarget() == null) {
Node node = ParsingContextExecution.getObjectToNodeMap().get(instance);
// the node template name is required
ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.REQUIREMENT_TARGET_NODE_TEMPLATE_NAME_REQUIRED, null, node.getStartMark(), null, node.getEndMark(), null));
}
RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, relationshipTemplate.getType());
propertyValueChecker.checkProperties(relationshipType, relationshipTemplate.getProperties(), instance.getKey());
RequirementDefinition rd = getRequirementDefinitionByName(nodeTemplateType, relationshipTemplate.getRequirementName());
if (rd == null) {
Node node = ParsingContextExecution.getObjectToNodeMap().get(relationshipTemplate.getRequirementName());
ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.REQUIREMENT_NOT_FOUND, null, node.getStartMark(), null, node.getEndMark(), relationshipTemplate.getRequirementName()));
return;
}
if (relationshipTemplate.getType() == null) {
// if the relationship type has not been defined on the requirement assignment it may be defined on the requirement definition.
relationshipTemplate.setType(rd.getRelationshipType());
}
referencePostProcessor.process(new ReferencePostProcessor.TypeReference(relationshipTemplate, relationshipTemplate.getType(), RelationshipType.class));
relationshipTemplate.setRequirementType(rd.getType());
ArchiveRoot archiveRoot = (ArchiveRoot) ParsingContextExecution.getRoot().getWrappedInstance();
// now find the target of the relation
NodeTemplate targetNodeTemplate = archiveRoot.getTopology().getNodeTemplates().get(relationshipTemplate.getTarget());
if (targetNodeTemplate == null) {
Node node = ParsingContextExecution.getObjectToNodeMap().get(relationshipTemplate.getTarget());
ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.REQUIREMENT_TARGET_NOT_FOUND, null, node.getStartMark(), null, node.getEndMark(), relationshipTemplate.getTarget()));
return;
}
// alien actually supports a capability type in the TOSCA yaml
String capabilityStr = relationshipTemplate.getTargetedCapabilityName();
Capability capability = null;
if (capabilityStr == null) {
// the capability type is not known, we assume that we are parsing a Short notation (node only)
if (targetNodeTemplate.getCapabilities() != null) {
// let's try to find all match for a given type
capability = getCapabilityByType(targetNodeTemplate, relationshipTemplate, relationshipTemplate.getRequirementType());
if (capability == null) {
capability = targetNodeTemplate.getCapabilities().get(relationshipTemplate.getRequirementName());
if (capability != null) {
relationshipTemplate.setTargetedCapabilityName(rd.getId());
}
}
}
} else {
// Let's try to find if the target node has a capability as named in the capability string of the relationship (requirement assignment)
if (targetNodeTemplate.getCapabilities() != null) {
capability = targetNodeTemplate.getCapabilities().get(capabilityStr);
}
if (capability == null) {
// The capabilityStr may be the name of a type
capability = getCapabilityByType(targetNodeTemplate, relationshipTemplate, capabilityStr);
}
}
if (capability == null) {
Node node = ParsingContextExecution.getObjectToNodeMap().get(relationshipTemplate);
// we should fail
ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.REQUIREMENT_CAPABILITY_NOT_FOUND, null, node.getStartMark(), null, node.getEndMark(), relationshipTemplate.getRequirementName()));
return;
}
RelationshipType indexedRelationshipType = ToscaContext.get(RelationshipType.class, relationshipTemplate.getType());
if (indexedRelationshipType == null) {
// Error managed by the reference post processor.
return;
}
Map<String, AbstractPropertyValue> properties = Maps.newLinkedHashMap();
TemplateBuilder.fillProperties(properties, indexedRelationshipType.getProperties(), relationshipTemplate.getProperties(), false);
relationshipTemplate.setProperties(properties);
relationshipTemplate.setAttributes(indexedRelationshipType.getAttributes());
// FIXME we should check that the artifact is defined at the type level.
safe(instance.getValue().getArtifacts()).values().forEach(templateDeploymentArtifactPostProcessor);
Map<String, DeploymentArtifact> mergedArtifacts = instance.getValue().getArtifacts();
if (mergedArtifacts == null) {
mergedArtifacts = new HashMap<>();
}
mergedArtifacts.putAll(safe(indexedRelationshipType.getArtifacts()));
relationshipTemplate.setArtifacts(mergedArtifacts);
// TODO Manage interfaces inputs to copy them to all operations.
for (Interface anInterface : safe(instance.getValue().getInterfaces()).values()) {
safe(anInterface.getOperations()).values().stream().map(Operation::getImplementationArtifact).filter(Objects::nonNull).forEach(implementationArtifactPostProcessor);
}
}
use of org.alien4cloud.tosca.model.types.RelationshipType in project alien4cloud by alien4cloud.
the class AbstractToscaParserSimpleProfileTest method testNodeType.
@SuppressWarnings("unchecked")
@Test
public void testNodeType() throws FileNotFoundException, ParsingException {
Mockito.reset(csarRepositorySearchService);
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("mytypes.mycapabilities.MyCapabilityTypeName"), 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.yml"));
ParserTestUtil.displayErrors(parsingResult);
assertNoBlocker(parsingResult);
ArchiveRoot archiveRoot = parsingResult.getResult();
Assert.assertNotNull(archiveRoot.getArchive());
Assert.assertEquals(getToscaVersion(), archiveRoot.getArchive().getToscaDefinitionsVersion());
Assert.assertEquals(1, archiveRoot.getNodeTypes().size());
// check node type.
NodeType nodeType = archiveRoot.getNodeTypes().get("my_company.my_types.MyAppNodeType");
Assert.assertNotNull(nodeType);
Assert.assertEquals(Lists.newArrayList("tosca.nodes.SoftwareComponent", "tosca.nodes.Root"), nodeType.getDerivedFrom());
Assert.assertEquals("My company’s custom applicaton", nodeType.getDescription());
// validate properties parsing
Assert.assertEquals(4, nodeType.getProperties().size());
PropertyDefinition def1 = new PropertyDefinition();
def1.setType("string");
def1.setDefault(new ScalarPropertyValue("default"));
def1.setDescription("application password");
List<PropertyConstraint> constraints = Lists.newArrayList();
constraints.add(new MinLengthConstraint(6));
constraints.add(new MaxLengthConstraint(10));
def1.setConstraints(constraints);
PropertyDefinition def2 = new PropertyDefinition();
def2.setType("integer");
def2.setDescription("application port number");
PropertyDefinition def3 = new PropertyDefinition();
def3.setType("scalar-unit.size");
def3.setDefault(new ScalarPropertyValue("1 GB"));
LessThanConstraint ltConstraint = new LessThanConstraint();
ltConstraint.setLessThan("1 TB");
constraints = Lists.<PropertyConstraint>newArrayList(ltConstraint);
def3.setConstraints(constraints);
PropertyDefinition def4 = new PropertyDefinition();
def4.setType("scalar-unit.time");
def4.setDefault(new ScalarPropertyValue("1 d"));
GreaterThanConstraint gtConstraint = new GreaterThanConstraint();
gtConstraint.setGreaterThan("1 h");
constraints = Lists.<PropertyConstraint>newArrayList(gtConstraint);
def4.setConstraints(constraints);
Assert.assertEquals(MapUtil.newHashMap(new String[] { "my_app_password", "my_app_duration", "my_app_size", "my_app_port" }, new PropertyDefinition[] { def1, def4, def3, def2 }), nodeType.getProperties());
// check requirements
Assert.assertEquals(2, nodeType.getRequirements().size());
RequirementDefinition rd0 = nodeType.getRequirements().get(0);
Assert.assertEquals("host", rd0.getId());
Assert.assertEquals(1, rd0.getLowerBound());
Assert.assertEquals(1, rd0.getUpperBound());
RequirementDefinition rd1 = nodeType.getRequirements().get(1);
Assert.assertEquals("other", rd1.getId());
Assert.assertEquals(0, rd1.getLowerBound());
Assert.assertEquals(Integer.MAX_VALUE, rd1.getUpperBound());
}
use of org.alien4cloud.tosca.model.types.RelationshipType in project alien4cloud by alien4cloud.
the class TopologyNavigationUtil method getHostOfTypeInHostingHierarchy.
/**
* Deeply explore the hosted_on hierarchy of the given node to find a node of the given type.
*/
public static NodeTemplate getHostOfTypeInHostingHierarchy(Topology topology, NodeTemplate nodeTemplate, String hostType) {
if (nodeTemplate.getRelationships() != null) {
for (RelationshipTemplate relationshipTemplate : nodeTemplate.getRelationships().values()) {
RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, relationshipTemplate.getType());
if (isOfType(relationshipType, NormativeRelationshipConstants.HOSTED_ON)) {
NodeTemplate hostNode = topology.getNodeTemplates().get(relationshipTemplate.getTarget());
NodeType hostNodeType = ToscaContext.get(NodeType.class, hostNode.getType());
if (isOfType(hostNodeType, hostType)) {
return hostNode;
} else {
return getHostOfTypeInHostingHierarchy(topology, hostNode, hostType);
}
}
}
}
return null;
}
use of org.alien4cloud.tosca.model.types.RelationshipType in project alien4cloud by alien4cloud.
the class ToscaParserAlien200Test method parseTopologyTemplateWithRelationshipWorkflow.
@Test
public void parseTopologyTemplateWithRelationshipWorkflow() throws ParsingException {
Mockito.reset(csarRepositorySearchService);
Mockito.when(csarRepositorySearchService.getArchive("tosca-normative-types", "1.0.0-ALIEN14")).thenReturn(Mockito.mock(Csar.class));
NodeType mockCompute = new NodeType();
mockCompute.setElementId(NormativeComputeConstants.COMPUTE_TYPE);
mockCompute.setArchiveName("tosca-normative-types");
mockCompute.setArchiveVersion("1.0.0-ALIEN14");
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq(NormativeComputeConstants.COMPUTE_TYPE), Mockito.any(Set.class))).thenReturn(mockCompute);
RelationshipType mockHostedOn = Mockito.mock(RelationshipType.class);
Mockito.when(mockHostedOn.getElementId()).thenReturn(NormativeRelationshipConstants.HOSTED_ON);
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(RelationshipType.class), Mockito.eq(NormativeRelationshipConstants.HOSTED_ON), Mockito.any(Set.class))).thenReturn(mockHostedOn);
ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-topology-template-relationship-workflow.yml"));
Assert.assertFalse(parsingResult.getResult().getTopology().getWorkflows().isEmpty());
Assert.assertTrue(parsingResult.getResult().getTopology().getWorkflows().get("install") != null);
Workflow wf = parsingResult.getResult().getTopology().getWorkflows().get("install");
WorkflowStep relStep = wf.getSteps().get("OracleJDK_hostedOnComputeHost_pre_configure_source");
Assert.assertNotNull(relStep);
Assert.assertTrue(relStep instanceof RelationshipWorkflowStep);
RelationshipWorkflowStep relationshipWorkflowStep = (RelationshipWorkflowStep) relStep;
Assert.assertNotNull(relationshipWorkflowStep.getTargetRelationship());
Assert.assertNotNull(relationshipWorkflowStep.getSourceHostId());
Assert.assertNotNull(relationshipWorkflowStep.getTargetHostId());
WorkflowStep nStep = wf.getSteps().get("OracleJDK_start");
Assert.assertNotNull(nStep);
Assert.assertTrue(nStep instanceof NodeWorkflowStep);
NodeWorkflowStep nodeWorkflowStep = (NodeWorkflowStep) nStep;
Assert.assertNotNull(nodeWorkflowStep.getHostId());
}
Aggregations