KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > agentx > master > AgentXMasterAgent


1 /*_############################################################################
2   _##
3   _## SNMP4J-AgentX - AgentXMasterAgent.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;
24
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 import org.snmp4j.TransportMapping;
29 import org.snmp4j.agent.BaseAgent;
30 import org.snmp4j.agent.DuplicateRegistrationException;
31 import org.snmp4j.agent.agentx.AgentX;
32 import org.snmp4j.agent.agentx.AgentXMessageDispatcherImpl;
33 import org.snmp4j.agent.agentx.AgentXProtocol;
34 import org.snmp4j.agent.mo.snmp.*;
35 import org.snmp4j.log.LogAdapter;
36 import org.snmp4j.log.LogFactory;
37 import org.snmp4j.mp.MPv3;
38 import org.snmp4j.security.USM;
39 import org.snmp4j.smi.IpAddress;
40 import org.snmp4j.smi.OctetString;
41 import org.snmp4j.transport.ConnectionOrientedTransportMapping;
42 import org.snmp4j.transport.TransportStateEvent;
43 import org.snmp4j.transport.TransportStateListener;
44 import java.net.InetAddress JavaDoc;
45 import java.util.Arrays JavaDoc;
46
47 /**
48  * The <code>AgentXMasterAgent</code> is the base agent class for
49  * AgentX master agents. It extends the {@link BaseAgent} class provided
50  * by SNMP4J-Agent.
51  * <p>
52  * To implement a master agent, simply extend this class instead of
53  * {@link BaseAgent} as you would do for a non-AgentX agent.
54  *
55  * @author Frank Fock
56  * @version 1.0
57  */

58 public class AgentXMasterAgent extends BaseAgent implements
59     TransportStateListener {
60
61   private static final LogAdapter LOGGER =
62       LogFactory.getLogger(AgentXMasterAgent.class);
63
64   private static short maxGetBulkRepetitions = Short.MAX_VALUE;
65   private AgentX agentX;
66   private AgentXCommandProcessor commandProcessor;
67   private AgentXMib agentXMIB;
68   private OctetString localEngineID =
69       new OctetString(MPv3.createLocalEngineID());
70   private boolean localhostSubagentsOnly;
71
72   public AgentXMasterAgent(File JavaDoc bootCounterFile,
73                            File JavaDoc configFile) {
74     super();
75     setBootCounterFile(bootCounterFile);
76     setConfigFile(configFile);
77     agentX = new AgentX(new AgentXMessageDispatcherImpl());
78   }
79
80   public void init() throws IOException JavaDoc {
81     AgentXQueue queue = new AgentXQueue();
82     commandProcessor =
83         new AgentXCommandProcessor(localEngineID, queue, agentX, server);
84     setAgent(commandProcessor);
85     agentX.addCommandResponder(commandProcessor);
86     this.agentXMIB = new AgentXMib(commandProcessor);
87     super.init();
88     commandProcessor.setNotificationOriginator(getNotificationOriginator());
89     commandProcessor.addAgentXMasterListener(this.agentXMIB);
90   }
91
92   public void addTransportMapping(TransportMapping transport) {
93     agentX.getMessageDispatcher().addTransportMapping(transport);
94     if (transport instanceof ConnectionOrientedTransportMapping) {
95       ConnectionOrientedTransportMapping cotm =
96           (ConnectionOrientedTransportMapping)transport;
97       cotm.addTransportStateListener(this);
98       cotm.setConnectionTimeout(0);
99       cotm.setMessageLengthDecoder(new AgentXProtocol());
100     }
101   }
102
103   public void removeTransportMapping(TransportMapping transport) {
104     agentX.getMessageDispatcher().removeTransportMapping(transport);
105     if (transport instanceof ConnectionOrientedTransportMapping) {
106       ConnectionOrientedTransportMapping cotm =
107           (ConnectionOrientedTransportMapping)transport;
108       cotm.removeTransportStateListener(this);
109     }
110   }
111
112   /**
113    * Gets the upper limit for AgentX Get Bulk repetitions field send on behalf
114    * of all master agents of this JVM.
115    * @return
116    * the upper limit for the maximum repetitions field for AgentX Get Bulk
117    * requests.
118    * @see #setMaxGetBulkRepetitions
119    */

120   public static short getMaxGetBulkRepetitions() {
121     return maxGetBulkRepetitions;
122   }
123
124   public AgentXMib getAgentXMIB() {
125     return agentXMIB;
126   }
127
128   public AgentXCommandProcessor getCommandProcessor() {
129     return commandProcessor;
130   }
131
132   public OctetString getLocalEngineID() {
133     return localEngineID;
134   }
135
136   /**
137    * Indicates whether only subagents from the local host or from any host
138    * are allowed to connect to this master agent (default is any host).
139    * @return
140    * <code>true</code> if only connections from the local host are allowed
141    * and <code>false</code> if connections from any host are allowed.
142    */

143   public boolean isLocalhostSubagentsOnly() {
144     return localhostSubagentsOnly;
145   }
146
147   /**
148    * Sets the maximum repetitions value used by this master agent for its
149    * AgentX Get Bulk requests to subagents. The default is the maximum short
150    * value. The SNMP GETBULK request already defines a maximum repetitions
151    * value that is always the upper limit also for AgentX Get Bulk requests on
152    * its behalf.
153    * <p>
154    * The NET-SNMP AgentX sub-agent has a bug in its AgentX Get Bulk processing
155    * that causes endless loops in the sub-agent when the max-repetitions value
156    * is greater than one. Since this bug is in NET-SNMP since v4.2 and still
157    * present in version 5.4, it is likely, that you will need to set this
158    * value to one, if your master agent should ever communicate with a NET-SNMP
159    * sub-agent.
160    *
161    * @param maxRepetitions
162    * the upper limit of the maximum repetitions for AgentX Get Bulk
163    * sub-requests.
164    */

165   public static void setMaxGetBulkRepetitions(short maxRepetitions) {
166     if (maxRepetitions < 1) {
167       throw new IllegalArgumentException JavaDoc(
168           "Max repetitions needs an unsigned value");
169     }
170     maxGetBulkRepetitions = maxRepetitions;
171   }
172
173   public void setLocalEngineID(OctetString localEngineID) {
174     this.localEngineID = localEngineID;
175   }
176
177   /**
178    * Sets the local host only connection filter flag.
179    * @param localhostSubagentsOnly
180    * <code>true</code> if only connections from the local host are allowed
181    * and <code>false</code> if connections from any host are allowed.
182    */

183   public void setLocalhostSubagentsOnly(boolean localhostSubagentsOnly) {
184     this.localhostSubagentsOnly = localhostSubagentsOnly;
185   }
186
187   public void connectionStateChanged(TransportStateEvent change) {
188     if (localhostSubagentsOnly &&
189         (change.getNewState() == TransportStateEvent.STATE_CONNECTED) &&
190         (change.getPeerAddress() instanceof IpAddress)) {
191       IpAddress peerAddress = (IpAddress)change.getPeerAddress();
192       if (!peerAddress.getInetAddress().isLoopbackAddress()) {
193         LOGGER.warn("Connection attempt made from non loopback (i.e. local) address '"+
194                     peerAddress+"' which will be ignored");
195         change.setCancelled(true);
196         return;
197       }
198     }
199     ((AgentXCommandProcessor)getAgent()).connectionStateChanged(change);
200   }
201
202   protected void registerManagedObjects() {
203     try {
204       agentXMIB.registerMOs(server, null);
205     }
206     catch (DuplicateRegistrationException ex) {
207       LOGGER.error("Unable to register AgentX MIB", ex);
208     }
209   }
210
211   protected void unregisterManagedObjects() {
212     agentXMIB.unregisterMOs(server, null);
213   }
214
215   protected void addUsmUser(USM usm) {
216   }
217
218   protected void addNotificationTargets(SnmpTargetMIB targetMIB,
219                                         SnmpNotificationMIB notificationMIB) {
220   }
221
222   protected void addViews(VacmMIB vacmMIB) {
223   }
224
225   protected void addCommunities(SnmpCommunityMIB communityMIB) {
226   }
227 }
228
Popular Tags