Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.apache.karaf.shell.commands.Option; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
Claudine Chiu | 31ad527 | 2016-02-17 20:56:24 +0000 | [diff] [blame] | 23 | import org.onosproject.net.key.DeviceKey; |
| 24 | import org.onosproject.net.key.DeviceKeyAdminService; |
| 25 | import org.onosproject.net.key.DeviceKeyId; |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 26 | |
| 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 | |
| 34 | public 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 { |
| 75 | print("Invalid Device key type: ", type); |
| 76 | return; |
| 77 | } |
| 78 | service.addKey(deviceKey); |
| 79 | print("Device Key successfully added."); |
| 80 | } |
| 81 | } |