def updateNow(self, timeout = 60): """ Performs the actual updating process. """ from g4dsconfigurationcontroller import getRoutingController from communitymanager import getMemberManager, getCommunityManager peerList = {} # the dict here is a bit chicky - but the simpliest way to avoid duplicates # get the ids of connected gateways and download their routing tables for communityid in getMemberManager().getLocalMember().getCommunityIds(): comm = getCommunityManager().getCommunity(communityid) for gw in comm.getSourceGateways(): if gw.getMemberId() != getMemberManager().getLocalMember().getId(): peerList[gw.getMemberId()] = None peerList = peerList.keys() for peerId in peerList: routesRemote = getRoutingController().downloadRoutingTable(peerId, timeout = timeout) from routingtablemanager import getRoutingTableManager import thread thread.start_new_thread(getRoutingTableManager().applyRoutingTableFromNode, (peerId, routesRemote)) from g4dslogging import getDefaultLogger, ROUTING_TABLE_UPDATED getDefaultLogger().newMessage(ROUTING_TABLE_UPDATED, 'Routing table updated')
def recalculateRoutingTable(self): """ Creates routing entries from the scratch by processing gateway information. The values for directly reachable communities are applied. Furthermore, communities, which may be reached through one community only (1 hop) are put into as well. The rest should be sorted out be the dynamic routing faciliites. """ numberOfEntries = 0 tmpList = [] # we have to maintain this list with new entries; just in case # the routing table was not flushed beforehand # first process directly reachable communities - cost = 1 from communitymanager import getMemberManager, getCommunityManager for communityid in getMemberManager().getLocalMember().getCommunityIds(): # everybody can route into its own community ...that's just important to pass it on to any gw within the community entry = RoutingTableEntry(None, communityid, communityid, getMemberManager().getLocalMember().getId(), communityid, 1) getRoutingTableManager().addEntry(entry) numberOfEntries += 1 tmpList.append(entry) # get all the gateways of the directly connected communities for communityid in getMemberManager().getLocalMember().getCommunityIds(): comm = getCommunityManager().getCommunity(communityid) for gw in comm.getSourceGateways(): # if it's myself, I can reach it directly (cost 1) if gw.getMemberId() == getMemberManager().getLocalMember().getId(): entry = RoutingTableEntry(None, communityid, gw.getDestinationCommunityId(), gw.getMemberId(), communityid, 1) getRoutingTableManager().addEntry(entry) numberOfEntries += 1 tmpList.append(entry) # now get all the entries with cost 2 # what do we do: We check the available entries with cost 1 and have a look, which (source) gateways are # available for them (for their destination gateway) for item in tmpList: srcTC = item.getDestinationTC() # the destination tc of the hop 1 entry will be the src tc of the hop 2 entry comm = getCommunityManager().getCommunity(srcTC) for gw in comm.getSourceGateways(): entry = RoutingTableEntry(None, item.getSourceTC(), gw.getDestinationCommunityId(), gw.getMemberId(), srcTC, 2) getRoutingTableManager().addEntry(entry) numberOfEntries += 1 from g4dslogging import getDefaultLogger, ROUTING_TABLE_UPDATED_MANUALLY getDefaultLogger().newMessage(ROUTING_TABLE_UPDATED_MANUALLY, 'Routing table recalculated - added %d entries.' %(numberOfEntries)) return numberOfEntries
def findBestEndpointForMember(self, memberid, communityid = None, allowDefaultCommunity = 0, allowRouting =1): """ Picks the most approriate endpoint for the given member. Per default, the default community is disabled here. Currently, simply the first one is taken. """ from config import default_tc_id from communicationmanager import getEndpointManager from communitymanager import getMemberManager negCommunityList = [] if not allowDefaultCommunity: negCommunityList.append(default_tc_id) endpoints = getEndpointManager().getEndpointsForMember(memberid, communityid, negCommunityList) # if no routing allowed, remove the entries where the TC is not one, we are subscribed to routingless_endpoints = [] local = getMemberManager().getLocalMember() for e in endpoints: try: local.getCommunityIds().index(e.getCommunityId()) routingless_endpoints.append(e) except ValueError, msg: pass
def _decodeGroup(self, type, wildcard): """ Processes wildcard information in one group. @param type: Type of role; either actor, action or target @type type: C{String} @param wildcard: Wildcard string as given in the XML description (most likely a star) @type wildcard: C{String} """ from errorhandling import G4dsDependencyException retList = [] if type == 'membergroup': from communitymanager import getMemberManager if wildcard == '*': # all members - easy stuff return getMemberManager().getMemberIds() elif wildcard[0] == 'C': # here we want all the members of a certain community from communitymanager import getCommunityManager return getCommunityManager().getCommunity(wildcard).getMembers() elif wildcard[0] == 'S': # all the members of a service from servicerepository import getServiceManager return getServiceManager().getService(wildcard).getMembers() elif type == 'communitygroup': from communitymanager import getCommunityManager if wildcard == '*': # that should be all communities then return getCommunityManager().getCommunityIds() elif type == 'servicegroup': from servicerepository import getServiceManager if wildcard == '*': # all the services here return getServiceManager().getServiceIds() else: raise G4dsDependencyException('Policy error - unknown group type "%s".' %(type)) return retList
def getNexthopForCommunity(self, destinationcommunityid, sourcecommunityid = None): """ Returns the member id of the next hop on the route towards the final destination's community. Automatically, the route with the lowest costs for this destination community is chosen. If L{sourcecommunityid} is not given, any will be chosen, in fact the one which causes the lowest costs. """ if not sourcecommunityid: # ok, there is not src given, so let's just iterate through the communities, the local member # is member of; take the one with the shortest route finally. from communitymanager import getMemberManager from errorhandling import G4dsCommunicationException retList = [] for commid in getMemberManager().getLocalMember().getCommunityIds(): try: bestForSource = self.getNexthopForCommunity(destinationcommunityid, commid) retList.append(bestForSource) except G4dsCommunicationException, msg: # fair enough; nothing to get for this src pass if not len(retList): raise G4dsCommunicationException('No route found for community %s.' %(destinationcommunityid)) fastest = retList[0] for entry in retList[1:]: if entry[2] < fastest[2]: fastest = entry return fastest
def testUpdatingAndDeletingOnManager(): from communitymanager import getMemberManager, Member from communicationmanager import getEndpointManager, Endpoint, getProtocolManager from securitymanager import getCredentialManager, Credential, getAlgorithmManager m = Member('123','kk test', '<mdl/>','1.0.0.0','2005-07-08') #m.addCommunity('C760394') getMemberManager().updateMember(m, 1) alg = getAlgorithmManager().getAlgorithmByNameAndInsert('nikos alg') getEndpointManager().removeEndpointsForMember(m.getId()) c = Credential(None, alg.getId(),'','key5','123') getCredentialManager().removeCredentialsForMember(m.getId()) getCredentialManager().addCredential(c) protocol = getProtocolManager().getProtocolByNameAndInsert('soap') e = Endpoint(None, '123', 'C426009', protocol.getId(), 'http://localhost', c.getId()) getEndpointManager().addEndpoint(e)
def installNewMember(i, memberid = None): """ Put myself in the member list. """ _printAction (i, "Initialise member database with myself as the only member",1) _printAction(i+1, 'Create new member') from communitymanager import getMemberManager from communitymanager import Member member = Member(memberid, "temp description", "<mdl/>", "", "2005-07-05") #output (i+1, "New Member '" + member.getName() + "'") _finishActionLine() _printAction(i+1, 'Add member to local manager') try: getMemberManager().addMember(member) _finishActionLine() except Exception, msg: _finishActionLine(SUCESS_NEG) raise Exception(msg)
def dispatch(self, message, incomingmessageid): """ Unwrappes the message and tries to deliver directly, or if not possible through another routing hop. """ ## print "\tRouting Dispatcher: Received something to pass on." from g4dslogging import getDefaultLogger, COMMUNICATION_INCOMING_MSG_DETAILS getDefaultLogger().newMessage(COMMUNICATION_INCOMING_MSG_DETAILS, '-- Control Msg - SS: Routing Engine') from messagewrapper import getControlMessageWrapper action, sucess, args, unwrapped = getControlMessageWrapper().unwrapSSRoutingMessage(message) destination = args['destination'] protocol = args['protocol'] community = args['community'] from authorisationcontroller import getAuthorisationController from messagehandler import getMessageContextController sourceCommunity = getMessageContextController().getValue(incomingmessageid, 'communityid') ## # let's check, whether the sender of this message is allowed to route into the community ## if not getAuthorisationController().validate(getMessageContextController().getValue(incomingmessageid, 'senderid'), ## sourceCommunity, 'g4ds.routing.route'): ## return from communitymanager import getMemberManager # check first, whether we are the final receipient if getMemberManager().getLocalMember().getId() == destination: # great stuff - pass it to the global dispatcher from messagehandler import getGlobalDispatcher getGlobalDispatcher().dispatch(protocol, unwrapped) else: args = {} args['destination'] = destination args['protocol'] = protocol args['community'] = community from messagewrapper import getControlMessageWrapper wrapped, doc, element = getControlMessageWrapper().wrapSSRoutingMessage('1', args = args, data = unwrapped) from g4dsconfigurationcontroller import getOutgoingControlMessagesHandler, CONTROL_ROUTER # check, whether we can reach the dest community directly try: getMemberManager().getLocalMember().getCommunityIds().index(community) # great to know; but are we allowed this action? if not getAuthorisationController().validate(getMemberManager().getLocalMember().getId(), community, 'g4ds.routing.route'): raise ValueError('I am in the dest community; but I am not allowed to route into it. Let us try to find somebody else.') # unfortunately, we can only check the dest tc with the access control - let's check for scr / dest combination additionally for gw in getMemberManager().getLocalMember().getGateways(): if gw.getSourceCommunityId() == sourceCommunity and gw.getDestinationCommunityId() == community: getOutgoingControlMessagesHandler().sendMessage(destination, CONTROL_ROUTER, "Routing message", wrapped, communityid = community) raise ValueError('I am in the dest community; but I am not allowed to route into it. Let us try to find somebody else.') except ValueError, msg: # ok - looks like we can only pass it on to the next hop gateway_member_id, peercommunity, hops = getRoutingTableManager().getNexthopForCommunity(community) # are we allowed this action then? if not getAuthorisationController().validate(getMemberManager().getLocalMember().getId(), peercommunity, 'g4ds.routing.route'): return # ah, fair enough - is it also allowed for the combination src TC / dst TC? for gw in getMemberManager().getLocalMember().getGateways(): if gw.getSourceCommunityId() == sourceCommunity and gw.getDestinationCommunityId() == peercommunity: getOutgoingControlMessagesHandler().sendMessage(gateway_member_id, CONTROL_ROUTER, "Routing message", wrapped, communityid = peercommunity)
def testCommunityManager(): cm = communitymanager.getCommunityManager() mm = communitymanager.getMemberManager() ## c = communitymanager.Community(None,"Community F","pas de comment","<tcdl/>","0.9.9","2005-06-28") ## m = communitymanager.Member(None, "Member F", "<mdl>Member F description file</mdl>","0.9.9", "2005-06-28") ## ## cm.addCommunity(c) ## mm.addMember(m) ## ## c2 = cm.getCommunity('C10001') ## ## c.addMember(m.getId()) ## m.addCommunity(c.getId()) ## c2.addMember(m.getId()) ## m.addCommunity(c2.getId()) ## c2.addAuthority(m.getId()) ## m.addAuthorityRole(c2.getId()) ## c.addAuthority(m.getId()) ## m.addAuthorityRole(c.getId()) ## communitymanager.CommunityGateway(m.getId(), c.getId(), c2.getId(), 1, 1) ## print cm, "\n", mm ## printCommunities() ## printMembers() ## print "" ## from communitymanager_db import CM_DB ## db = CM_DB() ## db.addProtocolToCommunity('C001', 'P906688') ## print db.getProtocolsForCommunity('C001') community = cm.getCommunity('C001') from communicationmanager import getProtocolManager ## prot = getProtocolManager().getProtocolByNameAndInsert('soap') ## community.addProtocol(prot.getId()) print community.getProtocols() from securitymanager import getAlgorithmManager alg = getAlgorithmManager().getAlgorithmByNameAndInsert('rsa') community.addAlgorithm(alg.getId()) print community.getAlgorithms()
def sendMessage(self, message, endpoint): """ Tries to deliver a message directly, if impossible via wrapping into routing message. All messages should be passed here and not be sent from anywhere else. The routing engine looks, whether the given endpoint may be reached directly from the local node. If this is possible, the message is sent off directly. If, however, this is not possible, the message is wrapped into a routing message and gateways are tried to identify for passing the message through the topology. """ # check, whether I am in the community of the given endpoint, if not - we need to attempt to route this message tc = endpoint.getCommunityId() from communitymanager import getMemberManager local = getMemberManager().getLocalMember() ## try: ## local.getCommunityIds().index(tc) from communicationmanager import getProtocolManager, getEndpointManager if len(getEndpointManager().getEndpointsForMember(local.getId(), tc)): from g4dslogging import getDefaultLogger, COMMUNICATION_OUTGOING_MSG, COMMUNICATION_OUTGOING_MSG_DETAILS getDefaultLogger().newMessage(COMMUNICATION_OUTGOING_MSG, 'New outgoing message - direct delivery (%s | %s)' %(endpoint.getMemberId(), tc)) getDefaultLogger().newMessage(COMMUNICATION_OUTGOING_MSG_DETAILS, '-- Endpoint %s' %(str(endpoint))) getDefaultLogger().newMessage(COMMUNICATION_OUTGOING_MSG_DETAILS, '-- Size of Data %d chars' %(len(message))) protocol = getProtocolManager().getProtocol(endpoint.getProtocolId()) from protocolcontroller import getProtocolController protocol = getProtocolController().getOpenProtocol(protocol.getName()) from socket import error from errorhandling import G4dsCommunicationException try: protocol.sendMessage(endpoint.getAddress(), message) except error, msg: raise G4dsCommunicationException('Sending Message: %s' %(msg))
def printMembers(): for memberid in communitymanager.getMemberManager().getMemberIds(): member = communitymanager.getMemberManager().getMember(memberid) print member print "\tXML (%s): %s" %(member.getMdlVersion(), member.getMdl())
targetList = self._determineIndependantTargets(targettype, target) from servicerepository import getServiceManager for sid in getServiceManager().getServiceIds(): s = getServiceManager().getService(sid) try: targetList.index(sid) for mid in s.getAuthorities(): retList.append((mid, sid)) except ValueError, msg: pass # alright, this service is not in our target list return retList elif actor == 'authorities_member': # each member is its own authority targetList = self._determineIndependantTargets(targettype, target) from communitymanager import getMemberManager for mid in getMemberManager().getMemberIds(): try: targetList.index(mid) retList.append((mid, mid)) except ValueError, msg: pass # ok, this is not in our target list return retList elif actor == 'gateways_community': # check targets as well targetList = self._determineIndependantTargets(targettype, target) # assemble list of all gateways gws = [] from communitymanager import getCommunityManager for cid in getCommunityManager().getCommunityIds(): for gw in getCommunityManager().getCommunity(cid).getSourceGateways(): gws.append(gw)