blob: c9d8f08827ad723ea76c1e8e12a61f768b8080fb [file] [log] [blame]
Thomas Vachuska1dcb0482015-02-18 18:21:28 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onosproject.app.ApplicationAdminService;
21import org.onosproject.cli.AbstractShellCommand;
Yuta HIGUCHI315baad2017-01-31 15:14:30 -080022import org.onosproject.core.Application;
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080023import org.onosproject.core.ApplicationId;
24
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070025import java.io.IOException;
26import java.net.URL;
Yuta HIGUCHI315baad2017-01-31 15:14:30 -080027import java.util.List;
28import java.util.stream.Collectors;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070029
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080030/**
31 * Manages application inventory.
32 */
33@Command(scope = "onos", name = "app",
34 description = "Manages application inventory")
35public class ApplicationCommand extends AbstractShellCommand {
36
37 static final String INSTALL = "install";
38 static final String UNINSTALL = "uninstall";
39 static final String ACTIVATE = "activate";
40 static final String DEACTIVATE = "deactivate";
41
42 @Argument(index = 0, name = "command",
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070043 description = "Command name (install|activate|deactivate|uninstall)",
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080044 required = true, multiValued = false)
45 String command = null;
46
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070047 @Argument(index = 1, name = "names", description = "Application name(s) or URL(s)",
Thomas Vachuskafba28572015-03-25 18:48:59 -070048 required = true, multiValued = true)
49 String[] names = null;
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080050
51 @Override
52 protected void execute() {
53 ApplicationAdminService service = get(ApplicationAdminService.class);
54 if (command.equals(INSTALL)) {
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070055 for (String name : names) {
56 if (!installApp(service, name)) {
57 return;
58 }
59 }
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080060
61 } else {
Thomas Vachuskafba28572015-03-25 18:48:59 -070062 for (String name : names) {
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070063 if (!manageApp(service, name)) {
Thomas Vachuskafba28572015-03-25 18:48:59 -070064 return;
65 }
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080066 }
67 }
68 }
69
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070070 // Installs the application from input of the specified URL
71 private boolean installApp(ApplicationAdminService service, String url) {
72 try {
Jon Halla3fcf672017-03-28 16:53:22 -070073 if ("-".equals(url)) {
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070074 service.install(System.in);
75 } else {
76 service.install(new URL(url).openStream());
77 }
78 } catch (IOException e) {
79 error("Unable to get URL: %s", url);
80 return false;
81 }
82 return true;
83 }
84
85 // Manages the specified application.
86 private boolean manageApp(ApplicationAdminService service, String name) {
87 ApplicationId appId = service.getId(name);
88 if (appId == null) {
Yuta HIGUCHI315baad2017-01-31 15:14:30 -080089 List<Application> matches = service.getApplications().stream()
90 .filter(app -> app.id().name().matches(".*\\." + name + "$"))
91 .collect(Collectors.toList());
92
93 if (matches.size() == 1) {
94 // Found match
95 appId = matches.iterator().next().id();
96 } else if (!matches.isEmpty()) {
97 print("Did you mean one of: %s",
98 matches.stream()
99 .map(Application::id)
100 .map(ApplicationId::name)
101 .collect(Collectors.toList()));
102 return false;
103 }
104 }
105 if (appId == null) {
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700106 print("No such application: %s", name);
107 return false;
108 }
109
Jonathan Hartb5132fa2017-02-02 13:20:35 -0800110 String action;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700111 if (command.equals(UNINSTALL)) {
112 service.uninstall(appId);
Jonathan Hartb5132fa2017-02-02 13:20:35 -0800113 action = "Uninstalled";
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700114 } else if (command.equals(ACTIVATE)) {
115 service.activate(appId);
Jonathan Hartb5132fa2017-02-02 13:20:35 -0800116 action = "Activated";
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700117 } else if (command.equals(DEACTIVATE)) {
118 service.deactivate(appId);
Jonathan Hartb5132fa2017-02-02 13:20:35 -0800119 action = "Deactivated";
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700120 } else {
121 print("Unsupported command: %s", command);
122 return false;
123 }
Jonathan Hartb5132fa2017-02-02 13:20:35 -0800124 print("%s %s", action, appId.name());
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700125 return true;
126 }
127
Thomas Vachuska1dcb0482015-02-18 18:21:28 -0800128}