Example #1
0
    def config_resolve_children(self,
                                class_id,
                                in_dn,
                                in_hierarchical=YesOrNo.FALSE,
                                dump_xml=None):
        """ Auto-generated IMC XML API Method. """
        method = ExternalMethod("ConfigResolveChildren")

        meta_class_id = CoreUtils.find_class_id_in_mo_meta_ignore_case(
            class_id)
        if meta_class_id != None:
            class_id = ImcUtils.word_l(meta_class_id)
        else:
            class_id = ImcUtils.word_l(class_id)
        method.ClassId = class_id
        method.Cookie = self.__cookie
        method.InDn = in_dn
        method.InHierarchical = ((
            "false", "true")[in_hierarchical in ImcUtils.AFFIRMATIVE_LIST])

        response = self.xml_query(method, WriteXmlOption.DIRTY, dump_xml)

        if response != None:
            return response
        return None
Example #2
0
    def write_xml(self, xml_doc=None, option=None, element_name=None):
        """ Method writes the XML representation of the external method object. """
        if xml_doc is None:
            xml_obj = Element(self.prop_mo_meta.xml_attribute)
        else:
            if element_name == None:
                xml_obj = SubElement(xml_doc, self.prop_mo_meta.xml_attribute)
            else:
                xml_obj = SubElement(xml_doc, element_name)
        
        for prop in CoreUtils.get_property_list(self._class_id):
            prop_meta = CoreUtils.get_method_property_meta(self._class_id, prop)
            if prop_meta.io == "Output":
                continue
            if prop_meta.is_complex_type:
                if self.get_attr(prop) != None:
                    self.__dict__[prop].write_xml(xml_obj, option, ImcUtils.word_l(prop))
                    #xml_obj.append(self.__dict__[prop].write_xml(xml_obj, option, ImcUtils.word_l(prop)))
                    #print ET.tostring(xml_obj)
            elif self.get_attr(prop) != None:
                xml_obj.set(prop_meta.xml_attribute, self.get_attr(prop))
                
        self.child_write_xml(xml_obj, option)
#        x_child = self.child_write_xml(xml_obj, option)
#        for xchild in x_child:
#            if xchild != None:
#                xml_obj.append(xchild)
        return xml_obj
Example #3
0
    def write_xml(self, xml_doc=None, option=None, element_name=None):
        """ Method writes the xml representation of the managed object. """
        if option == WriteXmlOption.DIRTY and not self.is_dirty():
            return
        if xml_doc is None:
            xml_obj = Element(self.prop_mo_meta.xml_attribute)
        else:
            if element_name == None:
                xml_obj = SubElement(xml_doc, self.prop_mo_meta.xml_attribute)
            else:
                xml_obj = SubElement(xml_doc, element_name)
        if CoreUtils.find_class_id_in_mo_meta_ignore_case(self._class_id) != None:
            for prop in CoreUtils.get_property_list(self._class_id):
                prop_meta = CoreUtils.get_mo_property_meta(self._class_id, prop)
                #if (at_meta.access == MoPropertyMeta.Internal):
            #        continue
                #elif ((option != WriteXmlOption.DIRTY) or ((at_meta.mask != None) and (self._dirty_mask & at_meta.mask) != 0)):
                if option != WriteXmlOption.DIRTY or (prop_meta.mask != None and (self._dirty_mask & prop_meta.mask) != 0):
                    if self.get_attr(prop) != None:
                        #xml_obj.setAttribute(prop_meta.xml_attribute, self.get_attr(prop))
                        xml_obj.set(prop_meta.xml_attribute,self.get_attr(prop))
        #Adding xtraProperties from object into Xml query document
        for xtra_prop in self.__xtra_property:
            #xml_obj.setAttribute(ImcUtils.word_l(xtra_prop), self.__xtra_property[xtra_prop])
            xml_obj.set(ImcUtils.word_l(xtra_prop),self.__xtra_property[xtra_prop])
        self.child_write_xml(xml_obj, option)
#        x_child = self.child_write_xml(xml_obj, option)
#        for xchild in x_child:
#            if xchild != None:
#                xml_obj.append(xchild)
        return xml_obj
Example #4
0
    def write_xml(self, xml_doc=None, option=None, element_name=None):
        """create Xml Document Node using object attributes"""
        if xml_doc is None:
            xml_obj = Element(self._class_id)
        else:
            if element_name == None:
                xml_obj = SubElement(xml_doc, self._class_id)
            else:
                xml_obj = SubElement(xml_doc, element_name)


#        if element_name == None:
#            xml_obj = xml_doc.createElement(self._class_id)
#        else:
#            xml_obj = xml_doc.createElement(element_name)

        for prop in self.__dict__['properties']:
            xml_obj.setAttribute(ImcUtils.word_l(prop),
                                 self.__dict__['properties'][prop])
        self.child_write_xml(xml_obj, option)
        #        x_child = self.child_write_xml(xml_doc, option)
        #        for xchild in x_child:
        #            if xchild != None:
        #                xml_obj.appendChild(xchild)
        return xml_obj
Example #5
0
    def write_xml(self, xml_doc=None, option=None, element_name=None):
        """ Method writes the XML representation of the external method object. """
        if xml_doc is None:
            xml_obj = Element(self.prop_mo_meta.xml_attribute)
        else:
            if element_name == None:
                xml_obj = SubElement(xml_doc, self.prop_mo_meta.xml_attribute)
            else:
                xml_obj = SubElement(xml_doc, element_name)

        for prop in CoreUtils.get_property_list(self._class_id):
            prop_meta = CoreUtils.get_method_property_meta(
                self._class_id, prop)
            if prop_meta.io == "Output":
                continue
            if prop_meta.is_complex_type:
                if self.get_attr(prop) != None:
                    self.__dict__[prop].write_xml(xml_obj, option,
                                                  ImcUtils.word_l(prop))
                    #xml_obj.append(self.__dict__[prop].write_xml(xml_obj, option, ImcUtils.word_l(prop)))
                    #print ET.tostring(xml_obj)
            elif self.get_attr(prop) != None:
                xml_obj.set(prop_meta.xml_attribute, self.get_attr(prop))

        self.child_write_xml(xml_obj, option)
        #        x_child = self.child_write_xml(xml_obj, option)
        #        for xchild in x_child:
        #            if xchild != None:
        #                xml_obj.append(xchild)
        return xml_obj
Example #6
0
    def config_resolve_class(self, class_id, in_hierarchical=YesOrNo.FALSE, dump_xml=None):
        """ Auto-generated IMC XML API Method. """
        method = ExternalMethod("ConfigResolveClass")

        meta_class_id = CoreUtils.find_class_id_in_mo_meta_ignore_case(class_id)
        if meta_class_id != None:
            class_id = ImcUtils.word_l(meta_class_id)
        else:
            class_id = ImcUtils.word_l(class_id)
        method.ClassId = class_id
        method.Cookie = self.__cookie
        method.InHierarchical = (("false", "true")[in_hierarchical in ImcUtils.AFFIRMATIVE_LIST])

        response = self.xml_query(method, WriteXmlOption.DIRTY, dump_xml)

        if response != None:
            return response
        return None
Example #7
0
    def write_xml(self, xml_doc=None, option=None, element_name=None):
        """create Xml Document Node using object attributes"""
        if xml_doc is None:
            xml_obj=Element(self._class_id)
        else:
            if element_name == None:
                xml_obj = SubElement(xml_doc,self._class_id)
            else:
                xml_obj = SubElement(xml_doc,element_name)
                               
#        if element_name == None:
#            xml_obj = xml_doc.createElement(self._class_id)
#        else:
#            xml_obj = xml_doc.createElement(element_name)

        for prop in self.__dict__['properties']:
            xml_obj.setAttribute(ImcUtils.word_l(prop), self.__dict__['properties'][prop])
        self.child_write_xml(xml_obj, option)
#        x_child = self.child_write_xml(xml_doc, option)
#        for xchild in x_child:
#            if xchild != None:
#                xml_obj.appendChild(xchild)
        return xml_obj
Example #8
0
 def write_xml(self, xml_doc=None, option=None, element_name=None):
     """ Method writes the xml representation of the managed object. """
     if option == WriteXmlOption.DIRTY and not self.is_dirty():
         return
     if xml_doc is None:
         xml_obj = Element(self.prop_mo_meta.xml_attribute)
     else:
         if element_name == None:
             xml_obj = SubElement(xml_doc, self.prop_mo_meta.xml_attribute)
         else:
             xml_obj = SubElement(xml_doc, element_name)
     if CoreUtils.find_class_id_in_mo_meta_ignore_case(
             self._class_id) != None:
         for prop in CoreUtils.get_property_list(self._class_id):
             prop_meta = CoreUtils.get_mo_property_meta(
                 self._class_id, prop)
             #if (at_meta.access == MoPropertyMeta.Internal):
             #        continue
             #elif ((option != WriteXmlOption.DIRTY) or ((at_meta.mask != None) and (self._dirty_mask & at_meta.mask) != 0)):
             if option != WriteXmlOption.DIRTY or (
                     prop_meta.mask != None and
                 (self._dirty_mask & prop_meta.mask) != 0):
                 if self.get_attr(prop) != None:
                     #xml_obj.setAttribute(prop_meta.xml_attribute, self.get_attr(prop))
                     xml_obj.set(prop_meta.xml_attribute,
                                 self.get_attr(prop))
     #Adding xtraProperties from object into Xml query document
     for xtra_prop in self.__xtra_property:
         #xml_obj.setAttribute(ImcUtils.word_l(xtra_prop), self.__xtra_property[xtra_prop])
         xml_obj.set(ImcUtils.word_l(xtra_prop),
                     self.__xtra_property[xtra_prop])
     self.child_write_xml(xml_obj, option)
     #        x_child = self.child_write_xml(xml_obj, option)
     #        for xchild in x_child:
     #            if xchild != None:
     #                xml_obj.append(xchild)
     return xml_obj
Example #9
0
    def set_imc_managedobject(self,
                              in_mo,
                              class_id=None,
                              params=None,
                              dump_xml=None):
        """
        Modifies Managed Object in IMC.

        - in_mo, if provided, it acts as the target object for the present operation.
          It should be None unless a username wants to provide an in_mo.
          It can be a single MO or a list containing multiple managed objects.
        - class_id of the managed object/s to be removed.
        - params contains semicolon (;) separated list of key/value pairs(key=value),
          that are used as filters for selecting specific managed objects.
          The key should be a valid property of the managed object to be modified.
        """

        #unknown_mo = False
        dn = None
        obj = None
        config_map = None
        dn_param_set = False

        if params != None:
            keys = params.keys()
        else:
            keys = []

        for key in keys:
            if key.lower() == "dn":
                # ClassId And dn Specified - No Parent Necessary
                dn_param_set = True
                dn = params[key]

        if in_mo == None or not isinstance(in_mo, list) or len(in_mo) == 0:
            if not dn_param_set:
                if class_id == None or class_id == "":
                    raise ImcValidationException(
                        '[Error]: set_imc_managedobject [Description]: in_mo and ClassId are both not specified'
                    )
                else:
                    raise ImcValidationException(
                        '[Error]: set_imc_managedobject [Description]: in_mo and dn are both not specified'
                    )
            else:
                if class_id == None or class_id == "":
                    raise ImcValidationException(
                        '[Error]: set_imc_managedobject [Description]: in_mo and ClassId are both not specified'
                    )
                else:
                    meta_class_id = CoreUtils.find_class_id_in_mo_meta_ignore_case(
                        class_id)
                    if meta_class_id != None:
                        class_id = meta_class_id
                    #    mo_meta = CoreUtils.get_mo_property_meta(class_id, "Meta")
                    #else:
                    #    unknown_mo = True

                    obj = ManagedObject(class_id)
                    for prop in keys:
                        prop_mo_meta = CoreUtils.is_property_in_meta_ignore_case(
                            class_id, prop)
                        if prop_mo_meta != None:
                            if prop.lower() == "rn" or prop.lower() == "dn":
                                pass
                            elif prop_mo_meta.access == MoPropertyMeta.READ_ONLY:
                                ImcUtils.write_imc_warning(
                                    "[Warning]: SetManagedObject [Description] Attempt to set non-writable property %s in Class %s"
                                    % (prop, class_id))

                            obj.set_attr(prop_mo_meta.name, str(params[prop]))
                        else:
                            #Sets the unknown property/value as Xtra_property in obj
                            obj.set_attr(ImcUtils.word_l(prop),
                                         str(params[prop]))

                    obj.set_attr("Dn", dn)
                    obj.set_attr("Status", Status().MODIFIED)
                    pair = Pair()
                    pair.set_attr("Key", obj.get_attr("Dn"))
                    pair.add_child(obj)
                    config_map = ConfigMap()
                    config_map.add_child(pair)

        else:
            if class_id != None and class_id != "":
                ImcUtils.write_imc_warning(
                    "[Warning]: SetManagedObject [Description] ClassId <%s> is ignored with InMo input"
                    % (class_id))

            config_map = ConfigMap()
            for mo in in_mo:
                obj = ManagedObject(mo.prop_mo_meta.name)
                dn = mo.get_attr("Dn")
                class_id = mo.prop_mo_meta.name

                for prop in keys:
                    prop_mo_meta = CoreUtils.is_property_in_meta_ignore_case(
                        class_id, prop)
                    if prop_mo_meta != None:
                        if prop.lower() == "rn" or prop.lower() == "dn":
                            pass
                        elif prop_mo_meta.access == MoPropertyMeta.READ_ONLY:
                            ImcUtils.write_imc_warning(
                                "[Warning]: SetManagedObject [Description] Attempt to set non-writeable property %s in Class %s"
                                % (prop, class_id))

                        obj.set_attr(prop_mo_meta.name, str(params[prop]))
                    else:
                        #Sets the unknown property/value as Xtra_property in obj
                        obj.set_attr(ImcUtils.word_l(prop), str(params[prop]))

                obj.set_attr("Dn", dn)
                obj.set_attr("Status", Status.MODIFIED)
                pair = Pair()
                pair.set_attr("Key", obj.get_attr("Dn"))
                pair.add_child(obj)
                config_map.add_child(pair)

        output_molist = []
        for pair in config_map.child:
            in_config = ConfigConfig()
            for mo in pair.child:
                in_config.add_child(mo)

            ccm = self.config_conf_mo(dn=pair.Key,
                                      in_config=in_config,
                                      in_hierarchical=YesOrNo.FALSE,
                                      dump_xml=dump_xml)

            if ccm.error_code == 0:
                molist = []
                for child in ccm.OutConfig.child:
                    if isinstance(child, Pair) == True:
                        for mo in child.child:
                            molist.append(mo)
                    elif isinstance(child, ManagedObject) == True:
                        molist.append(child)
                output_molist.extend(molist)
            else:
                raise ImcException(ccm.error_code, ccm.error_descr)

        return output_molist
Example #10
0
    def add_imc_managedobject(self,
                              in_mo=None,
                              class_id=None,
                              params=None,
                              dump_xml=None):
        """
        Adds a Managed Object to IMC.

        - in_mo, if provided, it acts as a parent for the present operation.
          It should be None unless a username wants to define a parent scope.
          It can be a single MO or a list containing multiple managed objects.
        - class_id of the managed object/s to be added.
        - params contains semicolon (;) separated list of key/value pairs(key=value),
          that are used as filters for selecting specific managed objects.
          The key should be a valid property of the managed object to be added.
        """

        unknown_mo = False
        if class_id == None or class_id == "":
            raise ImcValidationException(
                '[Error]: add_imc_managedobject [Description]: class_id is Null'
            )

        meta_class_id = CoreUtils.find_class_id_in_mo_meta_ignore_case(
            class_id)
        if meta_class_id != None:
            class_id = meta_class_id
            mo_meta = CoreUtils.get_mo_property_meta(class_id, "Meta")
        else:
            unknown_mo = True

        config_map = ConfigMap()
        rn = None
        dn = None
        #mo_meta = CoreUtils.get_mo_property_meta(class_id, "Meta")
        if params != None:
            keys = params.keys()
        else:
            keys = []

        if not unknown_mo:
            rn = mo_meta.rn
            for prop in CoreUtils.get_property_list(class_id):
                prop_meta = CoreUtils.get_mo_property_meta(class_id, prop)
                if prop_meta.access != MoPropertyMeta.NAMING:
                    continue
                naming_prop_found = False
                for key in keys:
                    if key.lower() == prop.lower():
                        rn = re.sub(r'\[%s\]' % prop, '%s' % params[key], rn)
                        naming_prop_found = True
                        break

                if naming_prop_found == False:
                    ImcUtils.write_imc_warning(
                        "[Warning]: add_imc_managedobject [Description]:Expected NAMING Property %s for ClassId %s not found"
                        % (prop, class_id))
                    rn = re.sub(r'\[%s\]' % prop, '%s' % "", rn)

        obj = ManagedObject(class_id)

        for prop in keys:
            if not unknown_mo:
                prop_mo_meta = CoreUtils.is_property_in_meta_ignore_case(
                    class_id, prop)
                if prop_mo_meta != None:
                    if prop.lower() == "rn" or prop.lower() == "dn":
                        pass
                    elif prop_mo_meta.access == MoPropertyMeta.READ_ONLY:
                        ImcUtils.write_imc_warning(
                            "[Warning]: AddManagedObject [Description]:Attempt to add non-writeable property %s in Class %s"
                            % (prop, class_id))

                    if prop.lower() == "rn":
                        if in_mo == None or not isinstance(
                                in_mo, list) or len(in_mo) == 0:
                            ImcUtils.write_imc_warning(
                                "[Warning]: AddManagedObject [Description]:Ignoring Rn since no parent provided"
                            )
                        if rn != params[prop]:
                            ImcUtils.write_imc_warning(
                                "[Warning]: AddManagedObject [Description]:Rn Mismatch. Provided %s Computed %s. Ignoring Computed Rn"
                                % (params[prop], rn))
                            rn = params[
                                prop]  #bug fix. if Rn and Name are both provided by username then Rn will get preference.

                    if prop.lower() == "dn":
                        dn = params[prop]

                    obj.set_attr(prop_mo_meta.name, str(params[prop]))
                else:
                    #Known MO - Unknown Property
                    obj.set_attr(ImcUtils.word_l(prop), str(params[prop]))
            else:
                #Unknown MO
                if prop.lower() == "dn":
                    dn = params[prop]

                if prop.lower() == "rn":
                    rn = params[prop]
                if rn == None:
                    rn = ""

                obj.set_attr(ImcUtils.word_l(prop), str(params[prop]))

        obj.set_attr("Status", Status().CREATED)

        if dn != None and dn != "":
            obj.set_attr("Dn", dn)
            pair = Pair()
            #pair.set_attr("Key", obj.dn)
            pair.set_attr("Key", obj.get_attr("Dn"))
            pair.add_child(obj)
            config_map.add_child(pair)
        elif in_mo != None and isinstance(in_mo, list) and len(in_mo) > 0:
            for mo in in_mo:
                pdn = mo.get_attr("Dn")
                if pdn != None:
                    obj.set_attr("Dn", pdn + '/' + rn)
                    pair = Pair()
                    #pair.set_attr("Key", obj.dn)
                    pair.set_attr("Key", obj.get_attr("Dn"))
                    pair.add_child(obj.clone())
                    config_map.add_child(pair)

        if config_map.get_child_count() == 0:
            ImcUtils.write_imc_warning(
                '[Warning]: AddManagedObject [Description]: Nothing to Add')
            return None

        output_molist = []
        for pair in config_map.child:
            in_config = ConfigConfig()
            for mo in pair.child:
                in_config.add_child(mo)
            ccm = self.config_conf_mo(dn=pair.Key,
                                      in_config=in_config,
                                      in_hierarchical=YesOrNo.FALSE,
                                      dump_xml=dump_xml)
            if ccm.error_code == 0:
                molist = []
                for child in ccm.OutConfig.child:
                    if isinstance(child, Pair) == True:
                        for mo in child.child:
                            molist.append(mo)
                    elif isinstance(child, ManagedObject) == True:
                        molist.append(child)
                output_molist.extend(molist)
            else:
                raise ImcException(ccm.error_code, ccm.error_descr)

        return output_molist
Example #11
0
    def get_imc_managedobject(self,
                              in_mo=None,
                              class_id=None,
                              params=None,
                              in_hierarchical=False,
                              dump_xml=None):
        """
        Gets Managed Object from IMC.

        - in_mo, if provided, it acts as a parent for the present operation.
          It should be None unless a username wants to define a parent scope.
          It can be a single MO or a list containing multiple managed objects.
        - class_id of the managed object/s to get.
        - params contains semicolon (;) separated list of key/value pairs(key=value),
          that are used as filters for selecting specific managed objects.
          The key should be a valid property of the managed object to be retrieved.
        - in_hierarchical, Explores hierarchy if true, else returns managed objects at a single level.
        """

        if params != None:
            keys = params.keys()
        else:
            keys = []

        out_config = []
        meta_class_id = ""
        if class_id != None and class_id != "":
            #ClassId param set
            meta_class_id = CoreUtils.find_class_id_in_mo_meta_ignore_case(
                class_id)
            if meta_class_id == None:
                meta_class_id = class_id
                mo_meta = MoMeta(ImcUtils.word_u(class_id),
                                 ImcUtils.word_l(class_id), "", "",
                                 "InputOutput", ManagedObject.DUMMYDIRTY, [],
                                 [], [], [], [])
            else:
                mo_meta = CoreUtils.get_mo_property_meta(meta_class_id, "Meta")

            if mo_meta == None:
                raise ImcValidationException(
                    '[Error]: get_imc_managedobject: mo_meta for class_id [%s] is not valid'
                    % (class_id))
                #return None

            if in_mo != None and isinstance(in_mo, list) and len(in_mo) > 0:
                for mo in in_mo:
                    crc = self.config_resolve_children(mo_meta.xml_attribute,
                                                       mo.get_attr("Dn"),
                                                       in_hierarchical,
                                                       dump_xml)
                    if crc.error_code != 0:
                        raise ImcException(crc.error_code, crc.error_descr)

                    for child in crc.OutConfigs.child:
                        out_config.append(child)
            else:
                crc = self.config_resolve_class(mo_meta.xml_attribute,
                                                in_hierarchical, dump_xml)
                if crc.error_code != 0:
                    raise ImcException(crc.error_code, crc.error_descr)
                for child in crc.OutConfigs.child:
                    out_config.append(child)
        else:
            dn = ""
            for key in keys:
                if key.lower() == "dn":
                    dn = params[key]
            if not dn:
                raise ImcValidationException(
                    '[Error]: Please provide ClassId or dn')
            cr_dn = self.config_resolve_dn(dn, in_hierarchical, dump_xml)
            if cr_dn.error_code != 0:
                raise ImcException(cr_dn.error_code, cr_dn.error_descr)
            for child in cr_dn.OutConfig.child:
                out_config.append(child)

        #client side filtering starts
        for key in keys:
            prop_mo_meta = CoreUtils.is_property_in_meta_ignore_case(
                meta_class_id, key)
            if prop_mo_meta != None:
                attr_name = prop_mo_meta.xml_attribute
            else:
                attr_name = key
            for mo in out_config[:]:
                attr_name = attr_name[0].upper() + attr_name[1:]
                if mo.get_attr(attr_name) != params[key]:
                    out_config.remove(mo)
        #client side filtering ends

        molist = []
        current_molist = out_config
        while len(current_molist) > 0:
            child_molist = []
            for mo in current_molist:
                molist.append(mo)
                while mo.get_child_count() > 0:
                    for child in mo.child:
                        mo.remove_child(child)
                        if child.__dict__.has_key('Dn'):
                            if child.Dn == None or child.Dn == "":
                                child.set_attr("Dn", mo.Dn + '/' + child.Rn)
                                child.mark_clean()
                        else:
                            child.set_attr("Dn", mo.Dn + '/' + child.Rn)
                            child.mark_clean()
                        child_molist.append(child)
                        break
            current_molist = child_molist

        return molist
Example #12
0
    def set_imc_managedobject(self, in_mo, class_id=None, params=None, dump_xml=None):
        """
        Modifies Managed Object in IMC.

        - in_mo, if provided, it acts as the target object for the present operation.
          It should be None unless a username wants to provide an in_mo.
          It can be a single MO or a list containing multiple managed objects.
        - class_id of the managed object/s to be removed.
        - params contains semicolon (;) separated list of key/value pairs(key=value),
          that are used as filters for selecting specific managed objects.
          The key should be a valid property of the managed object to be modified.
        """

        #unknown_mo = False
        dn = None
        obj = None
        config_map = None
        dn_param_set = False

        if params != None:
            keys = params.keys()
        else:
            keys = []

        for key in keys:
            if key.lower() == "dn":
                # ClassId And dn Specified - No Parent Necessary
                dn_param_set = True
                dn = params[key]

        if in_mo == None or not isinstance(in_mo, list) or len(in_mo) == 0:
            if not dn_param_set:
                if class_id == None or class_id == "":
                    raise ImcValidationException('[Error]: set_imc_managedobject [Description]: in_mo and ClassId are both not specified')
                else:
                    raise ImcValidationException('[Error]: set_imc_managedobject [Description]: in_mo and dn are both not specified')
            else:
                if class_id == None or class_id == "":
                    raise ImcValidationException('[Error]: set_imc_managedobject [Description]: in_mo and ClassId are both not specified')
                else:
                    meta_class_id = CoreUtils.find_class_id_in_mo_meta_ignore_case(class_id)
                    if meta_class_id != None:
                        class_id = meta_class_id
                    #    mo_meta = CoreUtils.get_mo_property_meta(class_id, "Meta")
                    #else:
                    #    unknown_mo = True

                    obj = ManagedObject(class_id)
                    for prop in keys:
                        prop_mo_meta = CoreUtils.is_property_in_meta_ignore_case(class_id, prop)
                        if prop_mo_meta != None:
                            if prop.lower() == "rn" or prop.lower() == "dn":
                                pass
                            elif prop_mo_meta.access == MoPropertyMeta.READ_ONLY:
                                ImcUtils.write_imc_warning("[Warning]: SetManagedObject [Description] Attempt to set non-writable property %s in Class %s" %(prop, class_id))

                            obj.set_attr(prop_mo_meta.name, str(params[prop]))
                        else:
                            #Sets the unknown property/value as Xtra_property in obj
                            obj.set_attr(ImcUtils.word_l(prop), str(params[prop]))

                    obj.set_attr("Dn", dn)
                    obj.set_attr("Status", Status().MODIFIED)
                    pair = Pair()
                    pair.set_attr("Key", obj.get_attr("Dn"))
                    pair.add_child(obj)
                    config_map = ConfigMap()
                    config_map.add_child(pair)

        else:
            if class_id != None and class_id != "":
                ImcUtils.write_imc_warning("[Warning]: SetManagedObject [Description] ClassId <%s> is ignored with InMo input" %(class_id))

            config_map = ConfigMap()
            for mo in in_mo:
                obj = ManagedObject(mo.prop_mo_meta.name)
                dn = mo.get_attr("Dn")
                class_id = mo.prop_mo_meta.name

                for prop in keys:
                    prop_mo_meta = CoreUtils.is_property_in_meta_ignore_case(class_id, prop)
                    if prop_mo_meta != None:
                        if prop.lower() == "rn" or prop.lower() == "dn":
                            pass
                        elif prop_mo_meta.access == MoPropertyMeta.READ_ONLY:
                            ImcUtils.write_imc_warning("[Warning]: SetManagedObject [Description] Attempt to set non-writeable property %s in Class %s" %(prop, class_id))

                        obj.set_attr(prop_mo_meta.name, str(params[prop]))
                    else:
                        #Sets the unknown property/value as Xtra_property in obj
                        obj.set_attr(ImcUtils.word_l(prop), str(params[prop]))

                obj.set_attr("Dn", dn)
                obj.set_attr("Status", Status.MODIFIED)
                pair = Pair()
                pair.set_attr("Key", obj.get_attr("Dn"))
                pair.add_child(obj)
                config_map.add_child(pair)

        output_molist = []
        for pair in config_map.child:
            in_config = ConfigConfig()
            for mo in pair.child:
                in_config.add_child(mo)

            ccm = self.config_conf_mo(dn=pair.Key, in_config=in_config, in_hierarchical=YesOrNo.FALSE, dump_xml=dump_xml)

            if ccm.error_code == 0:
                molist = []
                for child in ccm.OutConfig.child:
                    if isinstance(child, Pair) == True:
                        for mo in child.child:
                            molist.append(mo)
                    elif isinstance(child, ManagedObject) == True:
                        molist.append(child)
                output_molist.extend(molist)
            else:
                raise ImcException(ccm.error_code, ccm.error_descr)

        return output_molist
Example #13
0
    def add_imc_managedobject(self, in_mo=None, class_id=None, params=None, dump_xml=None):
        """
        Adds a Managed Object to IMC.

        - in_mo, if provided, it acts as a parent for the present operation.
          It should be None unless a username wants to define a parent scope.
          It can be a single MO or a list containing multiple managed objects.
        - class_id of the managed object/s to be added.
        - params contains semicolon (;) separated list of key/value pairs(key=value),
          that are used as filters for selecting specific managed objects.
          The key should be a valid property of the managed object to be added.
        """

        unknown_mo = False
        if class_id == None or class_id == "":
            raise ImcValidationException('[Error]: add_imc_managedobject [Description]: class_id is Null')

        meta_class_id = CoreUtils.find_class_id_in_mo_meta_ignore_case(class_id)
        if meta_class_id != None:
            class_id = meta_class_id
            mo_meta = CoreUtils.get_mo_property_meta(class_id, "Meta")
        else:
            unknown_mo = True

        config_map = ConfigMap()
        rn = None
        dn = None
        #mo_meta = CoreUtils.get_mo_property_meta(class_id, "Meta")
        if params != None:
            keys = params.keys()
        else:
            keys = []

        if not unknown_mo:
            rn = mo_meta.rn
            for prop in CoreUtils.get_property_list(class_id):
                prop_meta = CoreUtils.get_mo_property_meta(class_id, prop)
                if prop_meta.access != MoPropertyMeta.NAMING:
                    continue
                naming_prop_found = False
                for key in keys:
                    if key.lower() == prop.lower():
                        rn = re.sub(r'\[%s\]' % prop, '%s' % params[key], rn)
                        naming_prop_found = True
                        break

                if naming_prop_found == False:
                    ImcUtils.write_imc_warning("[Warning]: add_imc_managedobject [Description]:Expected NAMING Property %s for ClassId %s not found" %(prop, class_id))
                    rn = re.sub(r'\[%s\]' % prop, '%s' % "", rn)

        obj = ManagedObject(class_id)

        for prop in keys:
            if not unknown_mo:
                prop_mo_meta = CoreUtils.is_property_in_meta_ignore_case(class_id, prop)
                if prop_mo_meta != None:
                    if prop.lower() == "rn" or prop.lower() == "dn":
                        pass
                    elif prop_mo_meta.access == MoPropertyMeta.READ_ONLY:
                        ImcUtils.write_imc_warning("[Warning]: AddManagedObject [Description]:Attempt to add non-writeable property %s in Class %s" %(prop, class_id))

                    if prop.lower() == "rn":
                        if in_mo == None or not isinstance(in_mo, list) or len(in_mo) == 0:
                            ImcUtils.write_imc_warning("[Warning]: AddManagedObject [Description]:Ignoring Rn since no parent provided")
                        if rn != params[prop]:
                            ImcUtils.write_imc_warning("[Warning]: AddManagedObject [Description]:Rn Mismatch. Provided %s Computed %s. Ignoring Computed Rn" %(params[prop], rn))
                            rn = params[prop]#bug fix. if Rn and Name are both provided by username then Rn will get preference.

                    if prop.lower() == "dn":
                        dn = params[prop]

                    obj.set_attr(prop_mo_meta.name, str(params[prop]))
                else:
                    #Known MO - Unknown Property
                    obj.set_attr(ImcUtils.word_l(prop), str(params[prop]))
            else:
                #Unknown MO
                if prop.lower() == "dn":
                    dn = params[prop]

                if prop.lower() == "rn":
                    rn = params[prop]
                if rn == None:
                    rn = ""

                obj.set_attr(ImcUtils.word_l(prop), str(params[prop]))

        obj.set_attr("Status", Status().CREATED)

        if dn != None and dn != "":
            obj.set_attr("Dn", dn)
            pair = Pair()
            #pair.set_attr("Key", obj.dn)
            pair.set_attr("Key", obj.get_attr("Dn"))
            pair.add_child(obj)
            config_map.add_child(pair)
        elif in_mo != None and isinstance(in_mo, list) and len(in_mo) > 0:
            for mo in in_mo:
                pdn = mo.get_attr("Dn")
                if pdn != None:
                    obj.set_attr("Dn", pdn + '/' +rn)
                    pair = Pair()
                    #pair.set_attr("Key", obj.dn)
                    pair.set_attr("Key", obj.get_attr("Dn"))
                    pair.add_child(obj.clone())
                    config_map.add_child(pair)

        if config_map.get_child_count() == 0:
            ImcUtils.write_imc_warning('[Warning]: AddManagedObject [Description]: Nothing to Add')
            return None

        output_molist = []
        for pair in config_map.child:
            in_config = ConfigConfig()
            for mo in pair.child:
                in_config.add_child(mo)
            ccm = self.config_conf_mo(dn=pair.Key, in_config=in_config, in_hierarchical=YesOrNo.FALSE, dump_xml=dump_xml)
            if ccm.error_code == 0:
                molist = []
                for child in ccm.OutConfig.child:
                    if isinstance(child, Pair) == True:
                        for mo in child.child:
                            molist.append(mo)
                    elif isinstance(child, ManagedObject) == True:
                        molist.append(child)
                output_molist.extend(molist)
            else:
                raise ImcException(ccm.error_code, ccm.error_descr)

        return output_molist
Example #14
0
    def get_imc_managedobject(self, in_mo=None, class_id=None, params=None, in_hierarchical=False, dump_xml=None):
        """
        Gets Managed Object from IMC.

        - in_mo, if provided, it acts as a parent for the present operation.
          It should be None unless a username wants to define a parent scope.
          It can be a single MO or a list containing multiple managed objects.
        - class_id of the managed object/s to get.
        - params contains semicolon (;) separated list of key/value pairs(key=value),
          that are used as filters for selecting specific managed objects.
          The key should be a valid property of the managed object to be retrieved.
        - in_hierarchical, Explores hierarchy if true, else returns managed objects at a single level.
        """

        if params != None:
            keys = params.keys()
        else:
            keys = []

        out_config = []
        meta_class_id = ""
        if class_id != None and class_id != "":
            #ClassId param set
            meta_class_id = CoreUtils.find_class_id_in_mo_meta_ignore_case(class_id)
            if meta_class_id == None:
                meta_class_id = class_id
                mo_meta = MoMeta(ImcUtils.word_u(class_id), ImcUtils.word_l(class_id), "", "", "InputOutput", ManagedObject.DUMMYDIRTY, [], [], [], [], [])
            else:
                mo_meta = CoreUtils.get_mo_property_meta(meta_class_id, "Meta")

            if mo_meta == None:
                raise ImcValidationException('[Error]: get_imc_managedobject: mo_meta for class_id [%s] is not valid' %(class_id))
                #return None

            if in_mo != None and isinstance(in_mo, list) and len(in_mo) > 0:
                for mo in in_mo:
                    crc = self.config_resolve_children(mo_meta.xml_attribute, mo.get_attr("Dn"), in_hierarchical, dump_xml)
                    if crc.error_code != 0:
                        raise ImcException(crc.error_code, crc.error_descr)

                    for child in crc.OutConfigs.child:
                        out_config.append(child)
            else:
                crc = self.config_resolve_class(mo_meta.xml_attribute, in_hierarchical, dump_xml)
                if crc.error_code != 0:
                    raise ImcException(crc.error_code, crc.error_descr)
                for child in crc.OutConfigs.child:
                    out_config.append(child)
        else:
            dn = ""
            for key in keys:
                if key.lower() == "dn":
                    dn = params[key]
            if not dn:
                raise ImcValidationException('[Error]: Please provide ClassId or dn')
            cr_dn = self.config_resolve_dn(dn, in_hierarchical, dump_xml)
            if cr_dn.error_code != 0:
                raise ImcException(cr_dn.error_code, cr_dn.error_descr)
            for child in cr_dn.OutConfig.child:
                out_config.append(child)

        #client side filtering starts
        for key in keys:
            prop_mo_meta = CoreUtils.is_property_in_meta_ignore_case(meta_class_id, key)
            if prop_mo_meta != None:
                attr_name = prop_mo_meta.xml_attribute
            else:
                attr_name = key
            for mo in out_config[:]:
                attr_name = attr_name[0].upper() + attr_name[1:]
                if mo.get_attr(attr_name) != params[key]:
                    out_config.remove(mo)
        #client side filtering ends

        molist = []
        current_molist = out_config
        while len(current_molist) > 0:
            child_molist = []
            for mo in current_molist:
                molist.append(mo)
                while mo.get_child_count() > 0:
                    for child in mo.child:
                        mo.remove_child(child)
                        if child.__dict__.has_key('Dn'):
                            if child.Dn == None or child.Dn == "":
                                child.set_attr("Dn", mo.Dn + '/' + child.Rn)
                                child.mark_clean()
                        else:
                            child.set_attr("Dn", mo.Dn + '/' + child.Rn)
                            child.mark_clean()
                        child_molist.append(child)
                        break
            current_molist = child_molist

        return molist