Ray Milkey
Committed by Gerrit Code Review

ONOS-1597 - add an optional appId to CLI commands that create intents

Change-Id: Iaf14f1a98f617eb025dab1b16542d68184082ceb
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
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 + */
16 +package org.onosproject.cli.app;
17 +
18 +import java.util.Iterator;
19 +import java.util.List;
20 +import java.util.Spliterator;
21 +import java.util.Spliterators;
22 +import java.util.stream.StreamSupport;
23 +
24 +import org.onosproject.app.ApplicationService;
25 +import org.onosproject.cli.AbstractChoicesCompleter;
26 +import org.onosproject.core.Application;
27 +
28 +import static java.util.stream.Collectors.toList;
29 +import static org.onosproject.app.ApplicationState.INSTALLED;
30 +import static org.onosproject.cli.AbstractShellCommand.get;
31 +
32 +/**
33 + * All installed application name completer.
34 + */
35 +public class AllApplicationNamesCompleter extends AbstractChoicesCompleter {
36 + @Override
37 + public List<String> choices() {
38 +
39 + // Fetch the service and return the list of app names
40 + ApplicationService service = get(ApplicationService.class);
41 + Iterator<Application> it = service.getApplications().iterator();
42 +
43 + // Filter the list of apps, selecting only the installed ones.
44 + // Add each app name to the list of choices.
45 + return
46 + StreamSupport.stream(
47 + Spliterators.spliteratorUnknownSize(it, Spliterator.ORDERED), false)
48 + .filter(app -> service.getState(app.id()) == INSTALLED)
49 + .map(app -> app.id().name())
50 + .collect(toList());
51 +
52 + }
53 +
54 +}
...@@ -23,6 +23,8 @@ import java.util.List; ...@@ -23,6 +23,8 @@ import java.util.List;
23 23
24 import org.apache.karaf.shell.commands.Option; 24 import org.apache.karaf.shell.commands.Option;
25 import org.onosproject.cli.AbstractShellCommand; 25 import org.onosproject.cli.AbstractShellCommand;
26 +import org.onosproject.core.ApplicationId;
27 +import org.onosproject.core.CoreService;
26 import org.onosproject.net.Link; 28 import org.onosproject.net.Link;
27 import org.onosproject.net.flow.DefaultTrafficSelector; 29 import org.onosproject.net.flow.DefaultTrafficSelector;
28 import org.onosproject.net.flow.DefaultTrafficTreatment; 30 import org.onosproject.net.flow.DefaultTrafficTreatment;
...@@ -84,6 +86,10 @@ public abstract class ConnectivityIntentCommand extends AbstractShellCommand { ...@@ -84,6 +86,10 @@ public abstract class ConnectivityIntentCommand extends AbstractShellCommand {
84 required = false, multiValued = false) 86 required = false, multiValued = false)
85 private boolean lambda = false; 87 private boolean lambda = false;
86 88
89 + @Option(name = "-a", aliases = "--appId", description = "Application Id",
90 + required = false, multiValued = false)
91 + private String appId = null;
92 +
87 @Option(name = "-k", aliases = "--key", description = "Intent Key", 93 @Option(name = "-k", aliases = "--key", description = "Intent Key",
88 required = false, multiValued = false) 94 required = false, multiValued = false)
89 private String intentKey = null; 95 private String intentKey = null;
...@@ -229,6 +235,18 @@ public abstract class ConnectivityIntentCommand extends AbstractShellCommand { ...@@ -229,6 +235,18 @@ public abstract class ConnectivityIntentCommand extends AbstractShellCommand {
229 return constraints; 235 return constraints;
230 } 236 }
231 237
238 + @Override
239 + protected ApplicationId appId() {
240 + ApplicationId appIdForIntent;
241 + if (appId == null) {
242 + appIdForIntent = super.appId();
243 + } else {
244 + CoreService service = get(CoreService.class);
245 + appIdForIntent = service.getAppId(appId);
246 + }
247 + return appIdForIntent;
248 + }
249 +
232 /** 250 /**
233 * Creates a key for an intent based on command line arguments. If a key 251 * Creates a key for an intent based on command line arguments. If a key
234 * has been specified, it is returned. If no key is specified, null 252 * has been specified, it is returned. If no key is specified, null
...@@ -238,6 +256,8 @@ public abstract class ConnectivityIntentCommand extends AbstractShellCommand { ...@@ -238,6 +256,8 @@ public abstract class ConnectivityIntentCommand extends AbstractShellCommand {
238 */ 256 */
239 protected Key key() { 257 protected Key key() {
240 Key key = null; 258 Key key = null;
259 + ApplicationId appIdForIntent;
260 +
241 if (intentKey != null) { 261 if (intentKey != null) {
242 key = Key.of(intentKey, appId()); 262 key = Key.of(intentKey, appId());
243 } 263 }
......
...@@ -151,6 +151,9 @@ ...@@ -151,6 +151,9 @@
151 <ref component-id="hostIdCompleter"/> 151 <ref component-id="hostIdCompleter"/>
152 <null/> 152 <null/>
153 </completers> 153 </completers>
154 + <optional-completers>
155 + <entry key="-a" value-ref="allAppNameCompleter"/>
156 + </optional-completers>
154 </command> 157 </command>
155 <command> 158 <command>
156 <action class="org.onosproject.cli.net.AddPointToPointIntentCommand"/> 159 <action class="org.onosproject.cli.net.AddPointToPointIntentCommand"/>
...@@ -162,6 +165,7 @@ ...@@ -162,6 +165,7 @@
162 <optional-completers> 165 <optional-completers>
163 <entry key="-t" value-ref="ethTypeCompleter"/> 166 <entry key="-t" value-ref="ethTypeCompleter"/>
164 <entry key="--ipProto" value-ref="ipProtocolCompleter"/> 167 <entry key="--ipProto" value-ref="ipProtocolCompleter"/>
168 + <entry key="-a" value-ref="allAppNameCompleter"/>
165 </optional-completers> 169 </optional-completers>
166 </command> 170 </command>
167 <command> 171 <command>
...@@ -171,6 +175,9 @@ ...@@ -171,6 +175,9 @@
171 <ref component-id="connectPointCompleter"/> 175 <ref component-id="connectPointCompleter"/>
172 <null/> 176 <null/>
173 </completers> 177 </completers>
178 + <optional-completers>
179 + <entry key="-a" value-ref="allAppNameCompleter"/>
180 + </optional-completers>
174 </command> 181 </command>
175 <command> 182 <command>
176 <action class="org.onosproject.cli.net.GetStatistics"/> 183 <action class="org.onosproject.cli.net.GetStatistics"/>
...@@ -186,6 +193,7 @@ ...@@ -186,6 +193,7 @@
186 <optional-completers> 193 <optional-completers>
187 <entry key="-t" value-ref="ethTypeCompleter"/> 194 <entry key="-t" value-ref="ethTypeCompleter"/>
188 <entry key="--ipProto" value-ref="ipProtocolCompleter"/> 195 <entry key="--ipProto" value-ref="ipProtocolCompleter"/>
196 + <entry key="-a" value-ref="allAppNameCompleter"/>
189 </optional-completers> 197 </optional-completers>
190 </command> 198 </command>
191 <command> 199 <command>
...@@ -196,6 +204,7 @@ ...@@ -196,6 +204,7 @@
196 <optional-completers> 204 <optional-completers>
197 <entry key="-t" value-ref="ethTypeCompleter"/> 205 <entry key="-t" value-ref="ethTypeCompleter"/>
198 <entry key="--ipProto" value-ref="ipProtocolCompleter"/> 206 <entry key="--ipProto" value-ref="ipProtocolCompleter"/>
207 + <entry key="-a" value-ref="allAppNameCompleter"/>
199 </optional-completers> 208 </optional-completers>
200 </command> 209 </command>
201 <command> 210 <command>
...@@ -317,11 +326,15 @@ ...@@ -317,11 +326,15 @@
317 <ref component-id="connectPointCompleter"/> 326 <ref component-id="connectPointCompleter"/>
318 <null/> 327 <null/>
319 </completers> 328 </completers>
329 + <optional-completers>
330 + <entry key="-a" value-ref="allAppNameCompleter"/>
331 + </optional-completers>
320 </command> 332 </command>
321 </command-bundle> 333 </command-bundle>
322 334
323 <bean id="appCommandCompleter" class="org.onosproject.cli.app.ApplicationCommandCompleter"/> 335 <bean id="appCommandCompleter" class="org.onosproject.cli.app.ApplicationCommandCompleter"/>
324 <bean id="appNameCompleter" class="org.onosproject.cli.app.ApplicationNameCompleter"/> 336 <bean id="appNameCompleter" class="org.onosproject.cli.app.ApplicationNameCompleter"/>
337 + <bean id="allAppNameCompleter" class="org.onosproject.cli.app.AllApplicationNamesCompleter"/>
325 <bean id="appIdWithIntentNameCompleter" class="org.onosproject.cli.app.ApplicationIdWithIntentNameCompleter"/> 338 <bean id="appIdWithIntentNameCompleter" class="org.onosproject.cli.app.ApplicationIdWithIntentNameCompleter"/>
326 <bean id="cfgCommandCompleter" class="org.onosproject.cli.cfg.ComponentConfigCommandCompleter"/> 339 <bean id="cfgCommandCompleter" class="org.onosproject.cli.cfg.ComponentConfigCommandCompleter"/>
327 <bean id="componentNameCompleter" class="org.onosproject.cli.cfg.ComponentNameCompleter"/> 340 <bean id="componentNameCompleter" class="org.onosproject.cli.cfg.ComponentNameCompleter"/>
......