blob: 44bc4834ff1ac3442a4cb95c83170966b1e3dec4 [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.Argument;
20import org.apache.karaf.shell.commands.Command;
21import org.apache.karaf.shell.commands.Option;
22import org.onosproject.cli.AbstractShellCommand;
Claudine Chiu31ad5272016-02-17 20:56:24 +000023import org.onosproject.net.key.DeviceKey;
24import org.onosproject.net.key.DeviceKeyAdminService;
25import org.onosproject.net.key.DeviceKeyId;
Brian Stankee312fc72016-02-16 15:07:13 -050026
27/**
28 * Adds a device key.
29 */
30@Command(scope = "onos", name = "device-key-add",
31 description = "Adds a device key. Adding a new device key with " +
32 "the same id will replace the existing device key.")
33
34public class DeviceKeyAddCommand extends AbstractShellCommand {
35
36 private static final String COMMUNITY_NAME = "CommunityName";
37 private static final String USERNAME = "UsernamePassword";
38
39 @Argument(index = 0, name = "id", description = "Device Key ID",
40 required = true, multiValued = false)
41 String id = null;
42
43 @Argument(index = 1, name = "type", description = "Device Key Type, " +
44 "it includes CommunityName, UsernamePassword.",
45 required = true, multiValued = false)
46 String type = null;
47
48 @Option(name = "-c", aliases = "--communityName", description = "Device Key Community Name",
49 required = false, multiValued = false)
50 String communityName = null;
51
52 @Option(name = "-l", aliases = "--label", description = "Device Key Label",
53 required = false, multiValued = false)
54 String label = null;
55
56 @Option(name = "-u", aliases = "--username", description = "Device Key Username",
57 required = false, multiValued = false)
58 String username = null;
59
60 @Option(name = "-p", aliases = "--password", description = "Device Key Password",
61 required = false, multiValued = false)
62 String password = null;
63
64 @Override
65 protected void execute() {
66 DeviceKeyAdminService service = get(DeviceKeyAdminService.class);
67 DeviceKey deviceKey = null;
68 if (type.equalsIgnoreCase(COMMUNITY_NAME)) {
69 deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(DeviceKeyId.deviceKeyId(id),
70 label, communityName);
71 } else if (type.equalsIgnoreCase(USERNAME)) {
72 deviceKey = DeviceKey.createDeviceKeyUsingUsernamePassword(DeviceKeyId.deviceKeyId(id),
73 label, username, password);
74 } else {
柯志勇1006869553f36f32018-09-18 10:38:22 +080075 print("Invalid Device key type: {}", type);
Brian Stankee312fc72016-02-16 15:07:13 -050076 return;
77 }
78 service.addKey(deviceKey);
79 print("Device Key successfully added.");
80 }
81}