blob: 5d84f5996b5122b346ae3bb095bcc69a67557089 [file] [log] [blame]
Brian Stankee312fc72016-02-16 15:07:13 -05001/*
2 * Copyright 2016 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
17package org.onosproject.cli.net;
18
19import org.apache.karaf.shell.commands.Command;
20import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.cli.Comparators;
22import org.onosproject.incubator.net.key.DeviceKey;
23import org.onosproject.incubator.net.key.DeviceKeyService;
24
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 {
75 log.error("Unsupported device key type: " + deviceKey.type());
76 }
77 }
78
79}