blob: 88d9e0081bf334029c173cb15a667c922881557f [file] [log] [blame]
Jon Hall78b300c2015-08-04 15:52:55 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jon Hall78b300c2015-08-04 15:52:55 -07003 *
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 put a value into a transactional map.
30 */
31@Command(scope = "onos", name = "transactional-map-test-put",
32 description = "Put a value into a transactional map")
33public class TransactionalMapTestPutCommand 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 = "numKeys",
40 description = "Number of keys to put the value into",
41 required = true, multiValued = false)
42 private int numKeys = 1;
43
44 @Argument(index = 1, name = "value",
45 description = "Value to map with the keys in the map",
46 required = true, multiValued = false)
47 private String value = null;
48
49 TransactionalMap<String, String> map;
50 String prefix = "Key";
51 String mapName = "Test-Map";
52 Serializer serializer = Serializer.using(KryoNamespaces.BASIC);
53
54 @Override
55 protected void execute() {
56 StorageService storageService = get(StorageService.class);
57 TransactionContext context;
58 if (inMemory) {
59 context = storageService.transactionContextBuilder().withPartitionsDisabled().build();
60 } else {
61 context = storageService.transactionContextBuilder().build();
62 }
63 context.begin();
64 try {
65 map = context.getTransactionalMap(mapName, serializer);
66 for (int i = 1; i <= numKeys; i++) {
67 String key = prefix + i;
68 String response = map.put(key, value);
69 if (response == null) {
70 print("Created Key %s with value %s.", key, value);
71 } else {
72 print("Put %s into key %s. The old value was %s.", value, key, response);
73 }
74 }
75 context.commit();
76 } catch (Exception e) {
77 context.abort();
78 throw e;
79 }
80 }
81}