Thomas Vachuska | 1dcb048 | 2015-02-18 18:21:28 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 1dcb048 | 2015-02-18 18:21:28 -0800 | [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 | package org.onosproject.cli.app; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onosproject.app.ApplicationAdminService; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.core.ApplicationId; |
| 23 | |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 24 | import java.io.IOException; |
| 25 | import java.net.URL; |
| 26 | |
Thomas Vachuska | 1dcb048 | 2015-02-18 18:21:28 -0800 | [diff] [blame] | 27 | /** |
| 28 | * Manages application inventory. |
| 29 | */ |
| 30 | @Command(scope = "onos", name = "app", |
| 31 | description = "Manages application inventory") |
| 32 | public class ApplicationCommand extends AbstractShellCommand { |
| 33 | |
| 34 | static final String INSTALL = "install"; |
| 35 | static final String UNINSTALL = "uninstall"; |
| 36 | static final String ACTIVATE = "activate"; |
| 37 | static final String DEACTIVATE = "deactivate"; |
| 38 | |
| 39 | @Argument(index = 0, name = "command", |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 40 | description = "Command name (install|activate|deactivate|uninstall)", |
Thomas Vachuska | 1dcb048 | 2015-02-18 18:21:28 -0800 | [diff] [blame] | 41 | required = true, multiValued = false) |
| 42 | String command = null; |
| 43 | |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 44 | @Argument(index = 1, name = "names", description = "Application name(s) or URL(s)", |
Thomas Vachuska | fba2857 | 2015-03-25 18:48:59 -0700 | [diff] [blame] | 45 | required = true, multiValued = true) |
| 46 | String[] names = null; |
Thomas Vachuska | 1dcb048 | 2015-02-18 18:21:28 -0800 | [diff] [blame] | 47 | |
| 48 | @Override |
| 49 | protected void execute() { |
| 50 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 51 | if (command.equals(INSTALL)) { |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 52 | for (String name : names) { |
| 53 | if (!installApp(service, name)) { |
| 54 | return; |
| 55 | } |
| 56 | } |
Thomas Vachuska | 1dcb048 | 2015-02-18 18:21:28 -0800 | [diff] [blame] | 57 | |
| 58 | } else { |
Thomas Vachuska | fba2857 | 2015-03-25 18:48:59 -0700 | [diff] [blame] | 59 | for (String name : names) { |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 60 | if (!manageApp(service, name)) { |
Thomas Vachuska | fba2857 | 2015-03-25 18:48:59 -0700 | [diff] [blame] | 61 | return; |
| 62 | } |
Thomas Vachuska | 1dcb048 | 2015-02-18 18:21:28 -0800 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 67 | // Installs the application from input of the specified URL |
| 68 | private boolean installApp(ApplicationAdminService service, String url) { |
| 69 | try { |
| 70 | if (url.equals("-")) { |
| 71 | service.install(System.in); |
| 72 | } else { |
| 73 | service.install(new URL(url).openStream()); |
| 74 | } |
| 75 | } catch (IOException e) { |
| 76 | error("Unable to get URL: %s", url); |
| 77 | return false; |
| 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | // Manages the specified application. |
| 83 | private boolean manageApp(ApplicationAdminService service, String name) { |
| 84 | ApplicationId appId = service.getId(name); |
| 85 | if (appId == null) { |
| 86 | print("No such application: %s", name); |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | if (command.equals(UNINSTALL)) { |
| 91 | service.uninstall(appId); |
| 92 | } else if (command.equals(ACTIVATE)) { |
| 93 | service.activate(appId); |
| 94 | } else if (command.equals(DEACTIVATE)) { |
| 95 | service.deactivate(appId); |
| 96 | } else { |
| 97 | print("Unsupported command: %s", command); |
| 98 | return false; |
| 99 | } |
| 100 | return true; |
| 101 | } |
| 102 | |
Thomas Vachuska | 1dcb048 | 2015-02-18 18:21:28 -0800 | [diff] [blame] | 103 | } |