Bug 1458 - Migrate to next MD-SAL dataStore API
[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.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
15 import org.osgi.framework.BundleContext;
16
17 public class DropTestCommandProvider implements CommandProvider {
18
19     private DataBroker dataBroker;
20     private ProviderContext pc;
21     private BundleContext ctx;
22     private DropTestProvider provider;
23     private DropTestRpcProvider rpcProvider;
24     private boolean on = false;
25     private boolean sessionInitiated = false;
26
27
28     public DropTestCommandProvider(BundleContext ctx, DropTestProvider provider, DropTestRpcProvider rpcProvider) {
29         this.ctx = ctx;
30         this.provider = provider;
31         this.rpcProvider = rpcProvider;
32     }
33
34     public void onSessionInitiated(ProviderContext session) {
35         pc = session;
36         dataBroker = session.getSALService(DataBroker.class);
37         ctx.registerService(CommandProvider.class.getName(), this, null);
38         this.sessionInitiated = true;
39
40     }
41
42     public void _dropAllPackets(CommandInterpreter ci) {
43         if (sessionInitiated) {
44             String onoff = ci.nextArgument();
45             if (onoff.equals("on")) {
46                 if (on == false) {
47                     provider.start();
48                     ci.println("DropAllFlows transitions to on");
49                 } else {
50                     ci.println("DropAllFlows is already on");
51                 }
52                 on = true;
53             } else if (onoff.equals("off")) {
54                 if (on == true) {
55                     provider.close();
56                     ci.println("DropAllFlows transitions to off");
57                 } else {
58                     ci.println("DropAllFlows is already off");
59                 }
60                 on = false;
61             }
62         } else {
63             ci.println("Session not initiated, try again in a few seconds");
64         }
65     }
66
67     public void _dropAllPacketsRpc(CommandInterpreter ci) {
68         if (sessionInitiated) {
69             String onoff = ci.nextArgument();
70             if (onoff.equals("on")) {
71                 if (on == false) {
72                     rpcProvider.start();
73                     ci.println("DropAllFlows transitions to on");
74                 } else {
75                     ci.println("DropAllFlows is already on");
76                 }
77                 on = true;
78             } else if (onoff.equals("off")) {
79                 if (on == true) {
80                     rpcProvider.close();
81                     ci.println("DropAllFlows transitions to off");
82                 } else {
83                     ci.println("DropAllFlows is already off");
84                 }
85                 on = false;
86             }
87         } else {
88             ci.println("Session not initiated, try again in a few seconds");
89         }
90     }
91
92     public void _showDropStats(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(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 }