def getXml(self): elt = Element("USBThrottle") for mi in self.ctrlMenuItem: if (mi.isSelected()): elt.setAttribute("DesiredController", mi.getText()) break return elt
def getXml(self): elt = Element("USBThrottle") for mi in self.ctrlMenuItem : if (mi.isSelected()) : elt.setAttribute("DesiredController", mi.getText()) break return elt
def processXmlAndCreateGRLoaderXmlFiles(operation, xmlResult): fileName = "%s-%s.xml" % (TIMESTAMP, operation) doc = Document() saxBuilder = SAXBuilder() xmlData = saxBuilder.build(StringReader(xmlResult)) try: # Process CIs cisData = xmlData.getRootElement().getChild('data').getChild( 'objects').getChildren('Object') (mamIdToAttributesMap, itemsRootElement) = addCIs(Element('GRLoader'), cisData) # Process Relationships relationsData = xmlData.getRootElement().getChild('data').getChild( 'links').getChildren('link') itemsRootElementWithLinks = addRelations(itemsRootElement, relationsData, mamIdToAttributesMap) if itemsRootElementWithLinks != None: doc.setRootElement(itemsRootElementWithLinks) createGRLoaderXmlInputFile(fileName, doc) except: raise Exception, "Unable to process inbound XML" return fileName
def buildIntermediateCiXML(theCiElement, objectFromARIS): try: theCiElement.setAttribute('type', objectFromARIS.type) objectAttributeMap = objectFromARIS.attMap ## Add CI attributes if objectAttributeMap: for objectAttributeName in objectAttributeMap.keys(): if objectAttributeName and objectAttributeMap[objectAttributeName]: fieldElement = Element('field') fieldElement.setAttribute('name', objectAttributeName) fieldElement.setText(objectAttributeMap[objectAttributeName]) theCiElement.addContent(fieldElement) else: debugPrint(3, '[' + SCRIPT_NAME + ':buildIntermediateCiXML] Skipping attribute <%s> with invalid value' % objectAttributeName) return 1 except: excInfo = logger.prepareJythonStackTrace('') debugPrint('[' + SCRIPT_NAME + ':buildIntermediateCiXML] Exception: <%s>' % excInfo) return -1
def buildIntermediateCiXML(theCiElement, objectFromARIS): try: theCiElement.setAttribute('type', objectFromARIS.type) objectAttributeMap = objectFromARIS.attMap ## Add CI attributes if objectAttributeMap: for objectAttributeName in objectAttributeMap.keys(): if objectAttributeName and objectAttributeMap[ objectAttributeName]: fieldElement = Element('field') fieldElement.setAttribute('name', objectAttributeName) fieldElement.setText( objectAttributeMap[objectAttributeName]) theCiElement.addContent(fieldElement) else: debugPrint( 3, '[' + SCRIPT_NAME + ':buildIntermediateCiXML] Skipping attribute <%s> with invalid value' % objectAttributeName) return 1 except: excInfo = logger.prepareJythonStackTrace('') debugPrint('[' + SCRIPT_NAME + ':buildIntermediateCiXML] Exception: <%s>' % excInfo) return -1
def testCaCmdbConnection(conn): logger.debug( "Testing CA CMDB connection for Server: %s, Port: %s, Username: %s" % (conn.host, conn.port, conn.username)) xmlInputFilePath = createGRLoaderXmlInputFile( TEST_FILE_NAME, Document(Element('GRLoader'))) params = getGRLoaderParams(xmlInputFilePath, conn, None) status = invokeGRLoader(params) deleteGRLoaderXmlInputFile(TEST_FILE_NAME) if status: return 1 else: return 0
def addCIs(rootElement, allObjectChildren): iter = allObjectChildren.iterator() mamIdToAttributesMap = HashMap() while iter.hasNext(): ciElement = Element('ci') attributeMap = HashMap() objectElement = iter.next() familyClassName = objectElement.getAttributeValue('name') mamId = objectElement.getAttributeValue('mamId') # split dest class into family and class names splitIdx = string.rfind(familyClassName, '.') familyName = familyClassName[0:splitIdx] className = familyClassName[splitIdx+1:len(familyClassName)] #print familyName, " - ", className if familyName != None and className != None: classElement = Element('class') classElement.setText(className) ciElement.addContent(classElement) attributeMap.put('class', className) familyElement = Element('family') familyElement.setText(familyName) ciElement.addContent(familyElement) attributeMap.put('family', familyName) # get the fields fieldChildren = objectElement.getChildren('field') if fieldChildren is not None: iter2 = fieldChildren.iterator() while iter2.hasNext(): fieldElement = iter2.next() fieldName = fieldElement.getAttributeValue('name') fieldValue = fieldElement.getText() if fieldValue == None or fieldValue == '': fieldValue = 'EMPTY' #print fieldName, ' - ', fieldValue setFieldElement = Element(fieldName) setFieldElement.setText(fieldValue) ciElement.addContent(setFieldElement) attributeMap.put(fieldName, fieldValue) mamIdToAttributesMap.put(mamId, attributeMap) rootElement.addContent(ciElement) return (mamIdToAttributesMap, rootElement)
def addRelations(rootElement, allRelationChildren, mamIdToAttributesMap): removeAtts = ['family', 'class'] iter = allRelationChildren.iterator() while iter.hasNext(): relElement = Element('relation') linkElement = iter.next() linkType = linkElement.getAttribute('targetRelationshipClass').getValue() targetParent = linkElement.getAttribute('targetParent').getValue() splitIdx_targetParent = string.rfind(targetParent, '.') targetParentFamilyName = targetParent[0:splitIdx_targetParent] targetParentClassName = targetParent[splitIdx_targetParent+1:len(targetParent)] targetChild = linkElement.getAttribute('targetChild').getValue() splitIdx_targetChild = string.rfind(targetChild, '.') targetChildFamilyName = targetChild[0:splitIdx_targetChild] targetChildClassName = targetChild[splitIdx_targetChild+1:len(targetChild)] id1base = '' id2base = '' if linkType != None and targetParent != None and targetChild != None: fieldChildren = linkElement.getChildren('field') if fieldChildren is not None: iter2 = fieldChildren.iterator() while iter2.hasNext(): fieldElement = iter2.next() fieldName = fieldElement.getAttributeValue('name') if fieldName == 'DiscoveryID1': id1base = fieldElement.getText() if fieldName == 'DiscoveryID2': id2base = fieldElement.getText() # set type typeElement = Element('type') typeElement.setText(linkType) relElement.addContent(typeElement) # set provider providerElement = Element('provider') if mamIdToAttributesMap.containsKey(id1base): otherProviderAttributes = mamIdToAttributesMap.get(id1base) iter3 = otherProviderAttributes.entrySet().iterator() while iter3.hasNext(): entry = iter3.next() name = entry.getKey() if name in removeAtts: continue value = entry.getValue() otherElement = Element(name) otherElement.setText(value) providerElement.addContent(otherElement) relElement.addContent(providerElement) # set dependent dependentElement = Element('dependent') if mamIdToAttributesMap.containsKey(id2base): otherdependentAttributes = mamIdToAttributesMap.get(id2base) iter3 = otherdependentAttributes.entrySet().iterator() while iter3.hasNext(): entry = iter3.next() name = entry.getKey() if name in removeAtts: continue value = entry.getValue() otherElement = Element(name) otherElement.setText(value) dependentElement.addContent(otherElement) relElement.addContent(dependentElement) rootElement.addContent(relElement) return rootElement
def buildIntermediateXML(objectMapFromARIS): try: ## Build XML intermediateXML = Document() rootElement = Element('data') intermediateXML.setRootElement(rootElement) cisElement = Element('cis') linksElement = Element('links') rootElement.addContent(cisElement) rootElement.addContent(linksElement) if objectMapFromARIS: ## Add CIs for objectFromARIS in objectMapFromARIS.values(): ciElement = Element('ci') if buildIntermediateCiXML(ciElement, objectFromARIS): cisElement.addContent(ciElement) ## Add relationships relationshipsFromObject = objectFromARIS.links if relationshipsFromObject: for relationshipFromObject in relationshipsFromObject: linkElement = Element('link') linkElement.setAttribute('type', relationshipFromObject.type) linkEndCount = 1 for linkEndCi in [objectMapFromARIS[relationshipFromObject.end1], objectMapFromARIS[relationshipFromObject.end2]]: endCiElement = Element('end' + str(linkEndCount) + 'ci') linkEndCount = linkEndCount + 1 if buildIntermediateCiXML(endCiElement, linkEndCi): linkElement.addContent(endCiElement) else: debugPrint(3, '[' + SCRIPT_NAME + ':buildIntermediateXML] Error building link end for link with GUID <%s>! Skipping...' % relationshipFromObject.id) linksElement.addContent(linkElement) return intermediateXML except: excInfo = logger.prepareJythonStackTrace('') debugPrint('[' + SCRIPT_NAME + ':buildIntermediateXML] Exception: <%s>' % excInfo) pass
def addRelations(rootElement, allRelationChildren, mamIdToAttributesMap): removeAtts = ['family', 'class'] iter = allRelationChildren.iterator() while iter.hasNext(): relElement = Element('relation') linkElement = iter.next() linkType = linkElement.getAttribute( 'targetRelationshipClass').getValue() targetParent = linkElement.getAttribute('targetParent').getValue() splitIdx_targetParent = string.rfind(targetParent, '.') targetParentFamilyName = targetParent[0:splitIdx_targetParent] targetParentClassName = targetParent[splitIdx_targetParent + 1:len(targetParent)] targetChild = linkElement.getAttribute('targetChild').getValue() splitIdx_targetChild = string.rfind(targetChild, '.') targetChildFamilyName = targetChild[0:splitIdx_targetChild] targetChildClassName = targetChild[splitIdx_targetChild + 1:len(targetChild)] id1base = '' id2base = '' if linkType != None and targetParent != None and targetChild != None: fieldChildren = linkElement.getChildren('field') if fieldChildren is not None: iter2 = fieldChildren.iterator() while iter2.hasNext(): fieldElement = iter2.next() fieldName = fieldElement.getAttributeValue('name') if fieldName == 'DiscoveryID1': id1base = fieldElement.getText() if fieldName == 'DiscoveryID2': id2base = fieldElement.getText() # set type typeElement = Element('type') typeElement.setText(linkType) relElement.addContent(typeElement) # set provider providerElement = Element('provider') if mamIdToAttributesMap.containsKey(id1base): otherProviderAttributes = mamIdToAttributesMap.get(id1base) iter3 = otherProviderAttributes.entrySet().iterator() while iter3.hasNext(): entry = iter3.next() name = entry.getKey() if name in removeAtts: continue value = entry.getValue() otherElement = Element(name) otherElement.setText(value) providerElement.addContent(otherElement) relElement.addContent(providerElement) # set dependent dependentElement = Element('dependent') if mamIdToAttributesMap.containsKey(id2base): otherdependentAttributes = mamIdToAttributesMap.get( id2base) iter3 = otherdependentAttributes.entrySet().iterator() while iter3.hasNext(): entry = iter3.next() name = entry.getKey() if name in removeAtts: continue value = entry.getValue() otherElement = Element(name) otherElement.setText(value) dependentElement.addContent(otherElement) relElement.addContent(dependentElement) rootElement.addContent(relElement) return rootElement
def addCIs(rootElement, allObjectChildren): iter = allObjectChildren.iterator() mamIdToAttributesMap = HashMap() while iter.hasNext(): ciElement = Element('ci') attributeMap = HashMap() objectElement = iter.next() familyClassName = objectElement.getAttributeValue('name') mamId = objectElement.getAttributeValue('mamId') # split dest class into family and class names splitIdx = string.rfind(familyClassName, '.') familyName = familyClassName[0:splitIdx] className = familyClassName[splitIdx + 1:len(familyClassName)] #print familyName, " - ", className if familyName != None and className != None: classElement = Element('class') classElement.setText(className) ciElement.addContent(classElement) attributeMap.put('class', className) familyElement = Element('family') familyElement.setText(familyName) ciElement.addContent(familyElement) attributeMap.put('family', familyName) # get the fields fieldChildren = objectElement.getChildren('field') if fieldChildren is not None: iter2 = fieldChildren.iterator() while iter2.hasNext(): fieldElement = iter2.next() fieldName = fieldElement.getAttributeValue('name') fieldValue = fieldElement.getText() if fieldValue == None or fieldValue == '': fieldValue = 'EMPTY' #print fieldName, ' - ', fieldValue setFieldElement = Element(fieldName) setFieldElement.setText(fieldValue) ciElement.addContent(setFieldElement) attributeMap.put(fieldName, fieldValue) mamIdToAttributesMap.put(mamId, attributeMap) rootElement.addContent(ciElement) return (mamIdToAttributesMap, rootElement)
def buildIntermediateXML(objectMapFromARIS): try: ## Build XML intermediateXML = Document() rootElement = Element('data') intermediateXML.setRootElement(rootElement) cisElement = Element('cis') linksElement = Element('links') rootElement.addContent(cisElement) rootElement.addContent(linksElement) if objectMapFromARIS: ## Add CIs for objectFromARIS in objectMapFromARIS.values(): ciElement = Element('ci') if buildIntermediateCiXML(ciElement, objectFromARIS): cisElement.addContent(ciElement) ## Add relationships relationshipsFromObject = objectFromARIS.links if relationshipsFromObject: for relationshipFromObject in relationshipsFromObject: linkElement = Element('link') linkElement.setAttribute('type', relationshipFromObject.type) linkEndCount = 1 for linkEndCi in [ objectMapFromARIS[relationshipFromObject.end1], objectMapFromARIS[relationshipFromObject.end2] ]: endCiElement = Element('end' + str(linkEndCount) + 'ci') linkEndCount = linkEndCount + 1 if buildIntermediateCiXML(endCiElement, linkEndCi): linkElement.addContent(endCiElement) else: debugPrint( 3, '[' + SCRIPT_NAME + ':buildIntermediateXML] Error building link end for link with GUID <%s>! Skipping...' % relationshipFromObject.id) linksElement.addContent(linkElement) return intermediateXML except: excInfo = logger.prepareJythonStackTrace('') debugPrint('[' + SCRIPT_NAME + ':buildIntermediateXML] Exception: <%s>' % excInfo) pass