(file) Return to testSnmpHandler.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Handler / snmpIndicationHandler / tests / testclient

   1 yi.zhou 1.1 //%2006////////////////////////////////////////////////////////////////////////
   2             //
   3             // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
   4             // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
   5             // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
   6             // IBM Corp.; EMC Corporation, The Open Group.
   7             // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
   8             // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
   9             // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
  10             // EMC Corporation; VERITAS Software Corporation; The Open Group.
  11             // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
  12             // EMC Corporation; Symantec Corporation; The Open Group.
  13             //
  14             // Permission is hereby granted, free of charge, to any person obtaining a copy
  15             // of this software and associated documentation files (the "Software"), to
  16             // deal in the Software without restriction, including without limitation the
  17             // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  18             // sell copies of the Software, and to permit persons to whom the Software is
  19             // furnished to do so, subject to the following conditions:
  20             // 
  21             // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
  22 yi.zhou 1.1 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
  23             // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
  24             // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  25             // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  26             // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  27             // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28             // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29             //
  30             //==============================================================================
  31             //
  32             //%/////////////////////////////////////////////////////////////////////////////
  33             
  34             #include <Pegasus/Common/PegasusAssert.h>
  35             #include <Pegasus/Common/Thread.h>
  36             #include <Pegasus/Common/Constants.h>
  37             #include <Pegasus/Common/FileSystem.h>
  38             #include <Pegasus/Common/Stopwatch.h>
  39             #include <Pegasus/Client/CIMClient.h>
  40 dave.sudlik 1.6 #include <Pegasus/Common/HostAddress.h>
  41 yi.zhou     1.1 
  42                 PEGASUS_USING_PEGASUS;
  43                 PEGASUS_USING_STD;
  44                 
  45 karl        1.4 // Interop namespace used with PEGASUS_NAMESPACENAME_INTEROP in Constants.h
  46 kumpf       1.7 const CIMNamespaceName SOURCE_NAMESPACE =
  47 kumpf       1.9     CIMNamespaceName ("test/TestProvider");
  48 yi.zhou     1.1 
  49 kumpf       1.9 const String INDICATION_CLASS_NAME = "Test_IndicationProviderClass";
  50 yi.zhou     1.1 
  51 kumpf       1.9 const String SNMPV1_HANDLER_NAME = "SNMPHandler01";
  52                 const String SNMPV2C_HANDLER_NAME = "SNMPHandler02";
  53                 const String SNMPV2C_IPV6_HANDLER_NAME = "SNMPHandler03";
  54                 const String FILTER_NAME = "IPFilter01";
  55 yi.zhou     1.1 
  56                 enum SNMPVersion {_SNMPV1_TRAP = 2, _SNMPV2C_TRAP = 3};
  57 dave.sudlik 1.6 enum TargetHostFormat {_HOST_NAME = 2, _IPV4_ADDRESS = 3, _IPV6_ADDRESS = 4};
  58 yi.zhou     1.1 
  59                 #define PORT_NUMBER 2006
  60                 
  61                 AtomicInt errorsEncountered(0);
  62                 
  63                 ////////////////////////////////////////////////////////////////////////////////
  64                 //
  65                 // Thread Parameters Class
  66                 //
  67                 ////////////////////////////////////////////////////////////////////////////////
  68                 
  69 kumpf       1.7 class T_Parms
  70                 {
  71                 public:
  72 yi.zhou     1.1     AutoPtr<CIMClient> client;
  73                     Uint32 indicationSendCount;
  74                     Uint32 uniqueID;
  75                 };
  76                 
  77                 ///////////////////////////////////////////////////////////////////////////
  78                 
  79 kumpf       1.7 CIMObjectPath _getFilterObjectPath(const String & name)
  80 yi.zhou     1.1 {
  81                     Array<CIMKeyBinding> keyBindings;
  82                     keyBindings.append (CIMKeyBinding ("SystemCreationClassName",
  83                         System::getSystemCreationClassName (), CIMKeyBinding::STRING));
  84                     keyBindings.append (CIMKeyBinding ("SystemName",
  85                         System::getFullyQualifiedHostName (), CIMKeyBinding::STRING));
  86                     keyBindings.append (CIMKeyBinding ("CreationClassName",
  87                         PEGASUS_CLASSNAME_INDFILTER.getString(), CIMKeyBinding::STRING));
  88                     keyBindings.append (CIMKeyBinding ("Name", name,
  89                         CIMKeyBinding::STRING));
  90                     return(CIMObjectPath("", CIMNamespaceName (),
  91                         PEGASUS_CLASSNAME_INDFILTER, keyBindings));
  92                 }
  93                 
  94 kumpf       1.7 CIMObjectPath _getHandlerObjectPath(const String & name)
  95 yi.zhou     1.1 {
  96                     Array<CIMKeyBinding> keyBindings;
  97                     keyBindings.append (CIMKeyBinding ("SystemCreationClassName",
  98                         System::getSystemCreationClassName (), CIMKeyBinding::STRING));
  99                     keyBindings.append (CIMKeyBinding ("SystemName",
 100                         System::getFullyQualifiedHostName (), CIMKeyBinding::STRING));
 101                     keyBindings.append (CIMKeyBinding ("CreationClassName",
 102                         PEGASUS_CLASSNAME_INDHANDLER_SNMP.getString(),
 103                         CIMKeyBinding::STRING));
 104                     keyBindings.append (CIMKeyBinding ("Name", name,
 105                         CIMKeyBinding::STRING));
 106                     return(CIMObjectPath("", CIMNamespaceName (),
 107                         PEGASUS_CLASSNAME_INDHANDLER_SNMP, keyBindings));
 108                 }
 109                 
 110 kumpf       1.7 CIMObjectPath _getSubscriptionObjectPath(
 111                     const String & filterName,
 112                     const String & handlerName)
 113 yi.zhou     1.1 {
 114                     CIMObjectPath filterObjectPath = _getFilterObjectPath(filterName);
 115                 
 116                     CIMObjectPath handlerObjectPath = _getHandlerObjectPath(handlerName);
 117                 
 118                     Array<CIMKeyBinding> subscriptionKeyBindings;
 119                     subscriptionKeyBindings.append (CIMKeyBinding ("Filter",
 120                         CIMValue(filterObjectPath)));
 121                     subscriptionKeyBindings.append (CIMKeyBinding ("Handler",
 122                         CIMValue(handlerObjectPath)));
 123                     return(CIMObjectPath("", CIMNamespaceName (),
 124                         PEGASUS_CLASSNAME_INDSUBSCRIPTION, subscriptionKeyBindings));
 125                 }
 126                 
 127 kumpf       1.7 CIMObjectPath _createHandlerInstance(
 128                     CIMClient & client,
 129                     const String & name,
 130                     const String & targetHost,
 131                     const String & securityName,
 132                     const Uint16 targetHostFormat,
 133                     const Uint16 snmpVersion)
 134 yi.zhou     1.1 {
 135                     CIMInstance handlerInstance (PEGASUS_CLASSNAME_INDHANDLER_SNMP);
 136                     handlerInstance.addProperty (CIMProperty (CIMName
 137                         ("SystemCreationClassName"), System::getSystemCreationClassName ()));
 138                     handlerInstance.addProperty (CIMProperty (CIMName ("SystemName"),
 139                         System::getFullyQualifiedHostName ()));
 140                     handlerInstance.addProperty (CIMProperty (CIMName ("CreationClassName"),
 141                         PEGASUS_CLASSNAME_INDHANDLER_SNMP.getString ()));
 142                     handlerInstance.addProperty (CIMProperty (CIMName ("Name"), name));
 143                     handlerInstance.addProperty (CIMProperty (CIMName ("TargetHost"),
 144                         targetHost));
 145                     handlerInstance.addProperty (CIMProperty (CIMName ("TargetHostFormat"),
 146                         CIMValue ((Uint16) targetHostFormat)));
 147                     handlerInstance.addProperty (CIMProperty (CIMName ("SNMPSecurityName"),
 148                         securityName));
 149                     handlerInstance.addProperty (CIMProperty (CIMName ("SnmpVersion"),
 150                         CIMValue ((Uint16) snmpVersion)));
 151                     handlerInstance.addProperty (CIMProperty (CIMName ("PortNumber"),
 152                         CIMValue ((Uint32) PORT_NUMBER)));
 153                 
 154 kumpf       1.7     return client.createInstance(
 155                         PEGASUS_NAMESPACENAME_INTEROP, handlerInstance);
 156 yi.zhou     1.1 }
 157                 
 158 kumpf       1.7 CIMObjectPath _createFilterInstance(
 159                     CIMClient & client,
 160                     const String & name,
 161                     const String & query,
 162                     const String & qlang)
 163 yi.zhou     1.1 {
 164                     CIMInstance filterInstance (PEGASUS_CLASSNAME_INDFILTER);
 165                     filterInstance.addProperty (CIMProperty (CIMName
 166                         ("SystemCreationClassName"), System::getSystemCreationClassName ()));
 167                     filterInstance.addProperty (CIMProperty (CIMName ("SystemName"),
 168                         System::getFullyQualifiedHostName ()));
 169                     filterInstance.addProperty (CIMProperty (CIMName ("CreationClassName"),
 170                         PEGASUS_CLASSNAME_INDFILTER.getString ()));
 171                     filterInstance.addProperty (CIMProperty (CIMName ("Name"), name));
 172                     filterInstance.addProperty (CIMProperty (CIMName ("Query"), query));
 173                     filterInstance.addProperty (CIMProperty (CIMName ("QueryLanguage"),
 174                         String (qlang)));
 175                     filterInstance.addProperty (CIMProperty (CIMName ("SourceNamespace"),
 176                         SOURCE_NAMESPACE.getString ()));
 177                 
 178 kumpf       1.7     return client.createInstance(PEGASUS_NAMESPACENAME_INTEROP, filterInstance);
 179 yi.zhou     1.1 }
 180                 
 181 kumpf       1.7 CIMObjectPath _createSubscriptionInstance(
 182                     CIMClient & client,
 183                     const CIMObjectPath & filterPath,
 184                     const CIMObjectPath & handlerPath)
 185 yi.zhou     1.1 {
 186                     CIMInstance subscriptionInstance (PEGASUS_CLASSNAME_INDSUBSCRIPTION);
 187                     subscriptionInstance.addProperty (CIMProperty (CIMName ("Filter"),
 188                         filterPath, 0, PEGASUS_CLASSNAME_INDFILTER));
 189                     subscriptionInstance.addProperty (CIMProperty (CIMName ("Handler"),
 190                         handlerPath, 0, PEGASUS_CLASSNAME_INDHANDLER_SNMP));
 191                     subscriptionInstance.addProperty (CIMProperty
 192                         (CIMName ("SubscriptionState"), CIMValue ((Uint16) 2)));
 193                 
 194 kumpf       1.7     return client.createInstance(
 195                         PEGASUS_NAMESPACENAME_INTEROP, subscriptionInstance);
 196 yi.zhou     1.1 }
 197                 
 198 kumpf       1.7 void _sendTestIndication(
 199                     CIMClient* client,
 200                     const CIMName & methodName,
 201                     Uint32 indicationSendCount)
 202 yi.zhou     1.1 {
 203                     //
 204                     //  Invoke method to send test indication
 205                     //
 206                     Array <CIMParamValue> inParams;
 207                     Array <CIMParamValue> outParams;
 208                     Array <CIMKeyBinding> keyBindings;
 209                     Sint32 result;
 210                 
 211                     CIMObjectPath className (String::EMPTY, CIMNamespaceName (),
 212 kumpf       1.9         CIMName ("Test_IndicationProviderClass"), keyBindings);
 213 yi.zhou     1.1 
 214 kumpf       1.7     inParams.append(CIMParamValue(String("indicationSendCount"),
 215 yi.zhou     1.1         CIMValue(indicationSendCount)));
 216                 
 217                     CIMValue retValue = client->invokeMethod
 218                         (SOURCE_NAMESPACE,
 219                          className,
 220                          methodName,
 221                          inParams,
 222                          outParams);
 223                 
 224                     retValue.get (result);
 225                     PEGASUS_TEST_ASSERT (result == 0);
 226                 }
 227                 
 228 kumpf       1.7 void _deleteSubscriptionInstance(
 229                     CIMClient & client,
 230                     const String & filterName,
 231                     const String & handlerName)
 232 yi.zhou     1.1 {
 233                     CIMObjectPath subscriptionObjectPath =
 234                        _getSubscriptionObjectPath(filterName, handlerName);
 235 kumpf       1.7     client.deleteInstance(
 236                         PEGASUS_NAMESPACENAME_INTEROP, subscriptionObjectPath);
 237 yi.zhou     1.1 }
 238                 
 239 kumpf       1.7 void _deleteHandlerInstance(
 240                     CIMClient & client,
 241                     const String & name)
 242 yi.zhou     1.1 {
 243                     CIMObjectPath handlerObjectPath = _getHandlerObjectPath(name);
 244 karl        1.4     client.deleteInstance (PEGASUS_NAMESPACENAME_INTEROP, handlerObjectPath);
 245 yi.zhou     1.1 }
 246                 
 247 kumpf       1.7 void _deleteFilterInstance(
 248                     CIMClient & client,
 249                     const String & name)
 250 yi.zhou     1.1 {
 251                     CIMObjectPath filterObjectPath = _getFilterObjectPath(name);
 252 karl        1.4     client.deleteInstance (PEGASUS_NAMESPACENAME_INTEROP, filterObjectPath);
 253 yi.zhou     1.1 }
 254                 
 255 kumpf       1.7 void _usage()
 256 yi.zhou     1.1 {
 257                    cerr << endl
 258                         << "Usage:" << endl
 259                         << "    TestSnmpHandler setup [ WQL | DMTF:CQL ]\n"
 260                         << "    TestSnmpHandler run <indicationSendCount> "
 261                         << "[<threads>]\n"
 262                         << "    where: " << endl
 263                         << "       <indicationSendCount> is the number of indications to\n"
 264                         << "            generate and has to be greater than zero." << endl
 265                         << "       <threads> is an optional number of client threads to\n"
 266                         << "            create, default is one." << endl
 267                         << "    TestSnmpHandler cleanup\n"
 268                         << "    TestSnmpHandler removelog"
 269                         << endl << endl;
 270                 }
 271                 
 272                 void _setup (CIMClient & client, const String& qlang)
 273                 {
 274                     CIMObjectPath filterObjectPath;
 275                     CIMObjectPath snmpv1HandlerObjectPath;
 276                     CIMObjectPath snmpv2HandlerObjectPath;
 277 yi.zhou     1.8     CIMObjectPath snmpv2IPV6HandlerObjectPath;
 278 yi.zhou     1.1 
 279                     try
 280                     {
 281                         filterObjectPath = _createFilterInstance (client, FILTER_NAME,
 282 kumpf       1.9             String ("SELECT * FROM Test_IndicationProviderClass"),
 283 yi.zhou     1.1             qlang);
 284                     }
 285                     catch (CIMException& e)
 286                     {
 287                         if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
 288                         {
 289                             filterObjectPath = _getFilterObjectPath(FILTER_NAME);
 290                             cerr << "----- Warning: Filter Instance Not Created: "
 291                                 << e.getMessage () << endl;
 292                         }
 293                         else
 294                         {
 295                             cerr << "----- Error: Filter Instance Not Created: " << endl;
 296                             throw;
 297                         }
 298                     }
 299                 
 300                     try
 301                     {
 302 kumpf       1.7         // Create SNMPv1 trap handler
 303 yi.zhou     1.1         snmpv1HandlerObjectPath = _createHandlerInstance (client,
 304                             SNMPV1_HANDLER_NAME,
 305                             System::getFullyQualifiedHostName(),
 306                             "",
 307                             _HOST_NAME,
 308                             _SNMPV1_TRAP);
 309                     }
 310                     catch (CIMException& e)
 311                     {
 312                         if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
 313                         {
 314                             snmpv1HandlerObjectPath = _getHandlerObjectPath(
 315                                 SNMPV1_HANDLER_NAME);
 316                             cerr << "----- Warning: SNMPv1 Trap Handler Instance Not Created: "
 317                                 << e.getMessage () << endl;
 318                         }
 319                         else
 320                         {
 321                             cerr << "----- Error: SNMPv1 Trap Handler Instance Not Created: "
 322                                 << endl;
 323                             throw;
 324 yi.zhou     1.1         }
 325                     }
 326                 
 327                     try
 328                     {
 329                         _createSubscriptionInstance (client, filterObjectPath,
 330                              snmpv1HandlerObjectPath);
 331                     }
 332                     catch (CIMException& e)
 333                     {
 334                         if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
 335                         {
 336                             cerr << "----- Warning: Client Subscription Instance: "
 337                                 << e.getMessage () << endl;
 338                         }
 339                         else
 340                         {
 341                             cerr << "----- Error: Client Subscription Instance: " << endl;
 342                             throw;
 343                         }
 344                     }
 345 yi.zhou     1.1 
 346                     try
 347                     {
 348 dave.sudlik 1.6         String ipAddress;
 349                         int af;
 350                         System::getHostIP(System::getFullyQualifiedHostName (), &af, ipAddress);
 351 kumpf       1.7         // Create SNMPv2 trap handler
 352 yi.zhou     1.1         snmpv2HandlerObjectPath = _createHandlerInstance (client,
 353                             SNMPV2C_HANDLER_NAME,
 354 dave.sudlik 1.6             ipAddress,
 355 yi.zhou     1.1             "public",
 356 dave.sudlik 1.6             af == AF_INET ? _IPV4_ADDRESS : _IPV6_ADDRESS,
 357 yi.zhou     1.1             _SNMPV2C_TRAP);
 358                     }
 359                     catch (CIMException& e)
 360                     {
 361                         if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
 362                         {
 363                             snmpv2HandlerObjectPath = _getHandlerObjectPath(
 364                                 SNMPV2C_HANDLER_NAME);
 365                             cerr << "----- Warning: SNMPv2c Trap Handler Instance Not Created: "
 366                                 << e.getMessage () << endl;
 367                         }
 368                         else
 369                         {
 370                             cerr << "----- Error: SNMPv2c Trap Handler Instance Not Created: "
 371                                 << endl;
 372                             throw;
 373                         }
 374                     }
 375                 
 376                     try
 377                     {
 378 yi.zhou     1.1         _createSubscriptionInstance (client, filterObjectPath,
 379                              snmpv2HandlerObjectPath);
 380                     }
 381                     catch (CIMException& e)
 382                     {
 383                         if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
 384                         {
 385                             cerr << "----- Warning: Client Subscription Instance: "
 386                                 << e.getMessage () << endl;
 387                         }
 388                         else
 389                         {
 390                             cerr << "----- Error: Client Subscription Instance: " << endl;
 391                             throw;
 392                         }
 393                     }
 394 yi.zhou     1.8 
 395                 #if defined(PEGASUS_ENABLE_IPV6)
 396                     // create a subscription with trap destination of IPV6 address format
 397                     try
 398                     {
 399                         // Create SNMPv2 IPV6 trap handler
 400                         snmpv2IPV6HandlerObjectPath = _createHandlerInstance (client,
 401                             SNMPV2C_IPV6_HANDLER_NAME,
 402                             String("::1"),
 403                             "public",
 404                             _IPV6_ADDRESS,
 405                             _SNMPV2C_TRAP);
 406                     } 
 407                     catch (CIMException& e)
 408                     {
 409                         if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
 410                         {
 411                             snmpv2IPV6HandlerObjectPath = _getHandlerObjectPath(
 412                                 SNMPV2C_IPV6_HANDLER_NAME);
 413                             cerr << "----- Warning: SNMPv2c IPV6 Trap Handler Instance "
 414                                 "Not Created: " << e.getMessage () << endl;
 415 yi.zhou     1.8         }
 416                         else
 417                         {
 418                             cerr << "----- Error: SNMPv2c IPV6 Trap Handler Instance Not "
 419                                 "Created: " << endl;
 420                             throw;
 421                         }
 422                     }
 423                 
 424                     try
 425                     {
 426                         _createSubscriptionInstance (client, filterObjectPath,
 427                              snmpv2IPV6HandlerObjectPath);
 428                     }
 429                     catch (CIMException& e)
 430                     {
 431                         if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
 432                         {
 433                             cerr << "----- Warning: Client Subscription Instance: "
 434                                 << e.getMessage () << endl;
 435                         }
 436 yi.zhou     1.8         else
 437                         {
 438                             cerr << "----- Error: Client Subscription Instance: " << endl;
 439                             throw;
 440                         }
 441                     }
 442                 #endif
 443 yi.zhou     1.1 }
 444                 
 445                 void _cleanup (CIMClient & client)
 446                 {
 447                     try
 448                     {
 449                         _deleteSubscriptionInstance (client, FILTER_NAME,
 450                             SNMPV1_HANDLER_NAME);
 451                     }
 452                     catch (CIMException& e)
 453                     {
 454                         if (e.getCode() != CIM_ERR_NOT_FOUND)
 455                         {
 456                             cerr << "----- Error: deleteSubscriptionInstance failure: "
 457                                  << endl;
 458                             throw;
 459                         }
 460                     }
 461                     try
 462                     {
 463                         _deleteSubscriptionInstance (client, FILTER_NAME,
 464 yi.zhou     1.1             SNMPV2C_HANDLER_NAME);
 465                     }
 466                     catch (CIMException& e)
 467                     {
 468                         if (e.getCode() != CIM_ERR_NOT_FOUND)
 469                         {
 470                             cerr << "----- Error: deleteSubscriptionInstance failure: "
 471                                  << endl;
 472                             throw;
 473                         }
 474                     }
 475 yi.zhou     1.8 
 476                 #if defined(PEGASUS_ENABLE_IPV6)
 477                     try     
 478                     {
 479                         _deleteSubscriptionInstance (client, FILTER_NAME,
 480                             SNMPV2C_IPV6_HANDLER_NAME);
 481                     }
 482                     catch (CIMException& e)
 483                     {
 484                         if (e.getCode() != CIM_ERR_NOT_FOUND)
 485                         {
 486                             cerr << "----- Error: deleteSubscriptionInstance failure: "
 487                                  << endl;
 488                             throw;
 489                         }
 490                     }
 491                 #endif
 492                 
 493 yi.zhou     1.1     try
 494                     {
 495                         _deleteFilterInstance (client, FILTER_NAME);
 496                     }
 497                     catch (CIMException& e)
 498                     {
 499                         if (e.getCode() != CIM_ERR_NOT_FOUND)
 500                         {
 501                             cerr << "----- Error: deleteFilterInstance failure: " << endl;
 502                             throw;
 503                         }
 504                     }
 505                 
 506                     try
 507                     {
 508                         _deleteHandlerInstance (client, SNMPV1_HANDLER_NAME);
 509                     }
 510                     catch (CIMException& e)
 511                     {
 512                         if (e.getCode() != CIM_ERR_NOT_FOUND)
 513                         {
 514 yi.zhou     1.1             cerr << "----- Error: deleteHandlerInstance failure: " << endl;
 515                             throw;
 516                         }
 517                     }
 518                     try
 519                     {
 520                         _deleteHandlerInstance (client, SNMPV2C_HANDLER_NAME);
 521                     }
 522                     catch (CIMException& e)
 523                     {
 524                         if (e.getCode() != CIM_ERR_NOT_FOUND)
 525                         {
 526                             cerr << "----- Error: deleteHandlerInstance failure: " << endl;
 527                             throw;
 528                         }
 529                     }
 530 yi.zhou     1.8 #if defined(PEGASUS_ENABLE_IPV6)
 531                     try
 532                     {
 533                         _deleteHandlerInstance (client, SNMPV2C_IPV6_HANDLER_NAME);
 534                     }
 535                     catch (CIMException& e)
 536                     {
 537                         if (e.getCode() != CIM_ERR_NOT_FOUND)
 538                         {
 539                             cerr << "----- Error: deleteHandlerInstance failure: " << endl;
 540                             throw;
 541                         }
 542                     }
 543                 #endif
 544 yi.zhou     1.1 }
 545                 
 546                 static void _testEnd(const String& uniqueID, const double elapsedTime)
 547                 {
 548                     cout << "+++++ thread" << uniqueID << ": passed in " << elapsedTime
 549                         << " seconds" << endl;
 550                 }
 551                 
 552 mike        1.3 ThreadReturnType PEGASUS_THREAD_CDECL _executeTests(void *parm)
 553 yi.zhou     1.1 {
 554                     Thread *my_thread = (Thread *)parm;
 555                     T_Parms *parms = (T_Parms *)my_thread->get_parm();
 556                     CIMClient *client = parms->client.get();
 557                     Uint32 indicationSendCount = parms->indicationSendCount;
 558                     Uint32 id = parms->uniqueID;
 559                     char id_[4];
 560                     memset(id_,0x00,sizeof(id_));
 561 mike        1.12.4.1     sprintf(id_,"%u",id);
 562 yi.zhou     1.1          String uniqueID = "_";
 563                          uniqueID.append(id_);
 564                      
 565                          try
 566                          {
 567                              Stopwatch elapsedTime;
 568                      
 569                              elapsedTime.start();
 570                              try
 571                              {
 572                                  _sendTestIndication (client, CIMName ("SendTestIndicationTrap"),
 573                                  indicationSendCount);
 574                              }
 575                              catch (Exception & e)
 576                              {
 577 kumpf       1.7                  cerr << "----- sendTestIndication failed: " << e.getMessage () <<
 578                                      endl;
 579 yi.zhou     1.1                  exit (-1);
 580                              }
 581                              elapsedTime.stop();
 582                              _testEnd(uniqueID, elapsedTime.getElapsed());
 583                          }
 584                          catch(Exception  & e)
 585                          {
 586                              cout << e.getMessage() << endl;
 587                          }
 588 kumpf       1.12     
 589                          return ThreadReturnType(0);
 590 yi.zhou     1.1      }
 591                      
 592                      Thread * _runTestThreads(
 593                          CIMClient* client,
 594                          Uint32 indicationSendCount,
 595                          Uint32 uniqueID)
 596                      {
 597                          // package parameters, create thread and run...
 598                          AutoPtr<T_Parms> parms(new T_Parms());
 599                          parms->client.reset(client);
 600                          parms->indicationSendCount = indicationSendCount;
 601                          parms->uniqueID = uniqueID;
 602                          AutoPtr<Thread> t(new Thread(_executeTests, (void*)parms.release(), false));
 603                          t->run();
 604                          return t.release();
 605                      }
 606                      
 607 mike        1.12.4.1 Uint32 _getReceivedTrapCount(Uint16 snmpVersion, const String& logFile)
 608 yi.zhou     1.1      {
 609                          String trap1 = "Trap Info: TRAP, SNMP v1, community public";
 610                          String trap2 = "Trap Info: TRAP2, SNMP v2c, community public";
 611                      
 612                          Uint32 receivedTrap1Count = 0;
 613                          Uint32 receivedTrap2Count = 0;
 614 kumpf       1.7      
 615 mike        1.12.4.1     ifstream ifs(logFile.getCString());
 616 yi.zhou     1.1          if (!ifs)
 617                          {
 618                              return (0);
 619                          }
 620                      
 621                          String line;
 622                          while (GetLine(ifs, line))
 623                          {
 624                              if (String::compare(line, trap1) == 0)
 625                              {
 626                                  receivedTrap1Count++;
 627                              }
 628                              if (String::compare(line, trap2) == 0)
 629                              {
 630                                  receivedTrap2Count++;
 631                              }
 632                          }
 633                      
 634                          ifs.close();
 635                      
 636                          switch (snmpVersion)
 637 yi.zhou     1.1          {
 638                              case _SNMPV1_TRAP:
 639                              {
 640                                  return (receivedTrap1Count);
 641                              }
 642                              case _SNMPV2C_TRAP:
 643                              {
 644                                  return (receivedTrap2Count);
 645                              }
 646                              default:
 647                              {
 648                                  return (0);
 649                              }
 650                          }
 651                      
 652                      }
 653                      
 654                      #ifdef PEGASUS_USE_NET_SNMP
 655 kumpf       1.7      // Stop snmptrapd process if it is running and remove
 656 yi.zhou     1.1      // procIdFile file if it exists
 657 kumpf       1.7      //
 658 mike        1.12.4.1 void _stopSnmptrapd(const String& processIdFile)
 659 yi.zhou     1.1      {
 660                          Uint32 receiverPid;
 661                          FILE *fd;
 662 mike        1.12.4.1     if ((fd = fopen(processIdFile.getCString(), "r")) != NULL)
 663 yi.zhou     1.1          {
 664 kumpf       1.10             if (fscanf(fd, "%d\n", &receiverPid) != 1)
 665                              {
 666                                  throw Exception("Failed to read trapd pid from procIdFile.");
 667                              }
 668 kumpf       1.7      
 669 yi.zhou     1.1              kill(receiverPid, SIGTERM);
 670                      
 671                              fclose(fd);
 672                          }
 673                      
 674 mike        1.12.4.1     if (FileSystem::exists(processIdFile))
 675 yi.zhou     1.1          {
 676 mike        1.12.4.1         FileSystem::removeFile(processIdFile);
 677 yi.zhou     1.1          }
 678                      }
 679                      
 680                      static Boolean _startSnmptrapd(
 681 mike        1.12.4.1     FILE **trapInfo,
 682                          const String& processIdFile,
 683                          const String& logFile)
 684 yi.zhou     1.1      {
 685                          String snmptrapdCmd;
 686                      
 687                          Uint32 portNumber = PORT_NUMBER;
 688                          char portNumberStr[32];
 689                          sprintf(portNumberStr, "%lu", (unsigned long) portNumber);
 690                      
 691                          //
 692                          // build snmptrapd cmd options
 693                          //
 694                      
 695 kumpf       1.7          // Specify logging incoming traps to trapLogFile
 696 yi.zhou     1.1          // Save the process ID of the snmptrapd in procIdFile
 697                          snmptrapdCmd.append(
 698 mike        1.12.4.1         "/usr/sbin/snmptrapd -f -Lf ");
 699                          snmptrapdCmd.append(logFile);
 700                          snmptrapdCmd.append(" -p ");
 701                          snmptrapdCmd.append(processIdFile);
 702 yi.zhou     1.1      
 703                          // Specify incoming trap format
 704                          snmptrapdCmd.append( " -F \"\nTrap Info: %P\nVariable: %v\n\"");
 705                      
 706                          // Specify listening address
 707 yi.zhou     1.8      #if defined(PEGASUS_ENABLE_IPV6)
 708                          snmptrapdCmd.append(" UDP6:");
 709                          snmptrapdCmd.append(portNumberStr);
 710                          snmptrapdCmd.append(",UDP:");
 711                      #else
 712 yi.zhou     1.1          snmptrapdCmd.append(" UDP:");
 713 yi.zhou     1.8      #endif
 714 yi.zhou     1.1          snmptrapdCmd.append(System::getFullyQualifiedHostName ());
 715                      
 716                          snmptrapdCmd.append(":");
 717                          snmptrapdCmd.append(portNumberStr);
 718                      
 719                          if ((*trapInfo = popen(snmptrapdCmd.getCString(), "r")) == NULL)
 720                          {
 721                              throw Exception ("snmptrapd can not be started");
 722                          }
 723                      
 724                      #define MAX_ITERATIONS 300
 725                      #define SLEEP_SEC 1
 726                      
 727                          Uint32 iterations = 0;
 728                      
 729 kumpf       1.7          // Wait until snmptrapd started
 730 yi.zhou     1.1          while (iterations < MAX_ITERATIONS)
 731                          {
 732                              iterations++;
 733 mike        1.12.4.1         if (FileSystem::exists(processIdFile))
 734 yi.zhou     1.1              {
 735                                  return (true);
 736                              }
 737                              else
 738                              {
 739                                  System::sleep(SLEEP_SEC);
 740 kumpf       1.7      
 741 yi.zhou     1.1              }
 742                          }
 743                      
 744                          throw Exception ("snmptrapd can not be started");
 745                      }
 746                      #endif
 747                      
 748 mike        1.12.4.1 void _removeTrapLogFile(const String& logFile)
 749 yi.zhou     1.1      {
 750                          // if trapLogFile exists, remove it
 751                          if (FileSystem::exists(logFile))
 752                          {
 753                              FileSystem::removeFile(logFile);
 754                          }
 755                      }
 756                      
 757 kumpf       1.7      void _receiveExpectedTraps(
 758                          CIMClient& workClient,
 759 yi.zhou     1.1          Uint32 indicationSendCount,
 760 mike        1.12.4.1     Uint32 runClientThreadCount,
 761                          const String& logFile)
 762 yi.zhou     1.1      {
 763 yi.zhou     1.8          Uint32 indicationTrapV1SendCount = 0;
 764                          Uint32 indicationTrapV2SendCount = 0;
 765                      
 766 yi.zhou     1.1          CIMClient * clientConnections = new CIMClient[runClientThreadCount];
 767                      
 768                          // determine total number of indication send count
 769 yi.zhou     1.8          indicationTrapV1SendCount =
 770                              indicationSendCount * runClientThreadCount;
 771                      
 772                          // if IPV6 is enabled, an additional SNMPv2c trap is sent to IPV6 address
 773                      #if defined(PEGASUS_ENABLE_IPV6)
 774                          indicationTrapV2SendCount = 2 * indicationTrapV1SendCount; 
 775                      #else
 776                          indicationTrapV2SendCount = indicationTrapV1SendCount;
 777                      #endif
 778 yi.zhou     1.1      
 779                          // calculate the timeout based on the total send count allowing
 780 kumpf       1.7          // using the MSG_PER_SEC rate
 781                          // allow 20 seconds of test overhead for very small tests
 782 yi.zhou     1.1      
 783                      #define MSG_PER_SEC 4
 784                      
 785 marek       1.5          Uint32 testTimeout = PEGASUS_DEFAULT_CLIENT_TIMEOUT_MILLISECONDS
 786 yi.zhou     1.8                              + (indicationTrapV2SendCount/MSG_PER_SEC)*1000;
 787 yi.zhou     1.1      
 788                          // connect the clients
 789                          for(Uint32 i = 0; i < runClientThreadCount; i++)
 790                          {
 791                              clientConnections[i].setTimeout(testTimeout);
 792                              clientConnections[i].connectLocal();
 793                          }
 794                      
 795                          // run tests
 796                          Thread ** clientThreads = new Thread *[runClientThreadCount];
 797                      
 798                          Stopwatch trapReceiverElapsedTime;
 799                      
 800                          trapReceiverElapsedTime.start();
 801                      
 802                          for(Uint32 i = 0; i < runClientThreadCount; i++)
 803                          {
 804                              clientThreads[i] = _runTestThreads(&clientConnections[i],
 805                                  indicationSendCount, i);
 806                          }
 807                      
 808 yi.zhou     1.1          for(Uint32 i=0; i< runClientThreadCount; i++)
 809                          {
 810                              clientThreads[i]->join();
 811                          }
 812                      
 813                          delete[] clientConnections;
 814                          delete[] clientThreads;
 815                      
 816                          //
 817                          //  Allow time for the trap to be received
 818                          //  Wait in SLEEP_SEC second intervals.
 819                          //  Put msg out every MSG_SEC intervals
 820                          //
 821                      
 822                      #define SLEEP_SEC 1
 823                      #define COUT_TIME_INTERVAL 30
 824                      #define MAX_NO_CHANGE_ITERATIONS COUT_TIME_INTERVAL*3
 825                      
 826                          Uint32 noChangeIterations = 0;
 827                          Uint32 priorReceivedTrap1Count = 0;
 828                          Uint32 priorReceivedTrap2Count = 0;
 829 yi.zhou     1.1          Uint32 currentReceivedTrap1Count = 0;
 830                          Uint32 currentReceivedTrap2Count = 0;
 831                          Uint32 totalIterations = 0;
 832                      
 833                          //
 834                          // Wait for the trap receiver to receive the expected
 835 yi.zhou     1.8          // number of Indication traps, indicationTrapV1SendCount
 836                          // and indicationTrapV2SendCount.
 837 yi.zhou     1.1          //
 838 yi.zhou     1.8          // We will continue to wait until either (indicationTrapV1SendCount
 839                          // and indicationTrapV2SendCount) Indications have been received
 840                          // by the trap receiver or no new
 841 yi.zhou     1.1          // Indications have been received in the previous
 842                          // MAX_NO_CHANGE_ITERATIONS.
 843 kumpf       1.7          // iterations.
 844 yi.zhou     1.1          //
 845                      
 846                          Boolean receivedTrapCountComplete = false;
 847                          Boolean receiverTrap1NoChange = true;
 848                          Boolean receiverTrap2NoChange = true;
 849                      
 850                          while (noChangeIterations <= MAX_NO_CHANGE_ITERATIONS)
 851                          {
 852                              totalIterations++;
 853                      
 854 mike        1.12.4.1         currentReceivedTrap1Count = 
 855                                  _getReceivedTrapCount(_SNMPV1_TRAP, logFile);
 856                              currentReceivedTrap2Count = 
 857                                  _getReceivedTrapCount(_SNMPV2C_TRAP, logFile);
 858 yi.zhou     1.1      
 859                              if (totalIterations % COUT_TIME_INTERVAL == 1 &&
 860                                  !(receivedTrapCountComplete))
 861                              {
 862                                  cout << "++++ The trap receiver has received "
 863                                  << currentReceivedTrap1Count << " of "
 864 yi.zhou     1.8                  << indicationTrapV1SendCount << " SNMPv1 trap."
 865 yi.zhou     1.1                  << endl;
 866                                  cout << "++++ The trap receiver has received "
 867                                  << currentReceivedTrap2Count << " of "
 868 yi.zhou     1.8                  << indicationTrapV2SendCount << " SNMPv2c trap."
 869 yi.zhou     1.1                  << endl;
 870                              }
 871                      
 872 yi.zhou     1.8              if ((indicationTrapV1SendCount == currentReceivedTrap1Count) &&
 873                                  (indicationTrapV2SendCount == currentReceivedTrap2Count))
 874 yi.zhou     1.1              {
 875                                   receivedTrapCountComplete = true;
 876                                   trapReceiverElapsedTime.stop();
 877                              }
 878                              if (!(receiverTrap1NoChange =
 879                                      (priorReceivedTrap1Count == currentReceivedTrap1Count)))
 880                              {
 881                                   priorReceivedTrap1Count = currentReceivedTrap1Count;
 882                              }
 883                      
 884                              if (!(receiverTrap2NoChange =
 885                                      (priorReceivedTrap2Count == currentReceivedTrap2Count)))
 886                              {
 887                                   priorReceivedTrap2Count = currentReceivedTrap2Count;
 888                              }
 889                      
 890                              if (receivedTrapCountComplete)
 891                              {
 892                                  cout << "++++ The trap receiver has received "
 893                                  << currentReceivedTrap1Count << " of "
 894 yi.zhou     1.8                  << indicationTrapV1SendCount << " SNMPv1 trap."
 895 yi.zhou     1.1                  << endl;
 896                                  cout << "++++ The trap receiver has received "
 897                                  << currentReceivedTrap2Count << " of "
 898 yi.zhou     1.8                  << indicationTrapV2SendCount << " SNMPv2c trap."
 899 yi.zhou     1.1                  << endl;
 900                      
 901                                  break;
 902                              }
 903                              if (receiverTrap1NoChange || receiverTrap2NoChange)
 904                              {
 905                                 noChangeIterations++;
 906                              }
 907                              else
 908                              {
 909                                 noChangeIterations = 0;
 910                              }
 911                      
 912                              System::sleep (SLEEP_SEC);
 913                          }
 914                      
 915                          if (!receivedTrapCountComplete)
 916                          {
 917                              trapReceiverElapsedTime.stop();
 918                          }
 919                      
 920 kumpf       1.7          // assert that all indications sent have been received.
 921 yi.zhou     1.8          PEGASUS_TEST_ASSERT(indicationTrapV1SendCount ==
 922 yi.zhou     1.1             currentReceivedTrap1Count);
 923 yi.zhou     1.8          PEGASUS_TEST_ASSERT(indicationTrapV2SendCount ==
 924 yi.zhou     1.1             currentReceivedTrap2Count);
 925 yi.zhou     1.2      }
 926                      
 927 kumpf       1.7      int _beginTest(CIMClient& workClient,
 928 yi.zhou     1.2          Uint32 indicationSendCount,
 929 mike        1.12.4.1     Uint32 runClientThreadCount,
 930                          const String& processIdFile,
 931                          const String& logFile)
 932 yi.zhou     1.2      {
 933 yi.zhou     1.1      
 934                      #ifdef PEGASUS_USE_NET_SNMP
 935 yi.zhou     1.2      
 936                          // Stop snmptrapd process if it is running
 937 mike        1.12.4.1     _stopSnmptrapd(processIdFile);
 938 yi.zhou     1.2      
 939                          // if trapLogFile exists, remove it
 940 mike        1.12.4.1     _removeTrapLogFile(logFile);
 941 yi.zhou     1.2      
 942                          FILE * trapInfo;
 943                      
 944                          try
 945                          {
 946 mike        1.12.4.1         _startSnmptrapd(&trapInfo, processIdFile, logFile);
 947 yi.zhou     1.2          }
 948                          catch (Exception & e)
 949                          {
 950                              cerr << e.getMessage() << endl;
 951                              return (-1);
 952                          }
 953                      
 954                          // Extended for all snmp implementation
 955 kumpf       1.7          _receiveExpectedTraps(workClient, indicationSendCount,
 956 mike        1.12.4.1         runClientThreadCount, logFile);
 957 yi.zhou     1.2      
 958 yi.zhou     1.1          // Stop snmptrapd process if it is running and remove procIdFile
 959 mike        1.12.4.1     _stopSnmptrapd(processIdFile);
 960 yi.zhou     1.1      
 961                          pclose(trapInfo);
 962                      
 963                          // if error encountered then fail the test.
 964                          if (errorsEncountered.get())
 965 kumpf       1.7          {
 966 yi.zhou     1.1              cout << "+++++ test failed" << endl;
 967                              return (-1);
 968                          }
 969                          else
 970                          {
 971                              cout << "+++++ passed all tests" << endl;
 972                          }
 973 yi.zhou     1.2      
 974                          return (0);
 975                      
 976                      #else
 977                          cerr << "Cannot create a trap receiver." << endl;
 978                          return (-1);
 979                      #endif
 980 yi.zhou     1.1      }
 981                      
 982                      int main (int argc, char** argv)
 983                      {
 984                          // This client connection is used solely to create and delete subscriptions.
 985                          CIMClient workClient;
 986                          try
 987                          {
 988                              workClient.connectLocal();
 989                      
 990 mike        1.12.4.1         String processIdFile = TRAP_DIR;
 991                              processIdFile.append("/procIdFile");
 992                      
 993                              String logFile = TRAP_DIR;
 994                              logFile.append("/trapLogFile");
 995                      
 996 yi.zhou     1.1              if (argc <= 1 || argc > 4)
 997                              {
 998                                  cerr << "Invalid argument count: " << argc << endl;
 999                                  _usage();
1000                                  return 1;
1001                              }
1002                              else if (strcmp(argv[1], "setup") == 0)
1003                              {
1004                                  if (argc < 3)
1005                                  {
1006                                      cerr << "Missing query language" << endl;
1007                                      _usage();
1008                                      return -1;
1009                                  }
1010                      
1011                                  if ((strcmp(argv[2], "WQL") != 0) &&
1012                                      (strcmp(argv[2], "DMTF:CQL") != 0))
1013                                  {
1014                                      cerr << "Invalid query language: '" << argv[2] << "'" << endl;
1015                                      _usage();
1016                                      return -1;
1017 yi.zhou     1.1                  }
1018 kumpf       1.7      
1019 yi.zhou     1.1                  _setup(workClient, argv[2]);
1020                      
1021                                  cout << "+++++ setup completed successfully" << endl;
1022                                  return 0;
1023 kumpf       1.7              }
1024 yi.zhou     1.1              else if (String::equalNoCase(argv[1], "run"))
1025                              {
1026                                  if (argc < 3)
1027                                  {
1028                                      cerr << "Invalid indicationSendCount." << endl;
1029                                      _usage ();
1030                                      return -1;
1031                                  }
1032                      
1033                                  Uint32 indicationSendCount = atoi(argv[2]);
1034                      
1035                                  Uint32 runClientThreadCount = 1;
1036                      
1037 kumpf       1.7                  if (argc == 4)
1038 yi.zhou     1.1                  {
1039                                      runClientThreadCount = atoi(argv[3]);
1040                                  }
1041                      
1042 kumpf       1.7                  int rc = _beginTest(workClient, indicationSendCount,
1043 mike        1.12.4.1                 runClientThreadCount, processIdFile, logFile);
1044 yi.zhou     1.1                  return rc;
1045 kumpf       1.7              }
1046 yi.zhou     1.1              else if (String::equalNoCase(argv[1], "cleanup"))
1047                              {
1048                                  if (argc > 2)
1049                                  {
1050                                      cerr << "Invalid argument count." << endl;
1051                                      _usage ();
1052                                      return -1;
1053                                  }
1054 kumpf       1.7      
1055 yi.zhou     1.1                  _cleanup (workClient);
1056                      
1057                                  cout << "+++++ cleanup completed successfully" << endl;
1058                                  return 0;
1059                              }
1060                              else if (String::equalNoCase(argv[1], "removelog"))
1061                              {
1062                                  if (argc > 2)
1063                                  {
1064                                      cerr << "Invalid argument count." << endl;
1065                                      _usage ();
1066                                      return -1;
1067                                  }
1068 kumpf       1.7      
1069 mike        1.12.4.1             _removeTrapLogFile(logFile);
1070 yi.zhou     1.1                  cout << "+++++ removelog completed successfully" << endl;
1071                                  return 0;
1072                              }
1073                              else
1074                              {
1075                                  cerr << "Invalid option: " << argv[1] << endl;
1076                                  _usage ();
1077                                  return -1;
1078                              }
1079                          }
1080                          catch (Exception & e)
1081                          {
1082                              cerr << "Error: " << e.getMessage() << endl;
1083                          }
1084                      
1085 kumpf       1.11         return -1;
1086 yi.zhou     1.1      }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2