KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > agentx > master > test > TestMasterAgent


1 /*_############################################################################
2   _##
3   _## SNMP4J-AgentX - TestMasterAgent.java
4   _##
5   _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
6   _##
7   _## This program is free software; you can redistribute it and/or modify
8   _## it under the terms of the GNU General Public License version 2 as
9   _## published by the Free Software Foundation.
10   _##
11   _## This program is distributed in the hope that it will be useful,
12   _## but WITHOUT ANY WARRANTY; without even the implied warranty of
13   _## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   _## GNU General Public License for more details.
15   _##
16   _## You should have received a copy of the GNU General Public License
17   _## along with this program; if not, write to the Free Software
18   _## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19   _## MA 02110-1301 USA
20   _##
21   _##########################################################################*/

22
23 package org.snmp4j.agent.agentx.master.test;
24
25 import java.io.File JavaDoc;
26 import org.apache.log4j.BasicConfigurator;
27 import java.io.IOException JavaDoc;
28 import org.snmp4j.agent.io.ImportModes;
29 import org.snmp4j.agent.agentx.master.AgentXMasterAgent;
30 import org.snmp4j.mp.MessageProcessingModel;
31 import org.snmp4j.security.AuthSHA;
32 import org.snmp4j.security.UsmUser;
33 import org.snmp4j.security.SecurityLevel;
34 import org.snmp4j.agent.mo.snmp.SnmpNotificationMIB;
35 import org.snmp4j.smi.OID;
36 import org.snmp4j.agent.mo.snmp.StorageType;
37 import org.snmp4j.agent.mo.snmp.SnmpTargetMIB;
38 import org.snmp4j.smi.UdpAddress;
39 import org.snmp4j.smi.OctetString;
40 import org.snmp4j.security.SecurityModel;
41 import org.snmp4j.security.PrivDES;
42 import org.snmp4j.security.USM;
43 import org.snmp4j.agent.mo.snmp.VacmMIB;
44 import org.snmp4j.agent.mo.snmp.TransportDomains;
45 import org.snmp4j.TransportMapping;
46 import org.snmp4j.transport.DefaultUdpTransportMapping;
47 import org.snmp4j.transport.DefaultTcpTransportMapping;
48 import org.snmp4j.smi.TcpAddress;
49 import org.snmp4j.smi.Variable;
50 import org.snmp4j.agent.mo.MOTableRow;
51 import org.snmp4j.agent.mo.snmp.RowStatus;
52 import org.snmp4j.agent.mo.snmp.SnmpCommunityMIB;
53 import org.snmp4j.smi.Integer32;
54 import org.snmp4j.log.Log4jLogFactory;
55 import org.snmp4j.log.LogFactory;
56 import org.snmp4j.agent.MOGroup;
57
58 public class TestMasterAgent extends AgentXMasterAgent {
59
60   // initialize Log4J logging
61
static {
62     LogFactory.setLogFactory(new Log4jLogFactory());
63   }
64
65   protected String JavaDoc address;
66   protected TransportMapping masterTransport;
67
68   public TestMasterAgent(File JavaDoc bootCounterFile, File JavaDoc configFile) throws
69       IOException JavaDoc {
70     super(bootCounterFile, configFile);
71   }
72
73   protected OctetString getContext(MOGroup group) {
74     if (snmpv2MIB.equals(group)) {
75       return null;
76     }
77     return super.getContext(group);
78   }
79
80   protected void addNotificationTargets(SnmpTargetMIB targetMIB,
81                                         SnmpNotificationMIB notificationMIB) {
82     targetMIB.addDefaultTDomains();
83
84     targetMIB.addTargetAddress(new OctetString("notification"),
85                                TransportDomains.transportDomainUdpIpv4,
86                                new OctetString(new UdpAddress("127.0.0.1/162").getValue()),
87                                200, 1,
88                                new OctetString("notify"),
89                                new OctetString("v2c"),
90                                StorageType.permanent);
91     targetMIB.addTargetParams(new OctetString("v2c"),
92                               MessageProcessingModel.MPv2c,
93                               SecurityModel.SECURITY_MODEL_SNMPv2c,
94                               new OctetString("public"),
95                               SecurityLevel.NOAUTH_NOPRIV,
96                               StorageType.permanent);
97     notificationMIB.addNotifyEntry(new OctetString("default"),
98                                    new OctetString("notify"),
99                                    SnmpNotificationMIB.SnmpNotifyTypeEnum.trap,
100                                    StorageType.permanent);
101   }
102
103   protected void addViews(VacmMIB vacm) {
104     vacm.addGroup(SecurityModel.SECURITY_MODEL_SNMPv1,
105                   new OctetString("public"),
106                   new OctetString("v1v2group"),
107                   StorageType.nonVolatile);
108     vacm.addGroup(SecurityModel.SECURITY_MODEL_SNMPv2c,
109                   new OctetString("public"),
110                   new OctetString("v1v2group"),
111                   StorageType.nonVolatile);
112     vacm.addGroup(SecurityModel.SECURITY_MODEL_USM,
113                   new OctetString("SHADES"),
114                   new OctetString("v3group"),
115                   StorageType.nonVolatile);
116     vacm.addGroup(SecurityModel.SECURITY_MODEL_USM,
117                   new OctetString("TEST"),
118                   new OctetString("v3test"),
119                   StorageType.nonVolatile);
120     vacm.addGroup(SecurityModel.SECURITY_MODEL_USM,
121                   new OctetString("SHA"),
122                   new OctetString("v3restricted"),
123                   StorageType.nonVolatile);
124
125     vacm.addAccess(new OctetString("v1v2group"), new OctetString(),
126                    SecurityModel.SECURITY_MODEL_ANY,
127                    SecurityLevel.NOAUTH_NOPRIV, VacmMIB.vacmExactMatch,
128                    new OctetString("fullReadView"),
129                    new OctetString("fullWriteView"),
130                    new OctetString("fullNotifyView"),
131                    StorageType.nonVolatile);
132     vacm.addAccess(new OctetString("v3group"), new OctetString(),
133                    SecurityModel.SECURITY_MODEL_USM,
134                    SecurityLevel.AUTH_PRIV, VacmMIB.vacmExactMatch,
135                    new OctetString("fullReadView"),
136                    new OctetString("fullWriteView"),
137                    new OctetString("fullNotifyView"),
138                    StorageType.nonVolatile);
139     vacm.addAccess(new OctetString("v3restricted"), new OctetString(),
140                    SecurityModel.SECURITY_MODEL_USM,
141                    SecurityLevel.AUTH_NOPRIV, VacmMIB.vacmExactMatch,
142                    new OctetString("restrictedReadView"),
143                    new OctetString("restrictedWriteView"),
144                    new OctetString("restrictedNotifyView"),
145                    StorageType.nonVolatile);
146     vacm.addAccess(new OctetString("v3test"), new OctetString(),
147                    SecurityModel.SECURITY_MODEL_USM,
148                    SecurityLevel.AUTH_PRIV, VacmMIB.vacmExactMatch,
149                    new OctetString("testReadView"),
150                    new OctetString("testWriteView"),
151                    new OctetString("testNotifyView"),
152                    StorageType.nonVolatile);
153
154     vacm.addViewTreeFamily(new OctetString("fullReadView"), new OID("1.3"),
155                            new OctetString(), VacmMIB.vacmViewIncluded,
156                            StorageType.nonVolatile);
157     vacm.addViewTreeFamily(new OctetString("fullWriteView"), new OID("1.3"),
158                            new OctetString(), VacmMIB.vacmViewIncluded,
159                            StorageType.nonVolatile);
160     vacm.addViewTreeFamily(new OctetString("fullNotifyView"), new OID("1.3"),
161                            new OctetString(), VacmMIB.vacmViewIncluded,
162                            StorageType.nonVolatile);
163
164     vacm.addViewTreeFamily(new OctetString("restrictedReadView"),
165                            new OID("1.3.6.1.2"),
166                            new OctetString(), VacmMIB.vacmViewIncluded,
167                            StorageType.nonVolatile);
168     vacm.addViewTreeFamily(new OctetString("restrictedWriteView"),
169                            new OID("1.3.6.1.2.1"),
170                            new OctetString(),
171                            VacmMIB.vacmViewIncluded,
172                            StorageType.nonVolatile);
173     vacm.addViewTreeFamily(new OctetString("restrictedNotifyView"),
174                            new OID("1.3.6.1.2"),
175                            new OctetString(), VacmMIB.vacmViewIncluded,
176                            StorageType.nonVolatile);
177
178     vacm.addViewTreeFamily(new OctetString("testReadView"),
179                            new OID("1.3.6.1.2"),
180                            new OctetString(), VacmMIB.vacmViewIncluded,
181                            StorageType.nonVolatile);
182     vacm.addViewTreeFamily(new OctetString("testReadView"),
183                            new OID("1.3.6.1.2.1.1"),
184                            new OctetString(), VacmMIB.vacmViewExcluded,
185                            StorageType.nonVolatile);
186     vacm.addViewTreeFamily(new OctetString("testWriteView"),
187                            new OID("1.3.6.1.2.1"),
188                            new OctetString(),
189                            VacmMIB.vacmViewIncluded,
190                            StorageType.nonVolatile);
191     vacm.addViewTreeFamily(new OctetString("testNotifyView"),
192                            new OID("1.3.6.1.2"),
193                            new OctetString(), VacmMIB.vacmViewIncluded,
194                            StorageType.nonVolatile);
195
196   }
197
198   protected void addCommunities(SnmpCommunityMIB communityMIB) {
199     Variable[] com2sec = new Variable[] {
200         new OctetString("public"), // community name
201
new OctetString("public"), // security name
202
getAgent().getContextEngineID(), // local engine ID
203
new OctetString(), // default context name
204
new OctetString(), // transport tag
205
new Integer32(StorageType.nonVolatile), // storage type
206
new Integer32(RowStatus.active) // row status
207
};
208     MOTableRow row =
209         communityMIB.getSnmpCommunityEntry().createRow(
210           new OctetString("public2public").toSubIndex(true), com2sec);
211     communityMIB.getSnmpCommunityEntry().addRow(row);
212   }
213
214   protected void addUsmUser(USM usm) {
215     UsmUser user = new UsmUser(new OctetString("SHADES"),
216                                AuthSHA.ID,
217                                new OctetString("SHADESAuthPassword"),
218                                PrivDES.ID,
219                                new OctetString("SHADESPrivPassword"));
220     usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user);
221     user = new UsmUser(new OctetString("TEST"),
222                                AuthSHA.ID,
223                                new OctetString("maplesyrup"),
224                                PrivDES.ID,
225                                new OctetString("maplesyrup"));
226     usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user);
227     user = new UsmUser(new OctetString("SHA"),
228                                AuthSHA.ID,
229                                new OctetString("SHAAuthPassword"),
230                                null,
231                                null);
232     usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user);
233   }
234
235   protected void initTransportMappings() throws IOException JavaDoc {
236     transportMappings = new TransportMapping[1];
237     transportMappings[0] =
238         new DefaultUdpTransportMapping(new UdpAddress(address));
239   }
240
241   public static void main(String JavaDoc[] args) {
242     String JavaDoc address;
243     String JavaDoc masterAddress = "0.0.0.0/705";
244     if (args.length > 0) {
245       address = args[0];
246       if (args.length > 1) {
247         masterAddress = args[1];
248       }
249     }
250     else {
251       address = "0.0.0.0/161";
252     }
253     BasicConfigurator.configure();
254     try {
255       TestMasterAgent testAgent1 =
256           new TestMasterAgent(new File JavaDoc("SNMP4JTestMasterAgentBC.cfg"),
257                               new File JavaDoc("SNMP4JTestMasterAgentConfig.cfg"));
258       testAgent1.setDefaultContext(new OctetString());
259       testAgent1.address = address;
260       testAgent1.masterTransport = new DefaultTcpTransportMapping(
261           new TcpAddress(masterAddress));
262       testAgent1.addTransportMapping(testAgent1.masterTransport);
263       testAgent1.init();
264       testAgent1.loadConfig(ImportModes.UPDATE_CREATE);
265       testAgent1.addShutdownHook();
266       testAgent1.getCommandProcessor().setAcceptNewContexts(true);
267       testAgent1.getCommandProcessor().setMaxParseErrors(0);
268       testAgent1.masterTransport.listen();
269       testAgent1.finishInit();
270       testAgent1.run();
271       while (true) {
272         try {
273           Thread.sleep(1000);
274         }
275         catch (InterruptedException JavaDoc ex1) {
276           break;
277         }
278       }
279     }
280     catch (IOException JavaDoc ex) {
281       ex.printStackTrace();
282     }
283
284   }
285
286   public void init() throws IOException JavaDoc {
287     super.init();
288   }
289
290   protected void registerManagedObjects() {
291     super.registerManagedObjects();
292   }
293
294 }
295
Popular Tags