//----------------------------------------------------------------------------
//
// Copyright (c) Intel Corporation, 2008 - 2010 All Rights Reserved.
//
// File: IWSManClient.cs
//
// Contents: IWSManClient is a part of the CimFramework project.
// It defines an interface for wsman clients.
//
//----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Net; //.Sockets
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using Intel.Manageability.Cim;
using System.Runtime.InteropServices;
using System.Security;
using Common.Utils;
namespace Intel.Manageability.WSManagement
{
///
/// Interface that defines the WSManClient methods.
///
public interface IWSManClient: IDisposable
{
///
/// Creates a new instance of a resource and returns the URI of the new object.
///
/// Uri, the identifier of the resource to be created
/// XmlElement, the instance data
///
CimReference Create(Uri resourceUri, XmlElement resource);
///
/// Delete the resource specified in the resource URI
/// (Only if one instance of the object exists)
///
/// Uri, the identifier of the resource to be deleted
/// Selectors to id the object to delete
void Delete(Uri resourceUri, IEnumerable selectors);
///
/// Delete the resource specified in the resource URI
/// (Only if one instance of the object exists)
///
/// CimReference to delete
void Delete(CimReference reference);
///
/// Enumerate resource.
///
/// Uri, The identifier of the resource to be retrived
/// Selectors to id the object to enumerate
/// XmlElement array containing the xml response
XmlElement[] Enumerate(Uri resourceUri, IEnumerable selectors);
///
/// Enumerate resource.
///
/// Uri, The identifier of the resource to be retrived
/// Selectors to id the object to enumerate
/// EnumerateOptions, a struct holds special enum options. Case of a NULL regular enumerate will be execute
/// XmlElement array containing the xml response
XmlElement[] Enumerate(Uri resourceUri, IEnumerable selectors, EnumerationOptions options);
///
/// Retrieves the resource specified by 'resource' and returns an XML representation
/// of the current instance of the resource.
///
/// Uri, the identifier of the resource to be retrieved
/// Selectors to id the object to be retrieved
/// XmlElement, an XML of the current instance of the resource.
XmlElement Get(Uri resourceUri, IEnumerable selectors);
///
/// Retrieves the resource specified by 'resource' and returns an XML representation
/// of the current instance of the resource.
///
/// EndpointReference to delete
/// XmlElement, an XML of the current instance of the resource.
XmlElement Get(CimReference resource);
///
/// Invokes a method and returns the results of the method call.
///
/// Uri, The identifier of the resource to be retrived
/// Uri, the action to run
/// The input object for the method
/// The selectors of the object to invoke method
/// Method output
XmlElement Invoke(Uri resourceUri, Uri actionUri, XmlElement request, IEnumerable selectors);
///
/// Update a resource.
///
/// Uri, the identifier of the resource to update
///
/// Selectors to id the object to update
void Put(Uri resourceUri, XmlElement content, IEnumerable selectors);
///
/// Subscribe to specified event.
///
/// Uri, The identifier of the resource to be retrived
/// Class which contains all data regarding the subscription
///
XmlElement Subscribe(SubscribeInfo info);
///
/// Subscribe to specified event.
///
/// Uri, The identifier of the resource to be retrived
/// Class which contains all data regarding the subscription
///
XmlElement Subscribe(SubscribeInfo info, IssuedTokens issuedTokens);
///
/// Unsubscribe to specified event.
///
/// Uri, The identifier of the resource to be retrived
/// The selectors of the object to Unsubscribe method
void Unsubscribe(IEnumerable selectors);
///
/// Represents the connection information.
///
ConnectionInfo Connection { set; get; }
///
///
///
/// XmlElement which represent the identify
XmlElement Identify();
}
#region ENUMERATE_OPTION_CLASS
///
/// EnumerationOptions class.
///
[ComVisible(false)]
public class EnumerationOptions
{
///
/// Filter URI field.
///
public const string FILTER_URI = "Http://schemas.dmtf.org/wbem/wscim/1/*";
///
/// Define the EnumerationMode modes.
///
public enum EnumerationMode
{
///
/// No filter is define.
///
NONE = 0,
///
/// Enumeration reference mode.
///
ENUMERATION_REFERENCE = 2,
///
/// Enumeration object and reference mode.
///
ENUMERATION_OBJ_AND_REFERENCE = 4
};
///
/// EnumerationMode field.
///
public EnumerationMode EnumMode { get; set; }
///
/// Can be created using functions CreateAssosiationFilter / CreateAssosiatedFilter.
///
public IFilter Filter {get; set;}
///
/// Constructor.
///
public EnumerationOptions()
{
EnumMode = EnumerationMode.NONE;
}
}
#endregion ENUMERATE_OPTION_CLASS
#region ENUMERATION_FILTER
///
/// Interface that defines filter.
///
public interface IFilter
{
}
#endregion ENUMERATION_FILTER
#region WS_EVENTING
///
/// Defines four standard delivery modes.
///
[ComVisible(false)]
public enum NotificationDeliveryMode
{
///
/// Push mode for AMT version > 5.0
///
[XmlEnum(Name = "http://schemas.xmlsoap.org/ws/2004/08/eventing/DeliveryModes/Push")]
WSMAN_DELIVERY_PUSH_AMT_GT_5 = 0,
///
/// Push mode for AMT version <= 5.0.
///
[XmlEnum(Name = "http://schemas.dmtf.org/wbem/wsman/1/wsman/Push")]
WSMAN_DELIVERY_PUSH_AMT_LE_5 = 1,
///
/// PushWithAck mode for release 5.0 and above.
///
[XmlEnum(Name = "http://schemas.dmtf.org/wbem/wsman/1/wsman/PushWithAck")]
WSMAN_DELIVERY_PUSHWITHACK = 2
};
///
/// Describe the SubscribeInfo field.
///
[XmlRoot(ElementName = "Subscribe", Namespace = "http://schemas.xmlsoap.org/ws/2004/08/eventing")]
[ComVisible(false)]
public class SubscribeInfo
{
///
/// Constructor.
///
public SubscribeInfo()
{
Delivery = new SubscribeDelivery();
Delivery.NotifyTo = new SubscribeDeliveryNotifyTo();
Filter = new SubscribeFilter();
}
///
/// Keys field.
///
[XmlIgnoreAttribute]
public IEnumerable Keys;
///
/// Delivery field.
///
[XmlElement(ElementName = "Delivery", Namespace = "http://schemas.xmlsoap.org/ws/2004/08/eventing")]
public SubscribeDelivery Delivery { get; set; }
///
/// Expires field.
///
[XmlElement(ElementName = "Expires", Namespace = "http://schemas.xmlsoap.org/ws/2004/08/eventing")]
public float Expires { get; set; }
///
/// Filter field.
///
[XmlElement(ElementName = "Filter", Namespace = "http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd")]
public SubscribeFilter Filter { get; set; }
}
///
/// Describe the Subscribe Delivery.
///
[XmlRoot(ElementName = "Delivery", Namespace = "http://schemas.xmlsoap.org/ws/2004/08/eventing")]
[ComVisible(false)]
public class SubscribeDelivery
{
public SubscribeDelivery()
{
NotifyTo = new SubscribeDeliveryNotifyTo();
}
///
/// Notify To field.
///
[XmlElement(ElementName = "NotifyTo", Namespace = "http://schemas.xmlsoap.org/ws/2004/08/eventing", Order = 0)]
public SubscribeDeliveryNotifyTo NotifyTo { get; set; }
/////
///// Heartbeats field.
/////
//[XmlElement(ElementName = "Heartbeats", Namespace = "http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd")]
//public float Heartbeats { get; set; }
///
/// Mode field.
///
[XmlAttribute(AttributeName = "Mode")]
public NotificationDeliveryMode Mode { get; set; }
///
/// Auth filed
///
[XmlElement(ElementName = "Auth", Namespace = "http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd",Order = 1)]
public Auth Auth;
}
///
/// Class that describe the subscribe delivery.
///
[XmlRoot(ElementName = "NotifyTo", Namespace = "http://schemas.xmlsoap.org/ws/2004/08/eventing")]
[ComVisible(false)]
public class SubscribeDeliveryNotifyTo
{
///
/// Address of the object.
///
[XmlElement(ElementName = "Address", Namespace = "http://schemas.xmlsoap.org/ws/2004/08/addressing")]
public string Address { get; set; }
[XmlElement(ElementName = "ReferenceParameters", Namespace = "http://schemas.xmlsoap.org/ws/2004/08/addressing")]
public XmlDocument ReferenceParameter{ get; set; }
}
///
/// Class that describe the Subscribe Filter.
///
[XmlRoot(ElementName = "Filter", Namespace = "http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd")]
[ComVisible(false)]
public class SubscribeFilter
{
///
/// Dialect field.
///
[XmlAttribute(AttributeName = "Dialect")]
public String Dialect { get; set; }
///
/// Filter value field.
///
[XmlText()]
public String Value { get; set; }
}
//Optional: To request digest authentication when alerts are sent
#region Digest authentication
[XmlRoot(ElementName = "UsernameToken", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
[ComVisible(false)]
public class UsernameToken
{
public UsernameToken()
{
}
public UsernameToken(string username, SecureString password)
{
Username = username;
Password = new Password();
Password.Passwrd = password.ConvertToString();
}
[XmlElement(ElementName = "Username", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public String Username { get; set; }
[XmlElement(ElementName = "Password", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public Password Password { get; set; }
}
[XmlRoot(ElementName = "Password", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
[ComVisible(false)]
public class Password
{
[XmlAttribute("Type")]
public string type = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd#PasswordText";
[XmlText]
public string Passwrd {
get
{
return Passwrd;
}
set
{
Passwrd = value;
}
}
}
[XmlRoot(ElementName = "IssuedTokens", Namespace = "http://schemas.xmlsoap.org/ws/2005/02/trust")]
[ComVisible(false)]
public class IssuedTokens
{
public IssuedTokens()
{
}
public IssuedTokens(string username, SecureString password)
{
RequestSecurityTokenResponse = new RequestSecurityTokenResponse(username, password);
}
[XmlElement(ElementName = "RequestSecurityTokenResponse", Namespace = "http://schemas.xmlsoap.org/ws/2005/02/trust")]
public RequestSecurityTokenResponse RequestSecurityTokenResponse;
}
[XmlRoot(ElementName = "RequestSecurityTokenResponse", Namespace = "http://schemas.xmlsoap.org/ws/2005/02/trust")]
[ComVisible(false)]
public class RequestSecurityTokenResponse
{
public RequestSecurityTokenResponse()
{
}
public RequestSecurityTokenResponse(string username, SecureString password)
{
TokenType = new TokenType();
RequestedSecurityToken = new RequestedSecurityToken(username, password );
}
[XmlElement(ElementName = "TokenType", Namespace = "http://schemas.xmlsoap.org/ws/2005/02/trust")]
public TokenType TokenType;
[XmlElement(ElementName = "RequestedSecurityToken", Namespace = "http://schemas.xmlsoap.org/ws/2005/02/trust")]
public RequestedSecurityToken RequestedSecurityToken;
}
[XmlRoot(ElementName = "RequestedSecurityToken", Namespace = "http://schemas.xmlsoap.org/ws/2005/02/trust")]
[ComVisible(false)]
public class RequestedSecurityToken
{
public RequestedSecurityToken()
{
}
public RequestedSecurityToken(string usename, SecureString password)
{
UsernameToken = new UsernameToken(usename, password);
}
[XmlElement(ElementName = "UsernameToken", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public UsernameToken UsernameToken;
}
[XmlRoot(ElementName = "TokenType", Namespace = "http://schemas.xmlsoap.org/ws/2005/02/trust")]
[ComVisible(false)]
public class TokenType
{
[XmlText]
public string type = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#UsernameToken";
}
[ComVisible(false)]
public class Auth
{
[XmlAttribute("Profile")]
public string Profile = "http://schemas.dmtf.org/wbem/wsman/1/wsman/secprofile/http/digest";
}
#endregion
#endregion WS_EVENTING
///
/// Helper class for easy access to wsman identify properties.
///
[ComVisible(false)]
public class WSIdentify
{
///
/// CIM XML Documention
///
private XmlDocument XmlMembers { get; set; }
const string IDENTIFY_NAMESPACE = "http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd";
const string IDENTIFY_DASHVERSION_NAMESPACE = "http://schemas.dmtf.org/wbem/dash/1/dash.xsd";
///
/// Constructor
///
/// XmlElement which represent WSIdentify class
public WSIdentify(XmlElement element)
{
if (element == null)
throw new ArgumentNullException("element");
XmlMembers = new XmlDocument();
XmlMembers.LoadXml(element.OuterXml);
}
private string[] GetField(string name, string namespaceURI)
{
List resList = new List();
XmlNodeList elemList = XmlMembers.GetElementsByTagName(name, namespaceURI);
foreach (XmlNode xml in elemList)
{
resList.Add(xml.InnerXml);
}
return resList.ToArray();
}
///
/// Return the Protocol Version.
///
public string ProtocolVersion
{
get { return GetField("ProtocolVersion", IDENTIFY_NAMESPACE)[0]; }
}
///
/// Return the Protocol Vendor.
///
public string ProductVendor
{
get { return GetField("ProductVendor", IDENTIFY_NAMESPACE)[0]; }
}
///
/// Return the Product Version.
///
public string ProductVersion
{
get { return GetField("ProductVersion", IDENTIFY_NAMESPACE)[0]; }
}
///
/// Return the DASH Version.
///
public string DASHVersion
{
get { return GetField("DASHVersion", IDENTIFY_DASHVERSION_NAMESPACE)[0]; }
}
///
/// Return the Security Profiles.
///
public string[] SecurityProfiles
{
get { return GetField("SecurityProfileName", IDENTIFY_NAMESPACE); }
}
///
/// Returns a System.String that represents the current WSIdentify Object.
///
/// A System.String that represents the current WSIdentify Object.
public override string ToString()
{
StringBuilder ret = new StringBuilder();
ret.AppendLine("ProtocolVersion: " + ProtocolVersion);
ret.AppendLine("ProductVendor: " + ProductVendor);
ret.AppendLine("ProductVersion: " + ProductVersion);
ret.AppendLine("DASHVersion: " + DASHVersion);
string[] securityProfiles = SecurityProfiles;
ret.AppendLine("SecurityProfiles: ");
foreach (string str in securityProfiles)
{
ret.AppendLine(str);
}
return ret.ToString();
}
}
#region FAULT_STRUCT
///
/// A struct that represents the WS-Management fault data
///
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.w3.org/2003/05/soap-envelope")]
public struct Fault
{
///
/// Class which represent the error code.
///
public Code Code { get; set; }
///
/// Class which represent the reason of the error.
///
public Reason Reason { get; set; }
///
/// Class which represent the details of the error.
///
public Detail Detail { get; set; }
}
///
/// Class which represent the error code.
///
public struct Code
{
///
/// Code value field
///
public String Value { get; set; }
///
/// Subcode field
///
public Subcode Subcode { get; set; }
}
///
/// Class which represent the error sub code.
///
public struct Subcode
{
///
/// Describe the exception subcode value
///
public String Value { get; set; }
}
///
/// Class which represent the reason of the error.
///
public struct Reason
{
///
/// Describe the exception reason
///
//to do: add attribute lange: xml:lang="en-US"
public String Text { get; set; }
}
///
/// Class which represent the details of the error.
///
public struct Detail
{
///
/// Describe the exception detail
///
public String Text { get; set; }
//to do
//CHAR *__item; // [&:0] [stbl:0] [optional] [defaults: l:0 v:0 ]
//struct _SX_soap_TextType *Text; // [&:4] [stbl:833] [optional] [next:53] [defaults: l:0 v:0 ]
//CHAR *wsman_FaultDetail;
}
#endregion FAULT_STRUCT
#region WSMAN_EXCEPTION
//
///
/// Class which is thrown when a Ws-Man error has occured.
///
public class WSManException : WebException
{
///
/// Constructor
///
/// Error details
/// Other exception to be copied
public WSManException(Fault WSManFaultOther, WebException ex)
: base("WSManFault.Code: " + WSManFaultOther.Code.Value + " WSManFault.SubCode: " + WSManFaultOther.Code.Subcode.Value, ex)
{
WSManFault = WSManFaultOther;
}
///
/// Constructor
///
/// Error details
public WSManException(Fault WSManFaultOther)
: base("WSManFault.Code: " + WSManFaultOther.Code.Value + " WSManFault.SubCode: " + WSManFaultOther.Code.Subcode.Value)
{
WSManFault = WSManFaultOther;
}
///
/// Constructor
///
/// Other exception to be copied
public WSManException(WebException ex)
: base(ex.Message, ex)
{ }
///
/// Constructor
///
/// Error message
public WSManException(String msg)
: base("Transport layer error:\n" + msg)
{ }
///
/// Error details
///
public Fault WSManFault
{
get;
set;
}
///
/// Create a string from the exception.
///
/// String that represents the error
public string GetError()
{
StringBuilder ret = new StringBuilder();
if (WSManFault.Reason.Text != null)
ret.Append(this.GetType().Name + ": " + WSManFault.Reason.Text);
Exception inner = this.InnerException;
while (inner != null)
{
ret.Append("\n" + inner.GetType().Name + ": " + inner.Message);
inner = inner.InnerException;
}
return ret.ToString();
}
}
#endregion WSMAN_EXCEPTION
}