Search in sources :

Example 16 with NamespaceId

use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class ArtifactStore method getPluginClasses.

/**
 * Get all plugin classes of the given type and name that extend the given parent artifact.
 * Results are returned as a map from plugin artifact to plugins in that artifact.
 *
 * @param namespace the namespace to search for plugins. The system namespace is always included.
 * @param parentArtifactRange the parent artifact range to find plugins for
 * @param type the type of plugin to look for
 * @param name the name of the plugin to look for
 * @param pluginRange the predicate for the plugins
 * @param limit the limit number of the result
 * @param order the order of the result
 * @return an unmodifiable map of plugin artifact to plugin classes of the given type and name, accessible by the
 *         given artifact. The map will never be null, and will never be empty.
 * @throws PluginNotExistsException if no plugin with the given type and name exists in the namespace
 * @throws IOException if there was an exception reading metadata from the metastore
 */
public SortedMap<ArtifactDescriptor, PluginClass> getPluginClasses(NamespaceId namespace, ArtifactRange parentArtifactRange, String type, String name, @Nullable final Predicate<io.cdap.cdap.proto.id.ArtifactId> pluginRange, int limit, ArtifactSortOrder order) throws IOException, ArtifactNotFoundException, PluginNotExistsException {
    SortedMap<ArtifactDescriptor, PluginClass> result = TransactionRunners.run(transactionRunner, context -> {
        StructuredTable artifactDataTable = getTable(context, StoreDefinition.ArtifactStore.ARTIFACT_DATA_TABLE);
        List<ArtifactDetail> parentArtifactDetails = getArtifacts(artifactDataTable, parentArtifactRange, Integer.MAX_VALUE, null);
        if (parentArtifactDetails.isEmpty()) {
            throw new ArtifactNotFoundException(parentArtifactRange.getNamespace(), parentArtifactRange.getName());
        }
        SortedMap<ArtifactDescriptor, PluginClass> plugins = order == ArtifactSortOrder.DESC ? new TreeMap<>(Collections.reverseOrder()) : new TreeMap<>();
        List<Id.Artifact> parentArtifacts = new ArrayList<>();
        for (ArtifactDetail parentArtifactDetail : parentArtifactDetails) {
            parentArtifacts.add(Id.Artifact.from(Id.Namespace.from(parentArtifactRange.getNamespace()), parentArtifactDetail.getDescriptor().getArtifactId()));
            Set<PluginClass> parentPlugins = parentArtifactDetail.getMeta().getClasses().getPlugins();
            for (PluginClass pluginClass : parentPlugins) {
                if (pluginClass.getName().equals(name) && pluginClass.getType().equals(type) && isAllowed(pluginClass)) {
                    plugins.put(parentArtifactDetail.getDescriptor(), pluginClass);
                    break;
                }
            }
        }
        // Add all plugins that extends from the given set of parents
        StructuredTable pluginTable = getTable(context, StoreDefinition.ArtifactStore.PLUGIN_DATA_TABLE);
        PluginKeyPrefix pluginKey = new PluginKeyPrefix(parentArtifactRange.getNamespace(), parentArtifactRange.getName(), type, name);
        try (CloseableIterator<StructuredRow> iterator = pluginTable.scan(Range.singleton(pluginKey.keys), Integer.MAX_VALUE)) {
            addPluginsInRangeToMap(namespace, parentArtifacts, iterator, plugins, pluginRange, limit);
        }
        // Add all universal plugins
        StructuredTable uniPluginTable = getTable(context, StoreDefinition.ArtifactStore.UNIV_PLUGIN_DATA_TABLE);
        for (String ns : Arrays.asList(namespace.getNamespace(), NamespaceId.SYSTEM.getNamespace())) {
            UniversalPluginKeyPrefix universalPluginKey = new UniversalPluginKeyPrefix(ns, type, name);
            try (CloseableIterator<StructuredRow> iterator = uniPluginTable.scan(Range.singleton(universalPluginKey.keys), Integer.MAX_VALUE)) {
                addPluginsInRangeToMap(namespace, parentArtifacts, iterator, plugins, pluginRange, limit);
            }
        }
        return Collections.unmodifiableSortedMap(plugins);
    }, IOException.class, ArtifactNotFoundException.class);
    if (result.isEmpty()) {
        throw new PluginNotExistsException(new NamespaceId(parentArtifactRange.getNamespace()), type, name);
    }
    return result;
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable) ArrayList(java.util.ArrayList) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) PluginClass(io.cdap.cdap.api.plugin.PluginClass) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException)

Example 17 with NamespaceId

use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class AbstractContext method getDataset.

@Override
public <T extends Dataset> T getDataset(final String namespace, final String name, final Map<String, String> arguments, final AccessType accessType) throws DatasetInstantiationException {
    return Retries.callWithRetries(() -> {
        T dataset = datasetCache.getDataset(namespace, name, arguments, accessType);
        if (dataset instanceof RuntimeProgramContextAware) {
            DatasetId datasetId = new NamespaceId(namespace).dataset(name);
            ((RuntimeProgramContextAware) dataset).setContext(createRuntimeProgramContext(datasetId));
        }
        return dataset;
    }, retryStrategy);
}
Also used : NamespaceId(io.cdap.cdap.proto.id.NamespaceId) RuntimeProgramContextAware(io.cdap.cdap.data.RuntimeProgramContextAware) DatasetId(io.cdap.cdap.proto.id.DatasetId)

Example 18 with NamespaceId

use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class Artifacts method toProtoArtifactId.

/**
 * Converts a {@link ArtifactId} to {@link io.cdap.cdap.proto.id.ArtifactId}.
 *
 * @param namespaceId the user namespace to use
 * @param artifactId the artifact id to convert
 */
public static io.cdap.cdap.proto.id.ArtifactId toProtoArtifactId(NamespaceId namespaceId, ArtifactId artifactId) {
    ArtifactScope scope = artifactId.getScope();
    NamespaceId artifactNamespace = scope == ArtifactScope.SYSTEM ? NamespaceId.SYSTEM : namespaceId;
    return artifactNamespace.artifact(artifactId.getName(), artifactId.getVersion().getVersion());
}
Also used : ArtifactScope(io.cdap.cdap.api.artifact.ArtifactScope) NamespaceId(io.cdap.cdap.proto.id.NamespaceId)

Example 19 with NamespaceId

use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class LocalPreferencesFetcherInternal method get.

/**
 * Get preferences for the given identify
 */
public PreferencesDetail get(EntityId entityId, boolean resolved) {
    final PreferencesService service = preferencesService;
    PreferencesDetail detail = null;
    switch(entityId.getEntityType()) {
        case INSTANCE:
            detail = resolved ? service.getResolvedPreferences() : service.getPreferences();
            break;
        case NAMESPACE:
            NamespaceId namespaceId = (NamespaceId) entityId;
            detail = resolved ? service.getResolvedPreferences(namespaceId) : service.getPreferences(namespaceId);
            break;
        case APPLICATION:
            ApplicationId appId = (ApplicationId) entityId;
            detail = resolved ? service.getResolvedPreferences(appId) : service.getPreferences(appId);
            break;
        case PROGRAM:
            ProgramId programId = (ProgramId) entityId;
            detail = resolved ? service.getResolvedPreferences(programId) : service.getPreferences(programId);
            break;
        default:
            throw new UnsupportedOperationException(String.format("Preferences cannot be used on this entity type: %s", entityId.getEntityType()));
    }
    return detail;
}
Also used : PreferencesDetail(io.cdap.cdap.proto.PreferencesDetail) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) PreferencesService(io.cdap.cdap.config.PreferencesService)

Example 20 with NamespaceId

use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class LocalApplicationDetailFetcher method list.

/**
 * Get a list of {@link ApplicationDetail} for all applications in the given namespace
 *
 * @param namespace the name of the namespace to get the list of applications
 * @return a list of {@link ApplicationDetail} for all applications in the given namespace
 * @throws IOException if failed to get the list of {@link ApplicationDetail}
 * @throws NamespaceNotFoundException if the given namespace doesn't exit
 */
@Override
public List<ApplicationDetail> list(String namespace) throws IOException, NamespaceNotFoundException {
    NamespaceId namespaceId = new NamespaceId(namespace);
    List<ApplicationDetail> detailList = Collections.emptyList();
    try {
        // the existence of the namespace. Does a check here to explicitly throw an exception if nonexistent.
        if (!namespaceQueryAdmin.exists(namespaceId)) {
            throw new NamespaceNotFoundException(namespaceId);
        }
        detailList = applicationLifecycleService.getApps(namespaceId);
    } catch (Exception e) {
        Throwables.propagateIfPossible(e, NamespaceNotFoundException.class, IOException.class);
        throw new IOException(e);
    }
    return detailList;
}
Also used : ApplicationDetail(io.cdap.cdap.proto.ApplicationDetail) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) IOException(java.io.IOException) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) IOException(java.io.IOException) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException)

Aggregations

NamespaceId (io.cdap.cdap.proto.id.NamespaceId)648 Test (org.junit.Test)292 Path (javax.ws.rs.Path)136 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)124 NamespaceMeta (io.cdap.cdap.proto.NamespaceMeta)108 IOException (java.io.IOException)102 ProgramId (io.cdap.cdap.proto.id.ProgramId)86 GET (javax.ws.rs.GET)74 DatasetId (io.cdap.cdap.proto.id.DatasetId)68 ArrayList (java.util.ArrayList)64 BadRequestException (io.cdap.cdap.common.BadRequestException)60 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)58 Principal (io.cdap.cdap.proto.security.Principal)56 Set (java.util.Set)52 Id (io.cdap.cdap.common.id.Id)50 File (java.io.File)50 HashSet (java.util.HashSet)50 NotFoundException (io.cdap.cdap.common.NotFoundException)48 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)46 HashMap (java.util.HashMap)46