advt-merge.py.in 10.7 KB
Newer Older
1 2
#
# Copyright (c) 2010 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
22 23 24
#

#
25 26 27 28 29 30
# Merges two or more RSPEC advertisements
#
# [USAGE] advt-merge.py <destination_file> <source_file> [<source_file> ...]
#
# If <desination_file> does not exist, it will be created. 
# If destination file does exist, all the source files will be merged into it
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#

import datetime
import os
import sys 
import time
import xml.dom.minidom

CREATE_NEW_DESTINATION = 1
MERGE_INTO_EXISTING = 2

SUCCESS = 0
ERROR_INCONSISTENT_LINK_DATA = -1
ERROR_INVALID_INPUT = -2
ERROR_INVALID_OUTPUT = -3

47
TBROOT = "@prefix@"
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
PGENI_DOMAIN = "@PROTOGENI_DOMAIN@"
# A few other variables also ought to be added
# RSPEC_VER = "@RSPEC_VERSION@"

PGENI_ROOT = TBROOT + "/protogeni"
PATH_TO_SCHEMA = PGENI_ROOT + "/rspec/rspec-ad.xsd"

######## XXX: THIS HAS TO BE CHANGED! #########
#
# At some point, it should be able to use PGENI_DOMAIN and RSPEC_VER
XMLNS = "http://www.protogeni.net/resources/rspec/0.1"
# ------------------------------------------------------------------

DATETIME_FMT = '%Y-%m-%dT%H:%M:%S'


def printMessageAndReturnError (returnCode):
	if (returnCode == 0):
		print("Files merged successfully (" + str(returnCode) + ")")
	else:
		print("File merging failed (" + str(returnCode) + ")")
69
	return returnCode
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

# Returns the earliest date as a string from among the given datetimes
# ---
# String getEarliestDateTimes ([datetime])
def getEarliestDateTime (dateTimes):
	maxDateTime = datetime.datetime(datetime.MAXYEAR, 12, 31, 23, 59, 59, 999999)
	earliestDateTime = maxDateTime
	# We can't use a comparison operator directly between two datetime objects
	# but we can subtract two datetimes and compare them against a resulting
	# timedelta object. datetime.timedelta() returns 
	zeroDateTime = datetime.timedelta()
	for dt in dateTimes:
		if (dt - earliestDateTime < zeroDateTime):
			earliestDateTime = dt
	# This can realistically happen in only one case, the input list is empty
	if (earliestDateTime == maxDateTime):
		return ""
	return earliestDateTime.strftime(DATETIME_FMT)

# Returns the current date and time in international format as a string
def getCurrentDateTime ():
	return datetime.datetime.now().strftime(DATETIME_FMT)

# Returns the command to execute to validate the XML input
# ---
# String getValidationCmd (String)
def getValidationCmd (fileToValidate):
	return "xmllint --noout --schema " + PATH_TO_SCHEMA + " " + fileToValidate

# Returns the name of the uuid attribute, either (prefix_uuid, prefix_urn)
# ---
# String getUUIDAttr (xml.dom.Element, String)
def getUUIDAttr (element, prefix):
	if element.hasAttribute(prefix + "_urn"):
		return prefix + "_urn" 
	return prefix + "_uuid"

107
# Returns the name of the uuid attribute, (either component_uuid, component_urn)
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
# ---
# String getUUID (xml.dom.Element, String, String)
def getUUID (element, prefix, default=""):
	rv = element.getAttribute(getUUIDAttr(element, prefix))
	if rv != default:
		return rv
	return default

# Returns the contents of the child text node of element
# XXX: This will not return the expected value if the first child text node is a newline
# ---
# String getDataField (xml.dom.Element)
def getDataField (element):
	# Element could be a single element or null
	if (element == None):
		return ""
	# XXX: I don't like having to depend on the childNodes list to get the
	# value of the text node, but I don't see any reasonable alternative
	return (element.childNodes[0].nodeValue.strip())

# Returns the child node elementName of parent
# ---
# xml.dom.Element getElement (xml.dom.Element, String)
def getElement (parent, elementName):
	elements = parent.getElementsByTagName(elementName)
	if (len(elements) == 0):
		return None
	return elements[0]

# Boolean compareFields (xml.dom.Element, xml.dom.Element, String)
def compareFields (link1, link2, fieldName):
	link1FieldValue = getDataField(getElement(link1, fieldName))
	link2FieldValue = getDataField(getElement(link2, fieldName))
	if (link1FieldValue == link2FieldValue):
		return True
	return False

# Boolean compareCharacteristics (xml.dom.Element, xml.dom.Element)
def compareCharacteristics (link1, link2):
	
	if (compareFields(link1, link2, "bandwidth")
		and compareFields(link1, link2, "latency")
		and compareFields(link1, link2, "packet_loss") == True):
		return True
	return False
	
# Integer mergeRSpecs (String, String, Integer)
def mergeRSpecs(destFileName, srcFileNames, destFileAction):
	
	xmlDocument = xml.dom.minidom.Document()

	# This dictionary keeps track of the links that have been seen so far
	dLinks = {}
	# This dictionary keeps track of the external references that have been seen so far
	dExternal_refs = {}
	# This dictionary keeps track of all the nodes that have been seen so far
	dNodes = {}

	# This list keeps track of all the expiration times on the input files
	# Eventually, the expiration time of the merged advertisement,
	# will be the earliest among these.
	expirationTimes = []

	if (destFileAction == CREATE_NEW_DESTINATION):
		root = xmlDocument.createElement("rspec")
		root.setAttribute ("type", "advertisement")
		root.setAttribute ("generated", getCurrentDateTime())
		root.setAttribute ("xmlns", XMLNS)
	
	elif (destFileAction == MERGE_INTO_EXISTING):
		xmlDocument = xml.dom.minidom.parse(destFileName)
		
		# We know that there will be only one root 
		root = xmlDocument.getElementsByTagName("rspec")[0]
		
		expirationTime = root.getAttribute("valid_until")
		if (expirationTime != ""):
			expirationTimes.append(time.strptime(expirationTime, DATETIME_FMT))
		
		links = root.getElementsByTagName("link")
		for link in links:
			component_uuid = getUUID(link, "component")
			if not dLinks.has_key(component_uuid):                   
				dLinks[link.getAttribute(component_uuid)] = link
			else:
				if (not compareCharacteristics(link, dLinks.get(component_uuid))):
					print "ERROR: The links are not consistent. Mismatch found in " + component_uuid
					return ERROR_INCONSISTENT_LINK_DATA
				
		nodes = xmlDocument.getElementsByTagName("node")
		for node in nodes:
			dNodes[getUUID(node, "component")] = node
			
		refs = xmlDocument.getElementsByTagName("external_ref")
		for ref in refs:
			ref_key = getUUID(ref, "component_node")
			dExternal_refs[ref_key] = ref

	else: # This should never happen
		print "ERROR: Something bad happened. An invalid parameter was passed" 
		return False
	
	xmlDocument.appendChild(root)
	for srcFileName in srcFileNames:
		print "Opening file " + srcFileName
		srcXmlDocument = xml.dom.minidom.parse(srcFileName)

		srcRoot = srcXmlDocument.getElementsByTagName("rspec")[0]
		expirationTime = srcRoot.getAttribute("valid_until")
		if (expirationTime != ""):
			expirationTimes.append(time.strptime(expirationTime, DATETIME_FMT))

		nodes = srcXmlDocument.getElementsByTagName("node")
		print "Found " + str(len(nodes)) + " nodes"
		for node in nodes:
			component_uuid = getUUID(node, "component")
			if (dExternal_refs.has_key(component_uuid)):
				del dExternal_refs[component_uuid]
			dNodes[component_uuid] = node
			root.appendChild(node)
		
		refs = srcXmlDocument.getElementsByTagName("external_ref")
		for ref in refs:
			ref_key = getUUID(ref, "component_node")
			dExternal_refs[ref_key] = ref
		
		links = srcXmlDocument.getElementsByTagName("link")
		for link in links:
			component_uuid = getUUID(link, "component")
			if not dLinks.has_key(component_uuid):
				dLinks[component_uuid] = link
			else:
				if(not compareCharacteristics(link, dLinks.get(component_uuid))):
					print "ERROR: The links are not consistent. Mismatch found in " + component_uuid
242
					return -1
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
			root.appendChild(link)

	for key in dExternal_refs.keys():
		root.appendChild(dExternal_refs[key])
		
	expirationTime = getEarliestDateTime(expirationTimes)
	if (expirationTime != ""):
		root.setAttribute("valid_until", expirationTime)
		
	print "Writing to " + destFileName
	rspecFile = open (destFileName, "w")
	# -----------------------------------------------
	# Use this to write a prettily-formatted XML file
	# xmlDocument.writexml (rspecFile,"\t","\t","\n") 
	# ***********************************************
	# Use this to actually do work. 
	# Xerces has trouble reading prettily formatted XML files. It includes the tab and newline characters as part of the element names
	xmlDocument.writexml (rspecFile)
	# -----------------------------------------------
	rspecFile.close ()
	return SUCCESS

def main ():
	
	rv = False
	
	helpString = "[USAGE] advt-merge.py <destination_file> <source_file> [<source_file> ...]\nIf <desination_file> does not exist, it will be created. If destination file does exist, all the source files will be merged into it"
	
	# At least two files should be specified
	if len(sys.argv) < 3:
		print helpString
		return 
	else:
		# Validate all rspecs
		destFileName = sys.argv[1]
		if (os.path.exists(destFileName)):
			returnCode = os.system(getValidationCmd(destFileName))
			if (returnCode != SUCCESS):
				print "ERROR: " + destFileName + " does not validate"
282
				return printMessageAndReturnError(returnCode)
283 284 285 286 287 288 289 290 291 292
			destFileAction = MERGE_INTO_EXISTING
		else:
			destFileAction = CREATE_NEW_DESTINATION
			
		srcFileNames = []
		for index in range(2, len(sys.argv)):
			returnCode = os.system(getValidationCmd(sys.argv[index]))
			# A non-zero return value indicates that the command failed
			if (returnCode != SUCCESS):
				print "ERROR: " + sys.argv[index] + " does not validate"
293
				return printMessageAndReturnError(returnCode)
294 295
			srcFileNames.append(sys.argv[index])
			
296 297 298 299 300 301
		# If the destination file already exists, it has to be included in the
		# list of consistency checks
		inputFileNames = srcFileNames
		if (destFileAction == MERGE_INTO_EXISTING):
			inputFileNames.append(destFileName)
		
302 303 304 305 306 307 308 309 310 311 312 313 314
		rv = mergeRSpecs(destFileName, srcFileNames, destFileAction)
		
		# Check if the destination validates. It should, but just in case
		returnCode = os.system(getValidationCmd(destFileName))
		if (returnCode != SUCCESS):
			print "ERROR: " + destFileName + " does not validate"
		
	return printMessageAndReturnError(rv)
		
# Call the main function if this is called from the command line
if __name__ == "__main__":
	main ()