Jon Hall
Committed by Gerrit Code Review

WIP: Transactional map cli for system tests

 - get a key from the test map
 - Push a value to n number of keys named key(1 to n) in the test map
Change-Id: I1778acbaabcede2d123f77f01db87e3f59633668
1 +/*
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 + */
16 +package org.onosproject.distributedprimitives.cli;
17 +
18 +import org.apache.karaf.shell.commands.Argument;
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.apache.karaf.shell.commands.Option;
21 +import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.store.serializers.KryoNamespaces;
23 +import org.onosproject.store.service.Serializer;
24 +import org.onosproject.store.service.StorageService;
25 +import org.onosproject.store.service.TransactionContext;
26 +import 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")
33 +public 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 +}
1 +/*
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 + */
16 +package org.onosproject.distributedprimitives.cli;
17 +
18 +import org.apache.karaf.shell.commands.Argument;
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.apache.karaf.shell.commands.Option;
21 +import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.store.serializers.KryoNamespaces;
23 +import org.onosproject.store.service.Serializer;
24 +import org.onosproject.store.service.StorageService;
25 +import org.onosproject.store.service.TransactionContext;
26 +import 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")
33 +public 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 +}
...@@ -28,6 +28,12 @@ ...@@ -28,6 +28,12 @@
28 <command> 28 <command>
29 <action class="org.onosproject.distributedprimitives.cli.SetTestRemoveCommand"/> 29 <action class="org.onosproject.distributedprimitives.cli.SetTestRemoveCommand"/>
30 </command> 30 </command>
31 + <command>
32 + <action class="org.onosproject.distributedprimitives.cli.TransactionalMapTestGetCommand"/>
33 + </command>
34 + <command>
35 + <action class="org.onosproject.distributedprimitives.cli.TransactionalMapTestPutCommand"/>
36 + </command>
31 </command-bundle> 37 </command-bundle>
32 38
33 </blueprint> 39 </blueprint>
......