Search in sources :

Example 6 with PluginClass

use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.

the class ArtifactStore method getPluginsInArtifact.

private SortedMap<ArtifactDescriptor, Set<PluginClass>> getPluginsInArtifact(Table table, Id.Artifact artifactId, Predicate<PluginClass> filter) throws ArtifactNotFoundException {
    SortedMap<ArtifactDescriptor, Set<PluginClass>> result = new TreeMap<>();
    // Make sure the artifact exists
    ArtifactCell artifactCell = new ArtifactCell(artifactId);
    byte[] artifactDataBytes = table.get(artifactCell.rowkey, artifactCell.column);
    if (artifactDataBytes == null) {
        throw new ArtifactNotFoundException(artifactId.toEntityId());
    }
    // include any plugin classes that are inside the artifact itself and is accepted by the filter
    ArtifactData artifactData = GSON.fromJson(Bytes.toString(artifactDataBytes), ArtifactData.class);
    Set<PluginClass> plugins = artifactData.meta.getClasses().getPlugins().stream().filter(filter).collect(Collectors.toCollection(LinkedHashSet::new));
    if (!plugins.isEmpty()) {
        Location location = Locations.getLocationFromAbsolutePath(locationFactory, artifactData.getLocationPath());
        ArtifactDescriptor descriptor = new ArtifactDescriptor(artifactId.toArtifactId(), location);
        result.put(descriptor, plugins);
    }
    return result;
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) TreeMap(java.util.TreeMap) PluginClass(co.cask.cdap.api.plugin.PluginClass) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) Location(org.apache.twill.filesystem.Location)

Example 7 with PluginClass

use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.

the class ArtifactStore method writeMeta.

// write a new artifact snapshot and clean up the old snapshot data
private void writeMeta(Table table, Id.Artifact artifactId, ArtifactData data) throws IOException {
    ArtifactCell artifactCell = new ArtifactCell(artifactId);
    table.put(artifactCell.rowkey, artifactCell.column, Bytes.toBytes(GSON.toJson(data)));
    // column for plugin meta and app meta. {artifact-name}:{artifact-version}
    // does not need to contain namespace because namespace is in the rowkey
    byte[] artifactColumn = new ArtifactColumn(artifactId).getColumn();
    ArtifactClasses classes = data.meta.getClasses();
    Location artifactLocation = Locations.getLocationFromAbsolutePath(locationFactory, data.getLocationPath());
    // write pluginClass metadata
    for (PluginClass pluginClass : classes.getPlugins()) {
        // write metadata for each artifact this plugin extends
        for (ArtifactRange artifactRange : data.meta.getUsableBy()) {
            // p:{namespace}:{type}:{name}
            PluginKey pluginKey = new PluginKey(artifactRange.getNamespace(), artifactRange.getName(), pluginClass.getType(), pluginClass.getName());
            byte[] pluginDataBytes = Bytes.toBytes(GSON.toJson(new PluginData(pluginClass, artifactLocation, artifactRange)));
            table.put(pluginKey.getRowKey(), artifactColumn, pluginDataBytes);
        }
        // by any other artifact in the same namespace.
        if (data.meta.getUsableBy().isEmpty()) {
            // Write a special entry for plugin that doesn't have parent, which means any artifact can use it
            UniversalPluginKey pluginKey = new UniversalPluginKey(artifactId.getNamespace().getId(), pluginClass.getType(), pluginClass.getName());
            byte[] pluginDataBytes = Bytes.toBytes(GSON.toJson(new PluginData(pluginClass, artifactLocation, null)));
            table.put(pluginKey.getRowKey(), artifactColumn, pluginDataBytes);
        }
    }
    // write appClass metadata
    for (ApplicationClass appClass : classes.getApps()) {
        // a:{namespace}:{classname}
        AppClassKey appClassKey = new AppClassKey(artifactId.getNamespace().toEntityId(), appClass.getClassName());
        byte[] appDataBytes = Bytes.toBytes(GSON.toJson(new AppData(appClass, artifactLocation)));
        table.put(appClassKey.getRowKey(), artifactColumn, appDataBytes);
    }
}
Also used : ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) ApplicationClass(co.cask.cdap.api.artifact.ApplicationClass) ArtifactClasses(co.cask.cdap.api.artifact.ArtifactClasses) PluginClass(co.cask.cdap.api.plugin.PluginClass) Location(org.apache.twill.filesystem.Location)

Example 8 with PluginClass

use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.

the class RemotePluginFinder method findPlugin.

@Override
public Map.Entry<ArtifactDescriptor, PluginClass> findPlugin(NamespaceId pluginNamespaceId, ArtifactId parentArtifactId, String pluginType, String pluginName, PluginSelector selector) throws PluginNotExistsException {
    try {
        return Retries.callWithRetries(() -> {
            List<PluginInfo> infos = getPlugins(pluginNamespaceId, parentArtifactId, pluginType, pluginName);
            if (infos.isEmpty()) {
                throw new PluginNotExistsException(pluginNamespaceId, pluginType, pluginName);
            }
            SortedMap<co.cask.cdap.api.artifact.ArtifactId, PluginClass> plugins = new TreeMap<>();
            for (PluginInfo info : infos) {
                ArtifactSummary artifactSummary = info.getArtifact();
                co.cask.cdap.api.artifact.ArtifactId pluginArtifactId = new co.cask.cdap.api.artifact.ArtifactId(artifactSummary.getName(), new ArtifactVersion(artifactSummary.getVersion()), artifactSummary.getScope());
                PluginClass pluginClass = new PluginClass(info.getType(), info.getName(), info.getDescription(), info.getClassName(), info.getConfigFieldName(), info.getProperties(), info.getEndpoints());
                plugins.put(pluginArtifactId, pluginClass);
            }
            Map.Entry<co.cask.cdap.api.artifact.ArtifactId, PluginClass> selected = selector.select(plugins);
            if (selected == null) {
                throw new PluginNotExistsException(pluginNamespaceId, pluginType, pluginName);
            }
            Location artifactLocation = getArtifactLocation(Artifacts.toArtifactId(pluginNamespaceId, selected.getKey()));
            return Maps.immutableEntry(new ArtifactDescriptor(selected.getKey(), artifactLocation), selected.getValue());
        }, retryStrategy);
    } catch (PluginNotExistsException e) {
        throw e;
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : ArtifactId(co.cask.cdap.proto.id.ArtifactId) TreeMap(java.util.TreeMap) PluginNotExistsException(co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException) IOException(java.io.IOException) PluginNotExistsException(co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) PluginInfo(co.cask.cdap.proto.artifact.PluginInfo) PluginClass(co.cask.cdap.api.plugin.PluginClass) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) Location(org.apache.twill.filesystem.Location)

Example 9 with PluginClass

use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.

the class DefaultArtifactRepository method validatePluginSet.

/**
 * Validates the set of plugins for an artifact. Checks that the pair of plugin type and name are unique among
 * all plugins in an artifact.
 *
 * @param plugins the set of plugins to validate
 * @throws InvalidArtifactException if there is more than one class with the same type and name
 */
@VisibleForTesting
static void validatePluginSet(Set<PluginClass> plugins) throws InvalidArtifactException {
    boolean isInvalid = false;
    StringBuilder errMsg = new StringBuilder("Invalid plugins field.");
    Set<ImmutablePair<String, String>> existingPlugins = new HashSet<>();
    Set<ImmutablePair<String, String>> dupes = new HashSet<>();
    for (PluginClass plugin : plugins) {
        ImmutablePair<String, String> typeAndName = ImmutablePair.of(plugin.getType(), plugin.getName());
        if (!existingPlugins.add(typeAndName) && !dupes.contains(typeAndName)) {
            errMsg.append(" Only one plugin with type '");
            errMsg.append(typeAndName.getFirst());
            errMsg.append("' and name '");
            errMsg.append(typeAndName.getSecond());
            errMsg.append("' can be present.");
            dupes.add(typeAndName);
            isInvalid = true;
        }
    }
    // "Invalid plugins. Only one plugin with type 'source' and name 'table' can be present."
    if (isInvalid) {
        throw new InvalidArtifactException(errMsg.toString());
    }
}
Also used : ImmutablePair(co.cask.cdap.common.utils.ImmutablePair) PluginClass(co.cask.cdap.api.plugin.PluginClass) InvalidArtifactException(co.cask.cdap.common.InvalidArtifactException) HashSet(java.util.HashSet) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 10 with PluginClass

use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.

the class LookupTransform method getPluginClass.

private static PluginClass getPluginClass() {
    Map<String, PluginPropertyField> properties = new HashMap<>();
    properties.put("lookupKey", new PluginPropertyField("fields", "", "string", true, false));
    properties.put("destinationField", new PluginPropertyField("destinationField", "", "string", true, false));
    properties.put("lookupName", new PluginPropertyField("lookupName", "", "string", true, false));
    return new PluginClass(Transform.PLUGIN_TYPE, "Lookup", "", LookupTransform.class.getName(), "config", properties);
}
Also used : HashMap(java.util.HashMap) PluginClass(co.cask.cdap.api.plugin.PluginClass) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField)

Aggregations

PluginClass (co.cask.cdap.api.plugin.PluginClass)76 PluginPropertyField (co.cask.cdap.api.plugin.PluginPropertyField)46 HashMap (java.util.HashMap)37 Test (org.junit.Test)24 ArtifactId (co.cask.cdap.proto.id.ArtifactId)22 NamespaceId (co.cask.cdap.proto.id.NamespaceId)21 ArtifactRange (co.cask.cdap.api.artifact.ArtifactRange)19 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)17 Id (co.cask.cdap.common.id.Id)17 Set (java.util.Set)14 ImmutableSet (com.google.common.collect.ImmutableSet)13 PluginNotExistsException (co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException)11 Map (java.util.Map)11 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)10 SortedMap (java.util.SortedMap)10 ArtifactId (co.cask.cdap.api.artifact.ArtifactId)9 File (java.io.File)9 ApplicationClass (co.cask.cdap.api.artifact.ApplicationClass)8 HashSet (java.util.HashSet)8 ImmutableMap (com.google.common.collect.ImmutableMap)7