Jon Hall | 78b300c | 2015-08-04 15:52:55 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jon Hall | 78b300c | 2015-08-04 15:52:55 -0700 | [diff] [blame] | 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; |
Jon Hall | 78b300c | 2015-08-04 15:52:55 -0700 | [diff] [blame] | 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.store.serializers.KryoNamespaces; |
| 22 | import org.onosproject.store.service.Serializer; |
| 23 | import org.onosproject.store.service.StorageService; |
| 24 | import org.onosproject.store.service.TransactionContext; |
| 25 | import org.onosproject.store.service.TransactionalMap; |
| 26 | |
| 27 | /** |
| 28 | * CLI command to get a value associated with a specific key in a transactional map. |
| 29 | */ |
| 30 | @Command(scope = "onos", name = "transactional-map-test-get", |
| 31 | description = "Get a value associated with a specific key in a transactional map") |
| 32 | public class TransactionalMapTestGetCommand extends AbstractShellCommand { |
| 33 | |
Jon Hall | 78b300c | 2015-08-04 15:52:55 -0700 | [diff] [blame] | 34 | @Argument(index = 0, name = "key", |
| 35 | description = "Key to get the value of", |
| 36 | required = true, multiValued = false) |
| 37 | private String key = null; |
| 38 | |
| 39 | TransactionalMap<String, String> map; |
| 40 | String mapName = "Test-Map"; |
| 41 | Serializer serializer = Serializer.using(KryoNamespaces.BASIC); |
| 42 | |
| 43 | @Override |
| 44 | protected void execute() { |
| 45 | StorageService storageService = get(StorageService.class); |
| 46 | TransactionContext context; |
Madan Jampani | 54c5e23 | 2016-07-11 15:35:25 -0700 | [diff] [blame] | 47 | context = storageService.transactionContextBuilder().build(); |
Jon Hall | 78b300c | 2015-08-04 15:52:55 -0700 | [diff] [blame] | 48 | context.begin(); |
| 49 | try { |
| 50 | map = context.getTransactionalMap(mapName, serializer); |
| 51 | String response = map.get(key); |
| 52 | context.commit(); |
| 53 | |
| 54 | if (response == null) { |
| 55 | print("Key %s not found.", key); |
| 56 | } else { |
| 57 | print("Key-value pair (%s, %s) found.", key, response); |
| 58 | } |
| 59 | } catch (Exception e) { |
| 60 | context.abort(); |
| 61 | throw e; |
| 62 | } |
| 63 | } |
| 64 | } |