<commit_before>import logging
import time

from kyco.constants import POOLING_TIME
from kyco.core.events import KycoEvent
from kyco.core.napps import KycoCoreNApp
from kyco.utils import listen_to
from pyof.foundation.basic_types import HWAddress, UBInt8, UBInt16, UBInt64
from pyof.foundation.constants import UBINT16_MAX_VALUE
from pyof.foundation.network_types import Ethernet, LLDP
from pyof.v0x01.common.action import ActionOutput
from pyof.v0x01.common.constants import NO_BUFFER
from pyof.v0x01.common.flow_match import FlowWildCards, Match
from pyof.v0x01.common.phy_port import Port
from pyof.v0x01.controller2switch.flow_mod import FlowMod, FlowModCommand
from pyof.v0x01.controller2switch.packet_out import PacketOut

log = logging.getLogger('KycoNApp')

LLDP_ETH_TYPE = 0x88cc


class Main(KycoCoreNApp):
    """
    """

    def setup(self):
        """Creates an empty dict to store the switches references and data"""
        self.name = 'kytos/of.lldp'
        self.stop_signal = False
        # TODO: This switches object may change according to changes from #62

    def execute(self):
        """Implement a loop to check switches liveness"""
        while not self.stop_signal:
            for switch in self.controller.switches.values():
                # Gerar lldp para cada uma das portas do switch
                # Gerar o hash de cada um dos pacotes e armazenar

                for port in switch.features.ports:
                    output_action = ActionOutput()
                    output_action.port = port.port_no

                    packet_out = PacketOut()
                    packet_out.actions.append(output_action)
                    packet_out.data = LLDP(port.hw_addr,
                                           switch.dpid,
                                           port.port_no).pack()
                    event_out = KycoEvent()
                    event_out.name = 'kytos/of.lldp.messages.out.ofpt_packet_out'
                    event_out.content = {'destination': switch.connection,
                                         'message': packet_out}
                    self.controller.buffers.msg_out.put(event_out)

                    log.debug("Sending a LLDP PacketOut to the switch %s",
                              switch.dpid)

            # wait 1s until next check...
            time.sleep(POOLING_TIME)

    @listen_to('kytos/of.core.messages.in.ofpt_packet_in')
    def update_lldp(self, event):
        log.debug("PacketIn Received")
        ethernet_frame = Ethernet()
        ethernet_frame.unpack(event.message.data.value[:14])
        if ethernet_frame.type == LLDP_ETH_TYPE:
            lldp = LLDP()
            lldp.unpack(event.message.data.value)
            # update a link between two switches
            # The node are a tuple with (switch, port.port_no)
            nodeA = (event.source.switch.dpid, event.message.in_port)
            switchB = UBInt64()
            switchB.unpack(lldp.tlv_chassis_id.value.value)  # dpid
            portB = UBInt16()
            portB.unpack(lldp.tlv_port_id.value.value)  # port_no
            nodeB = (switchB.value, portB.value)
            self.controller.update_switches_link(nodeA, nodeB)

    @listen_to('kyco/core.switches.new')
    def install_lldp_flow(self, event):
        """Install initial flow to forward any lldp to controller.

        Args:
            event (KycoSwitchUp): Switch connected to the controller
        """
        switch = event.content['switch']
        log.debug("Installing LLDP Flow on Switch %s",
                  switch.dpid)

        flow_mod = FlowMod()
        flow_mod.command = FlowModCommand.OFPFC_ADD
        flow_mod.match = Match()
        flow_mod.match.wildcards -= FlowWildCards.OFPFW_DL_DST
        flow_mod.match.wildcards -= FlowWildCards.OFPFW_DL_TYPE
        flow_mod.match.dl_dst = '01:80:C2:00:00:0E'
        flow_mod.match.dl_type = LLDP_ETH_TYPE
        flow_mod.priority = 65000  # a high number TODO: Review
        flow_mod.actions.append(ActionOutput(port=Port.OFPP_CONTROLLER,
                                             max_length=UBINT16_MAX_VALUE))
        event_out = KycoEvent(name='kytos/of.lldp.messages.out.ofpt_flow_mod',
                              content={'destination': switch.connection,
                                       'message': flow_mod})

        self.controller.buffers.msg_out.put(event_out)<commit_msg>[of.lldp] initial refactoring on of.lldp napp<commit_after>import logging
import time

from kyco.constants import POOLING_TIME
from kyco.core.events import KycoEvent
from kyco.core.napps import KycoCoreNApp
from kyco.utils import listen_to
from pyof.foundation.basic_types import HWAddress, UBInt8, UBInt16, UBInt64, DPID
from pyof.foundation.constants import UBINT16_MAX_VALUE
from pyof.foundation.network_types import Ethernet, LLDP
from pyof.v0x01.common.action import ActionOutput
from pyof.v0x01.common.constants import NO_BUFFER
from pyof.v0x01.common.flow_match import FlowWildCards, Match
from pyof.v0x01.common.phy_port import Port
from pyof.v0x01.controller2switch.flow_mod import FlowMod, FlowModCommand
from pyof.v0x01.controller2switch.packet_out import PacketOut

log = logging.getLogger('KycoNApp')


class Main(KycoCoreNApp):
    """
    """

    def setup(self):
        """Creates an empty dict to store the switches references and data"""
        self.name = 'kytos/of.lldp'
        self.stop_signal = False
        # TODO: This switches object may change according to changes from #62

    def execute(self):
        """Implement a loop to check switches liveness"""
        while not self.stop_signal:
            for switch in self.controller.switches.values():
                # Gerar lldp para cada uma das portas do switch
                # Gerar o hash de cada um dos pacotes e armazenar

                for port in switch.features.ports:
                    output_action = ActionOutput()
                    output_action.port = port.port_no

                    if port.port_no.value == 65534:
                        continue

                    ethernet = Ethernet()
                    ethernet.type = 0x88cc # lldp
                    ethernet.source = port.hw_addr
                    ethernet.destination = '01:80:c2:00:00:0e' # lldp multicast

                    lldp = LLDP()
                    lldp.chassis_id.sub_value = DPID(switch.dpid)
                    lldp.port_id.sub_value = port.port_no

                    ethernet.data = lldp.pack()

                    packet_out = PacketOut()
                    packet_out.actions.append(output_action)
                    packet_out.data = ethernet.pack()

                    event_out = KycoEvent()
                    event_out.name = 'kytos/of.lldp.messages.out.ofpt_packet_out'
                    event_out.content = {'destination': switch.connection,
                                         'message': packet_out}
                    self.controller.buffers.msg_out.put(event_out)

                    log.debug("Sending a LLDP PacketOut to the switch %s",
                              switch.dpid)

            # wait 1s until next check...
            time.sleep(POOLING_TIME)

#    @listen_to('kytos/of.core.messages.in.ofpt_packet_in')
#    def update_lldp(self, event):
#        log.debug("PacketIn Received")
#        ethernet_frame = Ethernet()
#        ethernet_frame.unpack(event.message.data.value[:14])
#        if ethernet_frame.type == LLDP_ETH_TYPE:
#            lldp = LLDP()
#            lldp.unpack(event.message.data.value)
#            # update a link between two switches
#            # The node are a tuple with (switch, port.port_no)
#            nodeA = (event.source.switch.dpid, event.message.in_port)
#            switchB = UBInt64()
#            switchB.unpack(lldp.tlv_chassis_id.value.value)  # dpid
#            portB = UBInt16()
#            portB.unpack(lldp.tlv_port_id.value.value)  # port_no
#            nodeB = (switchB.value, portB.value)
#            self.controller.update_switches_link(nodeA, nodeB)

#    @listen_to('kyco/core.switches.new')
#    def install_lldp_flow(self, event):
#        """Install initial flow to forward any lldp to controller.
#
#        Args:
#            event (KycoSwitchUp): Switch connected to the controller
#        """
#        switch = event.content['switch']
#        log.debug("Installing LLDP Flow on Switch %s",
#                  switch.dpid)
#
#        flow_mod = FlowMod()
#        flow_mod.command = FlowModCommand.OFPFC_ADD
#        flow_mod.match = Match()
#        flow_mod.match.wildcards -= FlowWildCards.OFPFW_DL_DST
#        flow_mod.match.dl_type = LLDP_ETH_TYPE
#        flow_mod.priority = 65000  # a high number TODO: Review
#        flow_mod.actions.append(ActionOutput(port=Port.OFPP_CONTROLLER,
#                                             max_length=UBINT16_MAX_VALUE))
#        event_out = KycoEvent(name='kytos/of.lldp.messages.out.ofpt_flow_mod',
#                              content={'destination': switch.connection,
#                                       'message': flow_mod})
#
#        self.controller.buffers.msg_out.put(event_out)
{'fraction_non_alphanumeric': 0.08793009284543965, 'fraction_numerical': 0.0127799016930639, 'mean_word_length': 2.053017672557519, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4187855', 'n_tokens_mistral': 2889, 'n_tokens_neox': 2712, 'n_words': 572}