blob: 35afe52f09851455ecf24c48b922d05b8740e58f [file] [log] [blame]
Ray Milkeyd84f89b2018-08-17 14:54:17 -07001/*
2 * Copyright 2015-present Open Networking Foundation
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 */
16package org.onosproject.cli2.app;
17
18import com.google.common.io.ByteStreams;
19import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.Completion;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.onosproject.app.ApplicationAdminService;
24import org.onosproject.cli2.AbstractShellCommand;
25import org.onosproject.core.Application;
26import org.onosproject.core.ApplicationId;
27
28import java.io.IOException;
29import java.net.URL;
30import java.util.List;
31import java.util.stream.Collectors;
32
33/**
34 * Manages application inventory.
35 */
36@Service
37@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";
45 static final String DOWNLOAD = "download";
46
47 @Argument(index = 0, name = "command",
48 description = "Command name (install|activate|deactivate|uninstall|download)",
49 required = true, multiValued = false)
50 @Completion(ApplicationCommandCompleter.class)
51 String command = null;
52
53 @Argument(index = 1, name = "names", description = "Application name(s) or URL(s)",
54 required = true, multiValued = true)
55 @Completion(ApplicationNameCompleter.class)
56 String[] names = null;
57
58 @Override
59 protected void doExecute() {
60 ApplicationAdminService service = get(ApplicationAdminService.class);
61 if (command.equals(INSTALL)) {
62 for (String name : names) {
63 if (!installApp(service, name)) {
64 return;
65 }
66 }
67
68 } else if (command.equals(DOWNLOAD)) {
69 for (String name : names) {
70 if (!downloadApp(service, name)) {
71 return;
72 }
73 }
74 } else {
75 for (String name : names) {
76 if (!manageApp(service, name)) {
77 return;
78 }
79 }
80 }
81 return;
82 }
83
84 // Installs the application from input of the specified URL
85 private boolean installApp(ApplicationAdminService service, String url) {
86 try {
87 if ("-".equals(url)) {
88 service.install(System.in);
89 } else {
90 service.install(new URL(url).openStream());
91 }
92 } catch (IOException e) {
93 error("Unable to get URL: %s", url);
94 return false;
95 }
96 return true;
97 }
98
99 // Downloads the application bits to the standard output.
100 private boolean downloadApp(ApplicationAdminService service, String name) {
101 try {
102 ByteStreams.copy(service.getApplicationArchive(service.getId(name)),
103 System.out);
104 } catch (IOException e) {
105 error("Unable to download bits for application %s", name);
106 return false;
107 }
108 return true;
109 }
110
111 // Manages the specified application.
112 private boolean manageApp(ApplicationAdminService service, String name) {
113 ApplicationId appId = service.getId(name);
114 if (appId == null) {
115 List<Application> matches = service.getApplications().stream()
116 .filter(app -> app.id().name().matches(".*\\." + name + "$"))
117 .collect(Collectors.toList());
118
119 if (matches.size() == 1) {
120 // Found match
121 appId = matches.iterator().next().id();
122 } else if (!matches.isEmpty()) {
123 print("Did you mean one of: %s",
124 matches.stream()
125 .map(Application::id)
126 .map(ApplicationId::name)
127 .collect(Collectors.toList()));
128 return false;
129 }
130 }
131 if (appId == null) {
132 print("No such application: %s", name);
133 return false;
134 }
135
136 String action;
137 if (command.equals(UNINSTALL)) {
138 service.uninstall(appId);
139 action = "Uninstalled";
140 } else if (command.equals(ACTIVATE)) {
141 service.activate(appId);
142 action = "Activated";
143 } else if (command.equals(DEACTIVATE)) {
144 service.deactivate(appId);
145 action = "Deactivated";
146 } else {
147 print("Unsupported command: %s", command);
148 return false;
149 }
150 print("%s %s", action, appId.name());
151 return true;
152 }
153
154}