2 * Copyright (c) 2013 Ericsson , Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.droptest;
11 import com.google.common.base.Preconditions;
12 import org.eclipse.osgi.framework.console.CommandInterpreter;
13 import org.eclipse.osgi.framework.console.CommandProvider;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
15 import org.opendaylight.openflowplugin.testcommon.DropTestDsProvider;
16 import org.opendaylight.openflowplugin.testcommon.DropTestRpcProvider;
17 import org.osgi.framework.BundleContext;
21 * use the same methods defined via karaf commands (defined in drop-test-karaf artifact)
22 * Can be deleted after patch https://git.opendaylight.org/gerrit/#/c/15660/ for bug 2750 will be merged.
26 public class DropTestCommandProvider implements CommandProvider {
28 private static final String SESSION_NOT_INIT = "Session not initiated, try again in a few seconds";
29 private static final String DROP_ON = "DropAllFlows transitions to on";
30 private static final String DROP_ALREADY_ON = "DropAllFlows is already on";
31 private static final String DROP_OFF = "DropAllFlows transitions to off";
32 private static final String DROP_ALREADY_OFF = "DropAllFlows is already off";
33 private final BundleContext ctx;
34 private final DropTestDsProvider provider;
35 private final DropTestRpcProvider rpcProvider;
36 private boolean sessionInitiated = false;
38 public DropTestCommandProvider(final BundleContext ctx, final DropTestDsProvider provider,
39 final DropTestRpcProvider rpcProvider) {
40 this.ctx = Preconditions.checkNotNull(ctx, "BundleContext can not be null!");
41 this.provider = Preconditions.checkNotNull(provider, "DropTestProvider can't be null!");
42 this.rpcProvider = Preconditions.checkNotNull(rpcProvider, "DropTestRpcProvider can't be null!");
45 public void onSessionInitiated(final ProviderContext session) {
46 Preconditions.checkNotNull(session, "ProviderContext can not be null!");
47 ctx.registerService(CommandProvider.class.getName(), this, null);
48 this.sessionInitiated = true;
51 public void dropAllPackets(final CommandInterpreter ci) {
52 if (sessionInitiated) {
53 String onoff = ci.nextArgument();
54 if (onoff.equalsIgnoreCase("on")) {
55 if (! provider.isActive()) {
59 ci.println(DROP_ALREADY_ON);
61 } else if (onoff.equalsIgnoreCase("off")) {
62 if (provider.isActive()) {
66 ci.println(DROP_ALREADY_OFF);
70 ci.println(SESSION_NOT_INIT);
74 public void dropAllPacketsRpc(final CommandInterpreter ci) {
75 if (sessionInitiated) {
76 String onoff = ci.nextArgument();
77 if (onoff.equalsIgnoreCase("on")) {
78 if (! rpcProvider.isActive()) {
82 ci.println(DROP_ALREADY_ON);
84 } else if (onoff.equalsIgnoreCase("off")) {
85 if (rpcProvider.isActive()) {
89 ci.println(DROP_ALREADY_OFF);
93 ci.println(SESSION_NOT_INIT);
97 public void showDropStats(final CommandInterpreter ci) {
98 if (sessionInitiated) {
99 ci.println("RPC Test Statistics: " + this.rpcProvider.getStats().toString());
100 ci.println("FRM Test Statistics: " + this.provider.getStats().toString());
102 ci.println(SESSION_NOT_INIT);
106 public void clearDropStats(final CommandInterpreter ci) {
107 if (sessionInitiated) {
108 ci.print("Clearing drop statistics... ");
109 this.rpcProvider.clearStats();
110 this.provider.clearStats();
114 ci.println(SESSION_NOT_INIT);
119 public String getHelp() {
120 StringBuilder help = new StringBuilder();
121 help.append("---dropAllPackets---\n");
122 help.append("\t dropAllPackets on - Start dropping all packets\n");
123 help.append("\t dropAllPackets off - Stop dropping all packets\n");
124 help.append("\t dropAllPacketsRpc on - Start dropping all packets but bypassing dataStore\n");
125 help.append("\t - add flow goes directly to RPC provided OFPlugin\n");
126 help.append("\t dropAllPacketsRpc off - Stop dropping all packets but bypassing dataStore\n");
127 return help.toString();