blob: dc1007e3fa8634c5598ec4d61f29dd3247d220f1 [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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.lifecycle.Service;
Brian Stankee312fc72016-02-16 15:07:13 -050021import org.onosproject.cli.AbstractShellCommand;
Ray Milkeyc7477292016-03-11 10:53:43 -080022import org.onosproject.utils.Comparators;
Claudine Chiu31ad5272016-02-17 20:56:24 +000023import org.onosproject.net.key.DeviceKey;
24import org.onosproject.net.key.DeviceKeyService;
Brian Stankee312fc72016-02-16 15:07:13 -050025
26import java.util.Collections;
27import java.util.List;
28
29import static com.google.common.collect.Lists.newArrayList;
30
31/**
32 * Lists all device keys.
33 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070034@Service
Brian Stankee312fc72016-02-16 15:07:13 -050035@Command(scope = "onos", name = "device-keys",
36 description = "Lists all device keys")
37
38public 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 Milkeyd84f89b2018-08-17 14:54:17 -070045 protected void doExecute() {
Brian Stankee312fc72016-02-16 15:07:13 -050046 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 {
柯志勇1006869525dc5532018-09-14 16:33:06 +080077 log.error("Unsupported device key type: {}" + deviceKey.type());
Brian Stankee312fc72016-02-16 15:07:13 -050078 }
79 }
80
81}