Skip to content
This repository has been archived by the owner on Apr 12, 2019. It is now read-only.

Commit

Permalink
add a simple READ to DemoMain, and document usage for demos in README
Browse files Browse the repository at this point in the history
  • Loading branch information
vorburger committed Sep 18, 2018
1 parent 0259230 commit e34c780
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 21 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -102,6 +104,7 @@ Note how there is no `karaf/target/assembly/etc/org.opendaylight.controller.clus
echo '<HelloWorldContainer xmlns="urn:opendaylight:etcd:test"><name>hello, world</name></HelloWorldContainer>' >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

Expand All @@ -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
Expand Down
62 changes: 45 additions & 17 deletions demo/src/main/java/org/opendaylight/etcd/demo/DemoMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,29 +45,46 @@ public final class DemoMain {
private static void write(DataBroker dataBroker) throws InterruptedException, ExecutionException {
InstanceIdentifier<HelloWorldContainer> 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<String> 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<KeyValue> 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));
}
}

Expand Down

0 comments on commit e34c780

Please sign in to comment.