+ def get_sitenodes(self, nodes):
+ n = []
+ for i in self.node_ids:
+ n.append(nodes[i])
+ return n
+
+ def add_adjacency(self, site):
+ self.adj_site_ids.add(site.id)
+ for n in site.node_ids:
+ self.adj_node_ids.add(n)
+
+
+class Slice:
+ def __init__(self, slice):
+ self.id = slice['slice_id']
+ self.name = slice['name']
+ self.node_ids = set(slice['node_ids'])
+ self.slice_tag_ids = slice['slice_tag_ids']
+
+ def get_tag(self, tagname, slicetags, node = None):
+ for i in self.slice_tag_ids:
+ tag = slicetags[i]
+ if tag.tagname == tagname:
+ if (not node) or (node.id == tag.node_id):
+ return tag
+ else:
+ return None
+
+ def get_nodes(self, nodes):
+ n = []
+ for id in self.node_ids:
+ n.append(nodes[id])
+ return n
+
+
+ # Add a new slice tag
+ def add_tag(self, tagname, value, slicetags, node = None):
+ record = {'slice_tag_id':None, 'slice_id':self.id, 'tagname':tagname, 'value':value}
+ if node:
+ record['node_id'] = node.id
+ else:
+ record['node_id'] = None
+ tag = Slicetag(record)
+ slicetags[tag.id] = tag
+ self.slice_tag_ids.append(tag.id)
+ tag.changed = True
+ tag.updated = True
+ return tag
+
+ # Update a slice tag if it exists, else add it
+ def update_tag(self, tagname, value, slicetags, node = None):
+ tag = self.get_tag(tagname, slicetags, node)
+ if tag and tag.value == value:
+ value = "no change"
+ elif tag:
+ tag.value = value
+ tag.changed = True
+ else:
+ tag = self.add_tag(tagname, value, slicetags, node)
+ tag.updated = True
+
+ def assign_egre_key(self, slicetags):
+ if not self.get_tag('egre_key', slicetags):
+ try:
+ key = free_egre_key(slicetags)
+ self.update_tag('egre_key', key, slicetags)
+ except:
+ # Should handle this case...
+ pass
+ return
+
+ def turn_on_netns(self, slicetags):
+ tag = self.get_tag('netns', slicetags)
+ if (not tag) or (tag.value != '1'):
+ self.update_tag('netns', '1', slicetags)
+ return
+
+ def turn_off_netns(self, slicetags):
+ tag = self.get_tag('netns', slicetags)
+ if tag and (tag.value != '0'):
+ tag.delete()
+ return
+
+ def add_cap_net_admin(self, slicetags):
+ tag = self.get_tag('capabilities', slicetags)
+ if tag:
+ caps = tag.value.split(',')
+ for cap in caps:
+ if cap == "CAP_NET_ADMIN":
+ return
+ else:
+ newcaps = "CAP_NET_ADMIN," + tag.value
+ self.update_tag('capabilities', newcaps, slicetags)
+ else:
+ self.add_tag('capabilities', 'CAP_NET_ADMIN', slicetags)
+ return
+
+ def remove_cap_net_admin(self, slicetags):
+ tag = self.get_tag('capabilities', slicetags)
+ if tag:
+ caps = tag.value.split(',')
+ newcaps = []
+ for cap in caps:
+ if cap != "CAP_NET_ADMIN":
+ newcaps.append(cap)
+ if newcaps:
+ value = ','.join(newcaps)
+ self.update_tag('capabilities', value, slicetags)
+ else:
+ tag.delete()
+ return