blob: 448d68161ebbe78f9f8b59e2b613bcdc7fec6e5b [file] [log] [blame]
Jordan Halterman6b024432018-09-26 14:30:46 -07001/*
2 * Copyright 2016-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.distributedprimitives.cli;
17
Ray Milkeya218d432018-10-30 11:18:58 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
Jordan Halterman6b024432018-09-26 14:30:46 -070020import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.store.serializers.KryoNamespaces;
22import org.onosproject.store.service.DocumentPath;
23import org.onosproject.store.service.DocumentTree;
24import org.onosproject.store.service.Serializer;
25import org.onosproject.store.service.StorageService;
26import org.onosproject.store.service.Versioned;
27
28/**
29 * CLI command to manipulate a document tree.
30 */
31@Command(scope = "onos", name = "document-tree-test",
32 description = "Manipulate a document tree")
33public class DocumentTreeTestCommand extends AbstractShellCommand {
34
35 @Argument(index = 0, name = "name",
36 description = "tree name",
37 required = true, multiValued = false)
38 String name = null;
39
40 @Argument(index = 1, name = "operation",
41 description = "operation name",
42 required = true, multiValued = false)
43 String operation = null;
44
45 @Argument(index = 2, name = "path",
46 description = "first arg",
47 required = false, multiValued = false)
48 String arg1 = null;
49
50 @Argument(index = 3, name = "value",
51 description = "second arg",
52 required = false, multiValued = false)
53 String arg2 = null;
54
55 DocumentTree<String> tree;
56
57 @Override
Ray Milkeya218d432018-10-30 11:18:58 -070058 protected void doExecute() {
Jordan Halterman6b024432018-09-26 14:30:46 -070059 StorageService storageService = get(StorageService.class);
60 tree = storageService.<String>getDocumentTree(name, Serializer.using(KryoNamespaces.BASIC)).asDocumentTree();
61
62 tree.addListener(value -> {
63 log.debug("Received event: {}", value);
64 });
65
66 if ("set".equals(operation)) {
67 log.debug("set {} {}", DocumentPath.from(arg1), arg2);
68 print(tree.set(DocumentPath.from(arg1), arg2));
69 } else if ("get".equals(operation)) {
70 log.debug("get {}", DocumentPath.from(arg1));
71 print(tree.get(DocumentPath.from(arg1)));
72 } else if ("remove".equals(operation)) {
73 log.debug("removeNode {}", DocumentPath.from(arg1));
74 print(tree.removeNode(DocumentPath.from(arg1)));
75 } else if ("create".equals(operation)) {
76 log.debug("create {} {}", DocumentPath.from(arg1), arg2);
77 print("%b", tree.create(DocumentPath.from(arg1), arg2));
78 } else if ("createRecursive".equals(operation)) {
79 log.debug("createRecursive {} {}", DocumentPath.from(arg1), arg2);
80 print("%b", tree.createRecursive(DocumentPath.from(arg1), arg2));
81 }
82 }
83
84 void print(Versioned<String> value) {
85 if (value == null) {
86 print("null");
87 } else {
88 print("%s", value.value());
89 }
90 }
91}