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