blob: 0571fb2aa4afc552f997bf6b6e1ce9f546f251d3 [file] [log] [blame]
Thomas Vachuska1dcb0482015-02-18 18:21:28 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska1dcb0482015-02-18 18:21:28 -08003 *
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 */
16package org.onosproject.cli.app;
17
Thomas Vachuska08b4dec2017-08-31 15:20:17 -070018import com.google.common.io.ByteStreams;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
Ray Milkey9dcc0e82018-10-01 10:26:53 -070021import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080023import org.onosproject.app.ApplicationAdminService;
24import org.onosproject.cli.AbstractShellCommand;
Yuta HIGUCHI315baad2017-01-31 15:14:30 -080025import org.onosproject.core.Application;
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080026import org.onosproject.core.ApplicationId;
27
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070028import java.io.IOException;
29import java.net.URL;
Yuta HIGUCHI315baad2017-01-31 15:14:30 -080030import java.util.List;
31import java.util.stream.Collectors;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070032
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080033/**
34 * Manages application inventory.
35 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070036@Service
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080037@Command(scope = "onos", name = "app",
38 description = "Manages application inventory")
39public 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 Vachuska08b4dec2017-08-31 15:20:17 -070045 static final String DOWNLOAD = "download";
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080046 @Argument(index = 0, name = "command",
pierventre2018fa52022-01-31 09:11:32 +010047 description = "Command name (install|activate|deactivate|uninstall|download)",
Ray Milkey9dcc0e82018-10-01 10:26:53 -070048 required = true)
49 @Completion(ApplicationCommandCompleter.class)
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080050 String command = null;
51
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070052 @Argument(index = 1, name = "names", description = "Application name(s) or URL(s)",
Thomas Vachuskafba28572015-03-25 18:48:59 -070053 required = true, multiValued = true)
Ray Milkey9dcc0e82018-10-01 10:26:53 -070054 @Completion(ApplicationNameCompleter.class)
Thomas Vachuskafba28572015-03-25 18:48:59 -070055 String[] names = null;
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080056
57 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070058 protected void doExecute() {
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080059 ApplicationAdminService service = get(ApplicationAdminService.class);
60 if (command.equals(INSTALL)) {
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070061 for (String name : names) {
62 if (!installApp(service, name)) {
63 return;
64 }
65 }
Thomas Vachuska08b4dec2017-08-31 15:20:17 -070066 } else if (command.equals(DOWNLOAD)) {
Thomas Vachuskafba28572015-03-25 18:48:59 -070067 for (String name : names) {
Thomas Vachuska08b4dec2017-08-31 15:20:17 -070068 if (!downloadApp(service, name)) {
Thomas Vachuskafba28572015-03-25 18:48:59 -070069 return;
70 }
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080071 }
Thomas Vachuska08b4dec2017-08-31 15:20:17 -070072 } else {
73 for (String name : names) {
74 if (!manageApp(service, name)) {
75 return;
76 }
77 }
78 }
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080079 }
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080080
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070081 // Installs the application from input of the specified URL
82 private boolean installApp(ApplicationAdminService service, String url) {
83 try {
Jon Halla3fcf672017-03-28 16:53:22 -070084 if ("-".equals(url)) {
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070085 service.install(System.in);
86 } else {
pierventre2018fa52022-01-31 09:11:32 +010087 service.install(new URL(url).openStream());
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070088 }
89 } catch (IOException e) {
90 error("Unable to get URL: %s", url);
91 return false;
92 }
93 return true;
94 }
95
Thomas Vachuska08b4dec2017-08-31 15:20:17 -070096 // Downloads the application bits to the standard output.
97 private boolean downloadApp(ApplicationAdminService service, String name) {
98 try {
99 ByteStreams.copy(service.getApplicationArchive(service.getId(name)),
100 System.out);
101 } catch (IOException e) {
102 error("Unable to download bits for application %s", name);
103 return false;
104 }
105 return true;
106 }
107
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700108 // Manages the specified application.
109 private boolean manageApp(ApplicationAdminService service, String name) {
110 ApplicationId appId = service.getId(name);
111 if (appId == null) {
Yuta HIGUCHI315baad2017-01-31 15:14:30 -0800112 List<Application> matches = service.getApplications().stream()
113 .filter(app -> app.id().name().matches(".*\\." + name + "$"))
114 .collect(Collectors.toList());
115
116 if (matches.size() == 1) {
117 // Found match
118 appId = matches.iterator().next().id();
119 } else if (!matches.isEmpty()) {
120 print("Did you mean one of: %s",
121 matches.stream()
122 .map(Application::id)
123 .map(ApplicationId::name)
124 .collect(Collectors.toList()));
125 return false;
126 }
127 }
128 if (appId == null) {
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700129 print("No such application: %s", name);
130 return false;
131 }
132
Jonathan Hartb5132fa2017-02-02 13:20:35 -0800133 String action;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700134 if (command.equals(UNINSTALL)) {
135 service.uninstall(appId);
Jonathan Hartb5132fa2017-02-02 13:20:35 -0800136 action = "Uninstalled";
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700137 } else if (command.equals(ACTIVATE)) {
138 service.activate(appId);
Jonathan Hartb5132fa2017-02-02 13:20:35 -0800139 action = "Activated";
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700140 } else if (command.equals(DEACTIVATE)) {
141 service.deactivate(appId);
Jonathan Hartb5132fa2017-02-02 13:20:35 -0800142 action = "Deactivated";
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700143 } else {
144 print("Unsupported command: %s", command);
145 return false;
146 }
Jonathan Hartb5132fa2017-02-02 13:20:35 -0800147 print("%s %s", action, appId.name());
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700148 return true;
149 }
150
Thomas Vachuska1dcb0482015-02-18 18:21:28 -0800151}