From e34c780d68466730c7d5c461f373edf1ffd68df4 Mon Sep 17 00:00:00 2001 From: Michael Vorburger Date: Tue, 18 Sep 2018 14:57:34 +0200 Subject: [PATCH] add a simple READ to DemoMain, and document usage for demos in README see also https://github.com/etcd-io/etcd/issues/10102 --- README.md | 13 ++-- .../org/opendaylight/etcd/demo/DemoMain.java | 62 ++++++++++++++----- 2 files changed, 54 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 0e99f75..777df7a 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,9 @@ If you used etcd before you may [want to completely wipe it](https://github.com/ sudo systemctl start etcd etcdctl ls -Beware that `etcdctl ls -r` [does not seem to show unprintable keys](https://github.com/etcd-io/etcd/issues/10102). +Beware that `etcdctl ls -r` [does not seem to show unprintable keys](https://github.com/etcd-io/etcd/issues/10102), so better use our own: + + java -jar demo/target/*.jar read http://localhost:2379 ### RESTCONF @@ -102,6 +104,7 @@ Note how there is no `karaf/target/assembly/etc/org.opendaylight.controller.clus echo 'hello, world' >put.xml http -v -a admin:admin PUT :8181/restconf/config/opendaylight-etcd-test:HelloWorldContainer @put.xml http -a admin:admin GET :8181/restconf/config/opendaylight-etcd-test:HelloWorldContainer + java -jar demo/target/*.jar read http://localhost:2379 http -a admin:admin DELETE :8181/restconf/config/opendaylight-etcd-test:HelloWorldContainer http -a admin:admin GET :8181/restconf/config/opendaylight-etcd-test:HelloWorldContainer @@ -112,13 +115,15 @@ Here is how to run [the asciinema POC v0.1](https://asciinema.org/a/DShFpWOXFmaQ Now run this project's demo: - java -jar demo/target/*.jar http://localhost:4001 + java -jar demo/target/*.jar write http://localhost:4001 or if you started `etcd` directly without systemd then: - java -jar demo/target/*.jar http://localhost:2379 + java -jar demo/target/*.jar write http://localhost:2379 + +and have a closer look at the logs this will print to understand what happened, and read it via: -and have a closer look at the logs this will print, to understand what happened. + java -jar demo/target/*.jar read http://localhost:2379 ## FAQ diff --git a/demo/src/main/java/org/opendaylight/etcd/demo/DemoMain.java b/demo/src/main/java/org/opendaylight/etcd/demo/DemoMain.java index 7eb4a37..c109f8a 100644 --- a/demo/src/main/java/org/opendaylight/etcd/demo/DemoMain.java +++ b/demo/src/main/java/org/opendaylight/etcd/demo/DemoMain.java @@ -7,12 +7,23 @@ */ package org.opendaylight.etcd.demo; -import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL; +import static com.google.common.base.Charsets.US_ASCII; +import static io.etcd.jetcd.options.GetOption.SortOrder.ASCEND; +import static io.etcd.jetcd.options.GetOption.SortTarget.KEY; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION; +import com.google.common.collect.Lists; import io.etcd.jetcd.Client; -import java.util.Arrays; +import io.etcd.jetcd.data.ByteSequence; +import io.etcd.jetcd.data.KeyValue; +import io.etcd.jetcd.options.GetOption; +import java.util.List; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import org.opendaylight.etcd.ds.impl.EtcdDataStore; import org.opendaylight.etcd.testutils.TestEtcdDataBrokerProvider; +import org.opendaylight.etcd.utils.KeyValues; import org.opendaylight.mdsal.binding.api.DataBroker; import org.opendaylight.mdsal.binding.api.WriteTransaction; import org.opendaylight.yang.gen.v1.urn.opendaylight.etcd.test.rev180628.HelloWorldContainer; @@ -34,29 +45,46 @@ public final class DemoMain { private static void write(DataBroker dataBroker) throws InterruptedException, ExecutionException { InstanceIdentifier iid = InstanceIdentifier.create(HelloWorldContainer.class); WriteTransaction tx = dataBroker.newWriteOnlyTransaction(); - tx.put(OPERATIONAL, iid, new HelloWorldContainerBuilder().setName("hello, world").build()); + tx.put(CONFIGURATION, iid, new HelloWorldContainerBuilder().setName("hello, world").build()); tx.commit().get(); } public static void main(String[] args) { - if (args.length < 1) { - System.err.println("USAGE: etcd server host:port (list of)\nEXAMPLE: http://localhost:2379"); + if (args.length < 2) { + System.err.println("USAGE: write|read etcd-server-host:port (list of)\nEXAMPLE: write http://localhost:2379"); return; } - System.out.println("Connecting to etcd server/s on: " + Arrays.toString(args)); - try (Client client = Client.builder().endpoints(args).build()) { - try (TestEtcdDataBrokerProvider dbProvider = new TestEtcdDataBrokerProvider(client, "demo")) { - DataBroker dataBroker = dbProvider.getDataBroker(); + String operation = args[0]; + List endpoints = Lists.newArrayList(args).subList(1, args.length); - write(dataBroker); - - } catch (Exception e) { - LOG.error("Demo failed", e); - } finally { - client.close(); - // TODO find out why non-daemon thread "pool-2-thread-1" causes hung exit without this hack.. - System.exit(0); + System.out.println("Operation: " + operation + "; connecting to etcd server/s on: " + endpoints); + try (Client client = Client.builder().endpoints(endpoints.toArray(new String[0])).build()) { + if ("write".equalsIgnoreCase(operation)) { + try (TestEtcdDataBrokerProvider dbProvider = new TestEtcdDataBrokerProvider(client, "demo")) { + DataBroker dataBroker = dbProvider.getDataBroker(); + write(dataBroker); + // read(client); + } finally { + client.close(); + } + } else { + read(client); } + } catch (Exception e) { + LOG.error("Demo failed", e); + } + // TODO find out why non-daemon thread "pool-2-thread-1" causes hung exit without this hack.. + System.exit(0); + } + + private static void read(Client client) throws InterruptedException, ExecutionException, TimeoutException { + ByteSequence prefix = EtcdDataStore.CONFIGURATION_PREFIX; + // TODO huh, the sorting by key doesn't seem to work? Running this after restconf demo has aaa interspersed + GetOption getOpt = GetOption.newBuilder().withPrefix(prefix).withSortField(KEY).withSortOrder(ASCEND).build(); + List kvs = client.getKVClient().get(prefix, getOpt).get(3000, MILLISECONDS).getKvs(); + System.out.println(kvs.size() + " key/values in etcd under prefix " + prefix.toString(US_ASCII) + "/ :"); + for (KeyValue kv : kvs) { + System.out.println(KeyValues.asString(kv)); } }