//---------------------------------------------------------------------------- // // Copyright (c) Intel Corporation, 2008 - 2009 All Rights Reserved. // // File: CimObject.cs // // Contents: CimObject is a part of the CimFramework project. // It contains a class representing an untyped CIM object. // //---------------------------------------------------------------------------- using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Globalization; using System.Xml; using Intel.Manageability.Exceptions; using Intel.Manageability.WSManagement; namespace Intel.Manageability.Cim.Untyped { using Property = KeyValuePair; /// /// A Struct that represents a pair of CimObject and CimReference /// public struct CimObjectReferencePair { /// /// /// public CimObject cimObject { set; get; } /// /// /// public CimReference cimReference { set; get; } /// /// Constructor /// /// The CimObject parameter /// The CimReference parameter public CimObjectReferencePair(CimObject obj, CimReference reference) : this() { cimObject = obj; cimReference = reference; } } /// /// Class which represent input / output parameters for invoke functions /// public class CimParams { /// /// Cim data field /// public CimData cimDataObject { get; set; } /// /// Cim params field /// /// public CimParams(string xmlNamespace) { cimDataObject = new CimData("tempName", xmlNamespace); } /// /// Serialize the CIM object to XML /// /// XML representing the CIM object public string Serialize() { return cimDataObject.Serialize(); } /// /// Deserialize the CIM object from XML /// /// XML representing the CIM object public void Deserialize(string xml) { cimDataObject.Deserialize(xml); } /// /// Gets a CIM Field /// /// CIM Field name /// Array of the field's values public string[] GetField(string name) { return cimDataObject.GetField(name); } /// /// Sets or adds a CIM Field /// If the field does not exist, the function adds it, otherwise the function sets its values. /// /// CIM Field name /// Field values public virtual void SetOrAddField(string name, string[] values) { cimDataObject.SetOrAddField(name, values); } /// /// Sets or adds a CIM Field /// If the field does not exist, the function adds it, otherwise the function sets its value. /// /// CIM Field name /// Field value public virtual void SetOrAddField(string name, string value) { cimDataObject.SetOrAddField(name, value); } /// /// Copy the CIM data /// /// CIM data to be copied public void Copy(CimParams other) { if (other == null) throw new ArgumentNullException("other"); cimDataObject = other.cimDataObject; } /// /// Checks if a given Field exists in the current CIM object /// /// CIM Field name /// True if the given field exists in the CIM object, otherwise false public bool ContainsField(string name) { return cimDataObject.ContainsField(name); } /// /// Get/Set CIM Field /// /// CIM Field Name /// Array of the field's values public string[] this[string name] { get { return cimDataObject.GetField(name); } set { cimDataObject.SetField(name, value); } } } /// /// Represents an untyped CIM object /// public class CimObject : CimObjectData { /// /// WS-Management client /// public IWSManClient WSManClient { get; set; } /// /// Get / Set the object's class resource URI /// public Uri ResourceURI { get; protected set; } /// /// Get / Set the object's class XmlNamespace /// public string XmlNamespace{get{return _xmlNamespace;}protected set{_xmlNamespace = value;}} /// /// Constructor /// /// Name of the actual class of the object /// Namespace of the object's class /// WS-Management client public CimObject(string className, string nameSpace, IWSManClient client) : base(className, nameSpace) { //Arguments sanity check if (className == null) throw new ArgumentNullException("className"); if (nameSpace == null) throw new ArgumentNullException("nameSpace"); if (client == null) throw new ArgumentNullException("client"); _xmlNamespace = nameSpace; ResourceURI = new Uri(nameSpace); WSManClient = client; } /// /// Constructor /// /// Name of the actual class of the object /// Namespace of the object's class public CimObject(string className, string nameSpace) : base(className, nameSpace) { //Arguments sanity check if (className == null) throw new ArgumentNullException("className"); if (nameSpace == null) throw new ArgumentNullException("nameSpace"); _xmlNamespace = nameSpace; ResourceURI = new Uri(nameSpace); } /// /// Copy constructor /// /// public CimObject(CimObject other): base(other) { //Arguments sanity check if (other == null) throw new ArgumentNullException("other"); ResourceURI = other.ResourceURI; XmlMembers = other.XmlMembers; _xmlNamespace = other._xmlNamespace; } /// /// Add a new CIM Field /// /// Field name /// Field value public void AddField(string name, string value) { AddField(name, new string[1] { value }); } /// /// Add a new CIM Field /// /// KeyValuePair that contains the field's name and value public void AddField(KeyValuePair item) { AddField(item.Key, new string[1] { item.Value }); } /// /// Add a new CIM Field /// /// KeyValuePair that contains the field's name and values public void AddField(KeyValuePair item) { AddField(item.Key, item.Value); } /// /// Set a CIM field's value /// /// Field name /// Field value public void SetField(string name, string value) { SetField(name, new string[1] { value }); } /// /// Sets an existing field's value, or adds the value for a new field /// /// Field name /// Field value public override void SetOrAddField(string name, string value) { SetOrAddField(name, new string[1] { value }); } /// /// Retrieve a list of the object's fields /// public List Properties { get { List res = new List(); XmlNode root = XmlMembers.DocumentElement; IEnumerator ienum = root.GetEnumerator(); XmlNode field; while (ienum.MoveNext()) { field = (XmlNode)ienum.Current; res.Add(new Property(field.LocalName, field.InnerXml)); } return res; } } /// /// Copy constructor /// /// CimObject to be copied public void Copy(CimObject other) { if (other == null) throw new ArgumentNullException("other"); XmlMembers = other.XmlMembers; _xmlNamespace = other._xmlNamespace; WSManClient = other.WSManClient; ResourceURI = other.ResourceURI; } /// /// Create an object from an XML string /// /// WS-Management client /// Resource URI /// XML to deserialize /// Object built from the XML data private static CimObject CreateObject(IWSManClient wsmanClient, Uri resourceUri, string xml) { CimObject obj = new CimObject(GetClassNameFromNamespace(resourceUri.ToString()), resourceUri.ToString()); obj.ResourceURI = resourceUri; obj.Deserialize(xml); obj.WSManClient = wsmanClient; return obj; } /// /// Create an object from an XML string /// /// WS-Management client /// The object's class name /// The object's class namespace /// XML to deserialize /// Object built from the XML data private static CimObject CreateObject(IWSManClient wsmanClient, string className, string namespaceUri, string xml) { CimObject obj = new CimObject(className, namespaceUri); obj.ResourceURI = new Uri(namespaceUri); obj.Deserialize(xml); obj.WSManClient = wsmanClient; return obj; } /// /// Get an instance of a CIM class /// public void Get() { if (WSManClient == null) throw new CimException("Transport client not initialized"); string xml = (WSManClient.Get(ResourceURI, null)).OuterXml; // get the updated instance CimObject obj = CreateObject(WSManClient, ResourceURI, xml); // Save the data Copy(obj); } /// /// Get an instance of a CIM class /// /// Keys of the object to get /// CIM object public void Get(CimObject.CimKeys keys) { //Arguments sanity check if (keys == null) throw new ArgumentNullException("selectors"); if (WSManClient == null) throw new CimException("Transport client not initialized"); string xml = (WSManClient.Get(ResourceURI, keys.cimKeys)).OuterXml; // get the updated instance CimObject obj = CreateObject(WSManClient, ResourceURI, xml); // Save the data Copy(obj); } /// /// Get an instance of a CIM class /// /// Reference to a CIM object /// CIM object public void Get(CimReference reference) { //Arguments sanity check if (reference == null) throw new ArgumentNullException("reference"); if (WSManClient == null) throw new CimException("Transport client not initialized"); string xml = (WSManClient.Get(reference)).OuterXml; // get the updated instance CimObject obj = CreateObject(WSManClient, ResourceURI, xml); // Save the data Copy(obj); } /// /// Enumerate instances of a CIM class /// /// WS-Management client /// CIM class resource URI /// Enumeration options /// Collection of pairs - CimObject, CimReference public static Collection Enumerate(IWSManClient wsmanClient, EnumerationOptions options) { if (wsmanClient == null) throw new ArgumentNullException("wsmanClient"); if (options == null) throw new ArgumentNullException("options"); Uri resourceUri = new Uri(EnumerationOptions.FILTER_URI); Collection enumRes = new Collection(); XmlElement[] xmlRes; xmlRes = wsmanClient.Enumerate(resourceUri, null, options); switch (options.EnumMode) { case EnumerationOptions.EnumerationMode.ENUMERATION_REFERENCE: for (int i = 0; i < xmlRes.Length; i++) { String str = xmlRes[i].OuterXml; CimReference reference = CimReference.Deserialize(str); CimObjectReferencePair pair = new CimObjectReferencePair(null, reference); enumRes.Add(pair); } break; case EnumerationOptions.EnumerationMode.ENUMERATION_OBJ_AND_REFERENCE: for (int i = 0; i < xmlRes.Length; i++) { // the response is pairs of objects & references that are related to this object String CimLateStr = xmlRes[i].FirstChild.OuterXml; String eprStr = xmlRes[i].LastChild.OuterXml; string namespaceUri = xmlRes[i].FirstChild.NamespaceURI; string objName = xmlRes[i].FirstChild.LocalName; CimReference reference = CimReference.Deserialize(eprStr); CimObjectReferencePair pair = new CimObjectReferencePair(CreateObject(wsmanClient, objName, namespaceUri, CimLateStr), reference); enumRes.Add(pair); } break; default: // EnumerationOptions.EnumerationMode.NONE for (int i = 0; i < xmlRes.Length; i++) { String str = xmlRes[i].OuterXml; string namespaceUri = xmlRes[i].NamespaceURI; string objName = xmlRes[i].LocalName; CimObjectReferencePair pair = new CimObjectReferencePair(CreateObject(wsmanClient, objName, namespaceUri, str), null); enumRes.Add(pair); } break; }; return enumRes; } /// /// Enumerate Cim objects. /// /// WS-MAN client /// resourceUri of the class to enumerate /// keys of the class to enumerate /// public static Collection Enumerate(IWSManClient wsmanClient, Uri resourceUri, CimObject.CimKeys keys) { if (wsmanClient == null) throw new ArgumentNullException("wsmanClient"); if (keys == null) throw new ArgumentNullException("selectors"); Collection enumRes = new Collection(); XmlElement[] xmlRes; //Create a Enumeration options with selector filter EnumerationOptions enumOption = new EnumerationOptions(); enumOption.Filter = new SelectorFilter(keys.cimKeys); enumOption.EnumMode = EnumerationOptions.EnumerationMode.NONE; xmlRes = wsmanClient.Enumerate(resourceUri, keys.cimKeys); for (int i = 0; i < xmlRes.Length; i++) { String str = xmlRes[i].OuterXml; enumRes.Add(CreateObject(wsmanClient, resourceUri, str)); } return enumRes; } /// /// Enumerate Cim objects. /// /// WS-MAN client /// resourceUri of the class to enumerate /// public static Collection Enumerate(IWSManClient wsmanClient, Uri resourceUri) { if (wsmanClient == null) throw new ArgumentNullException("wsmanClient"); Collection enumRes = new Collection(); XmlElement[] xmlRes; xmlRes = wsmanClient.Enumerate(resourceUri, null); for (int i = 0; i < xmlRes.Length; i++) { String str = xmlRes[i].OuterXml; enumRes.Add(CreateObject(wsmanClient, resourceUri, str)); } return enumRes; } /// /// Deletes an object at an endpoint, specified by the keys /// /// Keys of the object to delete public void Delete(CimObject.CimKeys keys) { if (WSManClient == null) throw new CimException("Transport client not initialized"); //Arguments sanity check if (keys == null) throw new ArgumentNullException("keys"); WSManClient.Delete(ResourceURI, keys.cimKeys); } /// /// Deletes an object that is specified by a CimReference /// /// WS-Management client /// A reference to the object to be deleted public static void Delete(IWSManClient wsmanClient, CimReference reference) { //Arguments sanity check if (wsmanClient == null) throw new ArgumentNullException("wsmanClient"); if (reference == null) throw new ArgumentNullException("reference"); wsmanClient.Delete(reference); } /// /// Deletes an object at an endpoint, specified by the keys and the resourceURI /// /// WS-Management client /// CIM class resource URI /// Keys of the object to delete public static void Delete(IWSManClient wsmanClient, Uri resourceUri, CimObject.CimKeys keys) { //Arguments sanity check if (wsmanClient == null) throw new ArgumentNullException("wsmanClient"); if (resourceUri == null) throw new ArgumentNullException("resourceUri"); if (keys == null) throw new ArgumentNullException("keys"); wsmanClient.Delete(resourceUri, keys.cimKeys); } /// /// Deletes an object that is specified by a ResourceUri /// This method will succeed only if there is a single instance of this type at the endpoint /// /// WS-Management client /// CIM class resource URI public static void Delete(IWSManClient wsmanClient, Uri resourceUri) { //Arguments sanity check if (wsmanClient == null) throw new ArgumentNullException("wsmanClient"); if (resourceUri == null) throw new ArgumentNullException("resourceUri"); wsmanClient.Delete(resourceUri, null); } /// /// Updates an object at an endpoint /// /// Keys of the object to delete public void Put(CimObject.CimKeys keys) { //Arguments sanity check if (WSManClient == null) throw new CimException("Transport client not initialized"); if (keys == null) throw new ArgumentNullException("keys"); WSManClient.Put(ResourceURI, XmlMembers.DocumentElement, keys.cimKeys); } /// /// Creates an object at an endpoint /// /// CimReference that represents the created object public CimReference Create() { if (WSManClient == null) throw new CimException("Transport client not initialized"); CimReference refOut = WSManClient.Create(ResourceURI, XmlMembers.DocumentElement); return refOut; } /// /// Invoke a CIM method /// /// The name of the method to be invoked /// Keys of the object on which to invoke a method /// The input parameters /// The output parameters public uint Invoke(string methodName, CimObject.CimKeys keys, CimParams input, out CimParams output) { if (WSManClient == null) throw new CimException("Transport client not initialized"); //Arguments sanity check if (input == null) throw new ArgumentNullException("input"); if (keys == null) throw new ArgumentNullException("keys"); uint returnValue = 0; Uri methodNameUri = new Uri(ResourceURI.AbsoluteUri + "/" + methodName); // TODO: create XML with outer tag of methodName + _input XmlDocument inputXML = new XmlDocument(); inputXML.LoadXml(input.Serialize()); string temp = inputXML.InnerText; XmlElement root = XmlMembers.DocumentElement; root = XmlMembers.CreateElement(methodName + "_INPUT", ResourceURI.AbsoluteUri); root.InnerXml = inputXML.DocumentElement.InnerXml; string res = (WSManClient.Invoke(ResourceURI, methodNameUri, root, keys.cimKeys)).OuterXml; output = null; if (res != null) { output = new CimParams(ResourceURI.AbsoluteUri); output.Deserialize(res); returnValue = uint.Parse(output.GetField("ReturnValue")[0], CultureInfo.InvariantCulture); } return returnValue; } /// /// Generic class that represents the keys of a CIM class. /// public class CimKeys { /// /// A list containing the keys (pairs of name and value) /// public List cimKeys { get; private set; } /// /// Constructor /// public CimKeys() { cimKeys = new List(); } /// /// Constructor /// /// List of keys public CimKeys(List keysList) { cimKeys = new List(); cimKeys = keysList; } /// /// Set an existing key's value, or add a new key. /// /// Name of the key /// Value of the key public void SetOrAddKey(string name, Object value) { int index = getIndex(name); Key k = new Key(name, value); if (index == -1) { cimKeys.Add(k); } else { cimKeys[index] = k; } } /// /// Retrieves a key's value. /// /// Name of the key /// Value of the key public string GetKey(string name) { if (name == null) throw new ArgumentNullException("name"); int index = getIndex(name); if (index == -1) { throw new CimPropertyException("Key with the name " + name + " does not exist"); } else { return cimKeys[index].Value; } } private int getIndex(string name) { int i; for (i = 0; i < cimKeys.Count; i++) { if (cimKeys[i].Name.Equals(name)) { break; } } if (i == cimKeys.Count) return -1; return i; } } } }