private void ValidateCloudServiceArchive(ToscaCloudServiceArchive toscaCloudServiceArchive) { List <ValidationResult> validationResults; validator.TryValidateRecursively(toscaCloudServiceArchive, out validationResults); if (validationResults.Any()) { throw new ToscaValidationException(string.Join(Environment.NewLine, validationResults.Select(r => r.ErrorMessage))); } }
/// <summary> /// Loads Cloud Service Archive (CSAR) file and all its dependencies /// </summary> /// <param name="archiveStream">Stream to Cloud Service Archive (CSAR) zip file</param> /// <param name="alternativePath">Path for dependencies lookup outside the archive</param> /// <exception cref="ToscaMetadataFileNotFoundException">Thrown when TOSCA.meta file not found in the archive.</exception> /// <exception cref="InvalidDataException">The contents of the stream could not be interpreted as a zip archive or the archive is corrupt and cannot be read.</exception> /// <returns>A valid instance of ToscaCloudServiceArchive</returns> public ToscaCloudServiceArchive Load(Stream archiveStream, string alternativePath = null) { using (var archive = CreateZipArchiveFromStream(archiveStream)) { var archiveEntries = archive.GetArchiveEntriesDictionary(); var toscaMetaArchiveEntry = GetToscaMetaArchiveEntry(archiveEntries); var toscaMetadata = metadataParser.Parse(toscaMetaArchiveEntry.Open()); var toscaCloudServiceArchive = new ToscaCloudServiceArchive(toscaMetadata); LoadDependenciesRecursively(toscaCloudServiceArchive, archiveEntries, toscaMetadata.EntryDefinitions, alternativePath); ValidateCloudServiceArchive(toscaCloudServiceArchive); return(toscaCloudServiceArchive); } }
/// <summary> /// Saves Cloud Service Archive to ZIP file /// </summary> /// <param name="toscaCloudServiceArchive"></param> /// <param name="stream"></param> /// <exception cref="ArgumentException">The stream is already closed or does not support reading.</exception> /// <exception cref="ArgumentNullException"><paramref name="stream" /> is null.</exception> /// <exception cref="InvalidDataException">The contents of the stream are not in the zip archive format.</exception> public void Save(ToscaCloudServiceArchive toscaCloudServiceArchive, Stream stream) { using (var zipArchive = new ZipArchive(stream, ZipArchiveMode.Create, true)) { SaveMetadata(toscaCloudServiceArchive, zipArchive); foreach (var serviceTemplate in toscaCloudServiceArchive.ToscaServiceTemplates) { SaveServiceTemplates(zipArchive, serviceTemplate); } foreach (var artifact in toscaCloudServiceArchive.Artifacts) { SaveArtifact(zipArchive, artifact.Key, artifact.Value); } } }
public ToscaNodeTypeInheritanceWalker(ToscaCloudServiceArchive cloudServiceArchive, Action <string, ToscaNodeType> action) { nodeTypes = cloudServiceArchive.NodeTypes; graph = new AdjacencyGraph <string, ToscaGraphEdge>(); graph.AddVertexRange(cloudServiceArchive.NodeTypes.Select(_ => _.Key)); foreach (var toscaNodeType in cloudServiceArchive.NodeTypes) { if (!toscaNodeType.Value.IsRoot()) { graph.AddEdge(new ToscaGraphEdge( toscaNodeType.Value.DerivedFrom, toscaNodeType.Key)); } } this.action = action; }
private void LoadDependenciesRecursively(ToscaCloudServiceArchive cloudServiceArchive, IReadOnlyDictionary <string, ZipArchiveEntry> zipArchiveEntries, string serviceTemplateName, string alternativePath) { if (cloudServiceArchive.ToscaServiceTemplates.ContainsKey(serviceTemplateName)) { return; } var serviceTemplate = LoadToscaServiceTemplate(alternativePath, zipArchiveEntries, serviceTemplateName, cloudServiceArchive); cloudServiceArchive.AddToscaServiceTemplate(serviceTemplateName, serviceTemplate); foreach (var nodeType in serviceTemplate.NodeTypes) { foreach (var artifact in nodeType.Value.Artifacts) { ZipArchiveEntry zipArchiveEntry; var artifactPath = artifact.Value.File; if (zipArchiveEntries.TryGetValue(artifactPath, out zipArchiveEntry)) { cloudServiceArchive.AddArtifact(artifactPath, zipArchiveEntry.Open().ReadAllBytes()); } if (alternativePath != null) { var artifactFullPath = fileSystem.Path.Combine(alternativePath, artifactPath); if (fileSystem.File.Exists(artifactFullPath)) { using (var stream = fileSystem.File.Open(artifactFullPath, FileMode.Open)) { cloudServiceArchive.AddArtifact(artifactPath, stream.ReadAllBytes()); } } } } } foreach (var importFile in serviceTemplate.Imports.SelectMany(import => import.Values)) { LoadDependenciesRecursively(cloudServiceArchive, zipArchiveEntries, importFile.File, alternativePath); } }
private ToscaServiceTemplate LoadToscaServiceTemplate(string alternativePath, IReadOnlyDictionary <string, ZipArchiveEntry> archiveEntries, string filePath, ToscaCloudServiceArchive toscaCloudServiceArchive) { var importStream = GetImportStream(alternativePath, archiveEntries, filePath); try { return(serviceTemplateParser.Parse(importStream)); } catch (ToscaBaseException toscaBaseException) { throw new ToscaParsingException( string.Format("Failed to load definitions file {0} due to an error: {1}", filePath, toscaBaseException.GetaAllMessages()), toscaBaseException); } }
/// <summary> /// Sets archive that the node belongs to /// </summary> /// <param name="newCloudServiceArchive"></param> internal void SetCloudServiceArchive(ToscaCloudServiceArchive newCloudServiceArchive) { cloudServiceArchive = newCloudServiceArchive; }
private void SaveMetadata(ToscaCloudServiceArchive toscaCloudServiceArchive, ZipArchive zipArchive) { var metadataEntry = zipArchive.CreateEntry(ToscaCloudServiceArchiveLoader.ToscaMetaFileName); metadataSerializer.Serialize(metadataEntry.Open(), toscaCloudServiceArchive.ToscaMetadata); }