View Javadoc

1   /**
2    * This file is part of GoldenGate Project (named also GoldenGate or GG).
3    * 
4    * Copyright 2009, Frederic Bregier, and individual contributors by the @author
5    * tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    * 
8    * All GoldenGate Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   * 
13   * GoldenGate is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Public License along with
18   * GoldenGate . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package goldengate.snmp.test;
21  
22  import goldengate.common.logging.GgSlf4JLoggerFactory;
23  import goldengate.snmp.GgMOFactory;
24  import goldengate.snmp.GgSnmpAgent;
25  import goldengate.snmp.utils.GgMOScalar;
26  
27  import java.io.File;
28  import java.io.IOException;
29  
30  import org.jboss.netty.logging.InternalLoggerFactory;
31  import org.snmp4j.event.ResponseEvent;
32  import org.snmp4j.event.ResponseListener;
33  import org.snmp4j.mp.SnmpConstants;
34  
35  import ch.qos.logback.classic.Level;
36  
37  /**
38   * Test class for Agent and simple Client
39   * 
40   * @author Frederic Bregier
41   * 
42   */
43  public class GgTestSnmpClientAgent {
44      static GgSnmpAgent agent;
45  
46      static GgSimpleSnmpClient client;
47  
48      static GgImplPrivateMib test;
49  
50      public static void main(String[] args) throws Exception {
51          InternalLoggerFactory.setDefaultFactory(new GgSlf4JLoggerFactory(
52                  Level.ALL));
53          setUp(args[0]);
54          System.out.println("Test SysDescr");
55          verifySysDescr();
56          System.out.println("Test Table");
57          verifyTableContents();
58          Thread.sleep(10000);
59          sendNotification();
60          Thread.sleep(30000);
61          System.out.println("Stopping");
62          tearDown();
63      }
64  
65      public static void setUp(String file) throws Exception {
66          // Setup the client to use our newly started agent
67          client = new GgSimpleSnmpClient("udp:127.0.0.1/2001", 1162);
68          // Create a monitor
69          GgPrivateMonitor monitor = new GgPrivateMonitor();
70          // Create a Mib
71          test = new GgImplPrivateMib("GoldenGate Test SNMP", 6666, 66666, 66,
72                  "F. Bregier", "GoldenGate Test SNMP V1.0", "Paris, France", 72);
73          // Set the default VariableFactory
74          GgMOFactory.factory = new GgTestVariableFactory();
75          // Create the agent associated with the monitor and Mib
76          agent = new GgSnmpAgent(new File(file), monitor, test);
77          agent.start();
78      }
79  
80      public static void tearDown() throws Exception {
81          agent.stop();
82          client.stop();
83      }
84  
85      private static boolean assertEquals(String a, String b) {
86          if (!a.equals(b)) {
87              System.err.println("Not Equals! " + a + " and " + b);
88              return false;
89          }
90          System.out.println("Equal: " + a);
91          return true;
92      }
93  
94      /**
95       * Simply verifies that we get the same sysDescr as we have registered in
96       * our agent
97       */
98      public static void verifySysDescr() throws IOException {
99          assertEquals(test.textualSysDecr,
100                 client.getAsString(SnmpConstants.sysDescr));
101     }
102 
103     /**
104      * Verify that the table contents is ok.
105      */
106     public static void verifyTableContents() {
107         for (GgMOScalar scalar: test.rowInfo.row) {
108             try {
109                 System.out.println("Read " + scalar.getID() + ":" +
110                         client.getAsString(scalar.getID()));
111             } catch (IOException e) {
112                 System.err.println(scalar.getID() + ":" + e.getMessage());
113                 continue;
114             }
115         }
116     }
117 
118     public static void sendNotification() {
119         test.notifyInfo("Une alerte", "un autre texte d'alerte", 1971);
120         test.notifyError("Une seconde alerte", "un second texte d'alerte", 42);
121     }
122 
123     static class StringResponseListener implements ResponseListener {
124 
125         private String value = null;
126 
127         @Override
128         public void onResponse(ResponseEvent event) {
129             System.out.println(event.getResponse());
130             if (event.getResponse() != null) {
131                 value = GgSimpleSnmpClient.extractSingleString(event);
132             }
133         }
134 
135         public String getValue() {
136             System.out.println(value);
137             return value;
138         }
139 
140     }
141 
142 }