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