def createOshFromId(ciDict, id, ciClass=None): osh = None object = ciDict[id] # create the container osh if object != None: id = object[0] type = object[1] props = object[2] ciid = object[3] if type in host_types: real_ci_class = ciClass or type osh = modeling.createOshByCmdbId(real_ci_class, ciid) else: osh = ObjectStateHolder(type) if props != None: for prop in props: if prop[1] == 'Integer': osh.setIntegerAttribute(prop[0], prop[3]) elif prop[1] == 'Long': osh.setLongAttribute(prop[0], prop[3]) elif prop[1] == 'Enum': osh.setEnumAttribute(prop[0], int(prop[3])) else: osh.setAttribute(prop[0], prop[3]) return osh
def createOshFromId(ciDict, id, ciClass = None): osh = None object = ciDict[id] # create the container osh if object != None: id = object[0] type = object[1] props = object[2] ciid = object[3] if type in host_types: real_ci_class = ciClass or type osh = modeling.createOshByCmdbId(real_ci_class, ciid) else: osh = ObjectStateHolder(type) if props != None: for prop in props: if prop[1] == 'Integer': osh.setIntegerAttribute(prop[0], prop[3]) elif prop[1] == 'Long': osh.setLongAttribute(prop[0], prop[3]) elif prop[1] == 'Enum': osh.setEnumAttribute(prop[0], int(prop[3])) else: osh.setAttribute(prop[0], prop[3]) return osh
class OshBuilder(CiBuilder): def __init__(self, targetCiType): self.__type = targetCiType self.__osh = ObjectStateHolder(self.__type) def setCiAttribute(self, name, value): attributeType = self.__getAttributeType(self.__type, name) if value: self.__setValue(name, attributeType, value) else: logger.debug("Meet none value for %s, type:%s" % (name, attributeType)) def build(self): return self.__osh def __setValue(self, name, attributeType, value): if attributeType == 'string': self.__osh.setStringAttribute(name, str(value)) elif attributeType == 'integer': self.__osh.setIntegerAttribute(name, int(value)) elif attributeType.endswith('enum'): if isinstance(value, int): self.__osh.setEnumAttribute(name, value) else: self.__osh.setAttribute(name, value) elif attributeType == 'string_list': self.__osh.setListAttribute(name, value) else: raise ValueError('no setter defined for type %s' % attributeType) def __getAttributeType(self, ciType, attributeName): try: attributeDefinition = modeling._CMDB_CLASS_MODEL.getAttributeDefinition( ciType, attributeName) return attributeDefinition.getType() except: if DEBUG: if attributeName in ['memory_size', 'port_index']: return 'integer' return 'string' raise ValueError("Failed to determine type of %s.%s" % (ciType, attributeName))
def build(self, fcportpdo): osh = ObjectStateHolder(self.CIT) osh.setAttribute('fcport_wwn', fcportpdo.wwn) if fcportpdo.name is not None: osh.setAttribute('name', unicode(fcportpdo.name)) if fcportpdo.porttype: if isinstance(fcportpdo.porttype, int): osh.setEnumAttribute('port_type', fcportpdo.porttype) else: osh.setEnumAttribute('port_type', PORT_TYPES.get(fcportpdo.porttype, 5)) if fcportpdo.portindex is not None: osh.setAttribute('port_index', fcportpdo.portindex) if fcportpdo.type: osh.setAttribute('fcport_type', fcportpdo.type) if fcportpdo.trunkedstate: osh.setAttribute('fcport_trunkedstate', fcportpdo.trunkedstate) if fcportpdo.symbolicname: osh.setAttribute('fcport_symbolicname', fcportpdo.symbolicname) if fcportpdo.status: osh.setAttribute('fcport_status', fcportpdo.status) if fcportpdo.state: osh.setAttribute('fcport_state', fcportpdo.state) if fcportpdo.speed is not None: osh.setAttribute('fcport_speed', fcportpdo.speed) if fcportpdo.scsiport: osh.setAttribute('fcport_scsiport', fcportpdo.scsiport) if fcportpdo.id is not None: osh.setAttribute('fcport_portid', fcportpdo.id) if fcportpdo.maxspeed: osh.setAttribute('fcport_maxspeed', fcportpdo.maxspeed) if fcportpdo.fibertype: osh.setAttribute('fcport_fibertype', fcportpdo.fibertype) if fcportpdo.domainid: osh.setAttribute('fcport_domainid', fcportpdo.domainid) if fcportpdo.connectedtowwn: osh.setAttribute('fcport_connectedtowwn', fcportpdo.connectedtowwn) return osh
class OshBuilder(CiBuilder): def __init__(self, targetCiType): self.__type = targetCiType self.__osh = ObjectStateHolder(self.__type) def setCiAttribute(self, name, value): attributeType = self.__getAttributeType(self.__type, name) if value: self.__setValue(name, attributeType, value) else: logger.debug("Meet none value for %s, type:%s" % (name, attributeType)) def build(self): return self.__osh def __setValue(self, name, attributeType, value): if attributeType == 'string': self.__osh.setStringAttribute(name, str(value)) elif attributeType == 'integer': self.__osh.setIntegerAttribute(name, int(value)) elif attributeType.endswith('enum'): if isinstance(value, int): self.__osh.setEnumAttribute(name, value) else: self.__osh.setAttribute(name, value) elif attributeType == 'string_list': self.__osh.setListAttribute(name, value) else: raise ValueError('no setter defined for type %s' % attributeType) def __getAttributeType(self, ciType, attributeName): try: attributeDefinition = modeling._CMDB_CLASS_MODEL.getAttributeDefinition(ciType, attributeName) return attributeDefinition.getType() except: if DEBUG: if attributeName in ['memory_size', 'port_index']: return 'integer' return 'string' raise ValueError("Failed to determine type of %s.%s" % (ciType, attributeName))
def visitEndpoint(endpoint): r''' @types: netutils.Endpoint -> ObjectStateHolder @raise ValueError: Not supported protocol type @raise ValueError: Invalid IP address ''' address = endpoint.getAddress() if not isinstance(address, (ip_addr.IPv4Address, ip_addr.IPv6Address)): address = ip_addr.IPAddress(address) ipServerOSH = ObjectStateHolder('ip_service_endpoint') uri = "%s:%s" % (address, endpoint.getPort()) ipServerOSH.setAttribute('ipserver_address', uri) ipServerOSH.setAttribute('network_port_number', endpoint.getPort()) if endpoint.getProtocolType() == netutils.ProtocolType.TCP_PROTOCOL: portType = ('tcp', 1) elif endpoint.getProtocolType() == netutils.ProtocolType.UDP_PROTOCOL: portType = ('udp', 2) else: raise ValueError("Not supported protocol type") ipServerOSH.setAttribute('port_type', portType[0]) ipServerOSH.setEnumAttribute('ipport_type', portType[1]) ipServerOSH.setAttribute('bound_to_ip_address', str(address)) return ipServerOSH
def processObjects(allObjects, DateParsePattern): vector = ObjectStateHolderVector() iter = allObjects.iterator() #ciList = [[id, type, props]] ciList = [] ciDict = {} createCi = 1 while iter.hasNext(): #attributes = [name, type, key, value] attributes = [] objectElement = iter.next() mamId = objectElement.getAttribute('mamId').getValue() cit = objectElement.getAttribute('name').getValue() if mamId != None and cit != None: # add the attributes... allAttributes = objectElement.getChildren('field') iterAtt = allAttributes.iterator() while iterAtt.hasNext(): attElement = iterAtt.next() attName = attElement.getAttribute('name').getValue() attType = attElement.getAttribute('datatype').getValue() attKey = attElement.getAttribute('key') attValue = attElement.getText() if attType == None or attType == "": attType = "string" if attKey == None or attKey == "": attKey = "false" else: attKey = attKey.getValue() if attName != "" and attType != "": attributes.append([attName, attType, attKey, attValue]) # create CI or not? Is key empty or none? if attKey == "true": if attValue != None and attValue != "": createCi = 1 else: createCi = 0 #info (concatenate("Id: ", mamId, ", Type: ", cit, ", Properties: ", attributes)) if createCi == 1: ciList.append([mamId, cit, attributes]) #ciDict[mamId] = [mamId, cit, attributes] #print "MAMID = ", mamId, ", CIT = ", cit, ", Attributes = ", attributes for ciVal in ciList: logger.info("\tAdding %s [%s] => [%s]" % (ciVal[1], ciVal[0], ciVal[2]) ) id = ciVal[0] type = ciVal[1] osh = ObjectStateHolder(type) if ciVal[2] != None: props = ciVal[2] createContainer = 0 containerOsh = None for prop in props: if prop[0] == 'root_container' and prop[3] != "" and ciDict.has_key(prop[3]): containerOsh = ciDict[prop[3]] createContainer = 1 if prop[1] == 'integer': prop[3] and prop[3].isdigit() and osh.setIntegerAttribute(prop[0], prop[3]) elif prop[1] == 'long': prop[3] and prop[3].isdigit() and osh.setLongAttribute(prop[0], prop[3]) elif prop[1] == 'enum': osh.setEnumAttribute(prop[0], int(prop[3])) elif prop[1] == 'boolean': if str(prop[3]).lower == 'false': osh.setBoolAttribute(prop[0], 0) else: osh.setBoolAttribute(prop[0], 1) elif prop[1] == 'date': if DateParsePattern != None and DateParsePattern != "": formatter = SimpleDateFormat(DateParsePattern) osh.setDateAttribute(prop[0], formatter.parseObject(prop[3])) else: osh.setAttribute(prop[0], prop[3]) if createContainer == 1: osh.setContainer(containerOsh) vector.add(osh) ciDict[id] = osh return (vector, ciDict)
def processObjects(allObjects): vector = ObjectStateHolderVector() iter = allObjects.iterator() ciList = [] ciDict = {} createCi = 1 while iter.hasNext(): attributes = [] objectElement = iter.next() mamId = objectElement.getAttribute('mamId').getValue() cit = objectElement.getAttribute('name').getValue() if mamId != None and cit != None: allAttributes = objectElement.getChildren('field') iterAtt = allAttributes.iterator() attid = "" while iterAtt.hasNext(): attElement = iterAtt.next() attName = attElement.getAttribute('name').getValue() attType = attElement.getAttribute('datatype').getValue() attKey = attElement.getAttribute('key') attValue = attElement.getText() if cit in host_types and attName == 'id': attid = attValue attName = "" if attid == "": logger.info ('Cannot create host, no UCMDB ID supplied' ) if cit == 'person' and attName == 'identification_type': attType = 'Enum' if attType == None or attType == "": attType = "string" if attKey == None or attKey == "": attKey = "false" else: attKey = attKey.getValue() if attName != "" and attType != "": attributes.append([attName, attType, attKey, attValue]) if attKey == "true": if attValue != None and attValue != "": createCi = 1 else: createCi = 0 if createCi == 1: ciList.append([mamId, cit, attributes, attid]) ciDict[mamId] = [mamId, cit, attributes,attid] # # Process all the attibutes setting them into the OSH # for ciVal in ciList: id = ciVal[0] type = ciVal[1] if type in host_types: osh = modeling.createOshByCmdbId(type, ciVal[3]) else: osh = ObjectStateHolder(type) if ciVal[2] != None: props = ciVal[2] createContainer = 0 containerOsh = None for prop in props: if prop[0] == 'root_container': parent = ciDict[prop[3]] if parent != None: parentId = parent[0] parentType = parent[1] parentProps = parent[2] containerOsh = ObjectStateHolder(parentType) if parentProps != None: for parentProp in parentProps: containerOsh.setAttribute(parentProp[0], parentProp[3]) createContainer = 1 if prop[1] == 'Integer': osh.setIntegerAttribute(prop[0], prop[3]) elif prop[1] == 'Long': osh.setLongAttribute(prop[0], prop[3]) elif prop[1] == 'Enum': osh.setEnumAttribute(prop[0], int(prop[3])) else: osh.setAttribute(prop[0], prop[3]) if createContainer == 1: osh.setContainer(containerOsh) vector.add(osh) return (vector, ciDict)
def processObjects(allObjects): vector = ObjectStateHolderVector() iter = allObjects.iterator() ciList = [] ciDict = {} createCi = 1 while iter.hasNext(): attributes = [] objectElement = iter.next() mamId = objectElement.getAttribute('mamId').getValue() cit = objectElement.getAttribute('name').getValue() if mamId != None and cit != None: allAttributes = objectElement.getChildren('field') iterAtt = allAttributes.iterator() attid = "" while iterAtt.hasNext(): attElement = iterAtt.next() attName = attElement.getAttribute('name').getValue() attType = attElement.getAttribute('datatype').getValue() attKey = attElement.getAttribute('key') attValue = attElement.getText() if cit in host_types and attName == 'id': attid = attValue attName = "" if attid == "": logger.info('Cannot create host, no UCMDB ID supplied') if cit == 'person' and attName == 'identification_type': attType = 'Enum' if attType == None or attType == "": attType = "string" if attKey == None or attKey == "": attKey = "false" else: attKey = attKey.getValue() if attName != "" and attType != "": attributes.append([attName, attType, attKey, attValue]) if attKey == "true": if attValue != None and attValue != "": createCi = 1 else: createCi = 0 if createCi == 1: ciList.append([mamId, cit, attributes, attid]) ciDict[mamId] = [mamId, cit, attributes, attid] # # Process all the attibutes setting them into the OSH # for ciVal in ciList: id = ciVal[0] type = ciVal[1] if type in host_types: osh = modeling.createOshByCmdbId(type, ciVal[3]) else: osh = ObjectStateHolder(type) if ciVal[2] != None: props = ciVal[2] createContainer = 0 containerOsh = None for prop in props: if prop[0] == 'root_container': parent = ciDict[prop[3]] if parent != None: parentId = parent[0] parentType = parent[1] parentProps = parent[2] containerOsh = ObjectStateHolder(parentType) if parentProps != None: for parentProp in parentProps: containerOsh.setAttribute( parentProp[0], parentProp[3]) createContainer = 1 if prop[1] == 'Integer': osh.setIntegerAttribute(prop[0], prop[3]) elif prop[1] == 'Long': osh.setLongAttribute(prop[0], prop[3]) elif prop[1] == 'Enum': osh.setEnumAttribute(prop[0], int(prop[3])) else: osh.setAttribute(prop[0], prop[3]) if createContainer == 1: osh.setContainer(containerOsh) vector.add(osh) return (vector, ciDict)
def processObjects(allObjects, DateParsePattern): vector = ObjectStateHolderVector() iter = allObjects.iterator() #ciList = [[id, type, props]] ciList = [] ciDict = {} createCi = 1 while iter.hasNext(): #attributes = [name, type, key, value] attributes = [] objectElement = iter.next() mamId = objectElement.getAttribute('mamId').getValue() cit = objectElement.getAttribute('name').getValue() if mamId != None and cit != None: # add the attributes... allAttributes = objectElement.getChildren('field') iterAtt = allAttributes.iterator() while iterAtt.hasNext(): attElement = iterAtt.next() attName = attElement.getAttribute('name').getValue() attType = attElement.getAttribute('datatype').getValue() attKey = attElement.getAttribute('key') attValue = attElement.getText() if attType == None or attType == "": attType = "string" if attKey == None or attKey == "": attKey = "false" else: attKey = attKey.getValue() if attName != "" and attType != "": attributes.append([attName, attType, attKey, attValue]) # create CI or not? Is key empty or none? if attKey == "true": if attValue != None and attValue != "": createCi = 1 else: createCi = 0 #info (concatenate("Id: ", mamId, ", Type: ", cit, ", Properties: ", attributes)) if createCi == 1: ciList.append([mamId, cit, attributes]) #ciDict[mamId] = [mamId, cit, attributes] #print "MAMID = ", mamId, ", CIT = ", cit, ", Attributes = ", attributes for ciVal in ciList: logger.info("\tAdding %s [%s] => [%s]" % (ciVal[1], ciVal[0], ciVal[2])) id = ciVal[0] type = ciVal[1] osh = ObjectStateHolder(type) if ciVal[2] != None: props = ciVal[2] createContainer = 0 containerOsh = None for prop in props: if prop[0] == 'root_container' and prop[ 3] != "" and ciDict.has_key(prop[3]): containerOsh = ciDict[prop[3]] createContainer = 1 if prop[1] == 'integer': prop[3] and prop[3].isdigit() and osh.setIntegerAttribute( prop[0], prop[3]) elif prop[1] == 'long': prop[3] and prop[3].isdigit() and osh.setLongAttribute( prop[0], prop[3]) elif prop[1] == 'enum': osh.setEnumAttribute(prop[0], int(prop[3])) elif prop[1] == 'boolean': if str(prop[3]).lower == 'false': osh.setBoolAttribute(prop[0], 0) else: osh.setBoolAttribute(prop[0], 1) elif prop[1] == 'date': if DateParsePattern != None and DateParsePattern != "": formatter = SimpleDateFormat(DateParsePattern) osh.setDateAttribute(prop[0], formatter.parseObject(prop[3])) else: osh.setAttribute(prop[0], prop[3]) if createContainer == 1: osh.setContainer(containerOsh) vector.add(osh) ciDict[id] = osh return (vector, ciDict)
def createInterfaceObjects(ifList, host_id, ucmdbversion = None): result = ObjectStateHolderVector() discoveredHostOSH = modeling.createOshByCmdbId('host', host_id) for interface in ifList: if interface.ifType and int(interface.ifType) == 24: continue if not modeling.isValidInterface(interface.ifMac, interface.ifDescr, interface.ifName): continue interface.ifDescr = _stripVirtualMiniportInfo(interface.ifDescr) interfaceOSH = modeling.createInterfaceOSH(interface.ifMac, discoveredHostOSH, interface.ifDescr, interface.ifIndex, interface.ifType, interface.ifAdminStatus, interface.ifOperStatus, interface.ifSpeed, interface.ifName, interface.ifAlias) if not interfaceOSH: continue result.add(interfaceOSH) if ucmdbversion and ucmdbversion < 9: interfaceIndexOSH = ObjectStateHolder("interfaceindex") interfaceIndexOSH.setIntegerAttribute("interfaceindex_index", interface.ifIndex) if interface.ifAdminStatus: intValue = None try: intValue = int(interface.ifAdminStatus) except: logger.warn("Failed to convert the interface admin status '%s'" % interface.ifAdminStatus) else: if intValue > 0: interfaceIndexOSH.setEnumAttribute("interfaceindex_adminstatus", intValue) if interface.ifDescr != None and interface.ifDescr != '': interfaceIndexOSH.setStringAttribute("interfaceindex_description", interface.ifDescr) if interface.ifIndex != None and interface.ifIndex != '': interfaceIndexOSH.setIntegerAttribute("interfaceindex_index", interface.ifIndex) if interface.ifSpeed != None and interface.ifSpeed != '': interfaceIndexOSH.setDoubleAttribute("interfaceindex_speed", interface.ifSpeed) if interface.ifType: intValue = None try: intValue = int(interface.ifType) except: logger.warn("Failed to convert the interface type '%s'" % interface.ifType) else: if intValue > 0: interfaceIndexOSH.setEnumAttribute("interfaceindex_type", intValue) interfaceIndexOSH.setContainer(discoveredHostOSH) result.add(interfaceIndexOSH) parentLinkOSH = modeling.createLinkOSH('parent', interfaceIndexOSH, interfaceOSH) result.add(parentLinkOSH) for ip in interface.ipList: if str(ip.ipAddr).startswith('0.') or str(ip.ipAddr).startswith('127.'): continue ipOSH = modeling.createIpOSH(ip.ipAddr) parentLinkOSH = modeling.createLinkOSH('containment', interfaceOSH, ipOSH) result.add(parentLinkOSH) return result