blob: 55e7a93207a5cfb63cc76adffb25bcea20a64f3a [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;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080022import org.onosproject.app.ApplicationAdminService;
23import org.onosproject.cli.AbstractShellCommand;
Yuta HIGUCHI315baad2017-01-31 15:14:30 -080024import org.onosproject.core.Application;
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080025import org.onosproject.core.ApplicationId;
26
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070027import java.io.IOException;
28import java.net.URL;
Yuta HIGUCHI315baad2017-01-31 15:14:30 -080029import java.util.List;
30import java.util.stream.Collectors;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070031
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080032/**
33 * Manages application inventory.
34 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070035@Service
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080036@Command(scope = "onos", name = "app",
37 description = "Manages application inventory")
38public class ApplicationCommand extends AbstractShellCommand {
39
40 static final String INSTALL = "install";
41 static final String UNINSTALL = "uninstall";
42 static final String ACTIVATE = "activate";
43 static final String DEACTIVATE = "deactivate";
Thomas Vachuska08b4dec2017-08-31 15:20:17 -070044 static final String DOWNLOAD = "download";
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080045
46 @Argument(index = 0, name = "command",
Thomas Vachuska08b4dec2017-08-31 15:20:17 -070047 description = "Command name (install|activate|deactivate|uninstall|download)",
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080048 required = true, multiValued = false)
49 String command = null;
50
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070051 @Argument(index = 1, name = "names", description = "Application name(s) or URL(s)",
Thomas Vachuskafba28572015-03-25 18:48:59 -070052 required = true, multiValued = true)
53 String[] names = null;
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080054
55 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070056 protected void doExecute() {
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080057 ApplicationAdminService service = get(ApplicationAdminService.class);
58 if (command.equals(INSTALL)) {
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070059 for (String name : names) {
60 if (!installApp(service, name)) {
61 return;
62 }
63 }
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080064
Thomas Vachuska08b4dec2017-08-31 15:20:17 -070065 } else if (command.equals(DOWNLOAD)) {
Thomas Vachuskafba28572015-03-25 18:48:59 -070066 for (String name : names) {
Thomas Vachuska08b4dec2017-08-31 15:20:17 -070067 if (!downloadApp(service, name)) {
Thomas Vachuskafba28572015-03-25 18:48:59 -070068 return;
69 }
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080070 }
Thomas Vachuska08b4dec2017-08-31 15:20:17 -070071 } else {
72 for (String name : names) {
73 if (!manageApp(service, name)) {
74 return;
75 }
76 }
77 }
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080078 }
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080079
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070080 // Installs the application from input of the specified URL
81 private boolean installApp(ApplicationAdminService service, String url) {
82 try {
Jon Halla3fcf672017-03-28 16:53:22 -070083 if ("-".equals(url)) {
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070084 service.install(System.in);
85 } else {
86 service.install(new URL(url).openStream());
87 }
88 } catch (IOException e) {
89 error("Unable to get URL: %s", url);
90 return false;
91 }
92 return true;
93 }
94
Thomas Vachuska08b4dec2017-08-31 15:20:17 -070095 // Downloads the application bits to the standard output.
96 private boolean downloadApp(ApplicationAdminService service, String name) {
97 try {
98 ByteStreams.copy(service.getApplicationArchive(service.getId(name)),
99 System.out);
100 } catch (IOException e) {
101 error("Unable to download bits for application %s", name);
102 return false;
103 }
104 return true;
105 }
106
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700107 // Manages the specified application.
108 private boolean manageApp(ApplicationAdminService service, String name) {
109 ApplicationId appId = service.getId(name);
110 if (appId == null) {
Yuta HIGUCHI315baad2017-01-31 15:14:30 -0800111 List<Application> matches = service.getApplications().stream()
112 .filter(app -> app.id().name().matches(".*\\." + name + "$"))
113 .collect(Collectors.toList());
114
115 if (matches.size() == 1) {
116 // Found match
117 appId = matches.iterator().next().id();
118 } else if (!matches.isEmpty()) {
119 print("Did you mean one of: %s",
120 matches.stream()
121 .map(Application::id)
122 .map(ApplicationId::name)
123 .collect(Collectors.toList()));
124 return false;
125 }
126 }
127 if (appId == null) {
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700128 print("No such application: %s", name);
129 return false;
130 }
131
Jonathan Hartb5132fa2017-02-02 13:20:35 -0800132 String action;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700133 if (command.equals(UNINSTALL)) {
134 service.uninstall(appId);
Jonathan Hartb5132fa2017-02-02 13:20:35 -0800135 action = "Uninstalled";
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700136 } else if (command.equals(ACTIVATE)) {
137 service.activate(appId);
Jonathan Hartb5132fa2017-02-02 13:20:35 -0800138 action = "Activated";
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700139 } else if (command.equals(DEACTIVATE)) {
140 service.deactivate(appId);
Jonathan Hartb5132fa2017-02-02 13:20:35 -0800141 action = "Deactivated";
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700142 } else {
143 print("Unsupported command: %s", command);
144 return false;
145 }
Jonathan Hartb5132fa2017-02-02 13:20:35 -0800146 print("%s %s", action, appId.name());
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700147 return true;
148 }
149
Thomas Vachuska1dcb0482015-02-18 18:21:28 -0800150}