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