Search in sources :

Example 1 with CapabilityNotAvailableException

use of io.cdap.cdap.internal.capability.CapabilityNotAvailableException in project cdap by caskdata.

the class ApplicationLifecycleServiceTest method testCapabilityMetaDataDeletion.

@Test
public void testCapabilityMetaDataDeletion() throws Exception {
    Class<CapabilityAppWithWorkflow> appWithWorkflowClass = CapabilityAppWithWorkflow.class;
    Requirements declaredAnnotation = appWithWorkflowClass.getDeclaredAnnotation(Requirements.class);
    Set<String> expected = Arrays.stream(declaredAnnotation.capabilities()).collect(Collectors.toSet());
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, appWithWorkflowClass.getSimpleName(), "1.0.0-SNAPSHOT");
    Location appJar = AppJarHelper.createDeploymentJar(locationFactory, appWithWorkflowClass);
    File appJarFile = new File(tmpFolder.newFolder(), String.format("%s-%s.jar", artifactId.getName(), artifactId.getVersion().getVersion()));
    Locations.linkOrCopyOverwrite(appJar, appJarFile);
    appJar.delete();
    // deploy app
    try {
        applicationLifecycleService.deployAppAndArtifact(NamespaceId.DEFAULT, appWithWorkflowClass.getSimpleName(), artifactId, appJarFile, null, null, programId -> {
        }, true);
        Assert.fail("Expecting exception");
    } catch (CapabilityNotAvailableException ex) {
    // expected
    }
    for (String capability : declaredAnnotation.capabilities()) {
        CapabilityConfig capabilityConfig = new CapabilityConfig("Test", CapabilityStatus.ENABLED, capability, Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
        capabilityWriter.addOrUpdateCapability(capability, CapabilityStatus.ENABLED, capabilityConfig);
    }
    applicationLifecycleService.deployAppAndArtifact(NamespaceId.DEFAULT, appWithWorkflowClass.getSimpleName(), artifactId, appJarFile, null, null, programId -> {
    }, true);
    // Check for the capability metadata
    ApplicationId appId = NamespaceId.DEFAULT.app(appWithWorkflowClass.getSimpleName());
    MetadataEntity appMetadataId = appId.toMetadataEntity();
    Assert.assertFalse(metadataStorage.read(new Read(appMetadataId, MetadataScope.SYSTEM, MetadataKind.PROPERTY)).isEmpty());
    Map<String, String> metadataProperties = metadataStorage.read(new Read(appMetadataId)).getProperties(MetadataScope.SYSTEM);
    String capabilityMetaData = metadataProperties.get(AppSystemMetadataWriter.CAPABILITY_TAG);
    Set<String> actual = Arrays.stream(capabilityMetaData.split(AppSystemMetadataWriter.CAPABILITY_DELIMITER)).collect(Collectors.toSet());
    Assert.assertEquals(expected, actual);
    // Remove the application and verify that all metadata is removed
    applicationLifecycleService.removeApplication(appId);
    Assert.assertTrue(metadataStorage.read(new Read(appMetadataId)).isEmpty());
}
Also used : MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) CapabilityNotAvailableException(io.cdap.cdap.internal.capability.CapabilityNotAvailableException) CapabilityConfig(io.cdap.cdap.internal.capability.CapabilityConfig) Requirements(io.cdap.cdap.api.annotation.Requirements) Read(io.cdap.cdap.spi.metadata.Read) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ProgramId(io.cdap.cdap.proto.id.ProgramId) CapabilityAppWithWorkflow(io.cdap.cdap.CapabilityAppWithWorkflow) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) File(java.io.File) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test)

Example 2 with CapabilityNotAvailableException

use of io.cdap.cdap.internal.capability.CapabilityNotAvailableException in project cdap by caskdata.

the class ScheduleTaskRunner method launch.

public void launch(Job job) throws Exception {
    ProgramSchedule schedule = job.getSchedule();
    ProgramId programId = schedule.getProgramId();
    Map<String, String> userArgs = getUserArgs(schedule, propertiesResolver);
    Map<String, String> systemArgs = new HashMap<>(propertiesResolver.getSystemProperties(programId));
    // Let the triggers update the arguments first before setting the triggering schedule info
    ((SatisfiableTrigger) job.getSchedule().getTrigger()).updateLaunchArguments(job.getSchedule(), job.getNotifications(), userArgs, systemArgs);
    TriggeringScheduleInfo triggeringScheduleInfo = getTriggeringScheduleInfo(job);
    systemArgs.put(ProgramOptionConstants.TRIGGERING_SCHEDULE_INFO, GSON.toJson(triggeringScheduleInfo));
    try {
        execute(programId, systemArgs, userArgs);
        LOG.info("Successfully started program {} in schedule {}.", schedule.getProgramId(), schedule.getName());
    } catch (CapabilityNotAvailableException ex) {
        LOG.debug("Ignoring program {} in schedule {}.", schedule.getProgramId(), schedule.getName(), ex);
    }
}
Also used : SatisfiableTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger) CapabilityNotAvailableException(io.cdap.cdap.internal.capability.CapabilityNotAvailableException) HashMap(java.util.HashMap) TriggeringScheduleInfo(io.cdap.cdap.api.schedule.TriggeringScheduleInfo) ProgramId(io.cdap.cdap.proto.id.ProgramId)

Example 3 with CapabilityNotAvailableException

use of io.cdap.cdap.internal.capability.CapabilityNotAvailableException in project cdap by caskdata.

the class ApplicationLifecycleService method processApplications.

private void processApplications(List<Map.Entry<ApplicationId, ApplicationSpecification>> list, Consumer<ApplicationDetail> consumer) {
    Set<ApplicationId> appIds = list.stream().map(Map.Entry::getKey).collect(Collectors.toSet());
    Set<? extends EntityId> visible = accessEnforcer.isVisible(appIds, authenticationContext.getPrincipal());
    list.removeIf(entry -> !visible.contains(entry.getKey()));
    appIds.removeIf(id -> !visible.contains(id));
    try {
        Map<ApplicationId, String> owners = ownerAdmin.getOwnerPrincipals(appIds);
        for (Map.Entry<ApplicationId, ApplicationSpecification> entry : list) {
            ApplicationDetail applicationDetail = ApplicationDetail.fromSpec(entry.getValue(), owners.get(entry.getKey()));
            try {
                capabilityReader.checkAllEnabled(entry.getValue());
            } catch (CapabilityNotAvailableException ex) {
                LOG.debug("Application {} is ignored due to exception.", applicationDetail.getName(), ex);
                continue;
            }
            consumer.accept(applicationDetail);
        }
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}
Also used : ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) CapabilityNotAvailableException(io.cdap.cdap.internal.capability.CapabilityNotAvailableException) ApplicationDetail(io.cdap.cdap.proto.ApplicationDetail) IOException(java.io.IOException) JsonIOException(com.google.gson.JsonIOException) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Example 4 with CapabilityNotAvailableException

use of io.cdap.cdap.internal.capability.CapabilityNotAvailableException in project cdap by caskdata.

the class ArtifactHttpHandler method getArtifactPlugin.

@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/" + "versions/{artifact-version}/extensions/{plugin-type}/plugins/{plugin-name}")
public void getArtifactPlugin(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @PathParam("plugin-type") String pluginType, @PathParam("plugin-name") String pluginName, @QueryParam("scope") @DefaultValue("user") final String scope, @QueryParam("artifactName") final String pluginArtifactName, @QueryParam("artifactVersion") String pluginVersion, @QueryParam("artifactScope") final String pluginScope, @QueryParam("limit") @DefaultValue("2147483647") String limit, @QueryParam("order") @DefaultValue("UNORDERED") String order) throws NamespaceNotFoundException, BadRequestException, ArtifactNotFoundException, InvalidArtifactRangeException {
    NamespaceId namespace = Ids.namespace(namespaceId);
    NamespaceId artifactNamespace = validateAndGetScopedNamespace(namespace, scope);
    final NamespaceId pluginArtifactNamespace = validateAndGetScopedNamespace(namespace, pluginScope);
    ArtifactId parentArtifactId = validateAndGetArtifactId(artifactNamespace, artifactName, artifactVersion);
    final ArtifactVersionRange pluginRange = pluginVersion == null ? null : ArtifactVersionRange.parse(pluginVersion);
    int limitNumber = Integer.parseInt(limit);
    limitNumber = limitNumber <= 0 ? Integer.MAX_VALUE : limitNumber;
    ArtifactSortOrder sortOrder = ArtifactSortOrder.valueOf(order);
    Predicate<ArtifactId> predicate = new Predicate<ArtifactId>() {

        @Override
        public boolean apply(ArtifactId input) {
            // by default, the scoped namespace is for USER scope
            return (((pluginScope == null && NamespaceId.SYSTEM.equals(input.getParent())) || pluginArtifactNamespace.equals(input.getParent())) && (pluginArtifactName == null || pluginArtifactName.equals(input.getArtifact())) && (pluginRange == null || pluginRange.versionIsInRange(new ArtifactVersion(input.getVersion()))));
        }
    };
    try {
        SortedMap<ArtifactDescriptor, PluginClass> plugins = artifactRepository.getPlugins(namespace, Id.Artifact.fromEntityId(parentArtifactId), pluginType, pluginName, predicate, limitNumber, sortOrder);
        List<PluginInfo> pluginInfos = Lists.newArrayList();
        // flatten the map
        for (Map.Entry<ArtifactDescriptor, PluginClass> pluginsEntry : plugins.entrySet()) {
            ArtifactDescriptor pluginArtifact = pluginsEntry.getKey();
            ArtifactSummary pluginArtifactSummary = ArtifactSummary.from(pluginArtifact.getArtifactId());
            PluginClass pluginClass = pluginsEntry.getValue();
            try {
                capabilityReader.checkAllEnabled(pluginClass.getRequirements().getCapabilities());
            } catch (CapabilityNotAvailableException e) {
                LOG.debug("Skipping plugin {} because of disabled capability", pluginClass, e);
                continue;
            }
            pluginInfos.add(new PluginInfo(pluginClass, pluginArtifactSummary));
        }
        responder.sendJson(HttpResponseStatus.OK, GSON.toJson(pluginInfos));
    } catch (PluginNotExistsException e) {
        responder.sendString(HttpResponseStatus.NOT_FOUND, e.getMessage());
    } catch (IOException e) {
        LOG.error("Exception looking up plugins for artifact {}", parentArtifactId, e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error reading plugins for the artifact from the store.");
    }
}
Also used : CapabilityNotAvailableException(io.cdap.cdap.internal.capability.CapabilityNotAvailableException) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ArtifactVersionRange(io.cdap.cdap.api.artifact.ArtifactVersionRange) IOException(java.io.IOException) ArtifactSortOrder(io.cdap.cdap.proto.artifact.ArtifactSortOrder) Predicate(com.google.common.base.Predicate) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ArtifactDescriptor(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor) PluginInfo(io.cdap.cdap.proto.artifact.PluginInfo) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) PluginClass(io.cdap.cdap.api.plugin.PluginClass) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 5 with CapabilityNotAvailableException

use of io.cdap.cdap.internal.capability.CapabilityNotAvailableException in project cdap by caskdata.

the class ArtifactHttpHandler method getArtifactPlugins.

@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/versions/{artifact-version}/extensions/{plugin-type}")
public void getArtifactPlugins(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @PathParam("plugin-type") String pluginType, @QueryParam("scope") @DefaultValue("user") String scope) throws NamespaceNotFoundException, BadRequestException, ArtifactNotFoundException {
    NamespaceId namespace = Ids.namespace(namespaceId);
    NamespaceId artifactNamespace = validateAndGetScopedNamespace(namespace, scope);
    ArtifactId artifactId = validateAndGetArtifactId(artifactNamespace, artifactName, artifactVersion);
    try {
        SortedMap<ArtifactDescriptor, Set<PluginClass>> plugins = artifactRepository.getPlugins(namespace, Id.Artifact.fromEntityId(artifactId), pluginType);
        List<PluginSummary> pluginSummaries = Lists.newArrayList();
        // flatten the map
        for (Map.Entry<ArtifactDescriptor, Set<PluginClass>> pluginsEntry : plugins.entrySet()) {
            ArtifactDescriptor pluginArtifact = pluginsEntry.getKey();
            ArtifactSummary pluginArtifactSummary = ArtifactSummary.from(pluginArtifact.getArtifactId());
            for (PluginClass pluginClass : pluginsEntry.getValue()) {
                try {
                    capabilityReader.checkAllEnabled(pluginClass.getRequirements().getCapabilities());
                } catch (CapabilityNotAvailableException e) {
                    LOG.debug("Skipping plugin {} because of disabled capability", pluginClass, e);
                    continue;
                }
                pluginSummaries.add(new PluginSummary(pluginClass.getName(), pluginClass.getType(), pluginClass.getCategory(), pluginClass.getClassName(), pluginArtifactSummary, pluginClass.getDescription()));
            }
        }
        responder.sendJson(HttpResponseStatus.OK, GSON.toJson(pluginSummaries));
    } catch (IOException e) {
        LOG.error("Exception looking up plugins for artifact {}", artifactId, e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error reading plugins for the artifact from the store.");
    }
}
Also used : CapabilityNotAvailableException(io.cdap.cdap.internal.capability.CapabilityNotAvailableException) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) IOException(java.io.IOException) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ArtifactDescriptor(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) PluginSummary(io.cdap.cdap.proto.artifact.PluginSummary) PluginClass(io.cdap.cdap.api.plugin.PluginClass) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

CapabilityNotAvailableException (io.cdap.cdap.internal.capability.CapabilityNotAvailableException)5 HashMap (java.util.HashMap)4 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)3 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)3 IOException (java.io.IOException)3 Map (java.util.Map)3 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)2 PluginClass (io.cdap.cdap.api.plugin.PluginClass)2 ArtifactDescriptor (io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor)2 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)2 ProgramId (io.cdap.cdap.proto.id.ProgramId)2 SortedMap (java.util.SortedMap)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Predicate (com.google.common.base.Predicate)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 JsonIOException (com.google.gson.JsonIOException)1 CapabilityAppWithWorkflow (io.cdap.cdap.CapabilityAppWithWorkflow)1 Requirements (io.cdap.cdap.api.annotation.Requirements)1 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)1