3fe38ddaa5115c55f9300b3b2c2741ce17a5a79a
[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 final BundleContext ctx;
29     private final DropTestDsProvider provider;
30     private final DropTestRpcProvider rpcProvider;
31     private boolean sessionInitiated = false;
32
33     public DropTestCommandProvider(final BundleContext ctx, final DropTestDsProvider provider,
34             final DropTestRpcProvider rpcProvider) {
35         this.ctx = Preconditions.checkNotNull(ctx, "BundleContext can not be null!");
36         this.provider = Preconditions.checkNotNull(provider, "DropTestProvider can't be null!");
37         this.rpcProvider = Preconditions.checkNotNull(rpcProvider, "DropTestRpcProvider can't be null!");
38     }
39
40     public void onSessionInitiated(final ProviderContext session) {
41         Preconditions.checkNotNull(session, "ProviderContext can not be null!");
42         ctx.registerService(CommandProvider.class.getName(), this, null);
43         this.sessionInitiated = true;
44     }
45
46     public void dropAllPackets(final CommandInterpreter ci) {
47         if (sessionInitiated) {
48             String onoff = ci.nextArgument();
49             if (onoff.equalsIgnoreCase("on")) {
50                 if (! provider.isActive()) {
51                     provider.start();
52                     ci.println("DropAllFlows transitions to on");
53                 } else {
54                     ci.println("DropAllFlows is already on");
55                 }
56             } else if (onoff.equalsIgnoreCase("off")) {
57                 if (provider.isActive()) {
58                     provider.close();
59                     ci.println("DropAllFlows transitions to off");
60                 } else {
61                     ci.println("DropAllFlows is already off");
62                 }
63             }
64         } else {
65             ci.println("Session not initiated, try again in a few seconds");
66         }
67     }
68
69     public void dropAllPacketsRpc(final CommandInterpreter ci) {
70         if (sessionInitiated) {
71             String onoff = ci.nextArgument();
72             if (onoff.equalsIgnoreCase("on")) {
73                 if (! rpcProvider.isActive()) {
74                     rpcProvider.start();
75                     ci.println("DropAllFlows transitions to on");
76                 } else {
77                     ci.println("DropAllFlows is already on");
78                 }
79             } else if (onoff.equalsIgnoreCase("off")) {
80                 if (rpcProvider.isActive()) {
81                     rpcProvider.close();
82                     ci.println("DropAllFlows transitions to off");
83                 } else {
84                     ci.println("DropAllFlows is already off");
85                 }
86             }
87         } else {
88             ci.println("Session not initiated, try again in a few seconds");
89         }
90     }
91
92     public void showDropStats(final CommandInterpreter ci) {
93         if (sessionInitiated) {
94             ci.println("RPC Test Statistics: " + this.rpcProvider.getStats().toString());
95             ci.println("FRM Test Statistics: " + this.provider.getStats().toString());
96         } else {
97             ci.println("Session not initiated, try again in a few seconds");
98         }
99     }
100
101     public void clearDropStats(final CommandInterpreter ci) {
102         if (sessionInitiated) {
103             ci.print("Clearing drop statistics... ");
104             this.rpcProvider.clearStats();
105             this.provider.clearStats();
106             ci.println("Done.");
107
108         } else {
109             ci.println("Session not initiated, try again in a few seconds");
110         }
111     }
112
113     @Override
114     public String getHelp() {
115         StringBuffer help = new StringBuffer();
116         help.append("---dropAllPackets---\n");
117         help.append("\t dropAllPackets on     - Start dropping all packets\n");
118         help.append("\t dropAllPackets off    - Stop dropping all packets\n");
119         help.append("\t dropAllPacketsRpc on  - Start dropping all packets but bypassing dataStore\n");
120         help.append("\t                       - add flow goes directly to RPC provided OFPlugin\n");
121         help.append("\t dropAllPacketsRpc off - Stop dropping all packets but bypassing dataStore\n");
122         return help.toString();
123     }
124 }