use of alien4cloud.tosca.model.ArchiveRoot in project alien4cloud by alien4cloud.
the class ArchiveIndexer method importNewArchive.
/**
* <p>
* Import a new empty archive with a topology.
* </p>
* <p>
* Note: this archive is not created from parsing but from alien4cloud API. This service will index the archive and topology as well as initialize the file
* repository and tosca yaml.
* </p>
* <p>
* This method cannot be used to override a topology, even a SNAPSHOT as any update to a topology from the API MUST be done through the editor.
* </p>
*
* @param csar The archive to be imported.
* @param topology The topology to be part of the topology.
* @param topologyPath if the new topology must be created inside this directory to have all its artifacts
*/
@SneakyThrows
public synchronized void importNewArchive(Csar csar, Topology topology, Path topologyPath) {
ArchiveRoot archiveRoot = new ArchiveRoot();
archiveRoot.setArchive(csar);
archiveRoot.setTopology(topology);
csar.setHasTopology(true);
// dispatch event before indexing
publisher.publishEvent(new BeforeArchiveIndexed(this, archiveRoot));
// Ensure that the archive does not already exists
ensureUniqueness(csar.getName(), csar.getVersion());
workflowBuilderService.initWorkflows(workflowBuilderService.buildTopologyContext(topology, csar));
// generate the initial yaml in a temporary directory
if (csar.getYamlFilePath() == null) {
csar.setYamlFilePath("topology.yml");
}
String yaml = exportService.getYaml(csar, topology);
// synch the dependencies before indexing
csar.setDependencies(topology.getDependencies());
// index the archive and topology
csarService.save(csar);
topologyServiceCore.save(topology);
// Initialize the file repository for the archive
if (topologyPath == null) {
// This is an empty topology without artifacts
archiveRepositry.storeCSAR(csar, yaml);
} else {
Files.write(topologyPath.resolve(csar.getYamlFilePath()), yaml.getBytes(Charset.forName("UTF-8")));
archiveRepositry.storeCSAR(csar, topologyPath);
}
topologySubstitutionService.updateSubstitutionType(topology, archiveRoot.getArchive());
// dispatch event after indexing
publisher.publishEvent(new AfterArchiveIndexed(this, archiveRoot));
}
use of alien4cloud.tosca.model.ArchiveRoot in project alien4cloud by alien4cloud.
the class ArchiveIndexer method prepareForUpdate.
private void prepareForUpdate(ArchiveRoot root, Map<String, AbstractToscaType> previousElements) {
updateCreationDates(root.getArtifactTypes(), previousElements);
updateCreationDates(root.getCapabilityTypes(), previousElements);
updateCreationDates(root.getNodeTypes(), previousElements);
updateCreationDates(root.getRelationshipTypes(), previousElements);
updateCreationDates(root.getDataTypes(), previousElements);
updateCreationDates(root.getPolicyTypes(), previousElements);
if (root.getLocalImports() != null) {
for (ArchiveRoot child : root.getLocalImports()) {
prepareForUpdate(child, previousElements);
}
}
}
use of alien4cloud.tosca.model.ArchiveRoot in project alien4cloud by alien4cloud.
the class ArchiveUploadService method upload.
/**
* Upload a TOSCA archive and index its components.
*
* @param path The archive path.
* @param csarSource The source of the upload.
* @return The Csar object from the parsing.
* @throws ParsingException
* @throws CSARUsedInActiveDeployment
*/
@ToscaContextual
public ParsingResult<Csar> upload(Path path, CSARSource csarSource, String workspace) throws ParsingException, CSARUsedInActiveDeployment, ToscaTypeAlreadyDefinedInOtherCSAR {
// parse the archive.
ParsingResult<ArchiveRoot> parsingResult = parser.parseWithExistingContext(path, workspace);
final ArchiveRoot archiveRoot = parsingResult.getResult();
// check if any blocker error has been found during parsing process.
if (parsingResult.hasError(ParsingErrorLevel.ERROR)) {
// do not save anything if any blocker error has been found during import.
return ArchiveParserUtil.toSimpleResult(parsingResult);
}
archiveIndexer.importArchive(archiveRoot, csarSource, path, parsingResult.getContext().getParsingErrors());
try {
suggestionService.postProcessSuggestionFromArchive(parsingResult);
suggestionService.setAllSuggestionIdOnPropertyDefinition();
} catch (Exception e) {
log.error("Could not post process suggestion for the archive <" + archiveRoot.getArchive().getName() + "/" + archiveRoot.getArchive().getVersion() + ">", e);
}
return ArchiveParserUtil.toSimpleResult(parsingResult);
}
use of alien4cloud.tosca.model.ArchiveRoot in project alien4cloud by alien4cloud.
the class EditorTopologyUploadService method processTopology.
/**
* Process the import of a topology archive or yaml in the context of the editor.
*
* @param archivePath The path of the yaml or archive.
*/
public void processTopology(Path archivePath, String workspace) {
// parse the archive.
try {
ParsingResult<ArchiveRoot> parsingResult = toscaArchiveParser.parse(archivePath, true);
processTopologyParseResult(archivePath, parsingResult, workspace);
} catch (ParsingException e) {
// Manage parsing error and dispatch them in the right editor exception
throw new EditorToscaYamlParsingException("The uploaded file to override the topology yaml is not a valid Tosca Yaml.", toParsingResult(e));
}
}
use of alien4cloud.tosca.model.ArchiveRoot in project alien4cloud by alien4cloud.
the class PostMatchingNodeSetupModifierTest method testPropertiesCleanup.
@Test
public void testPropertiesCleanup() throws ParsingException {
Mockito.reset(csarRepositorySearchService);
Mockito.when(csarRepositorySearchService.getArchive("tosca-normative-types", "1.0.0-SNAPSHOT")).thenReturn(Mockito.mock(Csar.class));
NodeType mockType = Mockito.mock(NodeType.class);
Mockito.when(mockType.isAbstract()).thenReturn(true);
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Root"), Mockito.any(Set.class))).thenReturn(mockType);
CapabilityType mockCapaType = Mockito.mock(CapabilityType.class);
Mockito.when(mockCapaType.getElementId()).thenReturn("tosca.capabilities.Root");
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("tosca.capabilities.Root"), Mockito.any(Set.class))).thenReturn(mockCapaType);
ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get("../alien4cloud-test-common/src/test/resources/data/csars/matching-change-cleanup/tosca.yml"));
for (Entry<String, CapabilityType> capabilityTypeEntry : parsingResult.getResult().getCapabilityTypes().entrySet()) {
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq(capabilityTypeEntry.getKey()), Mockito.any(Set.class))).thenReturn(capabilityTypeEntry.getValue());
}
for (Entry<String, NodeType> nodeTypeEntry : parsingResult.getResult().getNodeTypes().entrySet()) {
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq(nodeTypeEntry.getKey()), Mockito.any(Set.class))).thenReturn(nodeTypeEntry.getValue());
}
try {
ToscaContext.init(Sets.newHashSet());
PostMatchingNodeSetupModifier postMatchingNodeSetupModifier = new PostMatchingNodeSetupModifier();
Topology topology = new Topology();
topology.setNodeTemplates(Maps.newHashMap());
topology.getNodeTemplates().put("my_node", TemplateBuilder.buildNodeTemplate(parsingResult.getResult().getNodeTypes().get("org.alien4cloud.test.matching.nodes.LocationCustomImplOne")));
// Configure the deployment config
DeploymentMatchingConfiguration matchingConfiguration = new DeploymentMatchingConfiguration();
matchingConfiguration.setMatchedLocationResources(Maps.newHashMap());
matchingConfiguration.getMatchedLocationResources().put("my_node", "a_location_resource");
NodePropsOverride nodePropsOverride = new NodePropsOverride();
nodePropsOverride.getProperties().put("common_property", new ScalarPropertyValue("p_val"));
nodePropsOverride.getProperties().put("unique_prop", new ScalarPropertyValue("p_val"));
nodePropsOverride.getProperties().put("type_variant_prop", new ScalarPropertyValue("p_val"));
nodePropsOverride.getProperties().put("constraint_variant_prop", new ScalarPropertyValue("p_val"));
NodeCapabilitiesPropsOverride nodeCapabilitiesPropsOverride = new NodeCapabilitiesPropsOverride();
nodeCapabilitiesPropsOverride.getProperties().put("common_property", new ScalarPropertyValue("p_val"));
nodeCapabilitiesPropsOverride.getProperties().put("unique_prop", new ScalarPropertyValue("p_val"));
nodeCapabilitiesPropsOverride.getProperties().put("type_variant_prop", new ScalarPropertyValue("p_val"));
nodeCapabilitiesPropsOverride.getProperties().put("constraint_variant_prop", new ScalarPropertyValue("p_val"));
nodePropsOverride.getCapabilities().put("my_capability", nodeCapabilitiesPropsOverride);
matchingConfiguration.setMatchedNodesConfiguration(Maps.newHashMap());
matchingConfiguration.getMatchedNodesConfiguration().put("my_node", nodePropsOverride);
FlowExecutionContext mockContext = Mockito.mock(FlowExecutionContext.class);
Mockito.when(mockContext.log()).thenReturn(Mockito.mock(FlowExecutionLog.class));
Mockito.when(mockContext.getConfiguration(DeploymentMatchingConfiguration.class, AbstractPostMatchingSetupModifier.class.getSimpleName())).thenReturn(Optional.of(matchingConfiguration));
// All properties resources should be remain as matched type is compliant
postMatchingNodeSetupModifier.process(topology, mockContext);
Assert.assertEquals(4, nodePropsOverride.getProperties().size());
Assert.assertEquals(4, nodePropsOverride.getCapabilities().get("my_capability").getProperties().size());
// Change the type and check that properties are cleared
topology.getNodeTemplates().clear();
topology.getNodeTemplates().put("my_node", TemplateBuilder.buildNodeTemplate(parsingResult.getResult().getNodeTypes().get("org.alien4cloud.test.matching.nodes.LocationCustomImplTwo")));
postMatchingNodeSetupModifier.process(topology, mockContext);
Assert.assertEquals(1, nodePropsOverride.getProperties().size());
Assert.assertEquals(1, nodePropsOverride.getCapabilities().get("my_capability").getProperties().size());
} finally {
ToscaContext.destroy();
}
}
Aggregations