blob: 8d70c2a2b5a1fc4e5efcb0573162d07c96ab3fd5 [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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070020import org.apache.karaf.shell.api.action.lifecycle.Service;
Jon Hall78b300c2015-08-04 15:52:55 -070021import 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 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070031@Service
Jon Hall78b300c2015-08-04 15:52:55 -070032@Command(scope = "onos", name = "transactional-map-test-put",
33 description = "Put a value into a transactional map")
34public class TransactionalMapTestPutCommand extends AbstractShellCommand {
35
Jon Hall78b300c2015-08-04 15:52:55 -070036 @Argument(index = 0, name = "numKeys",
37 description = "Number of keys to put the value into",
38 required = true, multiValued = false)
39 private int numKeys = 1;
40
41 @Argument(index = 1, name = "value",
42 description = "Value to map with the keys in the map",
43 required = true, multiValued = false)
44 private String value = null;
45
46 TransactionalMap<String, String> map;
47 String prefix = "Key";
48 String mapName = "Test-Map";
49 Serializer serializer = Serializer.using(KryoNamespaces.BASIC);
50
51 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070052 protected void doExecute() {
Jon Hall78b300c2015-08-04 15:52:55 -070053 StorageService storageService = get(StorageService.class);
54 TransactionContext context;
Madan Jampani54c5e232016-07-11 15:35:25 -070055 context = storageService.transactionContextBuilder().build();
Jon Hall78b300c2015-08-04 15:52:55 -070056 context.begin();
57 try {
58 map = context.getTransactionalMap(mapName, serializer);
59 for (int i = 1; i <= numKeys; i++) {
60 String key = prefix + i;
61 String response = map.put(key, value);
62 if (response == null) {
63 print("Created Key %s with value %s.", key, value);
64 } else {
65 print("Put %s into key %s. The old value was %s.", value, key, response);
66 }
67 }
68 context.commit();
69 } catch (Exception e) {
70 context.abort();
71 throw e;
72 }
73 }
74}