FlumeSpecGen { @SuppressWarnings("unchecked") public static String genEventSink(CommonTree t) throws FlumeSpecException { if (t == null) { throw new FlumeSpecException("Tree is null"); } ASTNODE type = ASTNODE.valueOf(t.getText()); switch (type) { case SINK: List<CommonTree> children = (List<CommonTree>) new ArrayList<CommonTree>( t.getChildren()); String sinkType = children.remove(0).getText(); List<String> args = new ArrayList<String>(); for (CommonTree tr : children) { args.add(genArg(tr)); } return sinkType + genArgs(args, "(", ",", ")"); case MULTI: List<CommonTree> elems = (List<CommonTree>) t.getChildren(); List<String> snks = new ArrayList<String>(); for (CommonTree tr : elems) { String snk = genEventSink(tr); snks.add(snk); } return genArgs(snks, "[", ",", "]"); case DECO: { List<CommonTree> decoNodes = (List<CommonTree>) t.getChildren(); Preconditions.checkArgument(decoNodes.size() == 2 || decoNodes.size() == 1); if (decoNodes.size() == 1) { CommonTree snk = decoNodes.get(0); return genEventSink(snk); } CommonTree deco = decoNodes.get(0); CommonTree decoSnk = decoNodes.get(1); String decoSink = genEventSinkDecorator(deco); String dsnk = genEventSink(decoSnk); return "{ " + decoSink + " => " + dsnk + " }"; } case BACKUP: { List<CommonTree> backupNodes = (List<CommonTree>) t.getChildren(); Preconditions.checkArgument(backupNodes.size() == 2); CommonTree main = backupNodes.get(0); CommonTree backup = backupNodes.get(1); String mainSink = genEventSink(main); String backupSink = genEventSink(backup); return "< " + mainSink + " ? " + backupSink + " >"; } case ROLL: { List<CommonTree> rollNodes = (List<CommonTree>) t.getChildren(); Preconditions.checkArgument(rollNodes.size() == 2); CommonTree body = rollNodes.get(0); CommonTree arg = rollNodes.get(1); String bodySink = genEventSink(body); List<String> rargs = new ArrayList<String>(); rargs.add(genArg(arg)); String argSink = genArgs(rargs, "(", ",", ")"); return "roll" + argSink + " { " + bodySink + " }"; } case FAILCHAIN: { List<CommonTree> failNodes = (List<CommonTree>) t.getChildren(); Preconditions.checkArgument(failNodes.size() >= 2); CommonTree body = failNodes.get(0); String bodySink = genEventSink(body); List<String> rargs = new ArrayList<String>(); for (int i = 1; i < failNodes.size(); i++) { CommonTree arg = failNodes.get(i); rargs.add(genArg(arg)); } String argSink = genArgs(rargs, "(", ",", ")"); return "failchain" + argSink + " { " + bodySink + " }"; } case GEN: { List<CommonTree> genNodes = (List<CommonTree>) t.getChildren(); Preconditions.checkArgument(genNodes.size() >= 2); String genType = genNodes.get(0).getText(); CommonTree body = genNodes.get(1); String bodySink = genEventSink(body); List<String> rargs = new ArrayList<String>(); for (int i = 2; i < genNodes.size(); i++) { CommonTree arg = genNodes.get(i); rargs.add(genArg(arg)); } String argSink = genArgs(rargs, "(", ",", ")"); return genType + argSink + " { " + bodySink + " }"; } default: throw new FlumeSpecException("bad parse tree! expected sink but got " + t.toStringTree()); } }  @SuppressWarnings("unchecked") static String genEventSource(CommonTree t); @SuppressWarnings("unchecked") static String genEventSink(CommonTree t); @SuppressWarnings("unchecked") static List<FlumeNodeSpec> generate(String s);  }
@Test public void testSink() throws RecognitionException, FlumeSpecException { String orig = "null"; CommonTree tree = FlumeBuilder.parseSink(orig); String out = FlumeSpecGen.genEventSink(tree); assertEquals(orig, out); orig = "thriftSink( 1234 )"; tree = FlumeBuilder.parseSink(orig); out = FlumeSpecGen.genEventSink(tree); assertEquals(orig, out); orig = "text( \"/tmp/file\" )"; tree = FlumeBuilder.parseSink(orig); out = FlumeSpecGen.genEventSink(tree); assertEquals(orig, out); }
@Test public void testDecoratedSink() throws RecognitionException, FlumeSpecException { String orig = "{ flakey( .90 ) => null }"; CommonTree tree = FlumeBuilder.parseSink(orig); String out = FlumeSpecGen.genEventSink(tree); assertEquals(orig, out); orig = "{ intervalSampler( 20 ) => thriftSink( 1234 ) }"; tree = FlumeBuilder.parseSink(orig); out = FlumeSpecGen.genEventSink(tree); assertEquals(orig, out); orig = "{ stubbornAppend => { flakey( .90 ) => text( \"/tmp/file\" ) } }"; tree = FlumeBuilder.parseSink(orig); out = FlumeSpecGen.genEventSink(tree); assertEquals(orig, out); }
@Test public void testMulti() throws RecognitionException, FlumeSpecException { String orig = "[ counter( \"foo\" ), counter( \"bar\" ), counter( \"baz\" ) ]"; CommonTree tree = FlumeBuilder.parseSink(orig); String out = FlumeSpecGen.genEventSink(tree); assertEquals(orig, out); }
@Test public void testBackup() throws RecognitionException, FlumeSpecException { String orig = "< counter( \"foo\" ) ? counter( \"bar\" ) >"; CommonTree tree = FlumeBuilder.parseSink(orig); String out = FlumeSpecGen.genEventSink(tree); assertEquals(orig, out); }
@Test public void testRoll() throws RecognitionException, FlumeSpecException { String orig = "roll( 12345 ) { counter( \"foo\" ) }"; CommonTree tree = FlumeBuilder.parseSink(orig); String out = FlumeSpecGen.genEventSink(tree); assertEquals(orig, out); }
@Test public void testGen() throws RecognitionException, FlumeSpecException { String orig = "collector( 12345 ) { counter( \"foo\" ) }"; CommonTree tree = FlumeBuilder.parseSink(orig); String out = FlumeSpecGen.genEventSink(tree); assertEquals(orig, out); }
@Test public void testKeywordArgGen() throws RecognitionException, FlumeSpecException { String s = "text( bogus=\"bogusdata\", foo=\"bar\" )"; CommonTree o = FlumeBuilder.parseSink(s); String out = FlumeSpecGen.genEventSink(o); assertEquals(s, out); String s2 = "text( \"bogusdata\", foo=\"bar\" )"; CommonTree o2 = FlumeBuilder.parseSink(s2); out = FlumeSpecGen.genEventSink(o2); assertEquals(s2, out); s2 = "text( \"bogusdata\", foo=\"bar\", boo=1.5 )"; o2 = FlumeBuilder.parseSink(s2); out = FlumeSpecGen.genEventSink(o2); assertEquals(s2, out); }
@Test public void testFuncs() throws RecognitionException, FlumeSpecException { String s = "text( baz(42), foo=\"bar\" )"; CommonTree o = FlumeBuilder.parseSink(s); String out = FlumeSpecGen.genEventSink(o); assertEquals(s, out); String s2 = "text( baz(bog(\"boo\")), foo=\"bar\" )"; CommonTree o2 = FlumeBuilder.parseSink(s2); out = FlumeSpecGen.genEventSink(o2); assertEquals(s2, out); String s3 = "text( bogus=baz(42), foo=\"bar\" )"; CommonTree o3 = FlumeBuilder.parseSink(s3); out = FlumeSpecGen.genEventSink(o3); assertEquals(s3, out); }