Ray Milkey

ONOS-1058 - Add application Id to intent withdraw command

Change-Id: I6f660f2e115e4acc9d3c7d1ab2e2115caaf1822f
......@@ -59,6 +59,10 @@
<groupId>org.apache.karaf.shell</groupId>
<artifactId>org.apache.karaf.shell.console</artifactId>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
</dependency>
</dependencies>
<build>
......@@ -67,6 +71,11 @@
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
</plugin>
</plugins>
</build>
......
......@@ -52,7 +52,8 @@ public abstract class AbstractShellCommand extends OsgiCommandSupport {
* @return command-line application identifier
*/
protected ApplicationId appId() {
return get(CoreService.class).registerApplication("org.onosproject.cli");
return get(CoreService.class)
.registerApplication("org.onosproject.cli");
}
/**
......
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.cli;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.onosproject.core.CoreService;
/**
* OSGI Component for the ONOS CLI.
*/
@Component(immediate = true)
public class CliComponent {
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected CoreService coreService;
@Activate
public void activate() {
coreService
.registerApplication("org.onosproject.cli");
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.cli.app;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import org.apache.karaf.shell.console.Completer;
import org.apache.karaf.shell.console.completer.StringsCompleter;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
/**
* Application name completer.
*/
public class ApplicationIdNameCompleter implements Completer {
@Override
public int complete(String buffer, int cursor, List<String> candidates) {
// Delegate string completer
StringsCompleter delegate = new StringsCompleter();
// Fetch our service and feed it's offerings to the string completer
CoreService service = AbstractShellCommand.get(CoreService.class);
Set<ApplicationId> ids = service.getAppIds();
SortedSet<String> strings = delegate.getStrings();
for (ApplicationId id : ids) {
strings.add(id.name());
}
// Now let the completer do the work for figuring out what to offer.
return delegate.complete(buffer, cursor, candidates);
}
}
......@@ -18,6 +18,8 @@ package org.onosproject.cli.net;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.net.intent.Intent;
import org.onosproject.net.intent.IntentService;
import org.onosproject.net.intent.Key;
......@@ -25,28 +27,44 @@ import org.onosproject.net.intent.Key;
import java.math.BigInteger;
/**
* Removes host-to-host connectivity intent.
* Removes an intent.
*/
@Command(scope = "onos", name = "remove-intent",
description = "Removes the specified intent")
public class IntentRemoveCommand extends AbstractShellCommand {
@Argument(index = 0, name = "id", description = "Intent ID",
@Argument(index = 0, name = "app",
description = "Application ID",
required = true, multiValued = false)
String applicationIdString = null;
@Argument(index = 1, name = "id",
description = "Intent ID",
required = true, multiValued = false)
String id = null;
@Override
protected void execute() {
IntentService service = get(IntentService.class);
IntentService intentService = get(IntentService.class);
CoreService coreService = get(CoreService.class);
ApplicationId appId = appId();
if (applicationIdString != null) {
appId = coreService.getAppId(applicationIdString);
if (appId == null) {
print("Cannot find application Id %s", applicationIdString);
return;
}
}
if (id.startsWith("0x")) {
id = id.replaceFirst("0x", "");
}
Key key = Key.of(new BigInteger(id, 16).longValue(), appId());
Intent intent = service.getIntent(key);
Key key = Key.of(new BigInteger(id, 16).longValue(), appId);
Intent intent = intentService.getIntent(key);
if (intent != null) {
service.withdraw(intent);
intentService.withdraw(intent);
}
}
}
......
......@@ -130,6 +130,7 @@
<command>
<action class="org.onosproject.cli.net.IntentRemoveCommand"/>
<completers>
<ref component-id="appIdNameCompleter" />
<ref component-id="intentIdCompleter"/>
<null/>
</completers>
......@@ -279,6 +280,7 @@
</command-bundle>
<bean id="appNameCompleter" class="org.onosproject.cli.app.ApplicationNameCompleter"/>
<bean id="appIdNameCompleter" class="org.onosproject.cli.app.ApplicationIdNameCompleter"/>
<bean id="nodeIdCompleter" class="org.onosproject.cli.NodeIdCompleter"/>
<bean id="deviceIdCompleter" class="org.onosproject.cli.net.DeviceIdCompleter"/>
<bean id="clusterIdCompleter" class="org.onosproject.cli.net.ClusterIdCompleter"/>
......
......@@ -49,6 +49,13 @@ public interface CoreService {
ApplicationId getAppId(Short id);
/**
* Returns an existing application id from a given id.
* @param name the name portion of the ID to look up
* @return an application id
*/
ApplicationId getAppId(String name);
/**
* Registers a new application by its name, which is expected
* to follow the reverse DNS convention, e.g.
* {@code org.flying.circus.app}
......
......@@ -38,6 +38,11 @@ public class CoreServiceAdapter implements CoreService {
}
@Override
public ApplicationId getAppId(String name) {
return null;
}
@Override
public ApplicationId registerApplication(String identifier) {
return null;
}
......
......@@ -74,6 +74,12 @@ public class CoreManager implements CoreService {
}
@Override
public ApplicationId getAppId(String name) {
return applicationIdStore.getAppId(name);
}
@Override
public ApplicationId registerApplication(String name) {
checkNotNull(name, "Application ID cannot be null");
return applicationIdStore.registerApplication(name);
......
......@@ -34,7 +34,12 @@ public class TestCoreManager implements CoreService {
}
@Override
public ApplicationId getAppId(Short id) {
public ApplicationId getAppId(Short id) {
return null;
}
@Override
public ApplicationId getAppId(String name) {
return null;
}
......
......@@ -600,6 +600,11 @@ public class FlowRuleManagerTest {
}
@Override
public ApplicationId getAppId(String name) {
return null;
}
@Override
public ApplicationId registerApplication(String identifier) {
return null;
}
......