use of org.onosproject.net.device.DeviceService in project onos by opennetworkinglab.
the class AddTestFlowsCommand method doExecute.
@Override
protected void doExecute() {
FlowRuleService flowService = get(FlowRuleService.class);
DeviceService deviceService = get(DeviceService.class);
CoreService coreService = get(CoreService.class);
ApplicationId appId = coreService.registerApplication("onos.test.flow.installer");
int flowsPerDevice = Integer.parseInt(flows);
int num = Integer.parseInt(numOfRuns);
ArrayList<Long> results = Lists.newArrayList();
Iterable<Device> devices = deviceService.getDevices();
TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(PortNumber.portNumber(RandomUtils.nextInt(MAX_OUT_PORT))).build();
TrafficSelector.Builder sbuilder;
FlowRuleOperations.Builder rules = FlowRuleOperations.builder();
FlowRuleOperations.Builder remove = FlowRuleOperations.builder();
for (Device d : devices) {
for (long i = 0; i < flowsPerDevice; i++) {
sbuilder = DefaultTrafficSelector.builder();
sbuilder.matchEthSrc(MacAddress.valueOf(RandomUtils.nextInt() * i)).matchEthDst(MacAddress.valueOf((Integer.MAX_VALUE - i) * RandomUtils.nextInt()));
int randomPriority = RandomUtils.nextInt(FlowRule.MAX_PRIORITY - FlowRule.MIN_PRIORITY + 1) + FlowRule.MIN_PRIORITY;
FlowRule addRule = DefaultFlowRule.builder().forDevice(d.id()).withSelector(sbuilder.build()).withTreatment(treatment).withPriority(randomPriority).fromApp(appId).makeTemporary(10).build();
FlowRule removeRule = DefaultFlowRule.builder().forDevice(d.id()).withSelector(sbuilder.build()).withTreatment(treatment).withPriority(randomPriority).fromApp(appId).makeTemporary(10).build();
rules.add(addRule);
remove.remove(removeRule);
}
}
// close stages
rules.newStage();
remove.newStage();
for (int i = 0; i < num; i++) {
printProgress("Run %d:", i);
latch = new CountDownLatch(2);
final CountDownLatch addSuccess = new CountDownLatch(1);
printProgress("..batch add request");
Stopwatch add = Stopwatch.createStarted();
flowService.apply(rules.build(new FlowRuleOperationsContext() {
private final Stopwatch timer = Stopwatch.createStarted();
@Override
public void onSuccess(FlowRuleOperations ops) {
timer.stop();
printProgress("..add success");
results.add(timer.elapsed(TimeUnit.MILLISECONDS));
if (results.size() == num) {
if (outputJson()) {
print("%s", json(new ObjectMapper(), true, results));
} else {
printTime(true, results);
}
}
latch.countDown();
addSuccess.countDown();
}
}));
try {
addSuccess.await();
// wait until all flows reaches ADDED state
while (!Streams.stream(flowService.getFlowEntriesById(appId)).allMatch(fr -> fr.state() == FlowEntryState.ADDED)) {
Thread.sleep(100);
}
add.stop();
printProgress("..completed %d ± 100 ms", add.elapsed(TimeUnit.MILLISECONDS));
} catch (InterruptedException e1) {
printProgress("Interrupted");
Thread.currentThread().interrupt();
}
printProgress("..cleaning up");
flowService.apply(remove.build(new FlowRuleOperationsContext() {
@Override
public void onSuccess(FlowRuleOperations ops) {
latch.countDown();
}
}));
try {
latch.await();
while (!Iterables.isEmpty(flowService.getFlowEntriesById(appId))) {
Thread.sleep(500);
}
} catch (InterruptedException e) {
printProgress("Interrupted.");
Thread.currentThread().interrupt();
}
}
if (outputJson()) {
print("%s", json(new ObjectMapper(), true, results));
} else {
printTime(true, results);
}
}
use of org.onosproject.net.device.DeviceService in project onos by opennetworkinglab.
the class AllocationsCommand method doExecute.
@Override
protected void doExecute() {
deviceService = get(DeviceService.class);
resourceService = get(ResourceService.class);
if (typeStrings != null) {
typesToPrint = new HashSet<>(Arrays.asList(typeStrings));
} else {
typesToPrint = Collections.emptySet();
}
if (intentStrings != null) {
intentsToPrint = new HashSet<>(Arrays.asList(intentStrings));
} else {
intentsToPrint = Collections.emptySet();
}
if (deviceIdStr != null && portNumberStr != null) {
DeviceId deviceId = deviceId(deviceIdStr);
PortNumber portNumber = PortNumber.fromString(portNumberStr);
printAllocation(deviceId, portNumber, 0);
} else if (deviceIdStr != null) {
DeviceId deviceId = deviceId(deviceIdStr);
printAllocation(deviceId, 0);
} else {
printAllocation();
}
}
use of org.onosproject.net.device.DeviceService in project onos by opennetworkinglab.
the class DeviceIdCompleter method complete.
@Override
public int complete(Session session, CommandLine commandLine, List<String> candidates) {
// Delegate string completer
StringsCompleter delegate = new StringsCompleter();
// Fetch our service and feed it's offerings to the string completer
DeviceService service = AbstractShellCommand.get(DeviceService.class);
Iterator<Device> it = service.getDevices().iterator();
SortedSet<String> strings = delegate.getStrings();
while (it.hasNext()) {
strings.add(it.next().id().toString());
}
// Now let the completer do the work for figuring out what to offer.
return delegate.complete(session, commandLine, candidates);
}
use of org.onosproject.net.device.DeviceService in project onos by opennetworkinglab.
the class DevicePortStateCommand method doExecute.
@Override
protected void doExecute() {
DeviceService deviceService = get(DeviceService.class);
DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
Device dev = deviceService.getDevice(DeviceId.deviceId(uri));
if (dev == null) {
print(" %s", "Device does not exist");
return;
}
PortNumber pnum = PortNumber.fromString(portNumber);
Port p = deviceService.getPort(dev.id(), pnum);
if (p == null) {
print(" %s", "Port does not exist");
return;
}
if ("enable".equals(portState)) {
deviceAdminService.changePortState(dev.id(), p.number(), true);
} else if ("disable".equals(portState)) {
deviceAdminService.changePortState(dev.id(), p.number(), false);
} else {
print(" %s", "State must be enable or disable");
}
}
use of org.onosproject.net.device.DeviceService in project onos by opennetworkinglab.
the class AnnotatePortCommand method doExecute.
@Override
protected void doExecute() {
DeviceService deviceService = get(DeviceService.class);
NetworkConfigService netcfgService = get(NetworkConfigService.class);
ConnectPoint connectPoint = ConnectPoint.deviceConnectPoint(port);
if (deviceService.getPort(connectPoint) == null) {
print("Port %s does not exist.", port);
return;
}
if (removeCfg && key == null) {
// remove whole port annotation config
netcfgService.removeConfig(connectPoint, PortAnnotationConfig.class);
print("Annotation Config about %s removed", connectPoint);
return;
}
if (key == null) {
print("[ERROR] Annotation key not specified.");
return;
}
PortAnnotationConfig cfg = netcfgService.addConfig(connectPoint, PortAnnotationConfig.class);
if (removeCfg) {
// remove config about entry
cfg.annotation(key);
} else {
// add remove request config
cfg.annotation(key, value);
}
cfg.apply();
}
Aggregations