<commit_before>#!/usr/bin/python

import sys
sys.path.append('../..')

import proact.common.basecore
import proact.common.radvdaemon
import proact.common.dhcpclient
import ConfigParser
import logging
import time


class HgwCore(proact.common.basecore.BaseCore):
    """
    a description would be useful here
    """
    systemctl_binary = '/usr/bin/systemctl'
    
    def __init__(self, **kwargs):
        try:            
            self.linkNames = {'wan':[], 'dmz':[], 'lan':[] }
            self.dmzLinks = {}
            self.lanLinks = {}
            self.wanLinks = {}

            for devname in kwargs.get('wanLinks', []):
                self.linkNames['wan'].append(devname)
            for devname in kwargs.get('dmzLinks', []):
                self.linkNames['dmz'].append(devname)
            for devname in kwargs.get('lanLinks', []):
                self.linkNames['lan'].append(devname)
                
            self.conffile = kwargs.get('conffile', 'hgwcore.conf')
            self.parseConfig()
            self.vendor = kwargs.get('vendor', 'bisdn.de')
            self.product = kwargs.get('product', 'hgwcore')
                    
            logging.basicConfig(filename=self.logfile,level=self.loglevel)
                    
            proact.common.basecore.BaseCore.__init__(self, self.brokerUrl, vendor=self.vendor, product=self.product)
            self.logger = logging.getLogger('proact')
            
        except Exception, e:
            self.logger.error('HgwCore::init() failed ' + str(e))
            self.cleanUp()
        
    def cleanUp(self):
        self.logger.info('cleaning up ...')
        try:
            self.agentHandler.cancel()
            self.stopOpenVpn()
        except:
            pass
        for devname in self.dmzLinks:
            self.dmzLinks[devname].disable()
        self.dmzLinks = {}
        for devname in self.lanLinks:
            self.lanLinks[devname].disable()
        self.lanLinks = {}
        for devname in self.wanLinks:
            self.wanLinks[devname].disable()
        self.wanLinks = {}
        
    def userSessionAdd(self, userIdentity, ipAddress, validLifetime):
        print 'adding user session for user ' + userIdentity + ' on IP address ' + ipAddress + ' with lifetime ' + validLifetime 

    def userSessionDel(self, userIdentity, ipAddress):
        print 'deleting user session for user ' + userIdentity + ' on IP address ' + ipAddress 

    def handleEvent(self, event):
        if event.type == proact.common.basecore.BaseCore.EVENT_NEW_LINK:
            link = event.source
            if link.devname in self.linkNames['dmz']:
                link.linkType = 'dmz'
                link.disable()
                link.enable(0)
                self.dmzLinks[link.devname] = link
                self.logger.info('DMZ link found: ' + str(link))
            elif link.devname in self.linkNames['lan']:
                link.linkType = 'lan'
                link.disable()
                link.enable(0)
                self.lanLinks[link.devname] = link
                self.logger.info('LAN link found: ' + str(link))
            elif link.devname in self.linkNames['wan']:
                link.linkType = 'wan'
                link.disable()
                link.enable(2)                
                self.wanLinks[link.devname] = link
                self.logger.info('WAN link found: ' + str(link))
                
        elif event.type == proact.common.basecore.BaseCore.EVENT_DEL_LINK:
            link = event.source
            if link.devname in self.dmzLinks:
                self.dmzLinks.pop(link.devname, None)
                self.logger.info('DMZ link lost: ' + str(link))
            if link.devname in self.lanLinks:
                self.lanLinks.pop(link.devname, None)
                self.logger.info('LAN link lost: ' + str(link))
            if link.devname in self.wanLinks:
                self.wanLinks.pop(link.devname, None)
                self.logger.info('WAN link lost: ' + str(link))
            
        elif event.type == proact.common.basecore.BaseCore.EVENT_NEW_ADDR:
            self.logger.debug('NewAddr (' + str(event.source) + ')')
        elif event.type == proact.common.basecore.BaseCore.EVENT_DEL_ADDR:
            self.logger.debug('DelAddr (' + str(event.source) + ')')
        elif event.type == proact.common.basecore.BaseCore.EVENT_NEW_ROUTE:
            self.logger.debug('NewRoute (' + str(event.source) + ')')
        elif event.type == proact.common.basecore.BaseCore.EVENT_DEL_ROUTE:
            self.logger.debug('DelRoute (' + str(event.source) + ')')
        elif event.type == proact.common.basecore.BaseCore.EVENT_LINK_UP:
            self.logger.debug('LinkUp (' + str(event.source) + ')')
        elif event.type == proact.common.basecore.BaseCore.EVENT_LINK_DOWN:
            self.logger.debug('LinkDown (' + str(event.source) + ')')
        elif event.type == proact.common.basecore.BaseCore.EVENT_RA_ATTACHED:
            self.logger.debug('RA-Attached (' + str(event.source) + ')')
            link = event.source
            if link.devname in self.wanLinks:
                link.dhclient.sendRequest()
            
        elif event.type == proact.common.basecore.BaseCore.EVENT_RA_DETACHED:
            self.logger.debug('RA-Detached (' + str(event.source) + ')')
            link = event.source
            if link.devname in self.wanLinks:
                link.dhclient.killClient()            
            
        elif event.type == proact.common.basecore.BaseCore.EVENT_PREFIX_ATTACHED:
            self.logger.debug('Prefix-Attached (' + str(event.source) + ')')
            dhclient = event.source
            for devname, lanLink in self.lanLinks.iteritems():
                for prefix in dhclient.new_prefixes:
                    p = prefix.get_subprefix(lanLink.uniquePrefix).prefix
                    lanLink.addIPv6Addr(str(p)+'1', 64)
                for prefix in dhclient.new_prefixes:
                    p = prefix.get_subprefix(lanLink.uniquePrefix).prefix
                    lanLink.radvd.addPrefix(proact.common.ipv6prefix.IPv6Prefix(str(p), 64))
                lanLink.radvd.restart()
                    
            for devname, dmzLink in self.dmzLinks.iteritems():
                for prefix in dhclient.new_prefixes:
                    p = prefix.get_subprefix(dmzLink.uniquePrefix).prefix
                    dmzLink.addIPv6Addr(str(p)+'1', 64)
                for prefix in dhclient.new_prefixes:
                    p = prefix.get_subprefix(dmzLink.uniquePrefix).prefix
                    dmzLink.radvd.addPrefix(proact.common.ipv6prefix.IPv6Prefix(str(p), 64))
                dmzLink.radvd.restart()
        
        elif event.type == proact.common.basecore.BaseCore.EVENT_PREFIX_DETACHED:
            self.logger.debug('Prefix-Detached (' + str(event.source) + ')')
            dhclient = event.source
            for devname, lanLink in self.lanLinks.iteritems():
                for prefix in dhclient.new_prefixes:
                    p = prefix.get_subprefix(lanLink.uniquePrefix).prefix
                    lanLink.radvd.delPrefix(proact.common.ipv6prefix.IPv6Prefix(str(p), 64))
                lanLink.radvd.restart()
                for prefix in dhclient.new_prefixes:
                    p = prefix.get_subprefix(lanLink.uniquePrefix).prefix
                    lanLink.delIPv6Addr(str(p)+'1', 64)
            
            for devname, dmzLink in self.dmzLinks.iteritems():
                for prefix in dhclient.new_prefixes:
                    p = prefix.get_subprefix(dmzLink.uniquePrefix).prefix
                    dmzLink.radvd.delPrefix(proact.common.ipv6prefix.IPv6Prefix(str(p), 64))
                dmzLink.radvd.restart()
                for prefix in dhclient.new_prefixes:
                    p = prefix.get_subprefix(dmzLink.uniquePrefix).prefix
                    dmzLink.delIPv6Addr(str(p)+'1', 64)<commit_msg>hgwcore: prefix-attached => set IPv6 address on wan links<commit_after>#!/usr/bin/python

import sys
sys.path.append('../..')

import proact.common.basecore
import proact.common.radvdaemon
import proact.common.dhcpclient
import ConfigParser
import logging
import time


class HgwCore(proact.common.basecore.BaseCore):
    """
    a description would be useful here
    """
    systemctl_binary = '/usr/bin/systemctl'
    
    def __init__(self, **kwargs):
        try:            
            self.linkNames = {'wan':[], 'dmz':[], 'lan':[] }
            self.dmzLinks = {}
            self.lanLinks = {}
            self.wanLinks = {}

            for devname in kwargs.get('wanLinks', []):
                self.linkNames['wan'].append(devname)
            for devname in kwargs.get('dmzLinks', []):
                self.linkNames['dmz'].append(devname)
            for devname in kwargs.get('lanLinks', []):
                self.linkNames['lan'].append(devname)
                
            self.conffile = kwargs.get('conffile', 'hgwcore.conf')
            self.parseConfig()
            self.vendor = kwargs.get('vendor', 'bisdn.de')
            self.product = kwargs.get('product', 'hgwcore')
                    
            logging.basicConfig(filename=self.logfile,level=self.loglevel)
                    
            proact.common.basecore.BaseCore.__init__(self, self.brokerUrl, vendor=self.vendor, product=self.product)
            self.logger = logging.getLogger('proact')
            
        except Exception, e:
            self.logger.error('HgwCore::init() failed ' + str(e))
            self.cleanUp()
        
    def cleanUp(self):
        self.logger.info('cleaning up ...')
        try:
            self.agentHandler.cancel()
            self.stopOpenVpn()
        except:
            pass
        for devname in self.dmzLinks:
            self.dmzLinks[devname].disable()
        self.dmzLinks = {}
        for devname in self.lanLinks:
            self.lanLinks[devname].disable()
        self.lanLinks = {}
        for devname in self.wanLinks:
            self.wanLinks[devname].disable()
        self.wanLinks = {}
        
    def userSessionAdd(self, userIdentity, ipAddress, validLifetime):
        print 'adding user session for user ' + userIdentity + ' on IP address ' + ipAddress + ' with lifetime ' + validLifetime 

    def userSessionDel(self, userIdentity, ipAddress):
        print 'deleting user session for user ' + userIdentity + ' on IP address ' + ipAddress 

    def handleEvent(self, event):
        if event.type == proact.common.basecore.BaseCore.EVENT_NEW_LINK:
            link = event.source
            if link.devname in self.linkNames['dmz']:
                link.linkType = 'dmz'
                link.disable()
                link.enable(0)
                self.dmzLinks[link.devname] = link
                self.logger.info('DMZ link found: ' + str(link))
            elif link.devname in self.linkNames['lan']:
                link.linkType = 'lan'
                link.disable()
                link.enable(0)
                self.lanLinks[link.devname] = link
                self.logger.info('LAN link found: ' + str(link))
            elif link.devname in self.linkNames['wan']:
                link.linkType = 'wan'
                link.disable()
                link.enable(2)                
                self.wanLinks[link.devname] = link
                self.logger.info('WAN link found: ' + str(link))
                
        elif event.type == proact.common.basecore.BaseCore.EVENT_DEL_LINK:
            link = event.source
            if link.devname in self.dmzLinks:
                self.dmzLinks.pop(link.devname, None)
                self.logger.info('DMZ link lost: ' + str(link))
            if link.devname in self.lanLinks:
                self.lanLinks.pop(link.devname, None)
                self.logger.info('LAN link lost: ' + str(link))
            if link.devname in self.wanLinks:
                self.wanLinks.pop(link.devname, None)
                self.logger.info('WAN link lost: ' + str(link))
            
        elif event.type == proact.common.basecore.BaseCore.EVENT_NEW_ADDR:
            self.logger.debug('NewAddr (' + str(event.source) + ')')
        elif event.type == proact.common.basecore.BaseCore.EVENT_DEL_ADDR:
            self.logger.debug('DelAddr (' + str(event.source) + ')')
        elif event.type == proact.common.basecore.BaseCore.EVENT_NEW_ROUTE:
            self.logger.debug('NewRoute (' + str(event.source) + ')')
        elif event.type == proact.common.basecore.BaseCore.EVENT_DEL_ROUTE:
            self.logger.debug('DelRoute (' + str(event.source) + ')')
        elif event.type == proact.common.basecore.BaseCore.EVENT_LINK_UP:
            self.logger.debug('LinkUp (' + str(event.source) + ')')
        elif event.type == proact.common.basecore.BaseCore.EVENT_LINK_DOWN:
            self.logger.debug('LinkDown (' + str(event.source) + ')')
        elif event.type == proact.common.basecore.BaseCore.EVENT_RA_ATTACHED:
            self.logger.debug('RA-Attached (' + str(event.source) + ')')
            link = event.source
            if link.devname in self.wanLinks:
                link.dhclient.sendRequest()
            
        elif event.type == proact.common.basecore.BaseCore.EVENT_RA_DETACHED:
            self.logger.debug('RA-Detached (' + str(event.source) + ')')
            link = event.source
            if link.devname in self.wanLinks:
                link.dhclient.killClient()            
            
        elif event.type == proact.common.basecore.BaseCore.EVENT_PREFIX_ATTACHED:
            self.logger.debug('Prefix-Attached (' + str(event.source) + ')')
            dhclient = event.source
            for devname, lanLink in self.lanLinks.iteritems():
                for prefix in dhclient.new_prefixes:
                    p = prefix.get_subprefix(lanLink.uniquePrefix).prefix
                    lanLink.addIPv6Addr(str(p)+'1', 64)
                for prefix in dhclient.new_prefixes:
                    p = prefix.get_subprefix(lanLink.uniquePrefix).prefix
                    lanLink.radvd.addPrefix(proact.common.ipv6prefix.IPv6Prefix(str(p), 64))
                lanLink.radvd.restart()
                    
            for devname, dmzLink in self.dmzLinks.iteritems():
                for prefix in dhclient.new_prefixes:
                    p = prefix.get_subprefix(dmzLink.uniquePrefix).prefix
                    dmzLink.addIPv6Addr(str(p)+'1', 64)
                for prefix in dhclient.new_prefixes:
                    p = prefix.get_subprefix(dmzLink.uniquePrefix).prefix
                    dmzLink.radvd.addPrefix(proact.common.ipv6prefix.IPv6Prefix(str(p), 64))
                dmzLink.radvd.restart()

            for devname, wanLink in self.wanLinks.iteritems():
                for prefix in dhclient.new_prefixes:
                    p = prefix.get_subprefix(wanLink.uniquePrefix).prefix
                    wanLink.addIPv6Addr(str(p)+'1', 64)
                        
        elif event.type == proact.common.basecore.BaseCore.EVENT_PREFIX_DETACHED:
            self.logger.debug('Prefix-Detached (' + str(event.source) + ')')
            dhclient = event.source
            for devname, lanLink in self.lanLinks.iteritems():
                for prefix in dhclient.new_prefixes:
                    p = prefix.get_subprefix(lanLink.uniquePrefix).prefix
                    lanLink.radvd.delPrefix(proact.common.ipv6prefix.IPv6Prefix(str(p), 64))
                lanLink.radvd.restart()
                for prefix in dhclient.new_prefixes:
                    p = prefix.get_subprefix(lanLink.uniquePrefix).prefix
                    lanLink.delIPv6Addr(str(p)+'1', 64)
            
            for devname, dmzLink in self.dmzLinks.iteritems():
                for prefix in dhclient.new_prefixes:
                    p = prefix.get_subprefix(dmzLink.uniquePrefix).prefix
                    dmzLink.radvd.delPrefix(proact.common.ipv6prefix.IPv6Prefix(str(p), 64))
                dmzLink.radvd.restart()
                for prefix in dhclient.new_prefixes:
                    p = prefix.get_subprefix(dmzLink.uniquePrefix).prefix
                    dmzLink.delIPv6Addr(str(p)+'1', 64)
{'fraction_non_alphanumeric': 0.11535382151875347, 'fraction_numerical': 0.004619079879288046, 'mean_word_length': 1.823018080667594, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2740255', 'n_tokens_mistral': 4837, 'n_tokens_neox': 4644, 'n_words': 775}