Search in sources :

Example 61 with PluginPropertyField

use of io.cdap.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.

the class PluginInstantiator method substituteMacros.

public PluginProperties substituteMacros(Plugin plugin, @Nullable MacroEvaluator macroEvaluator, @Nullable MacroParserOptions options) {
    Map<String, String> properties = new HashMap<>();
    Map<String, PluginPropertyField> pluginPropertyFieldMap = plugin.getPluginClass().getProperties();
    // create macro evaluator and parser based on if it is config or runtime
    boolean configTime = (macroEvaluator == null);
    TrackingMacroEvaluator trackingMacroEvaluator = new TrackingMacroEvaluator();
    for (Map.Entry<String, String> property : plugin.getProperties().getProperties().entrySet()) {
        PluginPropertyField field = pluginPropertyFieldMap.get(property.getKey());
        String propertyValue = property.getValue();
        if (field != null && field.isMacroSupported()) {
            // TODO: cleanup after endpoint to get plugin details is merged (#6089)
            if (configTime) {
                // parse for syntax check and check if trackingMacroEvaluator finds macro syntax present
                MacroParser macroParser = new MacroParser(trackingMacroEvaluator, MacroParserOptions.builder().setEscaping(field.isMacroEscapingEnabled()).build());
                macroParser.parse(propertyValue);
                propertyValue = getOriginalOrDefaultValue(propertyValue, property.getKey(), field.getType(), trackingMacroEvaluator);
            } else {
                MacroParserOptions parserOptions = options == null ? MacroParserOptions.builder().setEscaping(field.isMacroEscapingEnabled()).build() : options;
                MacroParser macroParser = new MacroParser(macroEvaluator, parserOptions);
                propertyValue = macroParser.parse(propertyValue);
            }
        }
        properties.put(property.getKey(), propertyValue);
    }
    return PluginProperties.builder().addAll(properties).build();
}
Also used : MacroParserOptions(io.cdap.cdap.api.macro.MacroParserOptions) HashMap(java.util.HashMap) PluginPropertyField(io.cdap.cdap.api.plugin.PluginPropertyField) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 62 with PluginPropertyField

use of io.cdap.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.

the class FindPluginHelper method getPlugin.

/**
 * Get the Plugin information from the specified information.
 *
 * @param parents plugin parents of the plugin. Each item in the iterable must be the parent of the item before it,
 *                with the first item as the direct parent of the plugin
 * @param pluginEntry artifact and class information for the plugin
 * @param properties plugin properties
 * @param pluginInstantiator instantiator to add the plugin artifact to
 * @return plugin information
 */
public static Plugin getPlugin(Iterable<ArtifactId> parents, Map.Entry<ArtifactDescriptor, PluginClass> pluginEntry, PluginProperties properties, PluginInstantiator pluginInstantiator) {
    CollectMacroEvaluator collectMacroEvaluator = new CollectMacroEvaluator();
    for (PluginPropertyField field : pluginEntry.getValue().getProperties().values()) {
        if (field.isMacroSupported() && properties.getProperties().containsKey(field.getName())) {
            MacroParser parser = new MacroParser(collectMacroEvaluator, MacroParserOptions.builder().setEscaping(field.isMacroEscapingEnabled()).build());
            parser.parse(properties.getProperties().get(field.getName()));
        }
    }
    ArtifactId artifact = pluginEntry.getKey().getArtifactId();
    try {
        pluginInstantiator.addArtifact(pluginEntry.getKey().getLocation(), artifact);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    return new Plugin(parents, artifact, pluginEntry.getValue(), properties.setMacros(collectMacroEvaluator.getMacros()));
}
Also used : ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) IOException(java.io.IOException) PluginPropertyField(io.cdap.cdap.api.plugin.PluginPropertyField) Plugin(io.cdap.cdap.api.plugin.Plugin)

Example 63 with PluginPropertyField

use of io.cdap.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.

the class ArtifactStoreTest method testExcludedPlugins.

@Test
public void testExcludedPlugins() throws Exception {
    ArtifactRange parentArtifacts = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"));
    // does not have any requirement
    PluginClass includedPlugin1 = PluginClass.builder().setName("includedPlugin1").setType("A").setDescription("desc").setClassName("c.p2").setConfigFieldName("conf").setProperties(ImmutableMap.of("stream", new PluginPropertyField("stream", "description", "string", true, false))).build();
    PluginClass excludedPlugin1 = PluginClass.builder().setName("excludedPlugin1").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE))).build();
    PluginClass excludedPlugin2 = PluginClass.builder().setName("excludedPlugin2").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of(KeyValueTable.TYPE))).build();
    PluginClass excludedPlugin3 = PluginClass.builder().setName("excludedPlugin3").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE, KeyValueTable.TYPE))).build();
    PluginClass excludedPlugin4 = PluginClass.builder().setName("excludedPlugin4").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE, KeyValueTable.TYPE, Cube.TYPE))).build();
    PluginClass excludedPlugin5 = PluginClass.builder().setName("excludedPlugin5").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE, Cube.TYPE))).build();
    PluginClass includedPlugin2 = PluginClass.builder().setName("includedPlugin2").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of("noTransactionNeeded"))).build();
    PluginClass includedPlugin3 = PluginClass.builder().setName("includedPlugin3").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of("noTransactionNeeded", "tpfs"))).build();
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "ArtifactWithTransactionalPlugins", "1.0.0");
    ArtifactMeta artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(includedPlugin1, includedPlugin2, includedPlugin3, excludedPlugin1, excludedPlugin2, excludedPlugin3, excludedPlugin4, excludedPlugin5).build(), ImmutableSet.of(parentArtifacts));
    writeArtifact(artifactId, artifactMeta, "no-content");
    ArtifactDescriptor artifactInfo = artifactStore.getArtifact(artifactId).getDescriptor();
    // plugins which have transaction or spark as requirement should be excluded from plugins listed for the artifact
    Map<ArtifactDescriptor, Set<PluginClass>> actual = artifactStore.getPluginClasses(NamespaceId.DEFAULT, artifactId);
    Set<PluginClass> expectedPlugins = ImmutableSet.of(includedPlugin1, includedPlugin2, includedPlugin3);
    Assert.assertEquals(ImmutableMap.of(artifactInfo, expectedPlugins), actual);
    // excluded plugins should also not be in the plugins listed for the namespace
    List<ArtifactDetail> actualArtifacts = artifactStore.getArtifacts(NamespaceId.DEFAULT);
    Assert.assertEquals(1, actualArtifacts.size());
    Assert.assertEquals(expectedPlugins, actualArtifacts.get(0).getMeta().getClasses().getPlugins());
    // excluded plugins should also not be in the plugins listed for the artifact id
    ArtifactDetail actualArtifact = artifactStore.getArtifact(artifactId);
    Assert.assertEquals(expectedPlugins, actualArtifact.getMeta().getClasses().getPlugins());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) PluginPropertyField(io.cdap.cdap.api.plugin.PluginPropertyField) Requirements(io.cdap.cdap.api.plugin.Requirements) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) PluginClass(io.cdap.cdap.api.plugin.PluginClass) Test(org.junit.Test)

Example 64 with PluginPropertyField

use of io.cdap.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.

the class DefaultArtifactInspectorTest method testInspectNestedConfigPlugin.

@Test
public void testInspectNestedConfigPlugin() throws Exception {
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, NestedConfigPlugin.class.getPackage().getName());
    File artifactFile = createJar(NestedConfigPlugin.class, new File(TMP_FOLDER.newFolder(), "NestedPlugin-1.0.0.jar"), manifest);
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "NestedPlugin", "1.0.0");
    Location artifactLocation = Locations.toLocation(artifactFile);
    List<ArtifactDescriptor> parentDescriptor = new ArrayList<>();
    parentDescriptor.add(new ArtifactDescriptor(artifactId.getNamespace().getId(), artifactId.toArtifactId(), artifactLocation));
    ArtifactClasses classes = artifactInspector.inspectArtifact(artifactId, artifactFile, parentDescriptor, Collections.emptySet()).getArtifactClasses();
    Set<PluginClass> plugins = classes.getPlugins();
    Map<String, PluginPropertyField> expectedFields = ImmutableMap.of("X", new PluginPropertyField("X", "", "int", true, false), "Nested", new PluginPropertyField("Nested", "", "nestedconfig", true, true, false, ImmutableSet.of("Nested1", "Nested2")), "Nested1", new PluginPropertyField("Nested1", "", "string", true, true), "Nested2", new PluginPropertyField("Nested2", "", "string", true, true));
    PluginClass expected = PluginClass.builder().setName("nested").setType("dummy").setDescription("Nested config").setClassName(NestedConfigPlugin.class.getName()).setConfigFieldName("config").setProperties(expectedFields).build();
    Assert.assertEquals(Collections.singleton(expected), plugins);
}
Also used : ArrayList(java.util.ArrayList) Manifest(java.util.jar.Manifest) PluginPropertyField(io.cdap.cdap.api.plugin.PluginPropertyField) ArtifactClasses(io.cdap.cdap.api.artifact.ArtifactClasses) Id(io.cdap.cdap.common.id.Id) PluginClass(io.cdap.cdap.api.plugin.PluginClass) File(java.io.File) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test)

Example 65 with PluginPropertyField

use of io.cdap.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.

the class DefaultArtifactInspector method inspectPlugins.

/**
 * Inspects the plugin file and extracts plugin classes information.
 */
private void inspectPlugins(ArtifactClasses.Builder builder, File artifactFile, io.cdap.cdap.proto.id.ArtifactId artifactId, PluginInstantiator pluginInstantiator, Set<PluginClass> additionalPlugins, List<MetadataMutation> mutations) throws IOException, InvalidArtifactException {
    ArtifactId artifact = artifactId.toApiArtifactId();
    PluginClassLoader pluginClassLoader = pluginInstantiator.getArtifactClassLoader(artifact);
    inspectAdditionalPlugins(artifact, additionalPlugins, pluginClassLoader);
    // See if there are export packages. Plugins should be in those packages
    Set<String> exportPackages = getExportPackages(artifactFile);
    if (exportPackages.isEmpty()) {
        return;
    }
    try {
        for (Class<?> cls : getPluginClasses(exportPackages, pluginClassLoader)) {
            Plugin pluginAnnotation = cls.getAnnotation(Plugin.class);
            if (pluginAnnotation == null) {
                continue;
            }
            Map<String, PluginPropertyField> pluginProperties = Maps.newHashMap();
            try {
                String configField = getProperties(TypeToken.of(cls), pluginProperties);
                String pluginName = getPluginName(cls);
                PluginId pluginId = new PluginId(artifactId.getNamespace(), artifactId.getArtifact(), artifactId.getVersion(), pluginName, pluginAnnotation.type());
                MetadataMutation mutation = getMetadataMutation(pluginId, cls);
                if (mutation != null) {
                    mutations.add(mutation);
                }
                PluginClass pluginClass = PluginClass.builder().setName(pluginName).setType(pluginAnnotation.type()).setCategory(getPluginCategory(cls)).setClassName(cls.getName()).setConfigFieldName(configField).setProperties(pluginProperties).setRequirements(getArtifactRequirements(cls)).setDescription(getPluginDescription(cls)).build();
                builder.addPlugin(pluginClass);
            } catch (UnsupportedTypeException e) {
                LOG.warn("Plugin configuration type not supported. Plugin ignored. {}", cls, e);
            }
        }
    } catch (Throwable t) {
        throw new InvalidArtifactException(String.format("Class could not be found while inspecting artifact for plugins. " + "Please check dependencies are available, and that the correct parent artifact was specified. " + "Error class: %s, message: %s.", t.getClass(), t.getMessage()), t);
    }
}
Also used : ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) PluginId(io.cdap.cdap.proto.id.PluginId) PluginPropertyField(io.cdap.cdap.api.plugin.PluginPropertyField) MetadataMutation(io.cdap.cdap.spi.metadata.MetadataMutation) UnsupportedTypeException(io.cdap.cdap.api.data.schema.UnsupportedTypeException) PluginClass(io.cdap.cdap.api.plugin.PluginClass) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) PluginClassLoader(io.cdap.cdap.internal.app.runtime.plugin.PluginClassLoader) Plugin(io.cdap.cdap.api.annotation.Plugin)

Aggregations

PluginPropertyField (io.cdap.cdap.api.plugin.PluginPropertyField)115 HashMap (java.util.HashMap)93 PluginClass (io.cdap.cdap.api.plugin.PluginClass)29 Test (org.junit.Test)16 ArtifactRange (io.cdap.cdap.api.artifact.ArtifactRange)12 ArtifactVersion (io.cdap.cdap.api.artifact.ArtifactVersion)12 Id (io.cdap.cdap.common.id.Id)12 File (java.io.File)10 ArtifactClasses (io.cdap.cdap.api.artifact.ArtifactClasses)8 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)8 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)8 Location (org.apache.twill.filesystem.Location)8 ImmutableMap (com.google.common.collect.ImmutableMap)6 ImmutableSet (com.google.common.collect.ImmutableSet)6 ApplicationClass (io.cdap.cdap.api.artifact.ApplicationClass)6 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)6 Requirements (io.cdap.cdap.api.plugin.Requirements)6 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6