(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                     sprintf(id_,"%i",id);
 562                     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 yi.zhou     1.1         }
 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 mike        1.3     my_thread->exit_self((ThreadReturnType)1);
 589 yi.zhou     1.1     return(0);
 590                 }
 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                 String _getLogFile()
 608                 {
 609                     return("trapLogFile");
 610 yi.zhou     1.1 }
 611                 
 612                 Uint32 _getReceivedTrapCount(Uint16 snmpVersion)
 613                 {
 614                     String trap1 = "Trap Info: TRAP, SNMP v1, community public";
 615                     String trap2 = "Trap Info: TRAP2, SNMP v2c, community public";
 616                 
 617                     Uint32 receivedTrap1Count = 0;
 618                     Uint32 receivedTrap2Count = 0;
 619 kumpf       1.7 
 620 yi.zhou     1.1     ifstream ifs(_getLogFile().getCString());
 621                     if (!ifs)
 622                     {
 623                         return (0);
 624                     }
 625                 
 626                     String line;
 627                     while (GetLine(ifs, line))
 628                     {
 629                         if (String::compare(line, trap1) == 0)
 630                         {
 631                             receivedTrap1Count++;
 632                         }
 633                         if (String::compare(line, trap2) == 0)
 634                         {
 635                             receivedTrap2Count++;
 636                         }
 637                     }
 638                 
 639                     ifs.close();
 640                 
 641 yi.zhou     1.1     switch (snmpVersion)
 642                     {
 643                         case _SNMPV1_TRAP:
 644                         {
 645                             return (receivedTrap1Count);
 646                         }
 647                         case _SNMPV2C_TRAP:
 648                         {
 649                             return (receivedTrap2Count);
 650                         }
 651                         default:
 652                         {
 653                             return (0);
 654                         }
 655                     }
 656                 
 657                 }
 658                 
 659                 #ifdef PEGASUS_USE_NET_SNMP
 660 kumpf       1.7 // Stop snmptrapd process if it is running and remove
 661 yi.zhou     1.1 // procIdFile file if it exists
 662 kumpf       1.7 //
 663 yi.zhou     1.1 void _stopSnmptrapd()
 664                 {
 665                     String procIdFileName = "procIdFile";
 666                 
 667                     Uint32 receiverPid;
 668                     FILE *fd;
 669                     if ((fd = fopen(procIdFileName.getCString(), "r")) != NULL)
 670                     {
 671 kumpf       1.10         if (fscanf(fd, "%d\n", &receiverPid) != 1)
 672                          {
 673                              throw Exception("Failed to read trapd pid from procIdFile.");
 674                          }
 675 kumpf       1.7  
 676 yi.zhou     1.1          kill(receiverPid, SIGTERM);
 677                  
 678                          fclose(fd);
 679                      }
 680                  
 681                      if (FileSystem::exists(procIdFileName))
 682                      {
 683                          FileSystem::removeFile(procIdFileName);
 684                      }
 685                  }
 686                  
 687                  static Boolean _startSnmptrapd(
 688                      FILE **trapInfo)
 689                  {
 690                      String snmptrapdCmd;
 691                  
 692                      Uint32 portNumber = PORT_NUMBER;
 693                      char portNumberStr[32];
 694                      sprintf(portNumberStr, "%lu", (unsigned long) portNumber);
 695                  
 696                      //
 697 yi.zhou     1.1      // build snmptrapd cmd options
 698                      //
 699                  
 700 kumpf       1.7      // Specify logging incoming traps to trapLogFile
 701 yi.zhou     1.1      // Save the process ID of the snmptrapd in procIdFile
 702                      snmptrapdCmd.append(
 703                          "/usr/sbin/snmptrapd -f -Lf trapLogFile -p procIdFile");
 704                  
 705                      // Specify incoming trap format
 706                      snmptrapdCmd.append( " -F \"\nTrap Info: %P\nVariable: %v\n\"");
 707                  
 708                      // Specify listening address
 709 yi.zhou     1.8  #if defined(PEGASUS_ENABLE_IPV6)
 710                      snmptrapdCmd.append(" UDP6:");
 711                      snmptrapdCmd.append(portNumberStr);
 712                      snmptrapdCmd.append(",UDP:");
 713                  #else
 714 yi.zhou     1.1      snmptrapdCmd.append(" UDP:");
 715 yi.zhou     1.8  #endif
 716 yi.zhou     1.1      snmptrapdCmd.append(System::getFullyQualifiedHostName ());
 717                  
 718                      snmptrapdCmd.append(":");
 719                      snmptrapdCmd.append(portNumberStr);
 720                  
 721                      if ((*trapInfo = popen(snmptrapdCmd.getCString(), "r")) == NULL)
 722                      {
 723                          throw Exception ("snmptrapd can not be started");
 724                      }
 725                  
 726                  #define MAX_ITERATIONS 300
 727                  #define SLEEP_SEC 1
 728                  
 729                      Uint32 iterations = 0;
 730                  
 731 kumpf       1.7      // Wait until snmptrapd started
 732 yi.zhou     1.1      while (iterations < MAX_ITERATIONS)
 733                      {
 734                          iterations++;
 735                          if (FileSystem::exists("procIdFile"))
 736                          {
 737                              return (true);
 738                          }
 739                          else
 740                          {
 741                              System::sleep(SLEEP_SEC);
 742 kumpf       1.7  
 743 yi.zhou     1.1          }
 744                      }
 745                  
 746                      throw Exception ("snmptrapd can not be started");
 747                  }
 748                  #endif
 749                  
 750                  void _removeTrapLogFile ()
 751                  {
 752                      String logFile = _getLogFile();
 753                  
 754                      // if trapLogFile exists, remove it
 755                      if (FileSystem::exists(logFile))
 756                      {
 757                          FileSystem::removeFile(logFile);
 758                      }
 759                  }
 760                  
 761 kumpf       1.7  void _receiveExpectedTraps(
 762                      CIMClient& workClient,
 763 yi.zhou     1.1      Uint32 indicationSendCount,
 764                      Uint32 runClientThreadCount)
 765                  {
 766 yi.zhou     1.8      Uint32 indicationTrapV1SendCount = 0;
 767                      Uint32 indicationTrapV2SendCount = 0;
 768                  
 769 yi.zhou     1.1      CIMClient * clientConnections = new CIMClient[runClientThreadCount];
 770                  
 771                      // determine total number of indication send count
 772 yi.zhou     1.8      indicationTrapV1SendCount =
 773                          indicationSendCount * runClientThreadCount;
 774                  
 775                      // if IPV6 is enabled, an additional SNMPv2c trap is sent to IPV6 address
 776                  #if defined(PEGASUS_ENABLE_IPV6)
 777                      indicationTrapV2SendCount = 2 * indicationTrapV1SendCount; 
 778                  #else
 779                      indicationTrapV2SendCount = indicationTrapV1SendCount;
 780                  #endif
 781 yi.zhou     1.1  
 782                      // calculate the timeout based on the total send count allowing
 783 kumpf       1.7      // using the MSG_PER_SEC rate
 784                      // allow 20 seconds of test overhead for very small tests
 785 yi.zhou     1.1  
 786                  #define MSG_PER_SEC 4
 787                  
 788 marek       1.5      Uint32 testTimeout = PEGASUS_DEFAULT_CLIENT_TIMEOUT_MILLISECONDS
 789 yi.zhou     1.8                          + (indicationTrapV2SendCount/MSG_PER_SEC)*1000;
 790 yi.zhou     1.1  
 791                      // connect the clients
 792                      for(Uint32 i = 0; i < runClientThreadCount; i++)
 793                      {
 794                          clientConnections[i].setTimeout(testTimeout);
 795                          clientConnections[i].connectLocal();
 796                      }
 797                  
 798                      // run tests
 799                      Thread ** clientThreads = new Thread *[runClientThreadCount];
 800                  
 801                      Stopwatch trapReceiverElapsedTime;
 802                  
 803                      trapReceiverElapsedTime.start();
 804                  
 805                      for(Uint32 i = 0; i < runClientThreadCount; i++)
 806                      {
 807                          clientThreads[i] = _runTestThreads(&clientConnections[i],
 808                              indicationSendCount, i);
 809                      }
 810                  
 811 yi.zhou     1.1      for(Uint32 i=0; i< runClientThreadCount; i++)
 812                      {
 813                          clientThreads[i]->join();
 814                      }
 815                  
 816                      delete[] clientConnections;
 817                      delete[] clientThreads;
 818                  
 819                      //
 820                      //  Allow time for the trap to be received
 821                      //  Wait in SLEEP_SEC second intervals.
 822                      //  Put msg out every MSG_SEC intervals
 823                      //
 824                  
 825                  #define SLEEP_SEC 1
 826                  #define COUT_TIME_INTERVAL 30
 827                  #define MAX_NO_CHANGE_ITERATIONS COUT_TIME_INTERVAL*3
 828                  
 829                      Uint32 noChangeIterations = 0;
 830                      Uint32 priorReceivedTrap1Count = 0;
 831                      Uint32 priorReceivedTrap2Count = 0;
 832 yi.zhou     1.1      Uint32 currentReceivedTrap1Count = 0;
 833                      Uint32 currentReceivedTrap2Count = 0;
 834                      Uint32 totalIterations = 0;
 835                  
 836                      //
 837                      // Wait for the trap receiver to receive the expected
 838 yi.zhou     1.8      // number of Indication traps, indicationTrapV1SendCount
 839                      // and indicationTrapV2SendCount.
 840 yi.zhou     1.1      //
 841 yi.zhou     1.8      // We will continue to wait until either (indicationTrapV1SendCount
 842                      // and indicationTrapV2SendCount) Indications have been received
 843                      // by the trap receiver or no new
 844 yi.zhou     1.1      // Indications have been received in the previous
 845                      // MAX_NO_CHANGE_ITERATIONS.
 846 kumpf       1.7      // iterations.
 847 yi.zhou     1.1      //
 848                  
 849                      Boolean receivedTrapCountComplete = false;
 850                      Boolean receiverTrap1NoChange = true;
 851                      Boolean receiverTrap2NoChange = true;
 852                  
 853                      while (noChangeIterations <= MAX_NO_CHANGE_ITERATIONS)
 854                      {
 855                          totalIterations++;
 856                  
 857 kumpf       1.7          currentReceivedTrap1Count = _getReceivedTrapCount(_SNMPV1_TRAP);
 858                          currentReceivedTrap2Count = _getReceivedTrapCount(_SNMPV2C_TRAP);
 859 yi.zhou     1.1  
 860                          if (totalIterations % COUT_TIME_INTERVAL == 1 &&
 861                              !(receivedTrapCountComplete))
 862                          {
 863                              cout << "++++ The trap receiver has received "
 864                              << currentReceivedTrap1Count << " of "
 865 yi.zhou     1.8              << indicationTrapV1SendCount << " SNMPv1 trap."
 866 yi.zhou     1.1              << endl;
 867                              cout << "++++ The trap receiver has received "
 868                              << currentReceivedTrap2Count << " of "
 869 yi.zhou     1.8              << indicationTrapV2SendCount << " SNMPv2c trap."
 870 yi.zhou     1.1              << endl;
 871                          }
 872                  
 873 yi.zhou     1.8          if ((indicationTrapV1SendCount == currentReceivedTrap1Count) &&
 874                              (indicationTrapV2SendCount == currentReceivedTrap2Count))
 875 yi.zhou     1.1          {
 876                               receivedTrapCountComplete = true;
 877                               trapReceiverElapsedTime.stop();
 878                          }
 879                          if (!(receiverTrap1NoChange =
 880                                  (priorReceivedTrap1Count == currentReceivedTrap1Count)))
 881                          {
 882                               priorReceivedTrap1Count = currentReceivedTrap1Count;
 883                          }
 884                  
 885                          if (!(receiverTrap2NoChange =
 886                                  (priorReceivedTrap2Count == currentReceivedTrap2Count)))
 887                          {
 888                               priorReceivedTrap2Count = currentReceivedTrap2Count;
 889                          }
 890                  
 891                          if (receivedTrapCountComplete)
 892                          {
 893                              cout << "++++ The trap receiver has received "
 894                              << currentReceivedTrap1Count << " of "
 895 yi.zhou     1.8              << indicationTrapV1SendCount << " SNMPv1 trap."
 896 yi.zhou     1.1              << endl;
 897                              cout << "++++ The trap receiver has received "
 898                              << currentReceivedTrap2Count << " of "
 899 yi.zhou     1.8              << indicationTrapV2SendCount << " SNMPv2c trap."
 900 yi.zhou     1.1              << endl;
 901                  
 902                              break;
 903                          }
 904                          if (receiverTrap1NoChange || receiverTrap2NoChange)
 905                          {
 906                             noChangeIterations++;
 907                          }
 908                          else
 909                          {
 910                             noChangeIterations = 0;
 911                          }
 912                  
 913                          System::sleep (SLEEP_SEC);
 914                      }
 915                  
 916                      if (!receivedTrapCountComplete)
 917                      {
 918                          trapReceiverElapsedTime.stop();
 919                      }
 920                  
 921 kumpf       1.7      // assert that all indications sent have been received.
 922 yi.zhou     1.8      PEGASUS_TEST_ASSERT(indicationTrapV1SendCount ==
 923 yi.zhou     1.1         currentReceivedTrap1Count);
 924 yi.zhou     1.8      PEGASUS_TEST_ASSERT(indicationTrapV2SendCount ==
 925 yi.zhou     1.1         currentReceivedTrap2Count);
 926 yi.zhou     1.2  }
 927                  
 928 kumpf       1.7  int _beginTest(CIMClient& workClient,
 929 yi.zhou     1.2      Uint32 indicationSendCount,
 930                      Uint32 runClientThreadCount)
 931                  {
 932 yi.zhou     1.1  
 933                  #ifdef PEGASUS_USE_NET_SNMP
 934 yi.zhou     1.2  
 935                      // Stop snmptrapd process if it is running
 936                      _stopSnmptrapd();
 937                  
 938                      // if trapLogFile exists, remove it
 939                      _removeTrapLogFile();
 940                  
 941                      FILE * trapInfo;
 942                  
 943                      try
 944                      {
 945                          _startSnmptrapd(&trapInfo);
 946                      }
 947                      catch (Exception & e)
 948                      {
 949                          cerr << e.getMessage() << endl;
 950                          return (-1);
 951                      }
 952                  
 953                      // Extended for all snmp implementation
 954 kumpf       1.7      _receiveExpectedTraps(workClient, indicationSendCount,
 955                          runClientThreadCount);
 956 yi.zhou     1.2  
 957 yi.zhou     1.1      // Stop snmptrapd process if it is running and remove procIdFile
 958                      _stopSnmptrapd();
 959                  
 960                      pclose(trapInfo);
 961                  
 962                      // if error encountered then fail the test.
 963                      if (errorsEncountered.get())
 964 kumpf       1.7      {
 965 yi.zhou     1.1          cout << "+++++ test failed" << endl;
 966                          return (-1);
 967                      }
 968                      else
 969                      {
 970                          cout << "+++++ passed all tests" << endl;
 971                      }
 972 yi.zhou     1.2  
 973                      return (0);
 974                  
 975                  #else
 976                      cerr << "Cannot create a trap receiver." << endl;
 977                      return (-1);
 978                  #endif
 979 yi.zhou     1.1  }
 980                  
 981                  int main (int argc, char** argv)
 982                  {
 983                      // This client connection is used solely to create and delete subscriptions.
 984                      CIMClient workClient;
 985                      try
 986                      {
 987                          workClient.connectLocal();
 988                  
 989                          if (argc <= 1 || argc > 4)
 990                          {
 991                              cerr << "Invalid argument count: " << argc << endl;
 992                              _usage();
 993                              return 1;
 994                          }
 995                          else if (strcmp(argv[1], "setup") == 0)
 996                          {
 997                              if (argc < 3)
 998                              {
 999                                  cerr << "Missing query language" << endl;
1000 yi.zhou     1.1                  _usage();
1001                                  return -1;
1002                              }
1003                  
1004                              if ((strcmp(argv[2], "WQL") != 0) &&
1005                                  (strcmp(argv[2], "DMTF:CQL") != 0))
1006                              {
1007                                  cerr << "Invalid query language: '" << argv[2] << "'" << endl;
1008                                  _usage();
1009                                  return -1;
1010                              }
1011 kumpf       1.7  
1012 yi.zhou     1.1              _setup(workClient, argv[2]);
1013                  
1014                              cout << "+++++ setup completed successfully" << endl;
1015                              return 0;
1016 kumpf       1.7          }
1017 yi.zhou     1.1          else if (String::equalNoCase(argv[1], "run"))
1018                          {
1019                              if (argc < 3)
1020                              {
1021                                  cerr << "Invalid indicationSendCount." << endl;
1022                                  _usage ();
1023                                  return -1;
1024                              }
1025                  
1026                              Uint32 indicationSendCount = atoi(argv[2]);
1027                  
1028                              Uint32 runClientThreadCount = 1;
1029                  
1030 kumpf       1.7              if (argc == 4)
1031 yi.zhou     1.1              {
1032                                  runClientThreadCount = atoi(argv[3]);
1033                              }
1034                  
1035 kumpf       1.7              int rc = _beginTest(workClient, indicationSendCount,
1036 yi.zhou     1.1                  runClientThreadCount);
1037                              return rc;
1038 kumpf       1.7          }
1039 yi.zhou     1.1          else if (String::equalNoCase(argv[1], "cleanup"))
1040                          {
1041                              if (argc > 2)
1042                              {
1043                                  cerr << "Invalid argument count." << endl;
1044                                  _usage ();
1045                                  return -1;
1046                              }
1047 kumpf       1.7  
1048 yi.zhou     1.1              _cleanup (workClient);
1049                  
1050                              cout << "+++++ cleanup completed successfully" << endl;
1051                              return 0;
1052                          }
1053                          else if (String::equalNoCase(argv[1], "removelog"))
1054                          {
1055                              if (argc > 2)
1056                              {
1057                                  cerr << "Invalid argument count." << endl;
1058                                  _usage ();
1059                                  return -1;
1060                              }
1061 kumpf       1.7  
1062 yi.zhou     1.1              _removeTrapLogFile ();
1063                              cout << "+++++ removelog completed successfully" << endl;
1064                              return 0;
1065                          }
1066                          else
1067                          {
1068                              cerr << "Invalid option: " << argv[1] << endl;
1069                              _usage ();
1070                              return -1;
1071                          }
1072                      }
1073                      catch (Exception & e)
1074                      {
1075                          cerr << "Error: " << e.getMessage() << endl;
1076                      }
1077                  
1078                      PEGASUS_UNREACHABLE( return 0; )
1079                  }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2