Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.cli.net; |
| 18 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 22 | import org.apache.karaf.shell.api.action.Option; |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
Claudine Chiu | 31ad527 | 2016-02-17 20:56:24 +0000 | [diff] [blame] | 24 | import org.onosproject.net.key.DeviceKey; |
| 25 | import org.onosproject.net.key.DeviceKeyAdminService; |
| 26 | import org.onosproject.net.key.DeviceKeyId; |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Adds a device key. |
| 30 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 31 | @Service |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 32 | @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 | |
| 36 | public 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 Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 67 | protected void doExecute() { |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 68 | 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 { |
柯志勇10068695 | 53f36f3 | 2018-09-18 10:38:22 +0800 | [diff] [blame] | 77 | print("Invalid Device key type: {}", type); |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 78 | return; |
| 79 | } |
| 80 | service.addKey(deviceKey); |
| 81 | print("Device Key successfully added."); |
| 82 | } |
| 83 | } |