311 lines
9.4 KiB
C++
311 lines
9.4 KiB
C++
//----------------------------------------------------------------------------
|
||
//
|
||
// Copyright (C) 2008 Intel Corporation
|
||
//
|
||
// File: WS-EventingFlow.cpp
|
||
//
|
||
// Contents: Api code for Intel(R) Active Management Technology
|
||
// (Intel<65> AMT) WS-Eventing Sample.
|
||
//
|
||
// Notes: This file contains the WS-EventingFlow methods implementation.
|
||
//
|
||
//----------------------------------------------------------------------------
|
||
|
||
#include "WS-EventingFlow.h"
|
||
#include "CIM_FilterCollectionSubscription.h"
|
||
#include "CIM_FilterCollection.h"
|
||
#include "CIM_ListenerDestinationWSManagement.h"
|
||
//#include "CIM_Account.h"
|
||
//#include "AMT_GeneralSettings.h"
|
||
//#include "CIM_PowerManagementService.h"
|
||
#include "CmdLineArguments.h"
|
||
#include "Exception.h"
|
||
#include "AssociationTraversalTypedUtils.h" // Include from CIM Framework
|
||
//#include "CimOpenWsmanClient.h" // Include from CIM Framework
|
||
|
||
using namespace std;
|
||
using namespace WsmanExceptionNamespace;
|
||
using namespace Intel::WSManagement;
|
||
using namespace Intel::Manageability::Cim::Typed;
|
||
|
||
#pragma region CONSTANTS
|
||
|
||
static const string AMT_COPYRIGHTS_STRING = "Intel(r) AMT";
|
||
static const char *DIGEST_REALM = "2";
|
||
static const char *PASSWORD = "Admin!123";
|
||
static const char *SUBSCRIPTION_NAME = "example_subscription";
|
||
static const char *SUBSCRIPTION_DESTINATION = "http://10.1.0.250:16990";
|
||
static const char *SUBSCRIPTION_FILTER = "Intel(r) AMT:AllEvents";
|
||
|
||
#pragma endregion
|
||
|
||
#pragma region MEMBERS
|
||
|
||
unsigned int returnValue;
|
||
CmdLineArguments::Format format;
|
||
vector<shared_ptr<CIM_FilterCollection>> fcVec;
|
||
|
||
#pragma endregion
|
||
|
||
#pragma region METHODS
|
||
|
||
#pragma region PRINT_MESSAGE
|
||
|
||
// Print success message
|
||
void DisplaySuccess()
|
||
{
|
||
format.SetConsoleTextColor(HGREEN);
|
||
cout << "Success" << endl;
|
||
format.SetConsoleTextColor(LGRAY);
|
||
}
|
||
|
||
// Print fail message
|
||
void DisplayFailure()
|
||
{
|
||
format.SetConsoleTextColor(HRED);
|
||
cout << "Failed" << endl;
|
||
format.SetConsoleTextColor(LGRAY);
|
||
}
|
||
|
||
#pragma endregion
|
||
|
||
/*
|
||
* Description: Traverse the CIM_FilterCollection.
|
||
* Arguments:
|
||
* wsmanClient - The client to connect
|
||
*/
|
||
vector<shared_ptr<CIM_FilterCollection>> GetFilterCollection(ICimWsmanClient* wsmanClient)
|
||
{
|
||
if(fcVec.size() == 0)
|
||
{
|
||
CIM_FilterCollection filterCollection;
|
||
fcVec = filterCollection.Enumerate(wsmanClient);
|
||
}
|
||
return fcVec;
|
||
}
|
||
|
||
/*
|
||
* Description: Register all events.
|
||
* Arguments:
|
||
* wsmanClient - The client to connect
|
||
* verbose - Indicates whether to print extended details about the operation
|
||
*/
|
||
void RegisterAllEvents(ICimWsmanClient* wsmanClient, bool verbose)
|
||
{
|
||
cout << endl << "Calling function RegisterAllEvents... ";
|
||
|
||
// Traverse CIM_FilterCollection
|
||
vector<shared_ptr<CIM_FilterCollection>> filterCollection = GetFilterCollection(wsmanClient);
|
||
|
||
int index = -1;
|
||
|
||
for(unsigned int i=0; i < filterCollection.size(); i++)
|
||
{
|
||
if((filterCollection[i].get())->CollectionName().compare(SUBSCRIPTION_FILTER) != 0)
|
||
continue;
|
||
index = i;
|
||
break;
|
||
}
|
||
if(index == -1)
|
||
{
|
||
DisplayFailure();
|
||
cout << "Error: Can't find " << SUBSCRIPTION_FILTER << " filter" << endl;
|
||
return;
|
||
}
|
||
|
||
map<string, string> selectorsExample = filterCollection.at(index)->Reference().Selectors();
|
||
|
||
// Create a SubscribeInfo struct which contains all the subsciption information.
|
||
SubscribeInfo info;
|
||
info.selectorset = &selectorsExample;
|
||
info.delivery_mode = WSMAN_DELIVERY_PUSHWITHACK;
|
||
info.delivery_uri = SUBSCRIPTION_DESTINATION;
|
||
info.heartbeat_interval = 0;
|
||
info.dialect = "http://schemas.dmtf.org/wbem/wsman/1/wsman/SelectorFilter";
|
||
string resourceURI = "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_FilterCollection";
|
||
string identifier = SUBSCRIPTION_NAME;
|
||
try
|
||
{
|
||
wsmanClient->Subscribe(resourceURI, info, identifier);
|
||
DisplaySuccess();
|
||
if(verbose)
|
||
{
|
||
cout << "Subscriber details:" << endl;
|
||
cout << "\tsubscription destination: " << SUBSCRIPTION_DESTINATION << endl;
|
||
cout << "\tsubscription name: " << SUBSCRIPTION_NAME << endl;
|
||
}
|
||
}
|
||
catch(GeneralWsmanException e)
|
||
{
|
||
DisplayFailure();
|
||
if(verbose)
|
||
cout << "Error: Register All Events failed with status " << e.getErr() << endl;
|
||
}
|
||
}//END: function RegisterAllEvents
|
||
|
||
/*
|
||
* Description: Un-Register all events.
|
||
* Arguments:
|
||
* wsmanClient - The client to connect
|
||
* verbose - Indicates whether to print extended details about the operation
|
||
*/
|
||
void UnRegisterAllEvents(ICimWsmanClient* wsmanClient, bool verbose)
|
||
{
|
||
cout << endl << "Calling function UnRegisterAllEvents... ";
|
||
|
||
// Traverse CIM_FilterCollection
|
||
vector<shared_ptr<CIM_FilterCollection>> fcVec = GetFilterCollection(wsmanClient);
|
||
|
||
int index = -1;
|
||
int count = 0;
|
||
vector<shared_ptr<CimBase> > fcsVec;
|
||
|
||
CIM_FilterCollectionSubscription curObj;
|
||
|
||
for(unsigned int ind = 0; ind < fcVec.size(); ind++)
|
||
{
|
||
CIM_FilterCollection fcObj = *((CIM_FilterCollection*)fcVec[ind].get());
|
||
|
||
// Traverse CIM_FilterCollectionSubscription
|
||
fcsVec = AssociationTraversalTypedUtils::EnumerateAssociations(wsmanClient, fcObj.Reference(), "CIM_FilterCollectionSubscription");
|
||
|
||
count += fcsVec.size();
|
||
|
||
for(unsigned int i = 0 ;i < fcsVec.size(); i++)
|
||
{
|
||
CIM_FilterCollectionSubscription fcsObj = *(static_cast<CIM_FilterCollectionSubscription*>(fcsVec[i].get()));
|
||
|
||
CIM_FilterCollection filter(wsmanClient);
|
||
filter.Get(fcsObj.Filter());
|
||
if(filter.CollectionName().compare(SUBSCRIPTION_FILTER))
|
||
continue;
|
||
|
||
CIM_ListenerDestinationWSManagement destination(wsmanClient);
|
||
destination.Get(fcsObj.Handler());
|
||
if(destination.Destination().compare(SUBSCRIPTION_DESTINATION))
|
||
continue;
|
||
|
||
index = i;
|
||
|
||
curObj = fcsObj;
|
||
|
||
break;
|
||
}
|
||
}
|
||
if(count == 0)
|
||
{
|
||
DisplayFailure();
|
||
cout << "Error: no subscriptions exists" << endl;
|
||
return;
|
||
}
|
||
if(index == -1)
|
||
{
|
||
DisplayFailure();
|
||
cout << "Error: does not perform unregister since there are\nno subscribers that were created by this application." << endl;
|
||
return;
|
||
}
|
||
string resourceURI = "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_FilterCollectionSubscription";
|
||
//string resourceURI = "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_FilterCollectionSubscription.xsd";
|
||
string identifier = SUBSCRIPTION_NAME;
|
||
try
|
||
{
|
||
map<string, string> selectors = curObj.Reference().Selectors();
|
||
|
||
wsmanClient->Unsubscribe(resourceURI, identifier, &selectors);
|
||
DisplaySuccess();
|
||
}
|
||
catch(GeneralWsmanException e)
|
||
{
|
||
DisplayFailure();
|
||
if(verbose)
|
||
cout << "Error: Unregister All Events failed with status " << e.getErr() << endl;
|
||
}
|
||
}// END: function UnRegisterAllEvents
|
||
|
||
/*
|
||
* Description: Enumerate all events.
|
||
* Arguments:
|
||
* wsmanClient - The client to connect
|
||
* verbose - Indicates whether to print extended details about the operation
|
||
*/
|
||
void EnumerateSubscribers(ICimWsmanClient* wsmanClient, bool verbose)
|
||
{
|
||
try
|
||
{
|
||
unsigned int index = 1;
|
||
|
||
cout << endl << "Calling function EnumerateSubscribers... ";
|
||
|
||
// Traverse CIM_FilterCollection
|
||
vector<shared_ptr<CIM_FilterCollection> > fcVec = GetFilterCollection(wsmanClient);
|
||
|
||
for(unsigned int ind = 0; ind < fcVec.size(); ind++)
|
||
{
|
||
CIM_FilterCollection fcObj = *(fcVec[ind].get());
|
||
|
||
// Traverse CIM_FilterCollectionSubscription
|
||
vector<shared_ptr<CimBase> > fcsVec = AssociationTraversalTypedUtils::EnumerateAssociations(wsmanClient, fcObj.Reference(), "CIM_FilterCollectionSubscription");
|
||
|
||
vector<tr1::shared_ptr <CimBase> >::iterator itr;
|
||
|
||
for(itr = fcsVec.begin() ;itr != fcsVec.end(); itr++)
|
||
{
|
||
try
|
||
{
|
||
CIM_FilterCollectionSubscription fcsObj = *(static_cast<CIM_FilterCollectionSubscription*>((*itr).get()));
|
||
|
||
CIM_FilterCollection filter(wsmanClient);
|
||
filter.Get(fcsObj.Filter());
|
||
|
||
CIM_ListenerDestinationWSManagement destination(wsmanClient);
|
||
destination.Get(fcsObj.Handler());
|
||
|
||
if(verbose)
|
||
{
|
||
cout << endl << "Subscriber # " << index << endl;
|
||
if(fcsObj.SubscriptionStateExists())
|
||
cout << "\tSubscription State: " << fcsObj.SubscriptionState() << endl;
|
||
if(fcsObj.RepeatNotificationCountExists())
|
||
cout << "\tRepeat Notification Policy Count: " << fcsObj.RepeatNotificationCount() << endl;
|
||
if(fcsObj.OnFatalErrorPolicyExists())
|
||
cout << "\tOn Fatal Error Policy: " << fcsObj.OnFatalErrorPolicy() << endl;
|
||
if(fcsObj.FailureTriggerTimeIntervalExists())
|
||
cout << "\tFailure Trigger Time Interval: " << fcsObj.FailureTriggerTimeInterval() << endl;
|
||
if(filter.CollectionNameExists())
|
||
cout << "\tFilter Name: " << filter.CollectionName() << endl;
|
||
if(filter.DescriptionExists())
|
||
cout << "\tFilter Description: " << filter.Description() << endl;
|
||
if(destination.ElementNameExists())
|
||
cout << "\tDestination Name: " << destination.ElementName() << endl;
|
||
cout << "\tDestination: " << destination.Destination() << endl;
|
||
if(destination.DeliveryModeExists())
|
||
cout << "\tDelivery Mode: " << destination.DeliveryMode() << endl;
|
||
if(destination.ProtocolExists())
|
||
cout << "\tProtocol: " << destination.Protocol() << endl;
|
||
if(destination.PersistenceTypeExists())
|
||
cout << "\tPersistence: " << destination.PersistenceType() << endl;
|
||
cout << endl;
|
||
|
||
index++;
|
||
}
|
||
}
|
||
catch(GeneralWsmanException e)
|
||
{
|
||
cout << "Error: can't retrieve legs of association CIM_FilterCollectionSubscription" << endl;
|
||
}
|
||
}
|
||
}
|
||
if(!verbose)
|
||
{
|
||
DisplaySuccess();
|
||
}
|
||
}
|
||
catch(GeneralWsmanException e)
|
||
{
|
||
DisplayFailure();
|
||
if(verbose)
|
||
cout << "Error: can't enumerate CIM_FilterCollectionSubscription" << endl;
|
||
}
|
||
}//END: function UnRegisterAllEvents
|
||
|
||
#pragma endregion |