1 21 package ist.coach.coachEmfServicesComponents ; 22 23 import java.util.LinkedList ; 24 import java.util.ListIterator ; 25 26 import org.opennms.protocols.snmp.*; 27 28 import ist.coach.coachEmfCommon.Utils; 29 import ist.coach.coachEmfCommon.ExceptionMessages; 30 import ist.coach.coachEmfServices.SnmpAdapter.SnmpApplicationError ; 31 import ist.coach.coachEmfCommon.DataTypeImpl; 32 import ist.coach.coachEmfServices.SnmpAdapter.DataType ; 33 import ist.coach.coachEmfServicesComponents.SnmpAdapterProviderPackage.trap_targetConnection ; 34 35 import org.omg.CORBA.Any ; 36 import org.omg.CosNaming.NameComponent ; 37 40 public class SnmpAdapterProviderMonolithicImpl 41 extends org.omg.CORBA.LocalObject 42 implements CCM_SnmpAdapterProvider, 43 ist.coach.coachEmfServices.SnmpAdapter.CCM_SnmpConnector , 44 ist.coach.coachEmfServices.SnmpAdapter.CCM_SnmpTrapDaemon , 45 org.omg.Components.SessionComponent, 46 Runnable , 47 SnmpTrapHandler 48 { 49 trap_targetConnection[] trap_target_facades = null; 55 56 59 private CCM_SnmpAdapterProvider_Context the_context_; 60 63 private org.omg.CORBA.ORB orb; 64 67 private LinkedList linkedList; 68 71 private Configuration config; 72 75 private static final SnmpObjectId SNMPv2_MIB_snmpTrapOid = 76 new SnmpObjectId(".1.3.6.1.6.3.1.1.4.1.0"); 77 78 public static final int SNMP_TRAP_PORT = 2010; 79 82 private Thread listenerThread; 83 86 private static final int UNKNOWN_TRAP_TYPE = 0; 87 95 public 96 SnmpAdapterProviderMonolithicImpl() 97 { 98 the_context_ = null; 99 this.linkedList = new LinkedList (); 100 101 } 102 108 private void listener_init() { 109 if (listenerThread == null) { 110 listenerThread = new Thread (this, "Listener"); 111 listenerThread.start(); 112 } 113 } 114 115 private void listener_stop() { 116 117 Thread dummyThread =listenerThread; 118 listenerThread = null; 119 dummyThread.interrupt(); 120 121 } 122 123 private Any convertSnmpSyntaxToAny(SnmpSyntax data) { 124 125 Any value = this.orb.create_any(); 126 127 128 switch (data.typeId()) { 129 case SnmpSMI.SMI_OBJECTID: 130 value.insert_string( new String (((SnmpObjectId)data).toString())); 131 break; 132 case SnmpSMI.SMI_COUNTER32: 133 value.insert_longlong( ((SnmpCounter32)data).getValue()); 134 break; 135 case SnmpSMI.SMI_COUNTER64: value.insert_longlong( ((SnmpCounter64)data).getValue().longValue()); 137 break; 138 case SnmpSMI.SMI_GAUGE32: 139 value.insert_longlong( ((SnmpGauge32)data).getValue()); 140 break; 141 case SnmpSMI.SMI_INTEGER: 142 value.insert_long( ((SnmpInt32)data).getValue()); 143 break; 144 case SnmpSMI.SMI_TIMETICKS: 145 value.insert_longlong( ((SnmpTimeTicks)data).getValue()); 146 break; 147 case SnmpSMI.SMI_IPADDRESS: 148 value.insert_string( new String (((SnmpIPAddress)data).toString())); 149 break; 150 case SnmpSMI.SMI_STRING: 151 167 SnmpOctetString octetData = (SnmpOctetString) data; 168 byte[] octetDataBytes = octetData.getString(); 169 org.omg.CORBA.OctetSeqHelper.insert(value, octetDataBytes); 170 171 break; 174 case SnmpSMI.SMI_OPAQUE: 175 value.insert_string( new String (((SnmpOpaque)data).getString())); 176 break; 177 case SnmpSMI.SMI_NOSUCHOBJECT: 178 value.insert_string("No Such Object"); 181 break; 182 case SnmpSMI.SMI_NOSUCHINSTANCE: 183 value.insert_string( "No Such Instance"); 186 break; 187 default: 188 System.err.println("Unknown Variable Binding."); 189 value = null; 190 break; 191 192 } 193 194 return value; 195 196 } 197 198 private SnmpSyntax convertAnyToSnmpSyntax(Any data, byte code) { 199 200 SnmpSyntax value = null; 201 202 switch (code) { 203 204 case SnmpSMI.SMI_COUNTER32: 205 value = ((SnmpSyntax)new SnmpCounter32(data.extract_longlong())); 206 break; 207 case SnmpSMI.SMI_COUNTER64: 208 value = ((SnmpSyntax)new SnmpCounter64(data.extract_longlong())); 209 break; 210 case SnmpSMI.SMI_UNSIGNED32: value = ((SnmpSyntax)new SnmpGauge32(data.extract_longlong())); 212 break; 213 case SnmpSMI.SMI_INTEGER: 214 value = ((SnmpSyntax)new SnmpInt32(data.extract_long())); 215 break; 216 case SnmpSMI.SMI_IPADDRESS: 217 value = ((SnmpSyntax)new SnmpIPAddress(data.extract_string().getBytes())); 218 break; 219 case SnmpSMI.SMI_OBJECTID: 220 value = ((SnmpSyntax)new SnmpObjectId(data.extract_string())); 221 break; 222 case SnmpSMI.SMI_OPAQUE: 223 value = ((SnmpSyntax)new SnmpOpaque(data.extract_string().getBytes())); 224 break; 225 case SnmpSMI.SMI_STRING: 226 value = (SnmpSyntax)new SnmpOctetString(org.omg.CORBA.OctetSeqHelper.extract(data)); 227 break; 230 case SnmpSMI.SMI_TIMETICKS: 231 value = ((SnmpSyntax)new SnmpTimeTicks(data.extract_longlong())); 232 break; 233 default: 234 System.err.println("SnmpAdapter:convertAnyToSnmpSyntax> " + 235 " Unknown (or un-supported) SMI Type"); 236 break; 237 } 238 239 return value; 240 } 241 242 private void processTrapData( 243 java.net.InetAddress agent, 244 SnmpOctetString community, 245 SnmpPduPacket pdu) 246 throws SnmpApplicationError{ 247 248 int snmpTrapType = 0; 249 String trapObjectIdentifier = null; 250 int k = pdu.getLength(); 251 DataTypeImpl[] dataSetType = new DataTypeImpl[k]; 252 253 for (int i = 0; i < k ; i++ ) { 254 255 SnmpVarBind vb = pdu.getVarBindAt(i); 256 257 String oid = vb.getName().toString(); 258 Any value = this.orb.create_any(); 259 260 switch (vb.getValue().typeId()) { 261 case SnmpSMI.SMI_OBJECTID: 262 value.insert_string( ((SnmpObjectId)vb.getValue()).toString()); 263 break; 264 case SnmpSMI.SMI_COUNTER32: 265 value.insert_longlong( ((SnmpCounter32)vb.getValue()).getValue()); 266 break; 267 case SnmpSMI.SMI_COUNTER64: 268 value.insert_longlong( ((SnmpCounter64)vb.getValue()).getValue().longValue()); 269 break; 270 case SnmpSMI.SMI_GAUGE32: 271 value.insert_longlong( ((SnmpGauge32)vb.getValue()).getValue()); 272 break; 273 case SnmpSMI.SMI_INTEGER: 274 value.insert_long( ((SnmpInt32)vb.getValue()).getValue()); 275 break; 276 case SnmpSMI.SMI_TIMETICKS: 277 value.insert_longlong( ((SnmpTimeTicks)vb.getValue()).getValue()); 278 break; 279 case SnmpSMI.SMI_IPADDRESS: 280 value.insert_string( ((SnmpIPAddress)vb.getValue()).toString()); 281 break; 282 case SnmpSMI.SMI_STRING: 283 SnmpOctetString octetData = (SnmpOctetString) vb.getValue(); 284 byte[] octetDataBytes = octetData.getString(); 285 org.omg.CORBA.OctetSeqHelper.insert(value, octetDataBytes); 286 break; 289 default: 290 System.err.println("SnmpAdapter: processTrapData> Unknown Variable Binding."); 291 break; 292 293 } 294 295 if (vb.getName().compare(SNMPv2_MIB_snmpTrapOid) == 0) 296 trapObjectIdentifier = ((SnmpObjectId)vb.getValue()).toString(); 297 298 dataSetType[i] = new DataTypeImpl(oid, value); 299 300 } 301 302 snmpTrapType = determineTrapType(trapObjectIdentifier); 303 if (snmpTrapType == UNKNOWN_TRAP_TYPE) 304 throw new SnmpApplicationError(ExceptionMessages.uknown_trap_error); 305 306 String objectClass = determineObjectClass(snmpTrapType); 307 if (objectClass == null) { 308 System.err.println("SnmpAdapter: processTrapData> Trap could not be mapped to an object class"); 309 throw new SnmpApplicationError("Trap could not be mapped to an object class"); 310 } 311 312 System.err.println("SnmpAdapter: processTrapData> Trap is " + trapTypeToString(snmpTrapType) + 313 " (trap type " + snmpTrapType + 314 " ), thrown by objects of objectclass " + objectClass); 315 316 NameComponent [] name = null; 317 318 if (isRootObject(objectClass)) { 319 name = findRegisteredRootObject(objectClass, agent.getHostAddress()); 320 if (name == null) { 321 System.err.println("SnmpAdapter: processTrapData> Trap could not be mapped to a registered (root) object."); 322 throw new SnmpApplicationError(ExceptionMessages.uknown_trap_origin_error); 323 } 324 } 325 else { 326 327 boolean valueIsNeeded = determineIfValueIsNeeded(objectClass); 328 329 Any value = null; 330 331 if (valueIsNeeded == true) { 332 value = determineValue(objectClass, dataSetType); 333 334 if (value == null) { 335 System.err.println("SnmpAdapter: processTrapData> Could not determine value for trap!"); 336 throw new SnmpApplicationError(ExceptionMessages.trap_misconfig_error); 337 } 338 } 339 340 name = findRegisteredLeafObject(objectClass, agent.getHostAddress(), value); 341 if (name == null) { 342 System.err.println("SnmpAdapter: processTrapData> Trap could not be mapped to a registered (leaf) object."); 343 throw new SnmpApplicationError(ExceptionMessages.uknown_trap_origin_error); 344 } 345 } 346 347 String objectFacadeKind = Utils.name2facade(Utils.name2string(name)); 348 String facadeNameKind = null; 349 boolean facadeFound = false; 350 351 System.err.println("SnmpAdapter: processTrapData> Object with name " + Utils.name2string(name) + 352 " is accessible through a facade with kind " + objectFacadeKind); 353 354 int index = 0; 355 String facadeName = new String (); 356 for (index = 0; index < trap_target_facades.length; index++) { 357 358 facadeName = trap_target_facades[index].objref.facade_name(); 359 facadeNameKind = Utils.name2facade(facadeName); 360 361 364 if (objectFacadeKind.equals(facadeNameKind)) { 365 366 System.err.println("SnmpAdapter: processTrapData> Object with name " + 367 Utils.name2string(name) + 368 " is accessible through facade with name " + facadeName + 369 " found at position " + index); 370 371 facadeFound = true; 372 373 break; 374 375 } 376 } 377 378 if (facadeFound == false) { 379 380 System.err.println("SnmpAdapter: processTrapData> Could not find a facade to send trap to"); 381 throw new SnmpApplicationError("No Facade trap sink found"); 382 } 383 384 int varBindNumber = pdu.getLength(); 385 386 DataTypeImpl [] trapData = new DataTypeImpl[varBindNumber]; 387 388 for (int j = 0; j < varBindNumber; j++) { 389 390 SnmpVarBind vb = pdu.getVarBindAt(j); 391 392 trapData[j] = new DataTypeImpl(vb.getName().toString(), 393 convertSnmpSyntaxToAny(vb.getValue()), 394 vb.getValue().typeId()); 395 396 } 397 398 String community_str = (community != null) ? 399 new String (community.getString()) : new String (); 400 401 408 try { 409 ist.coach.coachEmfServices.SnmpAdapter.TrapCallback facade_callback = 410 trap_target_facades[index].objref; 411 412 if (facade_callback != null) { 413 facade_callback.notifyTrap( name, 414 community_str, 415 pdu.getRequestId(), 416 snmpTrapType, 417 trapData); 418 } 419 else 420 System.err.println("SnmpAdapter: processTrapData> Could not retrieve reference to facade of " + 421 Utils.name2string(name)); 422 } 423 catch(SnmpApplicationError snmp_ex) { 424 System.err.println("SnmpAdapterProvider: SnmpApplicationError exception caught " + 425 " while trying to forward trap to " + Utils.name2string(name)); 426 } 427 catch(intt.itu.itut_x780.ApplicationError app_exc) { 428 System.err.println("SnmpAdapterProvider: ApplicationError exception caught " + 429 " while trying to forward trap to " + Utils.name2string(name)); 430 } 431 432 } 433 434 private int determineTrapType(String trapObjectIdentifier) { 435 436 437 int snmpTrapType = UNKNOWN_TRAP_TYPE; 438 439 for (int i = 0; i < this.config.availableTraps.length; i++) { 440 if (trapObjectIdentifier.equals( 441 this.config.availableTraps[i].oid.toString())) { 442 443 snmpTrapType = this.config.availableTraps[i].identifier; 444 break; 445 } 446 } 447 448 return snmpTrapType; 449 450 } 451 452 private String determineObjectClass(int snmpTrapType) { 453 454 for (int i = 0; i < this.config.availableTraps.length; i++) { 455 if (snmpTrapType == this.config.availableTraps[i].identifier) { 456 return this.config.availableTraps[i].objectclass; 457 } 458 } 459 460 return null; 461 462 } 463 464 private boolean determineIfValueIsNeeded(String objectClass) { 465 466 boolean valueNeeded = false; 467 468 for (int i = 0; i < this.config.availableTraps.length; i++) { 469 470 if (objectClass.equalsIgnoreCase(this.config.availableTraps[i].objectclass)) { 471 472 if (this.config.availableTraps[i].valueOid == null) { 473 System.err.println("SnmpAdapter: determineIfValueIsNeeded> No value needed to fetch " + 474 "leaf object of objectclass " + objectClass); 475 valueNeeded = false; 476 } 477 else 478 valueNeeded = true; 479 } 480 } 481 482 return valueNeeded; 483 } 484 485 486 private Any determineValue(String objectClass, DataType [] varBindings) { 487 488 String valueOid = null; 489 490 493 494 for (int i = 0; i < this.config.availableTraps.length; i++) { 495 496 if (objectClass.equalsIgnoreCase(this.config.availableTraps[i].objectclass)) { 497 498 if (this.config.availableTraps[i].valueOid != null) 499 valueOid = new String (this.config.availableTraps[i].valueOid.toString()); 500 504 break; 505 } 506 507 } 508 509 if (valueOid != null) { 510 for (int i = 0; i < varBindings.length; i++) { 511 if (varBindings[i].identifier.equals( 512 valueOid)) { 513 return (varBindings[i].value); 514 } 515 } 516 } 517 return null; 519 520 } 521 522 private String trapTypeToString(int snmpTrapType) { 523 for (int i = 0; i < this.config.availableTraps.length; i++) { 524 if (this.config.availableTraps[i].identifier == snmpTrapType) { 525 526 return this.config.availableTraps[i].id; 527 } 528 529 } 530 531 return null; 532 } 533 534 542 private NameComponent [] findRegisteredRootObject( 543 String objectClass, String ipAddress) { 544 545 ListIterator li = this.linkedList.listIterator(); 546 547 550 while (li.hasNext()) { 551 552 ObjectData objData = (ObjectData)li.next(); 553 554 NameComponent [] name = objData.getName(); 555 String myObjectClass = name[name.length - 2].kind; 556 String myIpAddress = objData.getIPAddress(); 557 558 561 if (myObjectClass.equals(objectClass) && 562 myIpAddress.equals(ipAddress)) { 563 564 return objData.getName(); 567 } 568 } 569 570 return null; 571 } 572 573 583 584 private NameComponent [] findRegisteredLeafObject( 585 String objectClass, String ipAddress, Any value) { 586 587 ListIterator li = this.linkedList.listIterator(); 588 589 592 while (li.hasNext()) { 593 594 ObjectData objData = (ObjectData)li.next(); 595 596 NameComponent [] name = objData.getName(); 597 String myObjectClass = getObjectClass(name); 598 String myIpAddress = objData.getIPAddress(); 599 600 if (myObjectClass.equals(objectClass) && 601 myIpAddress.equals(ipAddress)) { 602 603 if (value != null) { 604 if (objData.value.equal(value)) { 605 606 return objData.getName(); 609 } 610 } 611 else { 612 return objData.getName(); 615 } 616 } 617 } 618 619 return null; 620 } 621 622 629 630 private boolean isRootObject(String objectClass) { 631 632 for (int i = 0; i < config.rootObjects.length; i++) 633 if (config.rootObjects[i].equals(objectClass)) 634 return true; 635 636 return false; 637 } 638 639 private String getObjectClass(NameComponent [] name) { 640 return name[name.length - 2].kind; 641 } 642 643 private boolean nameExists(NameComponent [] name) { 644 645 ListIterator li = this.linkedList.listIterator(); 646 647 while (li.hasNext()) { 648 ObjectData objData = (ObjectData) li.next(); 649 if (objData.getName().equals(name)) { 650 return true; 651 } 652 } 653 return false; 654 } 655 private boolean rootObjectExists(String ipAddress, 656 NameComponent [] name) { 657 658 ListIterator li = this.linkedList.listIterator(); 659 660 while (li.hasNext()) { 661 662 ObjectData objData = (ObjectData)li.next(); 663 666 if (objData.getIPAddress().equals(ipAddress) && 667 isRootObject(getObjectClass(objData.getName())) && 668 Utils.isChild(name, objData.getName())) 669 return true; 670 } 671 672 return false; 673 } 674 675 676 677 private ObjectData findObjectWithName(NameComponent [] name) { 678 679 String name_str = Utils.name2string(name); 680 681 ListIterator li = this.linkedList.listIterator(); 682 683 ObjectData objData = null; 684 685 while (li.hasNext()) { 686 objData = (ObjectData) li.next(); 687 if (Utils.name2string(objData.getName()).equals(name_str)) { 688 return objData; 689 } 690 } 691 return null; 692 } 693 694 private class ObjectData { 695 696 private NameComponent [] name; 697 private String IPAddress; 698 private Any value; 699 700 ObjectData(NameComponent [] name, String IPAddress, Any value) { 701 this.name = name; 702 this.IPAddress = IPAddress; 703 this.value = value; 704 } 705 706 ObjectData (NameComponent [] name, String IPAddress) { 707 this.name = name; 708 this.IPAddress = IPAddress; 709 this.value = null; 710 } 711 712 public NameComponent [] getName() { 713 return this.name; 714 } 715 716 public String getIPAddress() { 717 return this.IPAddress; 718 } 719 } 720 721 public void run() { 727 728 Thread myThread = Thread.currentThread(); 729 SnmpTrapSession trapSession = null; 730 while (listenerThread == myThread) { 731 732 try { 733 trapSession = new SnmpTrapSession(this, 734 SNMP_TRAP_PORT); 735 System.err.println("SNMP Trap Receiver Started"); 736 synchronized(trapSession) 737 { 738 trapSession.wait(); 739 } 740 } 741 catch (InterruptedException ie) { 742 System.err.println("SNMP Trap Receiver Exiting"); 743 trapSession.close(); 744 } 745 catch (Exception e) { 746 System.err.println("Exception in init(): " + e); 747 e.printStackTrace(); 748 } 749 } 750 } 751 752 753 759 760 763 public void snmpReceivedTrap( 764 SnmpTrapSession session, 765 java.net.InetAddress agent, 766 int port, 767 SnmpOctetString community, 768 SnmpPduPacket pdu) { 769 770 System.err.println("V2 Trap from agent " + agent.toString() + " on port " + port); 771 System.err.println("V2 Trap Community........... " + new String (community.getString())); 772 System.err.println("V2 Trap PDU command......... " + pdu.getCommand()); 773 System.err.println("V2 Trap PDU ID.............. " + pdu.getRequestId()); 774 System.err.println("V2 Trap PDU Length.......... " + pdu.getLength()); 775 776 if(pdu instanceof SnmpPduRequest) 777 { 778 System.err.println("V2 Trap PDU Error Status.... " + ((SnmpPduRequest)pdu).getErrorStatus()); 779 System.err.println("V2 Trap PDU Error Index..... " + ((SnmpPduRequest)pdu).getErrorIndex()); 780 } 781 782 int k = pdu.getLength(); 783 784 for (int i = 0; i < k ; i++ ) 785 { 786 SnmpVarBind vb = pdu.getVarBindAt(i); 787 System.err.print("Varbind[" + i + "] := " + vb.getName().toString()); 788 System.err.println(" --> " + vb.getValue().toString()); 789 } 790 791 try { 792 793 processTrapData(agent, community, pdu); 794 } 795 catch (SnmpApplicationError e) { 796 System.err.println("Unable to process trap!"); 797 } 798 799 } 800 801 811 public void snmpReceivedTrap( 812 SnmpTrapSession session, 813 java.net.InetAddress agent, 814 int port, 815 SnmpOctetString community, 816 SnmpPduTrap pdu) 817 818 { 819 820 System.err.println("V1 Trap from agent " + agent.toString() + " on port " + port); 821 System.err.println("Ip Address................. " + pdu.getAgentAddress() ); 822 System.err.println("Enterprise Id.............. " + pdu.getEnterprise() ); 823 System.err.println("Generic ................... " + pdu.getGeneric() ); 824 System.err.println("Specific .................. " + pdu.getSpecific() ); 825 System.err.println("TimeStamp ................. " + pdu.getTimeStamp() ); 826 System.err.println("Length..................... " + pdu.getLength() ); 827 828 829 int k = pdu.getLength(); 830 for (int i = 0; i < k ; i++ ) 831 { 832 SnmpVarBind vb = pdu.getVarBindAt(i); 833 System.err.print("Varbind[" + i + "] := " + vb.getName().toString()); 834 System.err.println(" --> " + vb.getValue().toString()); 835 } 836 System.err.println(""); 837 838 } 843 844 851 public void snmpTrapSessionError( 852 SnmpTrapSession session, 853 int error, 854 java.lang.Object ref) 855 { 856 857 System.err.println("SnmpAdapter:snmpTrapSessionError> An error occured in the trap session"); 858 System.err.println("Session error code = " + error); 859 if(ref != null) 860 { 861 System.err.println("SnmpAdapter:snmpTrapSessionError> Session error reference: " + ref.toString()); 862 } 863 864 if(error == SnmpTrapSession.ERROR_EXCEPTION) 865 { 866 synchronized(session) 867 { 868 session.notify(); } 870 } 871 } 872 873 884 public CCM_SnmpAdapterProvider_Context 885 getContext() 886 { 887 return the_context_; 888 } 889 903 public void 904 configuration_complete() 905 throws org.omg.Components.InvalidConfiguration 906 { 907 911 this.config = Configuration.getInstance(); 912 913 if (config == null) { 914 System.err.println("Could not create a valid configuration. Exiting..."); 915 throw new org.omg.Components.InvalidConfiguration(); 916 } 917 918 this.orb = org.objectweb.ccm.CORBA.TheORB.getORB(); 919 listener_init(); 920 921 922 if ((trap_target_facades = the_context_.get_connections_trap_target()) == null) { 923 System.err.println("SnmpAdapter is not configured with Facades to serve...."); 924 } 925 926 System.err.println("SnmpAdapterProvider configuration completed..."); 927 } 928 944 public void 945 set_session_context(org.omg.Components.SessionContext context) 946 throws org.omg.Components.CCMException 947 { 948 the_context_ = (CCM_SnmpAdapterProvider_Context)context; 949 } 950 960 public void 961 ccm_activate() 962 throws org.omg.Components.CCMException 963 { 964 } 968 978 public void 979 ccm_passivate() 980 throws org.omg.Components.CCMException 981 { 982 } 986 996 public void 997 ccm_remove() 998 throws org.omg.Components.CCMException 999 { 1000 1004 listener_stop(); 1005 } 1006 1017 public ist.coach.coachEmfServices.SnmpAdapter.CCM_SnmpConnector 1018 get_connector() 1019 { 1020 return this; 1021 } 1022 1028 public ist.coach.coachEmfServices.SnmpAdapter.CCM_SnmpTrapDaemon 1029 get_config() 1030 { 1031 return this; 1032 } 1033 1044 public ist.coach.coachEmfServices.SnmpAdapter.DataType [] 1045 getSnmpTable(java.lang.String tableStart, 1046 java.lang.String host, 1047 int port, 1048 java.lang.String community) 1049 throws ist.coach.coachEmfServices.SnmpAdapter.SnmpApplicationError 1050 { 1051 1055 1056 SnmpProtocolConverter snmpConverter = new SnmpProtocolConverter(host, community, port, tableStart); 1057 1058 snmpConverter.snmpWalk(); 1059 1060 if (snmpConverter.agentResponded() == false) { 1061 1062 System.err.println("Agent did not respond!"); 1063 throw new SnmpApplicationError(ExceptionMessages.agent_response_error); 1064 } 1065 1066 1067 DataTypeImpl [] snmpTable = new DataTypeImpl[snmpConverter.resultSet.size()]; 1068 1069 for (int i = 0; i < snmpConverter.resultSet.size(); i++) { 1070 1071 SnmpVarBind vb = (SnmpVarBind)snmpConverter.resultSet.get(i); 1072 Any value = this.orb.create_any(); 1073 value = convertSnmpSyntaxToAny(vb.getValue()); 1074 if (value == null) { 1075 System.err.println("SnmpAdapter>Could not encapsulate SNMP attribute value into a CORBA Any"); 1076 throw new SnmpApplicationError(ExceptionMessages.agent_create_error); 1077 } 1078 else { 1079 snmpTable[i] = new DataTypeImpl(vb.getName().toString(), value, vb.getValue().typeId()); 1080 1081 } 1082 } 1083 1084 return snmpTable; 1085 } 1086 1092 public ist.coach.coachEmfServices.SnmpAdapter.DataType 1093 getNext(java.lang.String identifier, 1094 java.lang.String host, 1095 int port, 1096 java.lang.String community) 1097 throws ist.coach.coachEmfServices.SnmpAdapter.SnmpApplicationError 1098 { 1099 1103 SnmpProtocolConverter snmpConverter = new SnmpProtocolConverter(host, community, port, null); 1104 1105 snmpConverter.snmpGetNext(new SnmpObjectId(identifier)); 1106 1107 if (snmpConverter.agentResponded() == false) { 1108 1109 System.err.println("Agent did not respond!"); 1110 throw new SnmpApplicationError(ExceptionMessages.agent_response_error); 1111 } 1112 1113 if (snmpConverter.resultSet.size() == 0) { 1114 1115 System.err.println("Could not retrieve attribute with oid " + identifier); 1116 throw new SnmpApplicationError(ExceptionMessages.agent_get_error); 1117 } 1118 1119 Any value = this.orb.create_any(); 1120 SnmpVarBind vb = (SnmpVarBind)snmpConverter.resultSet.elementAt(0); 1121 String oid = vb.getName().toString(); 1122 value = convertSnmpSyntaxToAny(vb.getValue()); 1123 1124 if (value == null) { 1125 System.err.println("Could not retrieve attribute with oid " + identifier); 1126 throw new SnmpApplicationError(ExceptionMessages.agent_get_error); 1127 } 1128 1129 DataTypeImpl result = new DataTypeImpl(oid, value, vb.getValue().typeId()); 1130 1131 return result; 1132 1133 } 1134 1140 public ist.coach.coachEmfServices.SnmpAdapter.DataType 1141 get(java.lang.String identifier, 1142 java.lang.String host, 1143 int port, 1144 java.lang.String community) 1145 throws ist.coach.coachEmfServices.SnmpAdapter.SnmpApplicationError 1146 { 1147 1151 SnmpProtocolConverter snmpConverter = new SnmpProtocolConverter(host, community, port, null); 1152 1153 SnmpVarBind [] vblist = new SnmpVarBind[1]; 1154 vblist[0] = new SnmpVarBind(new SnmpObjectId(identifier)); 1155 1156 snmpConverter.snmpGet(vblist); 1157 1158 if (snmpConverter.agentResponded() == false) { 1159 1160 System.err.println("Agent did not respond!"); 1161 throw new SnmpApplicationError(ExceptionMessages.agent_response_error); 1162 } 1163 1164 if (snmpConverter.resultSet.size() == 0) { 1165 1166 System.err.println("Could not retrieve attribute with oid " + identifier); 1167 throw new SnmpApplicationError(ExceptionMessages.agent_get_error); 1168 } 1169 1170 1172 Any value = this.orb.create_any(); 1173 SnmpVarBind vb = (SnmpVarBind)snmpConverter.resultSet.elementAt(0); 1174 String oid = vb.getName().toString(); 1175 value = convertSnmpSyntaxToAny(vb.getValue()); 1176 1177 if (value == null) { 1178 System.err.println("Could not retrieve attribute with oid " + identifier); 1179 throw new SnmpApplicationError(ExceptionMessages.agent_get_error); 1180 } 1181 1182 DataTypeImpl result = new DataTypeImpl(oid, value, vb.getValue().typeId()); 1183 1184 return result; 1185 } 1186 1192 public ist.coach.coachEmfServices.SnmpAdapter.DataType [] 1193 getBulk(java.lang.String [] identifiers, 1194 java.lang.String host, 1195 int port, 1196 java.lang.String community) 1197 throws ist.coach.coachEmfServices.SnmpAdapter.SnmpApplicationError 1198 { 1199 SnmpProtocolConverter snmpConverter = new SnmpProtocolConverter(host, community, port, null); 1203 1204 int numRequested = identifiers.length; 1205 1206 SnmpVarBind [] vblist = new SnmpVarBind[numRequested]; 1207 1208 for (int i = 0; i < numRequested; i++) 1209 vblist[i] = new SnmpVarBind(new SnmpObjectId(identifiers[i])); 1210 1211 snmpConverter.snmpGet(vblist); 1212 1213 if (snmpConverter.agentResponded() == false) { 1214 1215 System.err.println("Agent did not respond!"); 1216 throw new SnmpApplicationError(ExceptionMessages.agent_response_error); 1217 } 1218 1219 if (snmpConverter.resultSet.size() == 0) { 1220 System.err.println("Could not retrieve attributes with requested identifiers"); 1221 throw new SnmpApplicationError(ExceptionMessages.agent_get_error); 1222 } 1223 1224 DataTypeImpl [] result = new DataTypeImpl[snmpConverter.resultSet.size()]; 1225 1226 1227 for (int i = 0; i < snmpConverter.resultSet.size(); i++) { 1228 1229 Any value = this.orb.create_any(); 1230 SnmpVarBind vb = (SnmpVarBind)snmpConverter.resultSet.elementAt(i); 1231 String oid = vb.getName().toString(); 1232 value = convertSnmpSyntaxToAny(vb.getValue()); 1233 1234 if (value == null) { 1235 System.err.println("Could not encapsulate SNMP attribute value into a CORBA Any"); 1236 throw new SnmpApplicationError(ExceptionMessages.agent_create_error); 1237 } 1238 else 1239 result[i] = new DataTypeImpl(((SnmpVarBind)snmpConverter.resultSet.elementAt(i)).getName().toString(), value, vb.getValue().typeId()); 1240 } 1241 1242 return result; 1244 1245 } 1246 1252 public void 1253 set(ist.coach.coachEmfServices.SnmpAdapter.DataType data, 1254 java.lang.String host, 1255 int port, 1256 java.lang.String community) 1257 throws ist.coach.coachEmfServices.SnmpAdapter.SnmpApplicationError 1258 { 1259 SnmpProtocolConverter snmpConverter = new SnmpProtocolConverter(host, community, port, null); 1263 1264 SnmpVarBind [] vblist = new SnmpVarBind[1]; 1265 SnmpSyntax value = convertAnyToSnmpSyntax(data.value, data.code); 1266 if (value == null) { 1267 System.err.println("Could not convert DataType to an SnmpSyntax"); 1268 throw new SnmpApplicationError(ExceptionMessages.agent_create_error); 1269 } 1270 1271 vblist[0] = new SnmpVarBind(new SnmpObjectId(data.identifier), value); 1272 1273 snmpConverter.snmpSet(vblist); 1274 1275 if (snmpConverter.agentResponded() == false) { 1276 1277 System.err.println("Agent did not respond!"); 1278 throw new SnmpApplicationError(ExceptionMessages.agent_response_error); 1279 } 1280 1281 if (snmpConverter.resultSet.size() == 0) { 1282 1283 System.err.println("Could not set attribute with oid " + data.identifier); 1284 throw new SnmpApplicationError(ExceptionMessages.agent_set_error); 1285 } 1286 1287 } 1288 1294 public void 1295 setBulk(ist.coach.coachEmfServices.SnmpAdapter.DataType [] data, 1296 java.lang.String host, 1297 int port, 1298 java.lang.String community) 1299 throws ist.coach.coachEmfServices.SnmpAdapter.SnmpApplicationError 1300 { 1301 SnmpProtocolConverter snmpConverter = new SnmpProtocolConverter(host, community, port, null); 1305 int numRequested = data.length; 1306 SnmpVarBind [] vblist = new SnmpVarBind[numRequested]; 1308 1309 for (int i = 0; i < numRequested; i++) { 1310 1312 SnmpSyntax value = convertAnyToSnmpSyntax(data[i].value, data[i].code); 1313 vblist[i] = new SnmpVarBind(new SnmpObjectId(data[i].identifier), value); 1314 } 1317 1318 snmpConverter.snmpSet(vblist); 1319 1320 if (snmpConverter.agentResponded() == false) { 1321 1322 System.err.println("Agent did not respond!"); 1323 throw new SnmpApplicationError(ExceptionMessages.agent_response_error); 1324 } 1325 1326 if (snmpConverter.resultSet.size() == 0) { 1327 System.err.println("setBulk>Could not set attributes"); 1328 1329 throw new SnmpApplicationError(ExceptionMessages.agent_get_error); 1330 } 1331 } 1333 1344 public void 1345 registerIP(org.omg.CosNaming.NameComponent [] name, java.lang.String ipAddress) 1346 throws ist.coach.coachEmfServices.SnmpAdapter.SnmpApplicationError 1347 { 1348 if (nameExists (name)) { 1352 System.err.println("Could not register object with Name " + 1353 Utils.name2string(name) + " because it already exists!"); 1354 throw new SnmpApplicationError(ExceptionMessages.snmp_register_error); 1355 1356 } 1357 else { 1358 ObjectData objData = new ObjectData (name, ipAddress); 1359 this.linkedList.add(objData); 1360 } 1363 } 1364 1370 public void 1371 registerValue(org.omg.CosNaming.NameComponent [] name, java.lang.String ipAddress, org.omg.CORBA.Any value) 1372 throws ist.coach.coachEmfServices.SnmpAdapter.SnmpApplicationError 1373 { 1374 if (nameExists (name)) { 1380 System.err.println("SNMPAdapter:registerValue> Could not register object with Name " 1381 + Utils.name2string(name) + " because it already exists!"); 1382 throw new SnmpApplicationError(ExceptionMessages.snmp_register_error); 1383 } 1384 else 1385 if (rootObjectExists(ipAddress, name) == false) { 1386 System.err.println("SNMPAdapter:registerValue> Could not find parent for this object."); 1387 throw new SnmpApplicationError(ExceptionMessages.snmp_superior_error); 1388 } 1389 else { 1390 ObjectData objData = new ObjectData (name, ipAddress, value); 1391 this.linkedList.add(objData); 1392 } 1394 } 1395 1401 public void 1402 unregister(org.omg.CosNaming.NameComponent [] name) 1403 throws ist.coach.coachEmfServices.SnmpAdapter.SnmpApplicationError 1404 { 1405 1409 ObjectData objData = null; 1410 1411 if ((objData = findObjectWithName(name)) != null) { 1412 this.linkedList.remove(objData); 1413 System.err.println("SnmpAdapter:unregister>Unregistered object with name " + Utils.name2string(name)); 1414 } 1415 else { 1416 System.err.println("SnmpAdapter:unregister>Could not unregister object with name " + Utils.name2string(name)); 1417 throw new SnmpApplicationError(ExceptionMessages.snmp_unregister_error); 1418 } 1419 1420 } 1421} 1422 | Popular Tags |