blob: 5a12bf6a66f628d5c7562c86a2d576577731f2ce [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;
22import org.onosproject.core.ApplicationId;
23
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070024import java.io.IOException;
25import java.net.URL;
26
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080027/**
28 * Manages application inventory.
29 */
30@Command(scope = "onos", name = "app",
31 description = "Manages application inventory")
32public class ApplicationCommand extends AbstractShellCommand {
33
34 static final String INSTALL = "install";
35 static final String UNINSTALL = "uninstall";
36 static final String ACTIVATE = "activate";
37 static final String DEACTIVATE = "deactivate";
38
39 @Argument(index = 0, name = "command",
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070040 description = "Command name (install|activate|deactivate|uninstall)",
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080041 required = true, multiValued = false)
42 String command = null;
43
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070044 @Argument(index = 1, name = "names", description = "Application name(s) or URL(s)",
Thomas Vachuskafba28572015-03-25 18:48:59 -070045 required = true, multiValued = true)
46 String[] names = null;
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080047
48 @Override
49 protected void execute() {
50 ApplicationAdminService service = get(ApplicationAdminService.class);
51 if (command.equals(INSTALL)) {
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070052 for (String name : names) {
53 if (!installApp(service, name)) {
54 return;
55 }
56 }
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080057
58 } else {
Thomas Vachuskafba28572015-03-25 18:48:59 -070059 for (String name : names) {
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070060 if (!manageApp(service, name)) {
Thomas Vachuskafba28572015-03-25 18:48:59 -070061 return;
62 }
Thomas Vachuska1dcb0482015-02-18 18:21:28 -080063 }
64 }
65 }
66
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070067 // Installs the application from input of the specified URL
68 private boolean installApp(ApplicationAdminService service, String url) {
69 try {
70 if (url.equals("-")) {
71 service.install(System.in);
72 } else {
73 service.install(new URL(url).openStream());
74 }
75 } catch (IOException e) {
76 error("Unable to get URL: %s", url);
77 return false;
78 }
79 return true;
80 }
81
82 // Manages the specified application.
83 private boolean manageApp(ApplicationAdminService service, String name) {
84 ApplicationId appId = service.getId(name);
85 if (appId == null) {
86 print("No such application: %s", name);
87 return false;
88 }
89
90 if (command.equals(UNINSTALL)) {
91 service.uninstall(appId);
92 } else if (command.equals(ACTIVATE)) {
93 service.activate(appId);
94 } else if (command.equals(DEACTIVATE)) {
95 service.deactivate(appId);
96 } else {
97 print("Unsupported command: %s", command);
98 return false;
99 }
100 return true;
101 }
102
Thomas Vachuska1dcb0482015-02-18 18:21:28 -0800103}