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; |
Yuta HIGUCHI | 315baad | 2017-01-31 15:14:30 -0800 | [diff] [blame] | 22 | import org.onosproject.core.Application; |
Thomas Vachuska | 1dcb048 | 2015-02-18 18:21:28 -0800 | [diff] [blame] | 23 | import org.onosproject.core.ApplicationId; |
| 24 | |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 25 | import java.io.IOException; |
| 26 | import java.net.URL; |
Yuta HIGUCHI | 315baad | 2017-01-31 15:14:30 -0800 | [diff] [blame] | 27 | import java.util.List; |
| 28 | import java.util.stream.Collectors; |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 29 | |
Thomas Vachuska | 1dcb048 | 2015-02-18 18:21:28 -0800 | [diff] [blame] | 30 | /** |
| 31 | * Manages application inventory. |
| 32 | */ |
| 33 | @Command(scope = "onos", name = "app", |
| 34 | description = "Manages application inventory") |
| 35 | public class ApplicationCommand extends AbstractShellCommand { |
| 36 | |
| 37 | static final String INSTALL = "install"; |
| 38 | static final String UNINSTALL = "uninstall"; |
| 39 | static final String ACTIVATE = "activate"; |
| 40 | static final String DEACTIVATE = "deactivate"; |
| 41 | |
| 42 | @Argument(index = 0, name = "command", |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 43 | description = "Command name (install|activate|deactivate|uninstall)", |
Thomas Vachuska | 1dcb048 | 2015-02-18 18:21:28 -0800 | [diff] [blame] | 44 | required = true, multiValued = false) |
| 45 | String command = null; |
| 46 | |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 47 | @Argument(index = 1, name = "names", description = "Application name(s) or URL(s)", |
Thomas Vachuska | fba2857 | 2015-03-25 18:48:59 -0700 | [diff] [blame] | 48 | required = true, multiValued = true) |
| 49 | String[] names = null; |
Thomas Vachuska | 1dcb048 | 2015-02-18 18:21:28 -0800 | [diff] [blame] | 50 | |
| 51 | @Override |
| 52 | protected void execute() { |
| 53 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 54 | if (command.equals(INSTALL)) { |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 55 | for (String name : names) { |
| 56 | if (!installApp(service, name)) { |
| 57 | return; |
| 58 | } |
| 59 | } |
Thomas Vachuska | 1dcb048 | 2015-02-18 18:21:28 -0800 | [diff] [blame] | 60 | |
| 61 | } else { |
Thomas Vachuska | fba2857 | 2015-03-25 18:48:59 -0700 | [diff] [blame] | 62 | for (String name : names) { |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 63 | if (!manageApp(service, name)) { |
Thomas Vachuska | fba2857 | 2015-03-25 18:48:59 -0700 | [diff] [blame] | 64 | return; |
| 65 | } |
Thomas Vachuska | 1dcb048 | 2015-02-18 18:21:28 -0800 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 70 | // Installs the application from input of the specified URL |
| 71 | private boolean installApp(ApplicationAdminService service, String url) { |
| 72 | try { |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 73 | if ("-".equals(url)) { |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 74 | service.install(System.in); |
| 75 | } else { |
| 76 | service.install(new URL(url).openStream()); |
| 77 | } |
| 78 | } catch (IOException e) { |
| 79 | error("Unable to get URL: %s", url); |
| 80 | return false; |
| 81 | } |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | // Manages the specified application. |
| 86 | private boolean manageApp(ApplicationAdminService service, String name) { |
| 87 | ApplicationId appId = service.getId(name); |
| 88 | if (appId == null) { |
Yuta HIGUCHI | 315baad | 2017-01-31 15:14:30 -0800 | [diff] [blame] | 89 | List<Application> matches = service.getApplications().stream() |
| 90 | .filter(app -> app.id().name().matches(".*\\." + name + "$")) |
| 91 | .collect(Collectors.toList()); |
| 92 | |
| 93 | if (matches.size() == 1) { |
| 94 | // Found match |
| 95 | appId = matches.iterator().next().id(); |
| 96 | } else if (!matches.isEmpty()) { |
| 97 | print("Did you mean one of: %s", |
| 98 | matches.stream() |
| 99 | .map(Application::id) |
| 100 | .map(ApplicationId::name) |
| 101 | .collect(Collectors.toList())); |
| 102 | return false; |
| 103 | } |
| 104 | } |
| 105 | if (appId == null) { |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 106 | print("No such application: %s", name); |
| 107 | return false; |
| 108 | } |
| 109 | |
Jonathan Hart | b5132fa | 2017-02-02 13:20:35 -0800 | [diff] [blame] | 110 | String action; |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 111 | if (command.equals(UNINSTALL)) { |
| 112 | service.uninstall(appId); |
Jonathan Hart | b5132fa | 2017-02-02 13:20:35 -0800 | [diff] [blame] | 113 | action = "Uninstalled"; |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 114 | } else if (command.equals(ACTIVATE)) { |
| 115 | service.activate(appId); |
Jonathan Hart | b5132fa | 2017-02-02 13:20:35 -0800 | [diff] [blame] | 116 | action = "Activated"; |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 117 | } else if (command.equals(DEACTIVATE)) { |
| 118 | service.deactivate(appId); |
Jonathan Hart | b5132fa | 2017-02-02 13:20:35 -0800 | [diff] [blame] | 119 | action = "Deactivated"; |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 120 | } else { |
| 121 | print("Unsupported command: %s", command); |
| 122 | return false; |
| 123 | } |
Jonathan Hart | b5132fa | 2017-02-02 13:20:35 -0800 | [diff] [blame] | 124 | print("%s %s", action, appId.name()); |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 125 | return true; |
| 126 | } |
| 127 | |
Thomas Vachuska | 1dcb048 | 2015-02-18 18:21:28 -0800 | [diff] [blame] | 128 | } |