KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > daemon > SnmpSubBulkRequestHandler


1 /*
2  * @(#)file SnmpSubBulkRequestHandler.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 4.23
5  * @(#)date 08/02/09
6  *
7  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
8  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
9  *
10  */

11
12
13 package com.sun.jmx.snmp.daemon;
14
15
16
17 // java import
18
//
19
import java.util.Enumeration JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 // jmx imports
23
//
24
import com.sun.jmx.snmp.SnmpPdu;
25 import com.sun.jmx.snmp.SnmpVarBind;
26 import com.sun.jmx.snmp.SnmpOid;
27 import com.sun.jmx.snmp.SnmpValue;
28 import com.sun.jmx.snmp.SnmpDefinitions;
29 import com.sun.jmx.snmp.SnmpStatusException;
30 import com.sun.jmx.snmp.SnmpEngine;
31 // SNMP Runtime import
32
//
33
import com.sun.jmx.snmp.agent.SnmpMibAgent;
34 import com.sun.jmx.snmp.agent.SnmpMibRequest;
35 import com.sun.jmx.snmp.ThreadContext;
36 import com.sun.jmx.snmp.daemon.SnmpAdaptorServer;
37 import com.sun.jmx.snmp.internal.SnmpIncomingRequest;
38 import com.sun.jmx.snmp.ThreadContext;
39
40 class SnmpSubBulkRequestHandler extends SnmpSubRequestHandler {
41     private SnmpAdaptorServer server = null;
42
43     /**
44      * The constuctor initialize the subrequest with the whole varbind list contained
45      * in the original request.
46      */

47     protected SnmpSubBulkRequestHandler(SnmpEngine engine,
48                     SnmpAdaptorServer server,
49                     SnmpIncomingRequest incRequest,
50                     SnmpMibAgent agent,
51                     SnmpPdu req,
52                     int nonRepeat,
53                                         int maxRepeat,
54                     int R) {
55     super(engine, incRequest, agent, req);
56     init(server, req, nonRepeat, maxRepeat, R);
57     }
58     
59     /**
60      * The constuctor initialize the subrequest with the whole varbind list contained
61      * in the original request.
62      */

63     protected SnmpSubBulkRequestHandler(SnmpAdaptorServer server,
64                     SnmpMibAgent agent,
65                     SnmpPdu req,
66                     int nonRepeat,
67                                         int maxRepeat,
68                     int R) {
69     super(agent, req);
70     init(server, req, nonRepeat, maxRepeat, R);
71     }
72     
73     public void run() {
74     
75         size= varBind.size();
76     
77         try {
78             // Invoke a getBulk operation
79
//
80
/* NPCTE fix for bugId 4492741, esc 0, 16-August-2001 */
81         final ThreadContext oldContext =
82                 ThreadContext.push("SnmpUserData",data);
83         try {
84         if (isTraceOn()) {
85                     trace("run", "[" + Thread.currentThread() +
86             "]:getBulk operation on " + agent.getMibName());
87         }
88         agent.getBulk(createMibRequest(varBind,version,data),
89                   nonRepeat, maxRepeat);
90         } finally {
91                 ThreadContext.restore(oldContext);
92             }
93         /* end of NPCTE fix for bugId 4492741 */
94         
95         } catch(SnmpStatusException x) {
96             errorStatus = x.getStatus() ;
97             errorIndex= x.getErrorIndex();
98             if (isDebugOn()) {
99                 debug("run", "[" + Thread.currentThread() +
100               "]:an Snmp error occured during the operation");
101                 debug("run", x);
102             }
103         }
104         catch(Exception JavaDoc x) {
105             errorStatus = SnmpDefinitions.snmpRspGenErr ;
106             if (isDebugOn()) {
107                 debug("run", "[" + Thread.currentThread() +
108               "]:a generic error occured during the operation");
109                 debug("run", x);
110             }
111         }
112         if (isTraceOn()) {
113             trace("run", "[" + Thread.currentThread() +
114           "]:operation completed");
115         }
116     }
117     
118     private void init(SnmpAdaptorServer server,
119               SnmpPdu req,
120               int nonRepeat,
121               int maxRepeat,
122               int R) {
123     this.server = server;
124         this.nonRepeat= nonRepeat;
125         this.maxRepeat= maxRepeat;
126         this.globalR= R;
127     
128     final int max= translation.length;
129         final SnmpVarBind[] list= req.varBindList;
130         final NonSyncVector nonSyncVarBind = ((NonSyncVector)varBind);
131         for(int i=0; i < max; i++) {
132             translation[i]= i;
133             // we need to allocate a new SnmpVarBind. Otherwise the first
134
// sub request will modify the list...
135
//
136
final SnmpVarBind newVarBind =
137         new SnmpVarBind(list[i].oid, list[i].value);
138             nonSyncVarBind.addNonSyncElement(newVarBind);
139         }
140     }
141     
142     /**
143      * The method updates find out which element to use at update time. Handle oid overlapping as well
144      */

145     private SnmpVarBind findVarBind(SnmpVarBind element,
146                     SnmpVarBind result) {
147     
148     if (element == null) return null;
149
150     if (result.oid == null) {
151          return element;
152     }
153
154     if (element.value == SnmpVarBind.endOfMibView) return result;
155
156     if (result.value == SnmpVarBind.endOfMibView) return element;
157
158     final SnmpValue val = result.value;
159
160     int comp = element.oid.compareTo(result.oid);
161     if(isDebugOn()) {
162         trace("findVarBind","Comparing OID element : " + element.oid +
163           " with result : " + result.oid);
164         trace("findVarBind","Values element : " + element.value +
165           " result : " + result.value);
166     }
167     if (comp < 0) {
168         // Take the smallest (lexicographically)
169
//
170
return element;
171     }
172     else {
173         if(comp == 0) {
174         // Must compare agent used for reply
175
// Take the deeper within the reply
176
if(isDebugOn()) {
177             trace("findVarBind"," oid overlapping. Oid : " +
178               element.oid + "value :" + element.value);
179             trace("findVarBind","Already present varBind : " +
180               result);
181         }
182         SnmpOid oid = result.oid;
183         SnmpMibAgent deeperAgent = server.getAgentMib(oid);
184
185         if(isDebugOn())
186             trace("findVarBind","Deeper agent : " + deeperAgent);
187         if(deeperAgent == agent) {
188             if(isDebugOn())
189             trace("updateResult","The current agent is the deeper one. Update the value with the current one");
190             return element;
191         } else {
192             if(isDebugOn())
193             trace("updateResult","Current is not the deeper, return the previous one.");
194             return result;
195         }
196             
197         /*
198            Vector v = new Vector();
199            SnmpMibRequest getReq = createMibRequest(v,
200            version,
201            null);
202            SnmpVarBind realValue = new SnmpVarBind(oid);
203            getReq.addVarBind(realValue);
204            try {
205            deeperAgent.get(getReq);
206            } catch(SnmpStatusException e) {
207            e.printStackTrace();
208            }
209            
210            if(isDebugOn())
211            trace("findVarBind", "Biggest priority value is : " +
212            realValue.value);
213            
214            return realValue;
215         */

216         
217         }
218         else {
219         if(isDebugOn())
220             trace("findVarBind",
221               "The right varBind is the already present one");
222         return result;
223         }
224     }
225     }
226     /**
227      * The method updates a given var bind list with the result of a
228      * previsouly invoked operation.
229      * Prior to calling the method, one must make sure that the operation was
230      * successful. As such the method getErrorIndex or getErrorStatus should be
231      * called.
232      */

233     protected void updateResult(SnmpVarBind[] result) {
234     // we can assume that the run method is over ...
235
//
236

237         final Enumeration JavaDoc e= varBind.elements();
238         final int max= result.length;
239
240         // First go through all the values once ...
241
for(int i=0; i < size; i++) {
242             // May be we should control the position ...
243
//
244
if (e.hasMoreElements() == false)
245                 return;
246
247         // bugId 4641694: must check position in order to avoid
248
// ArrayIndexOutOfBoundException
249
final int pos=translation[i];
250         if (pos >= max) {
251         debug("updateResult","Position `"+pos+"' is out of bound...");
252         continue;
253         }
254
255         final SnmpVarBind element= (SnmpVarBind) e.nextElement();
256         
257         if (element == null) continue;
258         if (isDebugOn())
259         trace("updateResult", "Non repeaters Current element : " +
260               element + " from agent : " + agent);
261         final SnmpVarBind res = findVarBind(element,result[pos]);
262         
263         if(res == null) continue;
264         
265         result[pos] = res;
266     }
267  
268         // Now update the values which have been repeated
269
// more than once.
270
int localR= size - nonRepeat;
271         for (int i = 2 ; i <= maxRepeat ; i++) {
272             for (int r = 0 ; r < localR ; r++) {
273                 final int pos = (i-1)* globalR + translation[nonRepeat + r] ;
274                 if (pos >= max)
275                     return;
276                 if (e.hasMoreElements() ==false)
277                     return;
278                 final SnmpVarBind element= (SnmpVarBind) e.nextElement();
279         
280         if (element == null) continue;
281         if (isDebugOn())
282             trace("updateResult", "Repeaters Current element : " +
283               element + " from agent : " + agent);
284         final SnmpVarBind res = findVarBind(element, result[pos]);
285         
286         if(res == null) continue;
287         
288         result[pos] = res;
289             }
290         }
291     }
292   
293     protected String JavaDoc makeDebugTag() {
294         return "SnmpSubBulkRequestHandler";
295     }
296     
297     // PROTECTED VARIABLES
298
//------------------
299

300     /**
301      * Specific to the sub request
302      */

303     protected int nonRepeat=0;
304   
305     protected int maxRepeat=0;
306   
307     /**
308      * R as defined in RCF 1902 for the global request the sub-request is associated to.
309      */

310     protected int globalR=0;
311
312     protected int size=0;
313 }
314
Popular Tags