1 11 12 13 package com.sun.jmx.snmp.daemon; 14 15 16 17 import java.util.Vector ; 20 21 import com.sun.jmx.snmp.SnmpPdu; 24 import com.sun.jmx.snmp.SnmpVarBind; 25 import com.sun.jmx.snmp.SnmpDefinitions; 26 import com.sun.jmx.snmp.SnmpStatusException; 27 import com.sun.jmx.snmp.SnmpEngine; 28 29 import com.sun.jmx.trace.Trace; 32 33 import com.sun.jmx.snmp.agent.SnmpMibAgent; 36 import com.sun.jmx.snmp.agent.SnmpMibRequest; 37 import com.sun.jmx.snmp.ThreadContext; 38 import com.sun.jmx.snmp.internal.SnmpIncomingRequest; 39 40 class SnmpSubRequestHandler implements SnmpDefinitions, Runnable { 41 42 protected SnmpIncomingRequest incRequest = null; 43 protected SnmpEngine engine = null; 44 47 protected SnmpSubRequestHandler(SnmpEngine engine, 48 SnmpIncomingRequest incRequest, 49 SnmpMibAgent agent, 50 SnmpPdu req) { 51 this(agent, req); 52 init(engine, incRequest); 53 } 54 55 58 protected SnmpSubRequestHandler(SnmpEngine engine, 59 SnmpIncomingRequest incRequest, 60 SnmpMibAgent agent, 61 SnmpPdu req, 62 boolean nouse) { 63 this(agent, req, nouse); 64 init(engine, incRequest); 65 } 66 69 protected SnmpSubRequestHandler(SnmpMibAgent agent, SnmpPdu req) { 70 71 dbgTag = makeDebugTag(); 72 if (isTraceOn()) { 73 trace("constructor", "creating instance for request " + String.valueOf(req.requestId)); 74 } 75 76 version= req.version; 77 type= req.type; 78 this.agent= agent; 79 80 reqPdu = req; 82 83 int length= req.varBindList.length; 86 translation= new int[length]; 87 varBind= new NonSyncVector(length); 88 } 89 90 94 protected SnmpSubRequestHandler(SnmpMibAgent agent, 95 SnmpPdu req, 96 boolean nouse) { 97 this(agent,req); 98 99 int max= translation.length; 102 SnmpVarBind[] list= req.varBindList; 103 for(int i=0; i < max; i++) { 104 translation[i]= i; 105 ((NonSyncVector)varBind).addNonSyncElement(list[i]); 106 } 107 } 108 109 SnmpMibRequest createMibRequest(Vector vblist, 110 int protocolVersion, 111 Object userData) { 112 113 if (type == pduSetRequestPdu && mibRequest != null) 118 return mibRequest; 119 120 SnmpMibRequest result = null; 123 if(incRequest != null) { 124 result = SnmpMibAgent.newMibRequest(engine, 125 reqPdu, 126 vblist, 127 protocolVersion, 128 userData, 129 incRequest.getPrincipal(), 130 incRequest.getSecurityLevel(), 131 incRequest.getSecurityModel(), 132 incRequest.getContextName(), 133 incRequest.getAccessContext()); 134 } else { 135 result = SnmpMibAgent.newMibRequest(reqPdu, 136 vblist, 137 protocolVersion, 138 userData); 139 } 140 if (type == pduWalkRequest) 144 mibRequest = result; 145 146 return result; 147 } 148 149 void setUserData(Object userData) { 150 data = userData; 151 } 152 153 public void run() { 154 155 try { 156 final ThreadContext oldContext = 157 ThreadContext.push("SnmpUserData",data); 158 try { 159 switch(type) { 160 case pduGetRequestPdu: 161 if (isTraceOn()) { 164 trace("run", "[" + Thread.currentThread() + 165 "]:get operation on " + agent.getMibName()); 166 } 167 168 agent.get(createMibRequest(varBind,version,data)); 169 break; 170 171 case pduGetNextRequestPdu: 172 if (isTraceOn()) { 173 trace("run", "[" + Thread.currentThread() + 174 "]:getNext operation on " + agent.getMibName()); 175 } 176 agent.getNext(createMibRequest(varBind,version,data)); 178 break; 179 180 case pduSetRequestPdu: 181 if (isTraceOn()) { 182 trace("run", "[" + Thread.currentThread() + 183 "]:set operation on " + agent.getMibName()); 184 } 185 agent.set(createMibRequest(varBind,version,data)); 186 break; 187 188 case pduWalkRequest: 189 if (isTraceOn()) { 190 trace("run", "[" + Thread.currentThread() + 191 "]:check operation on " + agent.getMibName()); 192 } 193 agent.check(createMibRequest(varBind,version,data)); 194 break; 195 196 default: 197 if (isDebugOn()) { 198 debug("run", "[" + Thread.currentThread() + 199 "]:unknown operation (" + type + ") on " + 200 agent.getMibName()); 201 } 202 errorStatus= snmpRspGenErr; 203 errorIndex= 1; 204 break; 205 206 } 208 } finally { 209 ThreadContext.restore(oldContext); 210 } 211 } catch(SnmpStatusException x) { 212 errorStatus = x.getStatus() ; 213 errorIndex= x.getErrorIndex(); 214 if (isDebugOn()) { 215 debug("run", "[" + Thread.currentThread() + 216 "]:an Snmp error occured during the operation"); 217 debug("run", x); 218 } 219 } 220 catch(Exception x) { 221 errorStatus = SnmpDefinitions.snmpRspGenErr ; 222 if (isTraceOn()) { 223 trace("run", "[" + Thread.currentThread() + 224 "]:a generic error occured during the operation"); 225 } 226 if (isDebugOn()) { 227 debug("run", "Error is: " + x); 228 debug("run", x); 229 } 230 } 231 if (isTraceOn()) { 232 trace("run", "[" + Thread.currentThread() + 233 "]:operation completed"); 234 } 235 } 236 237 static final int mapErrorStatusToV1(int errorStatus, int reqPduType) { 269 if (errorStatus == SnmpDefinitions.snmpRspNoError) 272 return SnmpDefinitions.snmpRspNoError; 273 274 if (errorStatus == SnmpDefinitions.snmpRspGenErr) 275 return SnmpDefinitions.snmpRspGenErr; 276 277 if (errorStatus == SnmpDefinitions.snmpRspNoSuchName) 278 return SnmpDefinitions.snmpRspNoSuchName; 279 280 if ((errorStatus == SnmpStatusException.noSuchInstance) || 281 (errorStatus == SnmpStatusException.noSuchObject) || 282 (errorStatus == SnmpDefinitions.snmpRspNoAccess) || 283 (errorStatus == SnmpDefinitions.snmpRspInconsistentName) || 284 (errorStatus == SnmpDefinitions.snmpRspAuthorizationError)){ 285 286 return SnmpDefinitions.snmpRspNoSuchName; 287 288 } else if ((errorStatus == 289 SnmpDefinitions.snmpRspAuthorizationError) || 290 (errorStatus == SnmpDefinitions.snmpRspNotWritable)) { 291 292 if (reqPduType == SnmpDefinitions.pduWalkRequest) 293 return SnmpDefinitions.snmpRspReadOnly; 294 else 295 return SnmpDefinitions.snmpRspNoSuchName; 296 297 } else if ((errorStatus == SnmpDefinitions.snmpRspNoCreation)) { 298 299 return SnmpDefinitions.snmpRspNoSuchName; 300 301 } else if ((errorStatus == SnmpDefinitions.snmpRspWrongType) || 302 (errorStatus == SnmpDefinitions.snmpRspWrongLength) || 303 (errorStatus == SnmpDefinitions.snmpRspWrongEncoding) || 304 (errorStatus == SnmpDefinitions.snmpRspWrongValue) || 305 (errorStatus == SnmpDefinitions.snmpRspWrongLength) || 306 (errorStatus == 307 SnmpDefinitions.snmpRspInconsistentValue)) { 308 309 if ((reqPduType == SnmpDefinitions.pduSetRequestPdu) || 310 (reqPduType == SnmpDefinitions.pduWalkRequest)) 311 return SnmpDefinitions.snmpRspBadValue; 312 else 313 return SnmpDefinitions.snmpRspNoSuchName; 314 315 } else if ((errorStatus == 316 SnmpDefinitions.snmpRspResourceUnavailable) || 317 (errorStatus == 318 SnmpDefinitions.snmpRspCommitFailed) || 319 (errorStatus == SnmpDefinitions.snmpRspUndoFailed)) { 320 321 return SnmpDefinitions.snmpRspGenErr; 322 323 } 324 325 if (errorStatus == SnmpDefinitions.snmpRspTooBig) 328 return SnmpDefinitions.snmpRspTooBig; 329 330 if( (errorStatus == SnmpDefinitions.snmpRspBadValue) || 331 (errorStatus == SnmpDefinitions.snmpRspReadOnly)) { 332 if ((reqPduType == SnmpDefinitions.pduSetRequestPdu) || 333 (reqPduType == SnmpDefinitions.pduWalkRequest)) 334 return errorStatus; 335 else 336 return SnmpDefinitions.snmpRspNoSuchName; 337 } 338 339 return SnmpDefinitions.snmpRspGenErr; 343 344 } 345 346 static final int mapErrorStatusToV2(int errorStatus, int reqPduType) { 383 if (errorStatus == SnmpDefinitions.snmpRspNoError) 386 return SnmpDefinitions.snmpRspNoError; 387 388 if (errorStatus == SnmpDefinitions.snmpRspGenErr) 389 return SnmpDefinitions.snmpRspGenErr; 390 391 if (errorStatus == SnmpDefinitions.snmpRspTooBig) 392 return SnmpDefinitions.snmpRspTooBig; 393 394 if ((reqPduType != SnmpDefinitions.pduSetRequestPdu) && 398 (reqPduType != SnmpDefinitions.pduWalkRequest)) { 399 if(errorStatus == SnmpDefinitions.snmpRspAuthorizationError) 400 return errorStatus; 401 else 402 return SnmpDefinitions.snmpRspGenErr; 403 } 404 405 411 414 if (errorStatus == SnmpDefinitions.snmpRspNoSuchName) 415 return SnmpDefinitions.snmpRspNoAccess; 416 417 if (errorStatus == SnmpDefinitions.snmpRspReadOnly) 419 return SnmpDefinitions.snmpRspNotWritable; 420 421 if (errorStatus == SnmpDefinitions.snmpRspBadValue) 423 return SnmpDefinitions.snmpRspWrongValue; 424 425 if ((errorStatus == SnmpDefinitions.snmpRspNoAccess) || 427 (errorStatus == SnmpDefinitions.snmpRspInconsistentName) || 428 (errorStatus == SnmpDefinitions.snmpRspAuthorizationError) || 429 (errorStatus == SnmpDefinitions.snmpRspNotWritable) || 430 (errorStatus == SnmpDefinitions.snmpRspNoCreation) || 431 (errorStatus == SnmpDefinitions.snmpRspWrongType) || 432 (errorStatus == SnmpDefinitions.snmpRspWrongLength) || 433 (errorStatus == SnmpDefinitions.snmpRspWrongEncoding) || 434 (errorStatus == SnmpDefinitions.snmpRspWrongValue) || 435 (errorStatus == SnmpDefinitions.snmpRspWrongLength) || 436 (errorStatus == SnmpDefinitions.snmpRspInconsistentValue) || 437 (errorStatus == SnmpDefinitions.snmpRspResourceUnavailable) || 438 (errorStatus == SnmpDefinitions.snmpRspCommitFailed) || 439 (errorStatus == SnmpDefinitions.snmpRspUndoFailed)) 440 return errorStatus; 441 442 return SnmpDefinitions.snmpRspGenErr; 444 } 445 446 static final int mapErrorStatus(int errorStatus, 447 int protocolVersion, 448 int reqPduType) { 449 if (errorStatus == SnmpDefinitions.snmpRspNoError) 450 return SnmpDefinitions.snmpRspNoError; 451 452 if (protocolVersion == SnmpDefinitions.snmpVersionOne) 455 return mapErrorStatusToV1(errorStatus,reqPduType); 456 if (protocolVersion == SnmpDefinitions.snmpVersionTwo || 457 protocolVersion == SnmpDefinitions.snmpVersionThree) 458 return mapErrorStatusToV2(errorStatus,reqPduType); 459 460 return SnmpDefinitions.snmpRspGenErr; 461 } 462 463 467 protected int getErrorStatus() { 468 if (errorStatus == snmpRspNoError) 469 return snmpRspNoError; 470 471 return mapErrorStatus(errorStatus,version,type); 472 } 473 474 479 protected int getErrorIndex() { 480 if (errorStatus == snmpRspNoError) 481 return -1; 482 483 if ((errorIndex == 0) || (errorIndex == -1)) 488 errorIndex = 1; 489 490 return translation[errorIndex -1]; 491 } 492 493 496 protected void updateRequest(SnmpVarBind var, int pos) { 497 int size= varBind.size(); 498 translation[size]= pos; 499 varBind.addElement(var); 500 } 501 502 509 protected void updateResult(SnmpVarBind[] result) { 510 511 if (result == null) return; 512 final int max=varBind.size(); 513 final int len=result.length; 514 for(int i= 0; i< max ; i++) { 515 final int pos=translation[i]; 518 if (pos < len) { 519 result[pos] = 520 (SnmpVarBind)((NonSyncVector)varBind).elementAtNonSync(i); 521 } else { 522 debug("updateResult","Position `"+pos+"' is out of bound..."); 523 } 524 } 525 } 526 527 private void init(SnmpEngine engine, 528 SnmpIncomingRequest incRequest) { 529 this.incRequest = incRequest; 530 this.engine = engine; 531 } 532 533 protected boolean isTraceOn() { 534 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP); 535 } 536 537 protected void trace(String clz, String func, String info) { 538 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP, clz, func, info); 539 } 540 541 protected void trace(String func, String info) { 542 trace(dbgTag, func, info); 543 } 544 545 protected boolean isDebugOn() { 546 return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP); 547 } 548 549 protected void debug(String clz, String func, String info) { 550 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP, clz, func, info); 551 } 552 553 protected void debug(String func, Throwable t) { 554 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP, dbgTag, func, t); 555 } 556 557 protected void debug(String func, String info) { 558 debug(dbgTag, func, info); 559 } 560 561 protected String makeDebugTag() { 562 return "SnmpSubRequestHandler"; 563 } 564 565 568 protected String dbgTag = null; 569 570 573 protected int version= snmpVersionOne; 574 575 579 protected int type= 0; 580 581 584 protected SnmpMibAgent agent; 585 586 589 protected int errorStatus= snmpRspNoError; 590 591 595 protected int errorIndex= -1; 596 597 601 protected Vector varBind; 602 603 607 protected int[] translation; 608 609 612 protected Object data; 613 614 618 private SnmpMibRequest mibRequest = null; 619 620 624 private SnmpPdu reqPdu = null; 625 626 class NonSyncVector extends Vector { 631 632 public NonSyncVector(int size) { 633 super(size); 634 } 635 636 final void addNonSyncElement(Object obj) { 637 ensureCapacity(elementCount + 1); 638 elementData[elementCount++] = obj; 639 } 640 641 final Object elementAtNonSync(int index) { 642 return elementData[index]; 643 } 644 }; 645 } 646 | Popular Tags |