use of alien4cloud.tosca.parser.ParsingError in project alien4cloud by alien4cloud.
the class ArchiveIndexer method importArchive.
/**
* Import a pre-parsed archive to alien 4 cloud indexed catalog.
*
* @param source the source of the archive (alien, orchestrator, upload, git).
* @param archiveRoot The parsed archive object.
* @param archivePath The optional path of the archive (should be null if the archive has been java-generated and not parsed).
* @param parsingErrors The non-null list of parsing errors in which to add errors.
* @throws CSARUsedInActiveDeployment
*/
public synchronized void importArchive(final ArchiveRoot archiveRoot, CSARSource source, Path archivePath, List<ParsingError> parsingErrors) throws CSARUsedInActiveDeployment, ToscaTypeAlreadyDefinedInOtherCSAR {
archiveIndexerAuthorizationFilter.checkAuthorization(archiveRoot);
String archiveName = archiveRoot.getArchive().getName();
String archiveVersion = archiveRoot.getArchive().getVersion();
Csar currentIndexedArchive = csarService.get(archiveName, archiveVersion);
if (currentIndexedArchive != null) {
if (Objects.equals(currentIndexedArchive.getWorkspace(), archiveRoot.getArchive().getWorkspace())) {
if (currentIndexedArchive.getHash() != null && currentIndexedArchive.getHash().equals(archiveRoot.getArchive().getHash())) {
// if the archive has not changed do nothing.
parsingErrors.add(new ParsingError(ParsingErrorLevel.INFO, ErrorCode.CSAR_ALREADY_INDEXED, "", null, "The archive already exists in alien4cloud with an identical content (SHA-1 on archive content excluding hidden files is identical).", null, archiveName));
return;
}
} else {
// If the archive existed in a different workspace then throw error
parsingErrors.add(new ParsingError(ParsingErrorLevel.ERROR, ErrorCode.CSAR_ALREADY_EXISTS_IN_ANOTHER_WORKSPACE, "", null, "The archive already exists in alien4cloud in a different workspace.", null, archiveName));
return;
}
}
// dispatch event before indexing
publisher.publishEvent(new BeforeArchiveIndexed(this, archiveRoot));
// Throw an exception if we are trying to override a released (non SNAPSHOT) version.
checkNotReleased(currentIndexedArchive);
// In the current version of alien4cloud we must prevent from overriding an archive that is used in a deployment as we still use catalog information at
// runtime.
checkNotUsedInActiveDeployment(currentIndexedArchive);
// FIXME If the archive already exists but can be indexed we should actually call an editor operation to keep git tracking, or should we just prevent
// that ?
checkIfToscaTypesAreDefinedInOtherArchive(archiveRoot);
// save the archive (before we index and save other data so we can cleanup if anything goes wrong).
if (source == null) {
source = CSARSource.OTHER;
}
archiveRoot.getArchive().setImportSource(source.name());
archiveRoot.getArchive().setHasTopology(archiveRoot.hasToscaTopologyTemplate() && !archiveRoot.getTopology().isEmpty());
archiveRoot.getArchive().setNodeTypesCount(archiveRoot.getNodeTypes().size());
// TODO load transitives dependencies here before saving, as it is not done when parsing
csarService.save(archiveRoot.getArchive());
log.debug("Imported archive {}", archiveRoot.getArchive().getId());
// save the archive in the repository
archiveRepositry.storeCSAR(archiveRoot.getArchive(), archivePath);
// manage images before archive storage in the repository
imageLoader.importImages(archivePath, archiveRoot, parsingErrors);
// index the archive content in elastic-search
indexArchiveTypes(archiveName, archiveVersion, archiveRoot.getArchive().getWorkspace(), archiveRoot, currentIndexedArchive);
indexTopology(archiveRoot, parsingErrors, archiveName, archiveVersion);
publisher.publishEvent(new AfterArchiveIndexed(this, archiveRoot));
}
use of alien4cloud.tosca.parser.ParsingError in project alien4cloud by alien4cloud.
the class ArchiveIndexer method indexTopology.
private void indexTopology(final ArchiveRoot archiveRoot, List<ParsingError> parsingErrors, String archiveName, String archiveVersion) {
Topology topology = archiveRoot.getTopology();
if (topology == null || topology.isEmpty()) {
return;
}
topology.setTags(archiveRoot.getArchive().getTags());
if (archiveRoot.hasToscaTypes()) {
// The archive contains types, we assume those types are used in the embedded topology so we add the dependency to this CSAR
CSARDependency selfDependency = new CSARDependency(archiveRoot.getArchive().getName(), archiveRoot.getArchive().getVersion(), archiveRoot.getArchive().getHash());
topology.getDependencies().add(selfDependency);
}
// init the workflows
TopologyContext topologyContext = workflowBuilderService.buildCachedTopologyContext(new TopologyContext() {
@Override
public String getDSLVersion() {
return archiveRoot.getArchive().getToscaDefinitionsVersion();
}
@Override
public Topology getTopology() {
return topology;
}
@Override
public <T extends AbstractToscaType> T findElement(Class<T> clazz, String elementId) {
return ToscaContext.get(clazz, elementId);
}
});
workflowBuilderService.initWorkflows(topologyContext);
parsingErrors.add(new ParsingError(ParsingErrorLevel.INFO, ErrorCode.TOPOLOGY_DETECTED, "", null, "A topology template has been detected", null, archiveName));
topologyServiceCore.saveTopology(topology);
topologySubstitutionService.updateSubstitutionType(topology, archiveRoot.getArchive());
}
use of alien4cloud.tosca.parser.ParsingError in project alien4cloud by alien4cloud.
the class AbstractArchivePostProcessor method processArtifact.
private void processArtifact(ArchivePathChecker archivePathResolver, AbstractArtifact artifact, ParsingResult<ArchiveRoot> parsedArchive) {
if (!(parsedArchive.getResult().getArchive().getName().equals(artifact.getArchiveName()) && parsedArchive.getResult().getArchive().getVersion().equals(artifact.getArchiveVersion()))) {
// if the artifact is not defined in the current archive then we don't have to perform validation.
return;
}
// Else also inject the workspace
String inputArtifactId = InputArtifactUtil.getInputArtifactId(artifact);
if (StringUtils.isNotBlank(inputArtifactId) && hasInputArtifacts(parsedArchive)) {
if (!parsedArchive.getResult().getTopology().getInputArtifacts().containsKey(inputArtifactId)) {
// The input artifact id does not exist in the topology's definition
parsedArchive.getContext().getParsingErrors().add(new ParsingError(ErrorCode.INVALID_ARTIFACT_REFERENCE, "Invalid artifact reference", null, "Artifact's reference " + artifact.getArtifactRef() + " is not valid", null, artifact.getArtifactRef()));
}
return;
}
URL artifactURL = null;
if (artifact.getRepositoryName() == null) {
// Short notation
try {
// Test if it's an URL
artifactURL = new URL(artifact.getArtifactRef());
} catch (MalformedURLException e) {
if (log.isDebugEnabled()) {
log.debug("Archive artifact validation - Processing local artifact {}", artifact);
}
// Not a URL then must be a relative path to a file inside the csar
processLocalArtifact(archivePathResolver, artifact, parsedArchive);
return;
}
}
if (log.isDebugEnabled()) {
log.debug("Archive artifact validation - Processing remote artifact {}", artifact);
}
if (!repositoryService.canResolveArtifact(artifact.getArtifactRef(), artifact.getRepositoryURL(), artifact.getArtifactRepository(), artifact.getRepositoryCredential())) {
if (artifactURL != null) {
try (InputStream ignored = artifactURL.openStream()) {
// In a best effort try in a generic manner to obtain the artifact
} catch (IOException e) {
parsedArchive.getContext().getParsingErrors().add(new ParsingError(ErrorCode.INVALID_ARTIFACT_REFERENCE, "Invalid artifact reference", null, "Artifact's reference " + artifact.getArtifactRef() + " is not valid", null, artifact.getArtifactRef()));
}
} else {
parsedArchive.getContext().getParsingErrors().add(new ParsingError(ErrorCode.UNRESOLVED_ARTIFACT, "Unresolved artifact", null, "Artifact " + artifact.getArtifactRef() + " cannot be resolved", null, artifact.getArtifactRef()));
}
}
}
use of alien4cloud.tosca.parser.ParsingError in project alien4cloud by alien4cloud.
the class TopologyUtils method normalizeAllNodeTemplateName.
/**
* In alien 4 Cloud we try
* Rename the node template with an invalid name on the topology.
*
* @param topology
* @param parsingErrors
* @param objectToNodeMap
*/
public static void normalizeAllNodeTemplateName(Topology topology, List<ParsingError> parsingErrors, Map<Object, Node> objectToNodeMap) {
if (topology.getNodeTemplates() != null && !topology.getNodeTemplates().isEmpty()) {
Map<String, NodeTemplate> nodeTemplates = Maps.newHashMap(topology.getNodeTemplates());
for (Map.Entry<String, NodeTemplate> nodeEntry : nodeTemplates.entrySet()) {
String nodeName = nodeEntry.getKey();
if (!NameValidationUtils.isValid(nodeName)) {
String newName = StringUtils.stripAccents(nodeName);
newName = NameValidationUtils.DEFAULT_NAME_REPLACE_PATTERN.matcher(newName).replaceAll("_");
if (topology.getNodeTemplates().containsKey(newName)) {
int i = 1;
while (topology.getNodeTemplates().containsKey(newName + i)) {
i++;
}
newName = newName + i;
}
renameNodeTemplate(topology, nodeName, newName);
if (parsingErrors != null) {
Node node = (Node) MapUtil.get(objectToNodeMap, nodeName);
Mark startMark = null;
Mark endMark = null;
if (node != null) {
startMark = node.getStartMark();
endMark = node.getEndMark();
}
parsingErrors.add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.INVALID_NAME, "NodeTemplate", startMark, nodeName, endMark, newName));
}
}
}
}
}
use of alien4cloud.tosca.parser.ParsingError in project alien4cloud by alien4cloud.
the class PropertyValueChecker method checkProperties.
/**
* Check that the value of a property has the right type and match constraints.
*
* @param type The type that defines the properties (NodeType, CapabilityType, RequirementType).
* @param propertyValues The map of values.
* @param templateName The name of the node template /capability template / requirement template.
*/
public void checkProperties(final AbstractInheritableToscaType type, final Map<String, AbstractPropertyValue> propertyValues, final String templateName) {
if (type == null) {
// if the type is null we cannot check properties against their definition. Error is managed elsewhere.
return;
}
ArchiveRoot archiveRoot = (ArchiveRoot) ParsingContextExecution.getRoot().getWrappedInstance();
Topology topology = archiveRoot.getTopology();
for (Map.Entry<String, AbstractPropertyValue> propertyEntry : safe(propertyValues).entrySet()) {
String propertyName = propertyEntry.getKey();
AbstractPropertyValue propertyValue = propertyEntry.getValue();
Node propertyValueNode = ParsingContextExecution.getObjectToNodeMap().get(propertyValue);
if (type.getProperties() == null || !type.getProperties().containsKey(propertyName)) {
ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.ERROR, ErrorCode.UNRECOGNIZED_PROPERTY, templateName, propertyValueNode.getStartMark(), "Property " + propertyName + " does not exist in type " + type.getElementId(), propertyValueNode.getEndMark(), propertyName));
continue;
}
PropertyDefinition propertyDefinition = type.getProperties().get(propertyName);
checkProperty(propertyName, propertyValueNode, propertyValue, propertyDefinition, topology.getInputs(), templateName);
}
}
Aggregations