blob: 11f54936cfdc80cbd3f085da4d04563a03853ead [file] [log] [blame]
Jon Hall78b300c2015-08-04 15:52:55 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Jon Hall78b300c2015-08-04 15:52:55 -070020import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.store.serializers.KryoNamespaces;
22import org.onosproject.store.service.Serializer;
23import org.onosproject.store.service.StorageService;
24import org.onosproject.store.service.TransactionContext;
25import org.onosproject.store.service.TransactionalMap;
26
27/**
28 * CLI command to put a value into a transactional map.
29 */
30@Command(scope = "onos", name = "transactional-map-test-put",
31 description = "Put a value into a transactional map")
32public class TransactionalMapTestPutCommand extends AbstractShellCommand {
33
Jon Hall78b300c2015-08-04 15:52:55 -070034 @Argument(index = 0, name = "numKeys",
35 description = "Number of keys to put the value into",
36 required = true, multiValued = false)
37 private int numKeys = 1;
38
39 @Argument(index = 1, name = "value",
40 description = "Value to map with the keys in the map",
41 required = true, multiValued = false)
42 private String value = null;
43
44 TransactionalMap<String, String> map;
45 String prefix = "Key";
46 String mapName = "Test-Map";
47 Serializer serializer = Serializer.using(KryoNamespaces.BASIC);
48
49 @Override
50 protected void execute() {
51 StorageService storageService = get(StorageService.class);
52 TransactionContext context;
Madan Jampani54c5e232016-07-11 15:35:25 -070053 context = storageService.transactionContextBuilder().build();
Jon Hall78b300c2015-08-04 15:52:55 -070054 context.begin();
55 try {
56 map = context.getTransactionalMap(mapName, serializer);
57 for (int i = 1; i <= numKeys; i++) {
58 String key = prefix + i;
59 String response = map.put(key, value);
60 if (response == null) {
61 print("Created Key %s with value %s.", key, value);
62 } else {
63 print("Put %s into key %s. The old value was %s.", value, key, response);
64 }
65 }
66 context.commit();
67 } catch (Exception e) {
68 context.abort();
69 throw e;
70 }
71 }
72}