blob: e41ccc8dd7105e29383dcb2587186b931e4ea9ac [file] [log] [blame]
Jon Hall78b300c2015-08-04 15:52:55 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.apache.karaf.shell.commands.Option;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.store.serializers.KryoNamespaces;
23import org.onosproject.store.service.Serializer;
24import org.onosproject.store.service.StorageService;
25import org.onosproject.store.service.TransactionContext;
26import org.onosproject.store.service.TransactionalMap;
27
28/**
29 * CLI command to get a value associated with a specific key in a transactional map.
30 */
31@Command(scope = "onos", name = "transactional-map-test-get",
32 description = "Get a value associated with a specific key in a transactional map")
33public class TransactionalMapTestGetCommand extends AbstractShellCommand {
34
35 @Option(name = "-i", aliases = "--inMemory", description = "use in memory map?",
36 required = false, multiValued = false)
37 private boolean inMemory = false;
38
39 @Argument(index = 0, name = "key",
40 description = "Key to get the value of",
41 required = true, multiValued = false)
42 private String key = null;
43
44 TransactionalMap<String, String> map;
45 String mapName = "Test-Map";
46 Serializer serializer = Serializer.using(KryoNamespaces.BASIC);
47
48 @Override
49 protected void execute() {
50 StorageService storageService = get(StorageService.class);
51 TransactionContext context;
52 if (inMemory) {
53 context = storageService.transactionContextBuilder().withPartitionsDisabled().build();
54 } else {
55 context = storageService.transactionContextBuilder().build();
56 }
57 context.begin();
58 try {
59 map = context.getTransactionalMap(mapName, serializer);
60 String response = map.get(key);
61 context.commit();
62
63 if (response == null) {
64 print("Key %s not found.", key);
65 } else {
66 print("Key-value pair (%s, %s) found.", key, response);
67 }
68 } catch (Exception e) {
69 context.abort();
70 throw e;
71 }
72 }
73}