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