//---------------------------------------------------------------------------- // // Copyright (c) Intel Corporation, 2008 - 2011 All Rights Reserved. // // File: CimBase.cs // // Contents: CimBase is a part of the CimFramework project. // It contains an abstract class that all typed Cim // Generated classes inherit from. // //---------------------------------------------------------------------------- using System.Collections.Generic; using System; using System.Collections.ObjectModel; using System.Reflection; using System.Xml; using Intel.Manageability.Cim.Untyped; using Intel.Manageability.Exceptions; using Intel.Manageability.WSManagement; namespace Intel.Manageability.Cim.Typed { using Property = KeyValuePair; /// /// Base Class for all typed CIM classes. /// This class implements the CIM common methods (e.g. - get, delete, create, etc.) /// public abstract class CimBase { #region PRIVATE_PROPERTIES /// /// internal untyped CIM object /// private CimObject cimObject { get; set; } /// /// WsMan Client /// public IWSManClient WSManClient { get { return cimObject.WSManClient; } set { cimObject.WSManClient = value; } } #endregion //PRIVATE_PROPERTIES #region PUBLIC_PROPERTIES /// /// The CIM namespace of the object /// public string XmlNamespace { get { return cimObject.XmlNamespace; } } /// /// Get a CimReference object corresponding to the current object /// public CimReference Reference { get { if (WSManClient == null) throw new CimException("Transport client not initialized"); // Create a new CimReference object and set its values Type myObjectType = this.GetType(); // TODO: change this!!!!! List lst = new List(); String fieldValue = String.Empty; PropertyInfo[] props = myObjectType.GetProperties(); foreach (PropertyInfo propObj in props) { foreach (object attr in Attribute.GetCustomAttributes(propObj)) { if (attr.GetType() == typeof(CimFieldAttribute)) { try { fieldValue = propObj.GetValue(this, null).ToString(); } catch (Exception) { fieldValue = String.Empty; } if ((attr as CimFieldAttribute).IsKey && fieldValue != String.Empty) { if (propObj.PropertyType.Name.Equals("CimReference")) { XmlDocument XmlMembers = new XmlDocument(); XmlMembers.LoadXml(fieldValue); CimReference value = new CimReference("EndpointReference", CimReference.WSMAN_ADDRESSING_NAMESPACE_URI, XmlMembers.DocumentElement.InnerXml); lst.Add(new Intel.Manageability.Cim.Key(propObj.Name, value)); //fieldValue = CimReference.CreateEPR(XmlMembers.DocumentElement.InnerXml, "EndpointReference", WSMAN_ADDRESSING_NAMESPACE_URI); } else { lst.Add(new Intel.Manageability.Cim.Key(propObj.Name, fieldValue)); } } } } } CimReference objRef = new CimReference("EndpointReference", CimReference.WSMAN_ADDRESSING_NAMESPACE_URI, CimReference.WSMAN_ADDRESSING_NAMESPACE_URI, new ReferenceParameters(XmlNamespace, lst)); //objRef.Address = ; //objRef.Name = "EndpointReference"; //objRef.Namespace = XmlNamespace; //objRef.referenceParameters = ; return objRef; } } #endregion //PUBLIC_PROPERTIES #region PRIVATE_METHODS /// /// Get CIM Field Attribute /// /// field name /// CimFieldAttribute object corresponding to the requested field (null if name is not found) private CimFieldAttribute GetFieldAtttribute(string name) { PropertyInfo[] props = this.GetType().GetProperties(); foreach (PropertyInfo propObj in props) { if (propObj.Name == name) { foreach (object attr in Attribute.GetCustomAttributes(propObj, true)) { if (attr.GetType() == typeof(CimFieldAttribute)) { CimFieldAttribute attr1 = attr as CimFieldAttribute; return attr1; } } } } return null; } /// /// Factory method to create a concrete class descended from CimBase /// /// Name of the class to create. /// An instance of the class that was requested. private static CimBase createInstanceByString(string className, string baseClassName) { Type type = Type.GetType(typeof(CimBase).Namespace + "." + className); // In case we can't create the class itself - creating the base class if (type == null) { type = Type.GetType(typeof(CimBase).Namespace + "." + baseClassName); if (type == null) { throw (new CimException(string.Format("Received an unknown class - " + className))); } } CimBase known = (CimBase)Activator.CreateInstance(type); return known; } #endregion // PRIVATE_METHODS #region PROTECTED_METHODS /// /// Constructor that receives a WsMan client /// /// Ws-Management client protected CimBase(IWSManClient client) { cimObject = new CimObject(this.GetType().Name, CimReference.GetResourceUri(this.GetType()).ToString()); cimObject.WSManClient = client; } /// /// Default constructor /// protected CimBase() { cimObject = new CimObject(this.GetType().Name, CimReference.GetResourceUri(this.GetType()).ToString()); cimObject.WSManClient = null; } /// /// Retrieves a field's values /// /// CIM Field name /// An array of the field's values protected string[] GetField(string name) { return cimObject.GetField(name); } /// /// Set CIM Field value /// /// CIM Field name /// CIM Field value protected void SetField(string name, string value) { cimObject.SetField(name, value); } /// /// Set CIM Field value /// /// CIM Field name /// CIM Field value array protected void SetField(string name, string[] value) { cimObject.SetField(name, value); } /// /// Sets an existing field's value, or adds the value for a new field /// /// CIM Field name /// CIM Field value protected void SetOrAddField(string name, string value) { cimObject.SetOrAddField(name, value); } /// /// Set an existing field's values, or adds the value for a new field /// /// CIM Field name /// CIM Field value array protected void SetOrAddField(string name, string[] value) { cimObject.SetOrAddField(name, value); } /// /// Get field value /// /// CIM Field name /// CIM Field value protected string[] this[string name] { get { return GetField(name); } set { SetField(name, value); } } /// /// Add a new CIM field /// /// Field name /// Value of the field protected void AddField(string name, string value) { cimObject.AddField(name, value); } /// /// Add a new CIM Field /// /// Pair of field name and value protected void AddField(KeyValuePair item) { cimObject.AddField(item.Key, item.Value); } /// /// Add a new CIM Field /// /// Pair of field name and an array of values protected void AddField(KeyValuePair item) { cimObject.AddField(item.Key, item.Value); } /// /// Invoke a CIM method /// /// /// /// The name of the method to be invoked /// The input parameters /// The output parameters protected uint Invoke(string methodName, TInput input, out TOutput output) where TInput : CimParams where TOutput : CimParams, new() { CimParams tmpOut; CimObject.CimKeys keys = new CimObject.CimKeys(Reference.GetKeys()); uint returnValue = cimObject.Invoke(methodName, keys, input, out tmpOut); output = new TOutput(); output.Copy(tmpOut); return returnValue; } #endregion //PROTECTED_METHODS #region PUBLIC_METHODS /// /// Checks if the object contains a field /// /// Name of the field /// True if the object contains the field, otherwise false. protected bool ContainsField(string name) { return cimObject.ContainsField(name); } /// /// Removes a field. /// /// CIM Field Name protected void RemoveField(string name) { if (IsKey(name) || IsRequired(name)) throw new CimException("Field " + name + " can not be deleted."); cimObject.RemoveField(name); } /// /// Checks if a given CIM field is a key /// /// Name of CIM field /// True if the given field is key, false otherwise public bool IsKey(string name) { CimFieldAttribute field = GetFieldAtttribute(name); if (field != null && field.IsKey) return true; return false; } /// /// Check if a given CIM field is Required /// /// Name of CIM Field /// True if the given field is required, false otherwise public bool IsRequired(string name) { CimFieldAttribute field = GetFieldAtttribute(name); if (field != null && field.IsRequired) return true; return false; } /// /// Creates an object at an endpoint /// /// CimReference that represents the created object public CimReference Create() { return cimObject.Create(); } /// /// Serialize a CIM object To XML /// /// XML that represents the Cim object public string Serialize() { return cimObject.Serialize(); } /// /// Serialize a CIM object To XML /// /// Inner XML that represents the Cim object public string SerializeInner() { return cimObject.SerializeInner(); } /// /// Deserialize a CIM object From XML /// /// XML that represents the Cim object public void Deserialize(string xml) { cimObject.Deserialize(xml); } /// /// Get an instance of a known CIM class /// /// Reference to an object on an endpoint public void Get(CimReference reference) { //Arguments sanity check if (reference == null) throw new ArgumentNullException("reference"); cimObject.Get(reference); } /// /// Get an instance of a known CIM class /// /// Keys of the object to get public void Get(CimBase.CimKeys keys) { //Arguments sanity check if (keys == null) throw new ArgumentNullException("keys"); cimObject.Get(keys.cimObjectKeys); } /// /// Get an instance of a known CIM class /// public void Get() { cimObject.Get(new CimObject.CimKeys(Reference.GetKeys())); } /// /// /// /// The enumeration mode - Object / Reference / Object and Reference /// The results class name /// The role in the association class of the given object /// The properties should be include in the results /// Collection of pairs - CimBase, CimReference public Collection EnumerateAssosiation( EnumerationOptions.EnumerationMode mode, String resultClassName, String role, String[] includeResultProperty) { EnumerationOptions enumOptions = new EnumerationOptions(); enumOptions.EnumMode = mode; enumOptions.Filter = new AssociationFilter(this.Reference, resultClassName, role, includeResultProperty); return CimBase.Enumerate(WSManClient, enumOptions); } /// /// /// /// The enumeration mode - Object / Reference / Object and Reference /// The results class name /// The role in the association class of the given object /// The properties should be include in the results /// The association class name /// The role in the association class of the result objects /// Collection of pairs - CimBase, CimReference public Collection EnumerateAssosiated( EnumerationOptions.EnumerationMode mode, String resultClassName, String role, String[] includeResultProperty, String associationClassName, String resultRole) { EnumerationOptions enumOptions = new EnumerationOptions(); enumOptions.EnumMode = mode; enumOptions.Filter = new AssociatedFilter(this.Reference, resultClassName, role, includeResultProperty, associationClassName, resultRole); return CimBase.Enumerate(WSManClient, enumOptions); } /// /// Enumerate instances of CimBase class at an endpoint. /// /// WS-Management client /// Enumeration options /// Collection of pairs - CimBase, CimReference public static Collection Enumerate(IWSManClient client, EnumerationOptions options) { if (options == null) throw new ArgumentNullException("options"); if (options.Filter == null) throw new ArgumentNullException("options.Filter"); Collection enumRes = CimObject.Enumerate(client, options); Collection ret = new Collection(); foreach (CimObjectReferencePair pair in enumRes) { CimBase CimBaseObject = null; if (pair.cimObject != null) { CimBaseObject = createInstanceByString(CimData.GetClassNameFromNamespace(pair.cimObject.XmlNamespace), string.Empty); CimBaseObject.cimObject = pair.cimObject; } CimBaseReferencePair CimBaseEpr = new CimBaseReferencePair(CimBaseObject, pair.cimReference); ret.Add(CimBaseEpr); } return ret; } /// /// Enumerate instances of a known CIM class /// /// Cim class concrete type /// WS-Management Client /// Keys of the objects to get /// List of CIM objects protected static List Enumerate(IWSManClient client, CimBase.CimKeys keys) where T : CimBase, new() { if (keys == null) throw new ArgumentNullException("keys"); List ret = new List(); // Perform enumerate (using CimObject) into list of Cim Objects list. Collection cims = CimObject.Enumerate(client, CimReference.GetResourceUri(typeof(T)), keys.cimObjectKeys); // Create foreach Cim object concrete Cim and insert the Cim object as data member foreach (CimObject cim in cims) { T concreteObject = new T(); try { concreteObject = (T)(createInstanceByString(CimData.GetClassNameFromNamespace(cim.XmlNamespace), CimData.GetClassNameFromNamespace(concreteObject.XmlNamespace))); } catch (CimException) { concreteObject = new T(); } concreteObject.cimObject = cim; ret.Add(concreteObject); } return ret; } /// /// Enumerate instances of a known CIM class /// /// Cim class concrete type /// WS-Management Client /// List of CIM objects protected static List Enumerate(IWSManClient client) where T : CimBase, new() { List ret = new List(); // Perform enumerate (using CimObject) into list of Cim objects Collection cims = CimObject.Enumerate(client, CimReference.GetResourceUri(typeof(T))); // Create Cim object for each concrete Cim and insert the Cim object as a data member foreach (CimObject cim in cims) { T concreteObject = new T(); try { concreteObject = (T)(createInstanceByString(CimData.GetClassNameFromNamespace(cim.XmlNamespace), CimData.GetClassNameFromNamespace(concreteObject.XmlNamespace))); } catch (CimException) { concreteObject = new T(); } concreteObject.cimObject = cim; ret.Add(concreteObject); } return ret; } /// /// Updates an object at an endpoint /// public void Put() { cimObject.Put(new CimObject.CimKeys(Reference.GetKeys())); } /// /// Deletes the current object from its endpoint /// public void Delete() { cimObject.Delete(new CimObject.CimKeys(Reference.GetKeys())); } /// /// Deletes an object at an endpoint, specified by the keys and the resourceURI /// /// /// /// Keys of the object to delete public static void Delete(IWSManClient wsmanClient, Uri ResourceURI, CimBase.CimKeys keys) { //Arguments sanity check if (keys == null) throw new ArgumentNullException("keys"); CimObject.Delete(wsmanClient, ResourceURI, keys.cimObjectKeys); } /// /// Deletes an object that is specified by a CimReference /// /// /// A reference to the object to be deleted public static void Delete(IWSManClient wsmanClient, CimReference reference) { CimObject.Delete(wsmanClient, reference); } /// /// Deletes an object that is specified by a ResourceUri /// /// /// IWSManClient protected static void Delete(IWSManClient wsmanClient) where T : CimBase { CimObject.Delete(wsmanClient, CimReference.GetResourceUri(typeof(T))); } /// /// Retrieve a list of the object's fields /// public List Properties { get { return cimObject.Properties; } } #endregion //PUBLIC_METHODS /// /// Generic abstract class that represents the keys of a CIM class. /// This class is inherited by all typed CIM key classes. /// public abstract class CimKeys { /// /// Returns the internal untyped CimObject.CimKeys object. /// internal CimObject.CimKeys cimObjectKeys; /// /// Constructor. /// protected CimKeys() { cimObjectKeys = new CimObject.CimKeys(); } /// /// Retrieves a key's value. /// /// Name of the key /// Value of the key protected string GetKey(string name) { return cimObjectKeys.GetKey(name); } /// /// Set an existing key's value, or add a new key. /// /// Name of the key /// Value of the key protected void SetOrAddKey(string name, Object value) { cimObjectKeys.SetOrAddKey(name, value); } } } /// /// Represents a pair of CimBase object and CimReference. /// public struct CimBaseReferencePair { /// /// /// public CimBase CimBaseObject { set; get; } /// /// /// public CimReference Reference { set; get; } /// /// Constructor /// /// /// public CimBaseReferencePair(CimBase cimBaseObject, CimReference reference) : this() { CimBaseObject = cimBaseObject; Reference = reference; } } }