use of org.alien4cloud.tosca.model.templates.RelationshipTemplate in project yorc-a4c-plugin by ystia.
the class ShowTopology method printNode.
/**
* Print info about a Node
* @param node
*/
private static void printNode(PaaSNodeTemplate node) {
log.debug("******* Compute Node " + node.getId() + " *******");
NodeTemplate nt = node.getTemplate();
log.debug("CsarPath = " + node.getCsarPath());
log.debug("Type = " + nt.getType());
// Children
List<PaaSNodeTemplate> children = node.getChildren();
for (PaaSNodeTemplate child : children) {
log.info("Child: " + child.getId());
}
// properties
for (String prop : nt.getProperties().keySet()) {
AbstractPropertyValue absval = nt.getProperties().get(prop);
if (absval instanceof ScalarPropertyValue) {
ScalarPropertyValue scaval = (ScalarPropertyValue) absval;
log.debug(">> Property: " + prop + "=" + scaval.getValue());
}
}
// Attributes
Map<String, IValue> attrs = nt.getAttributes();
if (attrs != null) {
for (String attname : attrs.keySet()) {
IValue att = attrs.get(attname);
log.debug(">> Attribute: " + attname + "=" + att);
}
}
// capabilities
Map<String, Capability> capabilities = nt.getCapabilities();
if (capabilities != null) {
for (String capname : capabilities.keySet()) {
Capability cap = capabilities.get(capname);
log.debug(">> Capability " + capname);
log.debug("type : " + cap.getType());
log.debug("properties : " + cap.getProperties());
}
}
// requirements
Map<String, Requirement> requirements = nt.getRequirements();
if (requirements != null) {
for (String reqname : requirements.keySet()) {
Requirement req = requirements.get(reqname);
log.debug(">> Requirement: " + reqname);
log.debug("type : " + req.getType());
log.debug("properties : " + req.getProperties());
}
}
// relationships
Map<String, RelationshipTemplate> relations = nt.getRelationships();
if (relations != null) {
for (String relname : relations.keySet()) {
RelationshipTemplate rel = relations.get(relname);
log.debug(">> Relationship: " + relname);
log.debug("type : " + rel.getType());
log.debug("properties : " + rel.getProperties());
}
}
// artifacts
Map<String, DeploymentArtifact> artifacts = nt.getArtifacts();
if (artifacts != null) {
for (DeploymentArtifact art : artifacts.values()) {
printArtifact(art);
}
}
}
use of org.alien4cloud.tosca.model.templates.RelationshipTemplate in project alien4cloud by alien4cloud.
the class PaaSUtilsTest method buildFakePaaSRelTemplate.
private PaaSRelationshipTemplate buildFakePaaSRelTemplate(String source, String target, String targetedCapability) {
PaaSRelationshipTemplate fakePaaSRelTemplate = Mockito.mock(PaaSRelationshipTemplate.class);
RelationshipTemplate relationshipTemplate = buildFakeRelationShip(target, targetedCapability);
Mockito.when(fakePaaSRelTemplate.getTemplate()).thenReturn(relationshipTemplate);
Map<String, Operation> operations = Maps.newHashMap();
operations.put(operation1, buildFakeOperation());
Map<String, Interface> interfaces = Maps.newHashMap();
interfaces.put(interface1, buildFakeInterface(operations));
Mockito.when(fakePaaSRelTemplate.getInterfaces()).thenReturn(interfaces);
Mockito.when(fakePaaSRelTemplate.getSource()).thenReturn(source);
return fakePaaSRelTemplate;
}
use of org.alien4cloud.tosca.model.templates.RelationshipTemplate in project alien4cloud by alien4cloud.
the class FunctionEvaluator method doGetProperty.
private static AbstractPropertyValue doGetProperty(FunctionEvaluatorContext evaluatorContext, AbstractInstantiableTemplate targetTemplate, FunctionPropertyValue function) {
if (targetTemplate == null) {
return null;
}
// If a requirement or capability name is defined then it is applied to the node template.
if (function.getCapabilityOrRequirementName() != null) {
if (targetTemplate instanceof RelationshipTemplate) {
throw new IllegalArgumentException("Get property that specifies a capability or relationship target must be placed on a node template and not a relationship template.");
}
AbstractPropertyValue propertyValue = null;
Capability targetCapability = safe(((NodeTemplate) targetTemplate).getCapabilities()).get(function.getCapabilityOrRequirementName());
if (targetCapability != null) {
propertyValue = getFromPath(evaluatorContext, targetTemplate, targetCapability.getProperties(), function.getElementNameToFetch());
}
if (propertyValue == null) {
Requirement requirement = safe(((NodeTemplate) targetTemplate).getRequirements()).get(function.getCapabilityOrRequirementName());
if (requirement != null) {
propertyValue = getFromPath(evaluatorContext, targetTemplate, requirement.getProperties(), function.getElementNameToFetch());
}
}
if (propertyValue == null) {
// try to find the value from the host node.
propertyValue = doGetProperty(evaluatorContext, TopologyNavigationUtil.getImmediateHostTemplate(evaluatorContext.getTopology(), (NodeTemplate) targetTemplate), function);
}
return tryResolveValue(evaluatorContext, targetTemplate, targetTemplate.getProperties(), propertyValue);
}
// Try to fetch from the node.
AbstractPropertyValue propertyValue = getFromPath(evaluatorContext, targetTemplate, targetTemplate.getProperties(), function.getElementNameToFetch());
if (propertyValue == null && targetTemplate instanceof NodeTemplate) {
propertyValue = doGetProperty(evaluatorContext, TopologyNavigationUtil.getImmediateHostTemplate(evaluatorContext.getTopology(), (NodeTemplate) targetTemplate), function);
}
// if the property refers to a function (get_input/get_property then try to resolve it).
return tryResolveValue(evaluatorContext, targetTemplate, targetTemplate.getProperties(), propertyValue);
}
use of org.alien4cloud.tosca.model.templates.RelationshipTemplate in project alien4cloud by alien4cloud.
the class ToscaSerializerTest method simpleTest.
@Ignore
@Test
public void simpleTest() throws IOException, URISyntaxException {
Topology topology = new Topology();
topology.setDependencies(new HashSet<CSARDependency>());
topology.getDependencies().add(new CSARDependency("name1", "1.0"));
topology.getDependencies().add(new CSARDependency("name2", "2.0"));
topology.setInputs(new HashMap<String, PropertyDefinition>());
PropertyDefinition pd1 = new PropertyDefinition();
pd1.setType("string");
pd1.setConstraints(getConstraintList());
pd1.setDescription("A description");
topology.getInputs().put("input1", pd1);
PropertyDefinition pd2 = new PropertyDefinition();
pd2.setType("integer");
pd2.setRequired(false);
pd2.setDefault(new ScalarPropertyValue("10"));
topology.getInputs().put("input2", pd2);
PropertyDefinition pd3 = new PropertyDefinition();
pd3.setType("map");
pd3.setRequired(false);
PropertyDefinition entrySchema = new PropertyDefinition();
entrySchema.setType("integer");
pd3.setEntrySchema(entrySchema);
topology.getInputs().put("input3", pd3);
topology.setNodeTemplates(new HashMap<String, NodeTemplate>());
topology.getNodeTemplates().put("node1", new NodeTemplate());
topology.getNodeTemplates().get("node1").setType("the.node.Type");
topology.getNodeTemplates().get("node1").setProperties(buildSamplePropertyValueMap());
topology.getNodeTemplates().get("node1").setRelationships(new HashMap<String, RelationshipTemplate>());
topology.getNodeTemplates().get("node1").getRelationships().put("hostedOn", new RelationshipTemplate());
topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setTarget("compute2");
topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setRequirementType("capabilities.Capa");
topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setRequirementName("host");
topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setType("relationship.Rel");
topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setProperties(buildSamplePropertyValueMap());
topology.getNodeTemplates().get("node1").setCapabilities(new HashMap<String, Capability>());
Capability capability = new Capability();
capability.setProperties(buildSamplePropertyValueMap());
topology.getNodeTemplates().get("node1").getCapabilities().put("capa1", capability);
// this capability should not appear
topology.getNodeTemplates().get("node1").getCapabilities().put("capa2", new Capability());
topology.getNodeTemplates().get("node1").setArtifacts(new HashMap<String, DeploymentArtifact>());
DeploymentArtifact da = new DeploymentArtifact();
da.setArtifactName("artifact.war");
da.setArtifactRef("010203904872876723");
da.setArtifactType("artifacttypes.Artifact");
topology.getNodeTemplates().get("node1").getArtifacts().put("artifact1", da);
topology.setOutputProperties(new HashMap<String, Set<String>>());
topology.getOutputProperties().put("node1", Sets.newHashSet("prop1", "prop2"));
topology.setOutputAttributes(new HashMap<String, Set<String>>());
topology.getOutputAttributes().put("node1", Sets.newHashSet("att1", "att2"));
Map<String, Object> velocityCtx = new HashMap<String, Object>();
velocityCtx.put("topology", topology);
velocityCtx.put("template_name", "template-id");
velocityCtx.put("template_version", "1.0.0-SNAPSHOT");
velocityCtx.put("template_author", "Foo Bar");
velocityCtx.put("application_description", "Here is a \nmultiline description");
StringWriter writer = new StringWriter();
VelocityUtil.generate("org/alien4cloud/tosca/exporter/topology-alien_dsl_1_4_0.yml.vm", writer, velocityCtx);
System.out.println(writer.toString());
}
use of org.alien4cloud.tosca.model.templates.RelationshipTemplate in project alien4cloud by alien4cloud.
the class TopologyTreeBuilderService method buildPaaSNodeTemplates.
/**
* Fetch information from the repository to complete the topology node template informations with additional data such as artifacts paths etc.
*
* @param topology The topology for which to build PaaSNodeTemplate map.
* @return A map of PaaSNodeTemplate that match the one of the NodeTempaltes in the given topology (and filled with artifact paths etc.).
*/
public Map<String, PaaSNodeTemplate> buildPaaSNodeTemplates(Topology topology) {
Map<String, PaaSNodeTemplate> nodeTemplates = Maps.newHashMap();
// Fill in PaaSNodeTemplate by fetching node types and CSAR path from the repositories.
if (topology.getNodeTemplates() != null) {
for (Entry<String, NodeTemplate> templateEntry : topology.getNodeTemplates().entrySet()) {
NodeTemplate template = templateEntry.getValue();
PaaSNodeTemplate paaSNodeTemplate = new PaaSNodeTemplate(templateEntry.getKey(), template);
fillType(topology, template, paaSNodeTemplate, NodeType.class);
mergeInterfaces(paaSNodeTemplate, template);
if (template.getRelationships() != null) {
for (Map.Entry<String, RelationshipTemplate> relationshipEntry : template.getRelationships().entrySet()) {
RelationshipTemplate relationshipTemplate = relationshipEntry.getValue();
PaaSRelationshipTemplate paaSRelationshipTemplate = new PaaSRelationshipTemplate(relationshipEntry.getKey(), relationshipTemplate, paaSNodeTemplate.getId());
fillType(topology, relationshipTemplate, paaSRelationshipTemplate, RelationshipType.class);
mergeInterfaces(paaSRelationshipTemplate, relationshipTemplate);
paaSNodeTemplate.getRelationshipTemplates().add(paaSRelationshipTemplate);
}
}
Capability scalableCapability = TopologyUtils.getScalableCapability(topology, templateEntry.getKey(), false);
if (scalableCapability != null) {
ScalingPolicy scalingPolicy = TopologyUtils.getScalingPolicy(scalableCapability);
// A node with a scaling policy 1, 1, 1 is a simple node and so do not set scaling policy
if (!ScalingPolicy.NOT_SCALABLE_POLICY.equals(scalingPolicy)) {
paaSNodeTemplate.setScalingPolicy(scalingPolicy);
}
}
if (topology.getGroups() != null) {
Set<String> nodeGroups = Sets.newHashSet();
for (Map.Entry<String, NodeGroup> groupEntry : topology.getGroups().entrySet()) {
if (groupEntry.getValue().getMembers() != null && groupEntry.getValue().getMembers().contains(templateEntry.getKey())) {
nodeGroups.add(groupEntry.getKey());
}
}
paaSNodeTemplate.setGroups(nodeGroups);
}
nodeTemplates.put(templateEntry.getKey(), paaSNodeTemplate);
}
}
return nodeTemplates;
}
Aggregations