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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2