from PLC.Table import Row, Table
from PLC.Nodes import Nodes,Node
-from PLC.ForeignNodes import ForeignNodes,ForeignNode
-from PLC.ForeignSlices import ForeignSlices,ForeignSlice
+from PLC.Slices import Slices,Slice
class Peer (Row):
"""
assert 'peer_id' in self
# remove nodes depending on this peer
- for foreign_node_id in self.get_foreign_nodes():
- try:
- foreign_node = ForeignNodes(self.api,[foreign_node_id])[0]
- foreign_node.delete(commit)
- except:
- print "Glitch : a foreign node instance was uncleanly deleted"
+ for foreign_node in Nodes (self.api, self.get_node_ids()):
+ foreign_node.delete(commit)
# remove the peer
self['deleted'] = True
self.sync(commit)
- def get_foreign_nodes (self):
+ def get_node_ids (self):
"""
- returns a list of the foreign nodes in this peer
+ returns a list of the node ids in this peer
"""
sql="SELECT node_ids FROM peer_nodes WHERE peer_id=%d"%self['peer_id']
node_ids = self.api.db.selectall(sql)
return node_ids[0]['node_ids']
- def manage_node (self, foreign_node, add_if_true, commit=True):
- """
- associate/dissociate a foreign node to/from a peer
- foreign_node is a local object that describes a remote node
- convention is:
- if add_if_true is None : performs dissociation
- otherwise: performs association
- """
-
- assert 'peer_id' in self
- assert 'node_id' in foreign_node
-
- peer_id = self['peer_id']
- node_id = foreign_node ['node_id']
-
- if add_if_true:
- ### ADDING
- sql = "INSERT INTO peer_node VALUES (%d,%d)" % (peer_id,node_id)
- self.api.db.do(sql)
- if self['node_ids'] is None:
- self['node_ids']=[node_id,]
- else:
- self['node_ids'].append(node_id)
- ### DELETING
- else:
- sql = "DELETE FROM peer_node WHERE peer_id=%d AND node_id=%d" % (peer_id,node_id)
- self.api.db.do(sql)
- self['node_ids'].remove(node_id)
-
- if commit:
- self.api.db.commit()
-
- def manage_slice (self, foreign_slice, add_if_true, commit=True):
- """
- associate/dissociate a foreign node to/from a peer
- foreign_slice is a local object that describes a remote slice
- alien_id is the unique id as provided by the remote peer
- convention is:
- if add_if_true is None : performs dissociation
- otherwise: performs association
- """
-
- assert 'peer_id' in self
- assert 'slice_id' in foreign_slice
-
- peer_id = self['peer_id']
- slice_id = foreign_slice ['slice_id']
-
- if add_if_true:
- ### ADDING
- sql = "INSERT INTO peer_slice VALUES (%d,%d)" % (peer_id,slice_id)
- self.api.db.do(sql)
- if self['slice_ids'] is None:
- self['slice_ids']=[slice_id,]
- else:
- self['slice_ids'].append(slice_id)
- ### DELETING
- else:
- sql = "DELETE FROM peer_slice WHERE peer_id=%d AND slice_id=%d" % (peer_id,slice_id)
- self.api.db.do(sql)
- self['slice_ids'].remove(slice_id)
-
- if commit:
- self.api.db.commit()
-
def refresh_nodes (self, peer_get_nodes):
"""
refreshes the foreign_nodes and peer_node tables
# we get the whole table just in case
# a host would have switched from one plc to the other
- local_foreign_nodes = ForeignNodes (self.api)
+ local_foreign_nodes = Nodes (self.api,None,None,'foreign')
+
# index it by hostname for searching later
- local_foreign_nodes_index = local_foreign_nodes.dict('hostname')
+ #local_foreign_nodes_index=local_foreign_nodes.dict('hostname')
+ local_foreign_nodes_index={}
+ for node in local_foreign_nodes:
+ local_foreign_nodes_index[node['hostname']]=node
### mark entries for this peer outofdate
old_count=0;
try:
foreign_node = local_foreign_nodes_index[hostname]
if foreign_node['peer_id'] != peer_id:
- ### the node has changed its plc, needs to update peer_node
- old_peer_id = foreign_node['peer_id']
- old_peers=Peers(self.api,[peer_id])
- assert old_peer[0]
- # remove from previous peer
- old_peers[0].manage_node(foreign_node,False,False)
- # add to new peer
- self.manage_node(foreign_node,True,True)
+# ### the node has changed its plc
foreign_node['peer_id'] = peer_id
### update it anyway: copy other relevant fields
for field in remote_fields:
foreign_node.uptodate=True
foreign_node.sync()
except:
- new_foreign_node = ForeignNode(self.api, {'hostname':hostname})
+ new_foreign_node = Node(self.api, {'hostname':hostname})
+ new_foreign_node['peer_id']=peer_id
for field in remote_fields:
new_foreign_node[field]=node[field]
### need to sync so we get a node_id
new_foreign_node.sync()
new_foreign_node.uptodate = True
- self.manage_node(new_foreign_node,True,True)
+# self.manage_node(new_foreign_node,True,True)
local_foreign_nodes_index[hostname]=new_foreign_node
### delete entries that are not uptodate
# we get the whole table just in case
# a host would have switched from one plc to the other
- local_foreign_slices = ForeignSlices (self.api)
+ local_foreign_slices = Slices (self.api,{'~peer_id':None})
# index it by name for searching later
local_foreign_slices_index = local_foreign_slices.dict('name')
### scan the new entries, and mark them uptodate
new_count=0
for slice in peer_get_slices:
+
### ignore system-wide slices
if slice['creator_person_id'] == 1:
continue
try:
foreign_slice = local_foreign_slices_index[name]
if foreign_slice['peer_id'] != peer_id:
- ### the slice has changed its plc, needs to update peer_slice
- old_peer_id = foreign_slice['peer_id']
- old_peers=Peers(self.api,[peer_id])
- assert old_peer[0]
- # remove from previous peer
- old_peers[0].manage_slice(foreign_slice,False,False)
- # add to new peer
- self.manage_slice(foreign_slice,True,True)
- foreign_slice['peer_id'] = peer_id
+ # more suspucious ? - the slice moved on another peer
+ foreign_slice['peer_id'] = peer_id;
except:
- foreign_slice = ForeignSlice(self.api, {'name':name})
+ foreign_slice = Slice(self.api, {'name':name})
+ foreign_slice['peer_id']=self['peer_id']
# ### xxx temporary
# foreign_slice['site_id']=1
### need to sync so we get a slice_id
foreign_slice.sync()
- self.manage_slice(foreign_slice,True,True)
# insert in index
local_foreign_slices_index[name]=foreign_slice
updated_node_ids = []
for alien_node_id in slice['node_ids']:
try:
- local_node_id=self.locate_alien_node_id_in_foreign_nodes(peer_foreign_nodes_dict,alien_node_id)
+ local_node_id=self.locate_alien_node_id_in_foreign_nodes(peer_foreign_nodes_dict,
+ alien_node_id)
updated_node_ids.append(local_node_id)
except:
# this node_id is not in our scope