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