use of alien4cloud.tosca.parser.ParsingError in project yorc-a4c-plugin by ystia.
the class DeployTask method csar2zip.
/**
* Get csar and add entries in zip file for it
* @return relative path to the yml, ex: welcome-types/3.0-SNAPSHOT/welcome-types.yaml
*/
private String csar2zip(ZipOutputStream zout, Csar csar, int location) throws IOException, ParsingException {
// Get path directory to the needed info:
// should be something like: ...../runtime/csar/<module>/<version>/expanded
// We should have a yml or a yaml here
Path csarPath = orchestrator.getCSAR(csar.getName(), csar.getVersion());
String dirname = csarPath.toString();
File directory = new File(dirname);
String relative = csar.getName() + "/" + csar.getVersion() + "/";
String ret = relative + csar.getYamlFilePath();
// All files under this directory must be put in the zip
URI base = directory.toURI();
Deque<File> queue = new LinkedList<>();
queue.push(directory);
while (!queue.isEmpty()) {
directory = queue.pop();
for (File kid : directory.listFiles()) {
String name = base.relativize(kid.toURI()).getPath();
if (kid.isDirectory()) {
queue.push(kid);
} else {
File file = kid;
createZipEntries(relative + name, zout);
if (name.equals(csar.getYamlFilePath())) {
ToscaContext.Context oldCtx = ToscaContext.get();
ParsingResult<ArchiveRoot> parsingResult;
try {
ToscaContext.init(Sets.newHashSet());
parsingResult = orchestrator.getParser().parseFile(Paths.get(file.getAbsolutePath()));
} finally {
ToscaContext.set(oldCtx);
}
if (parsingResult.getContext().getParsingErrors().size() > 0) {
Boolean hasFatalError = false;
for (ParsingError error : parsingResult.getContext().getParsingErrors()) {
if (error.getErrorLevel().equals(ParsingErrorLevel.ERROR)) {
log.error(error.toString());
hasFatalError = true;
} else {
log.warn(error.toString());
}
}
if (hasFatalError) {
continue;
}
}
ArchiveRoot root = parsingResult.getResult();
if (location == LOC_KUBERNETES) {
matchKubernetesImplementation(root);
}
String yaml = orchestrator.getToscaComponentExporter().getYaml(root);
zout.write(yaml.getBytes(Charset.forName("UTF-8")));
} else {
copy(file, zout);
}
}
}
}
return ret;
}
use of alien4cloud.tosca.parser.ParsingError in project alien4cloud by alien4cloud.
the class PropertyValueChecker method checkProperty.
public void checkProperty(String propertyName, Node propertyValueNode, AbstractPropertyValue propertyValue, PropertyDefinition propertyDefinition, Map<String, PropertyDefinition> inputs, String templateName) {
if (propertyValue instanceof FunctionPropertyValue) {
FunctionPropertyValue function = (FunctionPropertyValue) propertyValue;
String parameters = function.getParameters().get(0);
// check get_input only
if (function.getFunction().equals("get_input")) {
if (inputs == null || !inputs.keySet().contains(parameters)) {
ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.ERROR, ErrorCode.MISSING_TOPOLOGY_INPUT, templateName, propertyValueNode.getStartMark(), parameters, propertyValueNode.getEndMark(), propertyName));
}
}
} else if (propertyValue instanceof PropertyValue<?>) {
checkProperty(propertyName, propertyValueNode, (PropertyValue<?>) propertyValue, propertyDefinition, templateName);
}
}
use of alien4cloud.tosca.parser.ParsingError 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 alien4cloud.tosca.parser.ParsingError in project alien4cloud by alien4cloud.
the class RelationshipPostProcessor method getCapabilityByType.
private Capability getCapabilityByType(NodeTemplate targetNodeTemplate, RelationshipTemplate relationshipTemplate, String capabilityType) {
Capability capability = null;
Map<String, Capability> compatibleCapabilityByType = capabilityMatcherService.getCompatibleCapabilityByType(targetNodeTemplate, capabilityType);
Entry<String, Capability> capabilityEntry = null;
if (compatibleCapabilityByType.size() == 1) {
capabilityEntry = compatibleCapabilityByType.entrySet().iterator().next();
} else if (compatibleCapabilityByType.size() > 1) {
capabilityEntry = compatibleCapabilityByType.entrySet().iterator().next();
Node node = ParsingContextExecution.getObjectToNodeMap().get(relationshipTemplate);
ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.REQUIREMENT_CAPABILITY_MULTIPLE_MATCH, null, node.getStartMark(), null, node.getEndMark(), relationshipTemplate.getRequirementName()));
}
if (capabilityEntry != null) {
capability = capabilityEntry.getValue();
relationshipTemplate.setTargetedCapabilityName(capabilityEntry.getKey());
}
return capability;
}
use of alien4cloud.tosca.parser.ParsingError in project alien4cloud by alien4cloud.
the class TopologyPostProcessor method process.
@Override
public void process(Topology instance) {
if (instance == null) {
return;
}
ArchiveRoot archiveRoot = ParsingContextExecution.getRootObj();
// The yaml node for the topology
Node node = ParsingContextExecution.getObjectToNodeMap().get(instance);
setDependencies(instance, archiveRoot);
if (instance.isEmpty()) {
// if the topology doesn't contains any node template it won't be imported so add a warning.
ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.EMPTY_TOPOLOGY, null, node.getStartMark(), null, node.getEndMark(), ""));
}
// archive name and version
instance.setArchiveName(archiveRoot.getArchive().getName());
instance.setArchiveVersion(archiveRoot.getArchive().getVersion());
// Inputs validation
safe(instance.getInputs()).entrySet().forEach(propertyDefinitionPostProcessor);
safe(instance.getInputArtifacts()).values().forEach(typeDeploymentArtifactPostProcessor);
int groupIndex = 0;
// Groups validation
for (NodeGroup nodeGroup : safe(instance.getGroups()).values()) {
nodeGroup.setIndex(groupIndex++);
groupPostProcessor.process(nodeGroup);
}
// Policies templates validation
safe(instance.getPolicies()).forEach((policyName, policyTemplate) -> {
// set the templateName
policyTemplate.setName(policyName);
policyTemplatePostProcessor.process(policyTemplate);
});
// Node templates validation
for (Map.Entry<String, NodeTemplate> nodeTemplateEntry : safe(instance.getNodeTemplates()).entrySet()) {
nodeTemplateEntry.getValue().setName(nodeTemplateEntry.getKey());
nodeTemplatePostProcessor.process(nodeTemplateEntry.getValue());
}
safe(instance.getNodeTemplates()).values().forEach(nodeTemplateRelationshipPostProcessor);
substitutionMappingPostProcessor.process(instance.getSubstitutionMapping());
// first validate names
TopologyUtils.normalizeAllNodeTemplateName(instance, ParsingContextExecution.getParsingErrors(), ParsingContextExecution.getObjectToNodeMap());
// Post process workflows
workflowPostProcessor.processWorkflows(instance, node);
}
Aggregations