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