예제 #1
0
 def __init__(self, s_map=None, n_map=None, d_map=None):
     if s_map and n_map and d_map:
         self.selector_map=s_map
         self.node_map=n_map
         self.domain_map=d_map
     else:
         self.selector_map = {}
         self.node_map = {}
         self.domain_map = {}
     
     self.immcfg = IMMCFG()
     
     self.linked_lists = {}
     
     self.build_linked_list()
예제 #2
0
class IMM(object):
    def __init__(self, s_map=None, n_map=None, d_map=None):
        if s_map and n_map and d_map:
            self.selector_map=s_map
            self.node_map=n_map
            self.domain_map=d_map
        else:
            self.selector_map = {}
            self.node_map = {}
            self.domain_map = {}
        
        self.immcfg = IMMCFG()
        
        self.linked_lists = {}
        
        self.build_linked_list()
        
         
    def build_linked_list(self):
        for k in self.selector_map.keys():
            otp_s = self.selector_map[k]
            
            s = Selector()
            self.linked_lists[otp_s.get("otpdiaSelector")] = s
            s.rdn = otp_s.get("otpdiaSelector")
            
            otp_domain = self.domain_map[otp_s.get("destination")]
            d = Domain(otp_domain.get("otpdiaDomain"), otp_domain.get("host"), otp_domain.get("realm"))

            s.matcher = Matcher(otp_s.get("applicationId"), d)
            
            s.link_head=None
            
            node_rdn = otp_s.get("peer")
            is_head = True
            pre_node = None
            
            while self.node_map.has_key(node_rdn):
                otp_link = self.node_map[node_rdn]
                
                otp_domain = self.domain_map[otp_link.get("head")]
                d = Domain(otp_domain.get("otpdiaDomain"), otp_domain.get("host"), otp_domain.get("realm"))
                
                node = LinkNode(otp_link.get("otpdiaCons"), d, None, s)
                
                if is_head:
                    is_head = False
                    s.link_head = node
                else:
                    pre_node.next = node    
                    
                if otp_link.get("tail") == NULL_VALUE:
                    node.next = None
                else:
                    pre_node = node
                
                node_rdn = otp_link.get("tail")
                
    def load_imm_object(self):
        self.selector_map=self.immcfg.load_imm_object("OtpdiaSelector")
        self.node_map = self.immcfg.load_imm_object("OtpdiaCons")
        self.domain_map = self.immcfg.load_imm_object("OtpdiaDomain")
            
        self.build_linked_list()
    
     
    def sizeof_link(self, selector):
        count = 0
        node = selector.link_head
        while node:
            count += 1
            node = node.next
            
        return count
            
    
    def add_linked_list (self, app_list, dest, peer ):
        # assign id/name for new linked list 
        selector_id = 1
        rdn = "otpdiaSelector=%d" %(selector_id)
        while self.linked_lists.has_key(rdn):
            selector_id += 1
            rdn = "otpdiaSelector=%d" %(selector_id)
        
        s = Selector()
        s.rdn = rdn
        
        dest_rdn = "otpdiaDomain=dest_%d.%d" %(selector_id, 1)
        dest_domain = Domain(dest_rdn, dest[0], dest[1])
        
        peer_rdn = "otpdiaDomain=peer_%d.%d" %(selector_id, 1)
        peer_domain = Domain(peer_rdn, peer[0], peer[1])
        
         
        link_rdn = "otpdiaCons=%d.%d" %(selector_id, 1)
        link_node = LinkNode(link_rdn, peer_domain, None, s)

        s.link_head = link_node
        s.matcher = Matcher(app_list, dest_domain )
        
        self.immcfg.add_otpdiadomain(link_node.data)
        self.immcfg.add_otpdiadomain(s.matcher.dest)
        self.immcfg.add_otpdiacons(link_node)
        self.immcfg.add_otpdiaselector(s)
        
        self.linked_lists[s.rdn] = s
        
        return link_node
        
    
    def append_link_node(self,selector, peer):
        
        selector_id = selector.rdn.split('otpdiaSelector=')[1]
        
        # by default: append the new node to the end of linked list
        
        node_id = 0
        while True:
            peer_rdn = "otpdiaDomain=peer_%s.%d" %(selector_id, node_id+1)
            if not self.domain_map.has_key(peer_rdn):
                break;
            node_id += 1
            
        peer_obj = Domain(peer_rdn, peer[0], peer[1])
         
        link_rdn = "otpdiaCons=%s.%d" %(selector_id, node_id + 1)
        link_node = LinkNode(link_rdn, peer_obj, None, selector)
        
        node = selector.link_head
        while node:
            if not node.next:
                node.next = link_node
                break
            else:
                node = node.next
        
        
        self.immcfg.add_otpdiadomain(link_node.data)
        self.immcfg.add_otpdiacons(link_node)
        
        node = link_node.selector.link_head
        pre_node = node
        while node.next:
            pre_node = node
            node = node.next
                
        self.immcfg.modify_imm_object(pre_node.rdn, 'tail', link_node.rdn)
        
        return link_node
    
    def modify_domain(self, rdn, hosts, realm):
        
        self.immcfg.modify_imm_object(rdn, 'host', hosts)
        self.immcfg.modify_imm_object(rdn, 'realm', realm)
        
        
    def update_link_head(self, selector, link_node):
        selector.link_head = link_node
        self.immcfg.modify_imm_object(selector.rdn, "peer", link_node.rdn )
        
        
    def get_pre_link_node(self, link_node):
        prev_node = None
        
        node = link_node.selector.link_head
        while node:
            if node.next == link_node:
                prev_node = node 
                break
        
        return prev_node
        

    def rm_link_node(self, link_node):
        s = link_node.selector
        link_size = self.sizeof_link(s)
        
        if  link_size == 1:
            
            print "INFO: link size is one, so remove the selector"
            
            self.immcfg.rm_imm_object(s.rdn)
            self.immcfg.rm_imm_object(s.matcher.dest.rdn)
            self.immcfg.rm_imm_object(link_node.rdn)
            self.immcfg.rm_imm_object(link_node.data.rdn)
            
            
        elif s.link_head == link_node: # link head
            self.update_link_head(s, link_node.next)
            self.immcfg.rm_imm_object(link_node.rdn)               
            self.immcfg.rm_imm_object(link_node.data.rdn)
            
        else:
            pre_node = self.get_pre_link_node(link_node)
            pre_node.next = link_node.next 
        
            self.immcfg.modify_imm_object(pre_node.rdn, "tail", link_node.next)            
            self.immcfg.rm_imm_object(link_node.rdn)
            self.immcfg.rm_imm_object(link_node.data.rdn)
            
            
    def find_selector(self, apps, host_list, realm):
        result=[]
        for s in self.linked_lists.values():
            
            if not is_sub_list(apps, s.matcher.app_list):
                continue

            if realm != s.matcher.dest.realm: continue
            
            if not is_sub_list(host_list, s.matcher.dest.host_list): continue
            
            result.append(s)
            
        return result  
    
    def get_selector_id(self):
        result = []
        for s in self.selector_map.values():
            result.append(int(s.rdn[-1]))
        
        if not result:
            return 1
        else: 
            return sorted(result)[-1] + 1