Search in sources :

Example 6 with PluginPropertyField

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

the class PluginInstantiator method substituteMacros.

public PluginProperties substituteMacros(Plugin plugin, @Nullable MacroEvaluator macroEvaluator) {
    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, field.isMacroEscapingEnabled());
                macroParser.parse(propertyValue);
                propertyValue = getOriginalOrDefaultValue(propertyValue, property.getKey(), field.getType(), trackingMacroEvaluator);
            } else {
                MacroParser macroParser = new MacroParser(macroEvaluator, field.isMacroEscapingEnabled());
                propertyValue = macroParser.parse(propertyValue);
            }
        }
        properties.put(property.getKey(), propertyValue);
    }
    return PluginProperties.builder().addAll(properties).build();
}
Also used : HashMap(java.util.HashMap) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 7 with PluginPropertyField

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

the class ArtifactHttpHandlerTest method testPluginNamespaceIsolation.

@Test
public void testPluginNamespaceIsolation() throws Exception {
    // add a system artifact
    ArtifactId systemId = NamespaceId.SYSTEM.artifact("wordcount", "1.0.0");
    addSystemArtifacts();
    Set<ArtifactRange> parents = Sets.newHashSet(new ArtifactRange(systemId.getNamespace(), systemId.getArtifact(), new ArtifactVersion(systemId.getVersion()), true, new ArtifactVersion(systemId.getVersion()), true));
    NamespaceId namespace1 = new NamespaceId("ns1");
    NamespaceId namespace2 = new NamespaceId("ns2");
    createNamespace(namespace1.getNamespace());
    createNamespace(namespace2.getNamespace());
    try {
        // add some plugins in namespace1. Will contain Plugin1 and Plugin2
        Manifest manifest = new Manifest();
        manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, Plugin1.class.getPackage().getName());
        ArtifactId pluginsId1 = namespace1.artifact("plugins1", "1.0.0");
        Assert.assertEquals(HttpResponseStatus.OK.code(), addPluginArtifact(Id.Artifact.fromEntityId(pluginsId1), Plugin1.class, manifest, parents).getStatusLine().getStatusCode());
        // add some plugins in namespace2. Will contain Plugin1 and Plugin2
        manifest = new Manifest();
        manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, Plugin1.class.getPackage().getName());
        ArtifactId pluginsId2 = namespace2.artifact("plugins2", "1.0.0");
        Assert.assertEquals(HttpResponseStatus.OK.code(), addPluginArtifact(Id.Artifact.fromEntityId(pluginsId2), Plugin1.class, manifest, parents).getStatusLine().getStatusCode());
        ArtifactSummary artifact1 = new ArtifactSummary(pluginsId1.getArtifact(), pluginsId1.getVersion(), ArtifactScope.USER);
        ArtifactSummary artifact2 = new ArtifactSummary(pluginsId2.getArtifact(), pluginsId2.getVersion(), ArtifactScope.USER);
        PluginSummary summary1Namespace1 = new PluginSummary("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), artifact1);
        PluginSummary summary2Namespace1 = new PluginSummary("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), artifact1);
        PluginSummary summary1Namespace2 = new PluginSummary("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), artifact2);
        PluginSummary summary2Namespace2 = new PluginSummary("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), artifact2);
        PluginInfo info1Namespace1 = new PluginInfo("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), "config", artifact1, ImmutableMap.of("x", new PluginPropertyField("x", "", "int", true, false), "stuff", new PluginPropertyField("stuff", "", "string", true, true)), new HashSet<>());
        PluginInfo info2Namespace1 = new PluginInfo("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), "config", artifact1, ImmutableMap.of("v", new PluginPropertyField("v", "value to return when called", "int", true, false)), new HashSet<>());
        PluginInfo info1Namespace2 = new PluginInfo("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), "config", artifact2, ImmutableMap.of("x", new PluginPropertyField("x", "", "int", true, false), "stuff", new PluginPropertyField("stuff", "", "string", true, true)), new HashSet<>());
        PluginInfo info2Namespace2 = new PluginInfo("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), "config", artifact2, ImmutableMap.of("v", new PluginPropertyField("v", "value to return when called", "int", true, false)), new HashSet<>());
        ArtifactId namespace1Artifact = namespace1.artifact(systemId.getArtifact(), systemId.getVersion());
        ArtifactId namespace2Artifact = namespace2.artifact(systemId.getArtifact(), systemId.getVersion());
        // should see same types in both namespaces
        Assert.assertEquals(ImmutableSet.of("dummy", "callable"), getPluginTypes(namespace1Artifact, ArtifactScope.SYSTEM));
        Assert.assertEquals(ImmutableSet.of("dummy", "callable"), getPluginTypes(namespace2Artifact, ArtifactScope.SYSTEM));
        // should see that plugins in namespace1 come only from the namespace1 artifact
        Assert.assertEquals(ImmutableSet.of(summary1Namespace1), getPluginSummaries(namespace1Artifact, "dummy", ArtifactScope.SYSTEM));
        Assert.assertEquals(ImmutableSet.of(summary2Namespace1), getPluginSummaries(namespace1Artifact, "callable", ArtifactScope.SYSTEM));
        Assert.assertEquals(ImmutableSet.of(info1Namespace1), getPluginInfos(namespace1Artifact, "dummy", "Plugin1", ArtifactScope.SYSTEM));
        Assert.assertEquals(ImmutableSet.of(info2Namespace1), getPluginInfos(namespace1Artifact, "callable", "Plugin2", ArtifactScope.SYSTEM));
        // should see that plugins in namespace2 come only from the namespace2 artifact
        Assert.assertEquals(ImmutableSet.of(summary1Namespace2), getPluginSummaries(namespace2Artifact, "dummy", ArtifactScope.SYSTEM));
        Assert.assertEquals(ImmutableSet.of(summary2Namespace2), getPluginSummaries(namespace2Artifact, "callable", ArtifactScope.SYSTEM));
        Assert.assertEquals(ImmutableSet.of(info1Namespace2), getPluginInfos(namespace2Artifact, "dummy", "Plugin1", ArtifactScope.SYSTEM));
        Assert.assertEquals(ImmutableSet.of(info2Namespace2), getPluginInfos(namespace2Artifact, "callable", "Plugin2", ArtifactScope.SYSTEM));
    } finally {
        deleteNamespace("iso1");
        deleteNamespace("iso2");
        cleanupSystemArtifactsDirectory();
    }
}
Also used : Plugin2(co.cask.cdap.internal.app.runtime.artifact.plugin.Plugin2) Plugin1(co.cask.cdap.internal.app.runtime.artifact.plugin.Plugin1) ArtifactId(co.cask.cdap.proto.id.ArtifactId) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) Manifest(java.util.jar.Manifest) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) PluginInfo(co.cask.cdap.proto.artifact.PluginInfo) NamespaceId(co.cask.cdap.proto.id.NamespaceId) PluginSummary(co.cask.cdap.proto.artifact.PluginSummary) Test(org.junit.Test)

Example 8 with PluginPropertyField

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

the class ArtifactHttpHandlerTest method testGetPlugins.

@Test
public void testGetPlugins() throws Exception {
    // add an app for plugins to extend
    ArtifactId wordCount1Id = NamespaceId.DEFAULT.artifact("wordcount", "1.0.0");
    Assert.assertEquals(HttpResponseStatus.OK.code(), addAppArtifact(Id.Artifact.fromEntityId(wordCount1Id), WordCountApp.class).getStatusLine().getStatusCode());
    ArtifactId wordCount2Id = NamespaceId.DEFAULT.artifact("wordcount", "2.0.0");
    Assert.assertEquals(HttpResponseStatus.OK.code(), addAppArtifact(Id.Artifact.fromEntityId(wordCount2Id), WordCountApp.class).getStatusLine().getStatusCode());
    // add some plugins.
    // plugins-1.0.0 extends wordcount[1.0.0,2.0.0)
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, Plugin1.class.getPackage().getName());
    ArtifactId pluginsId1 = NamespaceId.DEFAULT.artifact("plugins", "1.0.0");
    Set<ArtifactRange> plugins1Parents = Sets.newHashSet(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "wordcount", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
    Assert.assertEquals(HttpResponseStatus.OK.code(), addPluginArtifact(Id.Artifact.fromEntityId(pluginsId1), Plugin1.class, manifest, plugins1Parents).getStatusLine().getStatusCode());
    // plugin-2.0.0 extends wordcount[1.0.0,3.0.0)
    ArtifactId pluginsId2 = NamespaceId.DEFAULT.artifact("plugins", "2.0.0");
    Set<ArtifactRange> plugins2Parents = Sets.newHashSet(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "wordcount", new ArtifactVersion("1.0.0"), new ArtifactVersion("3.0.0")));
    Assert.assertEquals(HttpResponseStatus.OK.code(), addPluginArtifact(Id.Artifact.fromEntityId(pluginsId2), Plugin1.class, manifest, plugins2Parents).getStatusLine().getStatusCode());
    ArtifactSummary plugins1Artifact = new ArtifactSummary("plugins", "1.0.0");
    ArtifactSummary plugins2Artifact = new ArtifactSummary("plugins", "2.0.0");
    // get plugin types, should be the same for both
    Set<String> expectedTypes = Sets.newHashSet("dummy", "callable");
    Set<String> actualTypes = getPluginTypes(wordCount1Id);
    Assert.assertEquals(expectedTypes, actualTypes);
    actualTypes = getPluginTypes(wordCount2Id);
    Assert.assertEquals(expectedTypes, actualTypes);
    // get plugin summaries. wordcount1 should see plugins from both plugin artifacts
    Set<PluginSummary> expectedSummaries = Sets.newHashSet(new PluginSummary("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), plugins1Artifact), new PluginSummary("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), plugins2Artifact));
    Set<PluginSummary> actualSummaries = getPluginSummaries(wordCount1Id, "dummy");
    Assert.assertEquals(expectedSummaries, actualSummaries);
    expectedSummaries = Sets.newHashSet(new PluginSummary("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), plugins1Artifact), new PluginSummary("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), plugins2Artifact));
    actualSummaries = getPluginSummaries(wordCount1Id, "callable");
    Assert.assertEquals(expectedSummaries, actualSummaries);
    // wordcount2 should only see plugins from plugins2 artifact
    expectedSummaries = Sets.newHashSet(new PluginSummary("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), plugins2Artifact));
    actualSummaries = getPluginSummaries(wordCount2Id, "dummy");
    Assert.assertEquals(expectedSummaries, actualSummaries);
    expectedSummaries = Sets.newHashSet(new PluginSummary("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), plugins2Artifact));
    actualSummaries = getPluginSummaries(wordCount2Id, "callable");
    Assert.assertEquals(expectedSummaries, actualSummaries);
    // get plugin info. Again, wordcount1 should see plugins from both artifacts
    Map<String, PluginPropertyField> p1Properties = ImmutableMap.of("x", new PluginPropertyField("x", "", "int", true, false), "stuff", new PluginPropertyField("stuff", "", "string", true, true));
    Map<String, PluginPropertyField> p2Properties = ImmutableMap.of("v", new PluginPropertyField("v", "value to return when called", "int", true, false));
    Set<PluginInfo> expectedInfos = Sets.newHashSet(new PluginInfo("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), "config", plugins1Artifact, p1Properties, new HashSet<>()), new PluginInfo("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), "config", plugins2Artifact, p1Properties, new HashSet<>()));
    Assert.assertEquals(expectedInfos, getPluginInfos(wordCount1Id, "dummy", "Plugin1"));
    expectedInfos = Sets.newHashSet(new PluginInfo("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), "config", plugins1Artifact, p2Properties, new HashSet<>()), new PluginInfo("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), "config", plugins2Artifact, p2Properties, new HashSet<>()));
    Assert.assertEquals(expectedInfos, getPluginInfos(wordCount1Id, "callable", "Plugin2"));
    // while wordcount2 should only see plugins from plugins2 artifact
    expectedInfos = Sets.newHashSet(new PluginInfo("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), "config", plugins2Artifact, p1Properties, new HashSet<>()));
    Assert.assertEquals(expectedInfos, getPluginInfos(wordCount2Id, "dummy", "Plugin1"));
    expectedInfos = Sets.newHashSet(new PluginInfo("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), "config", plugins2Artifact, p2Properties, new HashSet<>()));
    Assert.assertEquals(expectedInfos, getPluginInfos(wordCount2Id, "callable", "Plugin2"));
}
Also used : Plugin2(co.cask.cdap.internal.app.runtime.artifact.plugin.Plugin2) Plugin1(co.cask.cdap.internal.app.runtime.artifact.plugin.Plugin1) ArtifactId(co.cask.cdap.proto.id.ArtifactId) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) Manifest(java.util.jar.Manifest) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) WordCountApp(co.cask.cdap.WordCountApp) PluginInfo(co.cask.cdap.proto.artifact.PluginInfo) PluginSummary(co.cask.cdap.proto.artifact.PluginSummary) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with PluginPropertyField

use of co.cask.cdap.api.plugin.PluginPropertyField 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)

Example 10 with PluginPropertyField

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

the class MockExternalSource method getPluginClass.

private static PluginClass getPluginClass() {
    Map<String, PluginPropertyField> properties = new HashMap<>();
    properties.put("name", new PluginPropertyField("name", "", "string", true, false));
    properties.put("dirName", new PluginPropertyField("dirName", "", "string", true, false));
    return new PluginClass(BatchSource.PLUGIN_TYPE, PLUGIN_NAME, "", MockExternalSource.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

PluginPropertyField (co.cask.cdap.api.plugin.PluginPropertyField)53 PluginClass (co.cask.cdap.api.plugin.PluginClass)45 HashMap (java.util.HashMap)32 Test (org.junit.Test)14 ArtifactRange (co.cask.cdap.api.artifact.ArtifactRange)12 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)12 ArtifactId (co.cask.cdap.proto.id.ArtifactId)11 Id (co.cask.cdap.common.id.Id)10 NamespaceId (co.cask.cdap.proto.id.NamespaceId)10 ImmutableSet (com.google.common.collect.ImmutableSet)5 Set (java.util.Set)5 Manifest (java.util.jar.Manifest)5 ApplicationClass (co.cask.cdap.api.artifact.ApplicationClass)4 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)4 ReflectionSchemaGenerator (co.cask.cdap.internal.io.ReflectionSchemaGenerator)4 IOException (java.io.IOException)4 ArtifactClasses (co.cask.cdap.api.artifact.ArtifactClasses)3 ArtifactId (co.cask.cdap.api.artifact.ArtifactId)3 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)3 HashSet (java.util.HashSet)3