Search in sources :

Example 1 with ArchiveUsageRequestEvent

use of org.alien4cloud.tosca.catalog.events.ArchiveUsageRequestEvent in project alien4cloud by alien4cloud.

the class CsarService method getCsarRelatedResourceList.

/**
 * Get the list of resources that are using the given archive.
 *
 * @param csar The archive for which to get usage.
 * @return The list of usage of the archive.
 */
public List<Usage> getCsarRelatedResourceList(Csar csar) {
    if (csar == null) {
        log.debug("You have requested a resource list for a invalid csar object : <" + csar + ">");
        return Lists.newArrayList();
    }
    ArchiveUsageRequestEvent archiveUsageRequestEvent = new ArchiveUsageRequestEvent(this, csar.getName(), csar.getVersion());
    // Archive from applications are used by the application.
    if (Objects.equals(csar.getDelegateType(), ArchiveDelegateType.APPLICATION.toString())) {
        // The CSAR is from an application's topology
        Application application = applicationService.checkAndGetApplication(csar.getDelegateId());
        archiveUsageRequestEvent.addUsage(new Usage(application.getName(), Application.class.getSimpleName().toLowerCase(), csar.getDelegateId(), csar.getWorkspace()));
    }
    // a csar that is a dependency of another csar can not be deleted
    Csar[] relatedCsars = getDependantCsars(csar.getName(), csar.getVersion());
    if (ArrayUtils.isNotEmpty(relatedCsars)) {
        archiveUsageRequestEvent.addUsages(generateCsarsInfo(relatedCsars));
    }
    // check if some of the nodes are used in topologies.
    Topology[] topologies = getDependantTopologies(csar.getName(), csar.getVersion());
    if (topologies != null && topologies.length > 0) {
        archiveUsageRequestEvent.addUsages(generateTopologiesInfo(topologies));
    }
    // a csar that is a dependency of location can not be deleted
    Location[] relatedLocations = getDependantLocations(csar.getName(), csar.getVersion());
    if (relatedLocations != null && relatedLocations.length > 0) {
        archiveUsageRequestEvent.addUsages(generateLocationsInfo(relatedLocations));
    }
    publisher.publishEvent(archiveUsageRequestEvent);
    return archiveUsageRequestEvent.getUsages();
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) Usage(alien4cloud.model.common.Usage) ArchiveUsageRequestEvent(org.alien4cloud.tosca.catalog.events.ArchiveUsageRequestEvent) Topology(org.alien4cloud.tosca.model.templates.Topology) Application(alien4cloud.model.application.Application) Location(alien4cloud.model.orchestrators.locations.Location)

Example 2 with ArchiveUsageRequestEvent

use of org.alien4cloud.tosca.catalog.events.ArchiveUsageRequestEvent in project alien4cloud by alien4cloud.

the class ServiceResourceServiceTest method testReportArchiveUsage.

@Test
public void testReportArchiveUsage() {
    ServiceResource serviceResource = new ServiceResource();
    serviceResource.setId("service1");
    serviceResource.setName("service name 1");
    serviceResource.setDependency(new CSARDependency("org.alien4cloud.archives:my-archive", "1.0.0-SNAPSHOT"));
    alienDao.save(serviceResource);
    ArchiveUsageRequestEvent archiveUsageRequestEvent = new ArchiveUsageRequestEvent(this, "org.alien4cloud.archives:my-archive", "1.0.0-SNAPSHOT");
    serviceResourceService.reportArchiveUsage(archiveUsageRequestEvent);
    Assert.assertEquals(1, archiveUsageRequestEvent.getUsages().size());
    Assert.assertEquals("service1", archiveUsageRequestEvent.getUsages().get(0).getResourceId());
    Assert.assertEquals("serviceresource", archiveUsageRequestEvent.getUsages().get(0).getResourceType());
    Assert.assertEquals("service name 1", archiveUsageRequestEvent.getUsages().get(0).getResourceName());
    archiveUsageRequestEvent = new ArchiveUsageRequestEvent(this, "org.alien4cloud.archives:other-archive", "1.0.0-SNAPSHOT");
    serviceResourceService.reportArchiveUsage(archiveUsageRequestEvent);
    Assert.assertEquals(0, archiveUsageRequestEvent.getUsages().size());
    archiveUsageRequestEvent = new ArchiveUsageRequestEvent(this, "org.alien4cloud.archives:my-archive", "1.0.1-SNAPSHOT");
    serviceResourceService.reportArchiveUsage(archiveUsageRequestEvent);
    Assert.assertEquals(0, archiveUsageRequestEvent.getUsages().size());
}
Also used : ArchiveUsageRequestEvent(org.alien4cloud.tosca.catalog.events.ArchiveUsageRequestEvent) ServiceResource(alien4cloud.model.service.ServiceResource) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) Test(org.junit.Test)

Aggregations

ArchiveUsageRequestEvent (org.alien4cloud.tosca.catalog.events.ArchiveUsageRequestEvent)2 Application (alien4cloud.model.application.Application)1 Usage (alien4cloud.model.common.Usage)1 Location (alien4cloud.model.orchestrators.locations.Location)1 ServiceResource (alien4cloud.model.service.ServiceResource)1 CSARDependency (org.alien4cloud.tosca.model.CSARDependency)1 Csar (org.alien4cloud.tosca.model.Csar)1 Topology (org.alien4cloud.tosca.model.templates.Topology)1 Test (org.junit.Test)1