Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [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 | |
| 17 | package org.onosproject.cli.net; |
| 18 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Command; |
| 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | c747729 | 2016-03-11 10:53:43 -0800 | [diff] [blame] | 22 | import org.onosproject.utils.Comparators; |
Claudine Chiu | 31ad527 | 2016-02-17 20:56:24 +0000 | [diff] [blame] | 23 | import org.onosproject.net.key.DeviceKey; |
| 24 | import org.onosproject.net.key.DeviceKeyService; |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 25 | |
| 26 | import java.util.Collections; |
| 27 | import java.util.List; |
| 28 | |
| 29 | import static com.google.common.collect.Lists.newArrayList; |
| 30 | |
| 31 | /** |
| 32 | * Lists all device keys. |
| 33 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 34 | @Service |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 35 | @Command(scope = "onos", name = "device-keys", |
| 36 | description = "Lists all device keys") |
| 37 | |
| 38 | public class DeviceKeyListCommand extends AbstractShellCommand { |
| 39 | private static final String FMT_COMMUNITY_NAME = |
| 40 | "identifier=%s, type=%s, community name=%s"; |
| 41 | private static final String FMT_USERNAME_PASSWORD = |
| 42 | "identifier=%s, type=%s, username=%s, password=%s"; |
| 43 | |
| 44 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 45 | protected void doExecute() { |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 46 | DeviceKeyService service = get(DeviceKeyService.class); |
| 47 | for (DeviceKey deviceKey : getSortedDeviceKeys(service)) { |
| 48 | printDeviceKey(deviceKey); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns the list of devices keys sorted using the device key identifier. |
| 54 | * |
| 55 | * @param service device key service |
| 56 | * @return sorted device key list |
| 57 | */ |
| 58 | protected List<DeviceKey> getSortedDeviceKeys(DeviceKeyService service) { |
| 59 | List<DeviceKey> deviceKeys = newArrayList(service.getDeviceKeys()); |
| 60 | Collections.sort(deviceKeys, Comparators.DEVICE_KEY_COMPARATOR); |
| 61 | return deviceKeys; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Prints out each device key. |
| 66 | * |
| 67 | * @param deviceKey the device key to be printed |
| 68 | */ |
| 69 | private void printDeviceKey(DeviceKey deviceKey) { |
| 70 | if (DeviceKey.Type.COMMUNITY_NAME.equals(deviceKey.type())) { |
| 71 | print(FMT_COMMUNITY_NAME, deviceKey.deviceKeyId().id(), deviceKey.type(), |
| 72 | deviceKey.asCommunityName().name()); |
| 73 | } else if (DeviceKey.Type.USERNAME_PASSWORD.equals(deviceKey.type())) { |
| 74 | print(FMT_USERNAME_PASSWORD, deviceKey.deviceKeyId().id(), deviceKey.type(), |
| 75 | deviceKey.asUsernamePassword().username(), deviceKey.asUsernamePassword().password()); |
| 76 | } else { |
柯志勇10068695 | 25dc553 | 2018-09-14 16:33:06 +0800 | [diff] [blame] | 77 | log.error("Unsupported device key type: {}" + deviceKey.type()); |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | } |