Removal of obsolete distributions
[openflowplugin.git] / drop-test / src / main / java / org / opendaylight / openflowplugin / droptest / DropTestCommandProvider.java
1 /*
2  * Copyright (c) 2013 Ericsson , Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.droptest;
10
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;
18
19 /**
20  *
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.
23  *
24  */
25 @Deprecated
26 public class DropTestCommandProvider implements CommandProvider {
27     
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;
37
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!");
43     }
44
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;
49     }
50
51     public void dropAllPackets(final CommandInterpreter ci) {
52         if (sessionInitiated) {
53             String onoff = ci.nextArgument();
54             if (onoff.equalsIgnoreCase("on")) {
55                 if (! provider.isActive()) {
56                     provider.start();
57                     ci.println(DROP_ON);
58                 } else {
59                     ci.println(DROP_ALREADY_ON);
60                 }
61             } else if (onoff.equalsIgnoreCase("off")) {
62                 if (provider.isActive()) {
63                     provider.close();
64                     ci.println(DROP_OFF);
65                 } else {
66                     ci.println(DROP_ALREADY_OFF);
67                 }
68             }
69         } else {
70             ci.println(SESSION_NOT_INIT);
71         }
72     }
73
74     public void dropAllPacketsRpc(final CommandInterpreter ci) {
75         if (sessionInitiated) {
76             String onoff = ci.nextArgument();
77             if (onoff.equalsIgnoreCase("on")) {
78                 if (! rpcProvider.isActive()) {
79                     rpcProvider.start();
80                     ci.println(DROP_ON);
81                 } else {
82                     ci.println(DROP_ALREADY_ON);
83                 }
84             } else if (onoff.equalsIgnoreCase("off")) {
85                 if (rpcProvider.isActive()) {
86                     rpcProvider.close();
87                     ci.println(DROP_OFF);
88                 } else {
89                     ci.println(DROP_ALREADY_OFF);
90                 }
91             }
92         } else {
93             ci.println(SESSION_NOT_INIT);
94         }
95     }
96
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());
101         } else {
102             ci.println(SESSION_NOT_INIT);
103         }
104     }
105
106     public void clearDropStats(final CommandInterpreter ci) {
107         if (sessionInitiated) {
108             ci.print("Clearing drop statistics... ");
109             this.rpcProvider.clearStats();
110             this.provider.clearStats();
111             ci.println("Done.");
112
113         } else {
114             ci.println(SESSION_NOT_INIT);
115         }
116     }
117
118     @Override
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();
128     }
129 }