From 2cc883a37e410bda0fe1cd038b76095481b2185f Mon Sep 17 00:00:00 2001
From: Guilherme Sperb Machado <gsm@machados.org>
Date: Sun, 14 Sep 2014 16:08:26 +0200
Subject: [PATCH] modified ipv6.py and update_ipv6addr_slivertag.py --
 identation, removed unused methods/variables

---
 plugins/ipv6.py                      | 94 ++++++++--------------------
 plugins/update_ipv6addr_slivertag.py | 19 +++---
 2 files changed, 38 insertions(+), 75 deletions(-)

diff --git a/plugins/ipv6.py b/plugins/ipv6.py
index 05b7446..cbc4ea1 100644
--- a/plugins/ipv6.py
+++ b/plugins/ipv6.py
@@ -1,5 +1,8 @@
 """
-IPv6 test! version: 0.3
+Description: IPv6 Support and Management to Slices
+ipv6 nodemanager plugin
+Version: 0.5
+Author: Guilherme Sperb Machado <gsm@machados.org>
 """
 
 import logger
@@ -16,7 +19,6 @@ from xml.dom.minidom import parseString
 priority=4
 
 radvdConfFile = '/etc/radvd.conf'
-ipv6addrtag = 'ipv6_address'
 
 def start():
     logger.log("ipv6: plugin starting up...")
@@ -61,7 +63,7 @@ def buildLibvirtDefaultNetConfig(dom):
 	# build the whole thing
 	natElem.appendChild(portElem)
 	forwardElem.appendChild(natElem)
-	
+
 	dhcpElem.appendChild(rangeElem)
 	ipElem0.appendChild(dhcpElem)
 	networkElem.appendChild(nameElem)
@@ -81,7 +83,7 @@ def checkForIPv6(defaultNetworkConfig):
                                 logger.log("ipv6: the configuration already have an IPv6 address/prefix set for the slivers! %s/%s" % (ip.getAttribute('address'), ip.getAttribute('prefix')) )
 				hasIPv6 = True
 	return hasIPv6
-			
+
 
 
 def addIPv6(defaultNetworkConfig, ipv6addr, prefix):
@@ -111,7 +113,7 @@ def changeIPv6(dom, ipv6addr, prefix):
 			ip.setAttribute("address", ipv6addr)
 			ip.setAttribute("prefix", prefix)
 	return dom
-			
+
 
 def removeIPv6(dom):
 	networks = dom.getElementsByTagName('network')
@@ -121,7 +123,7 @@ def removeIPv6(dom):
         	        if ip.getAttribute("family")=='ipv6':
 				network.removeChild(ip)
 	return dom
-	
+
 
 def checkIfIPv6IsDifferent(dom, ipv6addr, prefix):
 	netnodes = dom.getElementsByTagName('network')
@@ -178,68 +180,25 @@ interface virbr0
 	logger.log("ipv6: set up process finalized. Enabled IPv6 address to the slivers!")
 
 def cleanUp(networkLibvirt, connLibvirt, networkElem):
-	dom = removeIPv6(networkElem)
-	newXml = dom.toxml()
-        networkLibvirt.undefine()
-        networkLibvirt.destroy()
-        # TODO: set autostart for the network
-        connLibvirt.networkCreateXML(newXml)
-        networkDefault = connLibvirt.networkDefineXML(newXml)
-	setAutostart(networkDefault)
-	killRadvd()
-	logger.log("ipv6: cleanup process finalized. The IPv6 support on the slivers was removed.")
+    dom = removeIPv6(networkElem)
+    newXml = dom.toxml()
+    networkLibvirt.undefine()
+    networkLibvirt.destroy()
+    # TODO: set autostart for the network
+    connLibvirt.networkCreateXML(newXml)
+    networkDefault = connLibvirt.networkDefineXML(newXml)
+    setAutostart(networkDefault)
+    killRadvd()
+    logger.log("ipv6: cleanup process finalized. The IPv6 support on the slivers was removed.")
 
 def killRadvd():
-	commandKillRadvd = ['killall', 'radvd']
-        logger.log_call(commandKillRadvd, timeout=15*60)
+    commandKillRadvd = ['killall', 'radvd']
+    logger.log_call(commandKillRadvd, timeout=15*60)
 
 def startRadvd():
 	commandRadvd = ['radvd']
         logger.log_call(commandRadvd, timeout=15*60)
 
-def getSliverTagId(slivertags):
-	for slivertag in slivertags:
-		if slivertag['tagname']==ipv6addrtag:
-			return slivertag['slice_tag_id']
-
-
-def SetSliverTag(plc, data, tagname):
-
-    for sliver in data['slivers']:
-	# TODO: what about the prefixlen? should we add on it as well?
-	# here, I'm just taking the ipv6addr (value)
-	value,prefixlen = tools.get_sliver_ipv6(sliver['name'])
-
-    	node_id = tools.node_id()
-	logger.log("ipv6: slice %s" % (slice) )
-	logger.log("ipv6: nodeid %s" % (node_id) )
-	slivertags = plc.GetSliceTags({"name":slice['name'],"node_id":node_id,"tagname":tagname})
-	logger.log(repr(str(slivertags)))
-	for tag in slivertags:
-		logger.log(repr(str(tag)))
-
-	ipv6addr = plc.GetSliceIPv6Address(slice['name'])
-	# if the value to set is null...
-	if value is None:
-		if ipv6addr is not None or len(ipv6addr)==0:
-			# then, let's remove the slice tag
-			slivertag_id = getSliverTagId(slivertags)
-			plc.DeleteSliceTag(slivertag_id)
-	else:
-		# if the ipv6 addr set on the slice does not exist yet, so, let's add it
-		if (len(ipv6addr)==0 or ipv6addr is None) and len(value)>0:
-			try:
-				slivertag_id=plc.AddSliceTag(slice['name'],tagname,value,node_id)
-				logger.log("ipv6: slice tag added to slice %s" % (slice['name']) )
-			except:
-				logger.log_exc ("ipv6: could not set ipv6 addr tag to the slive. slice=%(slice['name'])s tag=%(tagname)s node_id=%(node_id)d" % locals() )
-		# if the ipv6 addr set on the slice is different on the value provided, let's update it
-		if len(value)>0 and ipv6addr!=value:
-			#slivertag_id=slivertags[0]['slice_tag_id']
-			slivertag_id = getSliverTagId(slivertags)
-	        	plc.UpdateSliceTag(slivertag_id,value)
-	
-
 def GetSlivers(data, config, plc):
 
     #return
@@ -250,7 +209,7 @@ def GetSlivers(data, config, plc):
 	#logger.log("tools: result=%s" % (str(result)) )
         #tools.remove_all_ipv6addr_hosts(sliver['name'], data['hostname'])
     #return
-    
+
 
     type = 'sliver.LXC'
 
@@ -260,11 +219,12 @@ def GetSlivers(data, config, plc):
 	logger.log('ipv6: get interface 1: %r'%(interface))
 	if 'interface_tag_ids' in interface:
 		interface_tag_ids = "interface_tag_ids"
-	        interface_tag_id = "interface_tag_id"
-		settings = plc.GetInterfaceTags({interface_tag_id:interface[interface_tag_ids]})
-		isSliversIPv6PrefixSet = False
-		for setting in settings:
+        interface_tag_id = "interface_tag_id"
+        settings = plc.GetInterfaceTags({interface_tag_id:interface[interface_tag_ids]})
+        isSliversIPv6PrefixSet = False
+        for setting in settings:
 			#logger.log(repr(setting))
+            # TODO: create a static variable to describe the "sliversipv6prefix" tag
 			if setting['tagname']=='sliversipv6prefix':
 				ipv6addrprefix = setting['value'].split('/', 1)
 				ipv6addr = ipv6addrprefix[0]
@@ -298,7 +258,7 @@ def GetSlivers(data, config, plc):
 							logger.log("ipv6: trying to reboot the slivers...")
 							tools.reboot_sliver('blah')
 				isSliversIPv6PrefixSet = True
-		if not(isSliversIPv6PrefixSet):
+        if not(isSliversIPv6PrefixSet):
 			# connecting to the libvirtd
 			connLibvirt = Sliver_Libvirt.getConnection(type)
 			list = connLibvirt.listAllNetworks()
diff --git a/plugins/update_ipv6addr_slivertag.py b/plugins/update_ipv6addr_slivertag.py
index a1190cf..c10c83d 100644
--- a/plugins/update_ipv6addr_slivertag.py
+++ b/plugins/update_ipv6addr_slivertag.py
@@ -1,5 +1,8 @@
 """
-IPv6 test! version: 0.3
+Description: Update the IPv6 Address sliver tag accordingly to the IPv6 address set
+update_ipv6addr_slivertag nodemanager plugin
+Version: 0.5
+Author: Guilherme Sperb Machado <gsm@machados.org>
 """
 
 import logger
@@ -36,16 +39,16 @@ def SetSliverTag(plc, data, tagname):
 	# here, I'm just taking the ipv6addr (value)
 	value,prefixlen = tools.get_sliver_ipv6(slice['name'])
 
-    	node_id = tools.node_id()
-	slivertags = plc.GetSliceTags({"name":slice['name'],"node_id":node_id,"tagname":tagname})
+    node_id = tools.node_id()
+    slivertags = plc.GetSliceTags({"name":slice['name'],"node_id":node_id,"tagname":tagname})
 	#logger.log(repr(str(slivertags)))
 	#for tag in slivertags:
 	#	logger.log(repr(str(tag)))
 
-	ipv6addr = plc.GetSliceIPv6Address(slice['name'])
-	logger.log("update_ipv6addr_slivertag: slice=%s getSliceIPv6Address=%s" % (slice['name'],ipv6addr) )
+    ipv6addr = plc.GetSliceIPv6Address(slice['name'])
+    logger.log("update_ipv6addr_slivertag: slice=%s getSliceIPv6Address=%s" % (slice['name'],ipv6addr) )
 	# if the value to set is null...
-	if value is None:
+    if value is None:
 		if ipv6addr is not None:
 			# then, let's remove the slice tag
 			slivertag_id = getSliverTagId(slivertags)
@@ -57,7 +60,7 @@ def SetSliverTag(plc, data, tagname):
 					logger.log("update_ipv6addr_slivertag: slice tag not deleted for slice=%s" % (slice['name']) )
 		# if there's no ipv6 address anymore, then remove everything from the /etc/hosts
 		tools.remove_all_ipv6addr_hosts(slice['name'], data['hostname'])
-	else:
+    else:
 		# if the ipv6 addr set on the slice does not exist yet, so, let's add it
 		if (ipv6addr is None) and len(value)>0:
 			try:
@@ -75,7 +78,7 @@ def SetSliverTag(plc, data, tagname):
         	if not result:
 			tools.remove_all_ipv6addr_hosts(slice['name'], data['hostname'])
 			tools.add_ipv6addr_hosts_line(slice['name'], data['hostname'], value)
-	logger.log("update_ipv6addr_slivertag: finishing the update process for slice=%s" % (slice['name']) )
+    logger.log("update_ipv6addr_slivertag: finishing the update process for slice=%s" % (slice['name']) )
 
 def GetSlivers(data, config, plc):
 
-- 
2.47.0