Thomas Vachuska
Committed by Gerrit Code Review

ONOS- 2946 Adding ability to view existing packet intercept requests and packet processors.

Change-Id: Id0d82fb4a19506ec607a71856dbf0c33c8e51baf
/*
* 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.net;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.net.packet.PacketProcessor;
import org.onosproject.net.packet.PacketService;
import static org.onosproject.net.packet.PacketProcessor.ADVISOR_MAX;
import static org.onosproject.net.packet.PacketProcessor.DIRECTOR_MAX;
/**
* Lists packet processors.
*/
@Command(scope = "onos", name = "packet-processors",
description = "Lists packet processors")
public class PacketProcessorsListCommand extends AbstractShellCommand {
private static final String FMT = "priority=%s, class=%s";
@Override
protected void execute() {
PacketService service = get(PacketService.class);
if (outputJson()) {
// TODO: implement this
print("Not implemented.");
} else {
service.getProcessors().forEach(this::print);
}
}
private void print(int priority, PacketProcessor processor) {
print(FMT, priorityFormat(priority), processor.getClass().getName());
}
private String priorityFormat(int priority) {
if (priority > DIRECTOR_MAX) {
return "observer(" + (priority - DIRECTOR_MAX - 1) + ")";
} else if (priority > ADVISOR_MAX) {
return "director(" + (priority - ADVISOR_MAX - 1) + ")";
}
return "advisor(" + (priority - 1) + ")";
}
}
/*
* 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.net;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.net.packet.PacketRequest;
import org.onosproject.net.packet.PacketService;
/**
* Lists packet requests.
*/
@Command(scope = "onos", name = "packet-requests",
description = "Lists packet requests")
public class PacketRequestsListCommand extends AbstractShellCommand {
private static final String FMT = "priority=%s, appId=%s, criteria=%s";
@Override
protected void execute() {
PacketService service = get(PacketService.class);
if (outputJson()) {
// TODO: implement this
print("Not implemented.");
} else {
service.getRequests().forEach(this::print);
}
}
private void print(PacketRequest request) {
print(FMT, request.priority(), request.appId().name(), request.selector().criteria());
}
}
......@@ -351,6 +351,13 @@
</command>
<command>
<action class="org.onosproject.cli.net.PacketRequestsListCommand"/>
</command>
<command>
<action class="org.onosproject.cli.net.PacketProcessorsListCommand"/>
</command>
<command>
<action class="org.onosproject.cli.net.AddTestFlowsCommand"/>
</command>
<command>
......
......@@ -24,7 +24,7 @@ public interface PacketProcessor {
int ADVISOR_MAX = Integer.MAX_VALUE / 3;
int DIRECTOR_MAX = (Integer.MAX_VALUE / 3) * 2;
static final int OBSERVER_MAX = Integer.MAX_VALUE;
int OBSERVER_MAX = Integer.MAX_VALUE;
/**
* Returns a priority in the ADVISOR range, where processors can take early action and
......@@ -38,7 +38,7 @@ public interface PacketProcessor {
static int advisor(int priority) {
int overallPriority = priority + 1;
checkArgument(overallPriority > 0 && overallPriority <= ADVISOR_MAX,
"Priority not within ADVISOR range");
"Priority not within ADVISOR range");
return overallPriority;
}
......@@ -53,7 +53,7 @@ public interface PacketProcessor {
static int director(int priority) {
int overallPriority = ADVISOR_MAX + priority + 1;
checkArgument(overallPriority > ADVISOR_MAX && overallPriority <= DIRECTOR_MAX,
"Priority not within DIRECTOR range");
"Priority not within DIRECTOR range");
return overallPriority;
}
......@@ -68,8 +68,8 @@ public interface PacketProcessor {
*/
static int observer(int priority) {
int overallPriority = DIRECTOR_MAX + priority + 1;
checkArgument(overallPriority > DIRECTOR_MAX && overallPriority <= OBSERVER_MAX,
"Priority not within OBSERVER range");
checkArgument(overallPriority > DIRECTOR_MAX,
"Priority not within OBSERVER range");
return overallPriority;
}
......
......@@ -15,9 +15,13 @@
*/
package org.onosproject.net.packet;
import com.google.common.annotations.Beta;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.flow.TrafficSelector;
import java.util.List;
import java.util.Map;
/**
* Service for intercepting data plane packets and for emitting synthetic
* outbound packets.
......@@ -48,6 +52,15 @@ public interface PacketService {
void removeProcessor(PacketProcessor processor);
/**
* Returns priority bindings of all registered packet processors.
*
* @return list of existing packet processors
*/
@Beta
// TODO: Consider returning list of PacketProcessorEntry with processor, priority and stats
Map<Integer, PacketProcessor> getProcessors();
/**
* Requests that packets matching the given selector are punted from the
* dataplane to the controller.
*
......@@ -70,6 +83,13 @@ public interface PacketService {
ApplicationId appId);
/**
* Returns list of all existing requests ordered by priority.
*
* @return list of existing packet requests
*/
List<PacketRequest> getRequests();
/**
* Emits the specified outbound packet onto the network.
*
* @param packet outbound packet
......
......@@ -17,7 +17,7 @@ package org.onosproject.net.packet;
import org.onosproject.store.Store;
import java.util.Set;
import java.util.List;
/**
* Manages routing of outbound packets.
......@@ -52,8 +52,8 @@ public interface PacketStore extends Store<PacketEvent, PacketStoreDelegate> {
/**
* Obtains all existing requests in the system.
*
* @return a set of packet requests
* @return list of packet requests in order of priority
*/
Set<PacketRequest> existingRequests();
List<PacketRequest> existingRequests();
}
......
......@@ -18,6 +18,9 @@ package org.onosproject.net.packet;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.flow.TrafficSelector;
import java.util.List;
import java.util.Map;
/**
* Test adapter for packet service.
*/
......@@ -31,6 +34,16 @@ public class PacketServiceAdapter implements PacketService {
}
@Override
public Map<Integer, PacketProcessor> getProcessors() {
return null;
}
@Override
public List<PacketRequest> getRequests() {
return null;
}
@Override
public void requestPackets(TrafficSelector selector, PacketPriority priority, ApplicationId appId) {
}
......
......@@ -15,6 +15,7 @@
*/
package org.onosproject.store.trivial;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
......@@ -26,8 +27,7 @@ import org.onosproject.net.packet.PacketStore;
import org.onosproject.net.packet.PacketStoreDelegate;
import org.onosproject.store.AbstractStore;
import java.util.Collections;
import java.util.List;
import java.util.Set;
/**
......@@ -57,8 +57,8 @@ public class SimplePacketStore
}
@Override
public Set<PacketRequest> existingRequests() {
return Collections.unmodifiableSet(requests);
public List<PacketRequest> existingRequests() {
return ImmutableList.copyOf(requests);
}
}
......
......@@ -15,6 +15,7 @@
*/
package org.onosproject.net.packet.impl;
import com.google.common.collect.ImmutableMap;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
......@@ -53,6 +54,7 @@ import org.onosproject.net.provider.AbstractProviderRegistry;
import org.onosproject.net.provider.AbstractProviderService;
import org.slf4j.Logger;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
......@@ -61,8 +63,8 @@ import java.util.concurrent.Executors;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onlab.util.Tools.groupedThreads;
import static org.onosproject.security.AppGuard.checkPermission;
import static org.slf4j.LoggerFactory.getLogger;
import static org.onosproject.security.AppPermission.Type.*;
import static org.slf4j.LoggerFactory.getLogger;
/**
* Provides a basic implementation of the packet SB &amp; NB APIs.
......@@ -138,6 +140,11 @@ public class PacketManager
}
@Override
public Map<Integer, PacketProcessor> getProcessors() {
return ImmutableMap.copyOf(processors);
}
@Override
public void requestPackets(TrafficSelector selector, PacketPriority priority,
ApplicationId appId) {
checkPermission(PACKET_READ);
......@@ -163,6 +170,11 @@ public class PacketManager
}
}
@Override
public List<PacketRequest> getRequests() {
return store.existingRequests();
}
/**
* Pushes a packet request flow rule to all devices.
*
......
......@@ -15,7 +15,7 @@
*/
package org.onosproject.store.packet.impl;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
......@@ -45,6 +45,7 @@ import org.onosproject.store.service.Versioned;
import org.slf4j.Logger;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
......@@ -152,7 +153,7 @@ public class DistributedPacketStore
}
@Override
public Set<PacketRequest> existingRequests() {
public List<PacketRequest> existingRequests() {
return tracker.requests();
}
......@@ -197,10 +198,11 @@ public class DistributedPacketStore
return requests.replace(request.selector(), old.version(), newSet);
}
public Set<PacketRequest> requests() {
ImmutableSet.Builder<PacketRequest> builder = ImmutableSet.builder();
requests.values().forEach(v -> builder.addAll(v.value()));
return builder.build();
public List<PacketRequest> requests() {
List<PacketRequest> list = Lists.newArrayList();
requests.values().forEach(v -> list.addAll(v.value()));
list.sort((o1, o2) -> o1.priority().priorityValue() - o2.priority().priorityValue());
return list;
}
}
......