<commit_before>"""This App is the responsible for the main OpenFlow basic operations."""

import logging

from pyof.v0x01.common.utils import new_message_from_header
from pyof.v0x01.controller2switch.features_request import FeaturesRequest
from pyof.v0x01.symmetric.echo_reply import EchoReply
from pyof.v0x01.symmetric.hello import Hello

from kyco.core.events import KycoEvent
from kyco.core.napps import KycoCoreNApp
from kyco.core.switch import Interface
from kyco.utils import listen_to


log = logging.getLogger('Kyco')


class Main(KycoCoreNApp):
    """Main class of KycoCoreNApp, responsible for the main OpenFlow basic
    operations.

    """

    def setup(self):
        """'Replaces' the 'init' method for the KycoApp subclass.

        The setup method is automatically called by the run method.
        Users shouldn't call this method directly."""
        # TODO: App information goes to app_name.json
        self.name = 'kytos/of.core'

    def execute(self):
        """Method to be runned once on app 'start' or in a loop.

        The execute method is called by the run method of KycoNApp class.
        Users shouldn't call this method directly."""
        pass

    @listen_to('kytos/of.core.messages.in.ofpt_features_reply')
    def handle_features_reply(self, event):
        """Handle received FeaturesReply event.

        Reads the FeaturesReply Event sent by the client, save this data and
        sends three new messages to the client:

            * SetConfig Message;
            * FlowMod Message with a FlowDelete command;
            * BarrierRequest Message;

        This is the end of the Handshake workflow of the OpenFlow Protocol.

        Args:
            event (KycoMessageInFeaturesReply):
        """
        log.debug('Handling KycoMessageInFeaturesReply Event')

        features = event.content['message']
        dpid = features.datapath_id.value

        switch = self.controller.get_switch_or_create(dpid=dpid,
                                                      connection=event.source)

        for port in features.ports:
            interface = Interface(name=port.name.value,
                                  address=port.hw_addr.value,
                                  port_number=port.port_no.value,
                                  switch=switch,
                                  state=port.state.value)
            switch.update_interface(interface)


        switch.update_features(features)

    @listen_to('kyco/core.messages.openflow.new')
    def handle_new_openflow_message(self, event):
        """Handle a RawEvent and generate a KycoMessageIn event.

        Args:
            event (KycoRawOpenFlowMessage): RawOpenFlowMessage to be unpacked
        """
        log.debug('RawOpenFlowMessage received by RawOFMessage handler')

        # creates an empty OpenFlow Message based on the message_type defined
        # on the unpacked header.
        # TODO: Deal with openFlow version prior to message instantiation
        message = new_message_from_header(event.content['header'])
        binary_data = event.content['binary_data']

        # The unpack will happen only to those messages with body beyond header
        if binary_data and len(binary_data) > 0:
            message.unpack(binary_data)
        log.debug('RawOpenFlowMessage unpacked')

        name = message.header.message_type.name.lower()
        of_event = KycoEvent(name="kytos/of.core.messages.in.{}".format(name),
                             content={'message': message,
                                      'source': event.source})
        self.controller.buffers.msg_in.put(of_event)

    @listen_to('kytos/of.core.messages.in.ofpt_echo_request')
    def handle_echo_request(self, event):
        """Handle EchoRequest Event by Generating an EchoReply Answer

        Args:
            event (KycoMessageInEchoRequest): Received Event
        """
        log.debug("Echo Request message read")

        echo_request = event.content['message']
        echo_reply = EchoReply(xid=echo_request.header.xid)
        event_out = KycoEvent(name='kytos/of.core.messages.out.ofpt_echo_reply',
                              content={'message': echo_reply,
                                       'destination': event.source})
        self.controller.buffers.msg_out.put(event_out)

    @listen_to('kytos/of.core.messages.in.ofpt_hello')
    def handle_openflow_in_hello(self, event):
        """Handle a Hello MessageIn Event and sends a Hello to the client.

        Args:
            event (KycoMessageInHello): KycoMessageInHelloEvent
        """
        log.debug('Handling kytos/of.core.messages.ofpt_hello')

        # TODO: Evaluate the OpenFlow version that will be used...
        hello = Hello(xid=event.content['message'].header.xid)<commit_msg>Fix Pep8<commit_after>"""This App is the responsible for the main OpenFlow basic operations."""

import logging

from pyof.v0x01.common.utils import new_message_from_header
from pyof.v0x01.controller2switch.features_request import FeaturesRequest
from pyof.v0x01.symmetric.echo_reply import EchoReply
from pyof.v0x01.symmetric.hello import Hello

from kyco.core.events import KycoEvent
from kyco.core.napps import KycoCoreNApp
from kyco.core.switch import Interface
from kyco.utils import listen_to


log = logging.getLogger('Kyco')


class Main(KycoCoreNApp):
    """Main class of KycoCoreNApp, responsible for the main OpenFlow basic
    operations.

    """

    def setup(self):
        """'Replaces' the 'init' method for the KycoApp subclass.

        The setup method is automatically called by the run method.
        Users shouldn't call this method directly."""
        # TODO: App information goes to app_name.json
        self.name = 'kytos/of.core'

    def execute(self):
        """Method to be runned once on app 'start' or in a loop.

        The execute method is called by the run method of KycoNApp class.
        Users shouldn't call this method directly."""
        pass

    @listen_to('kytos/of.core.messages.in.ofpt_features_reply')
    def handle_features_reply(self, event):
        """Handle received FeaturesReply event.

        Reads the FeaturesReply Event sent by the client, save this data and
        sends three new messages to the client:

            * SetConfig Message;
            * FlowMod Message with a FlowDelete command;
            * BarrierRequest Message;

        This is the end of the Handshake workflow of the OpenFlow Protocol.

        Args:
            event (KycoMessageInFeaturesReply):
        """
        log.debug('Handling KycoMessageInFeaturesReply Event')

        features = event.content['message']
        dpid = features.datapath_id.value

        switch = self.controller.get_switch_or_create(dpid=dpid,
                                                      connection=event.source)

        for port in features.ports:
            interface = Interface(name=port.name.value,
                                  address=port.hw_addr.value,
                                  port_number=port.port_no.value,
                                  switch=switch,
                                  state=port.state.value)
            switch.update_interface(interface)

        switch.update_features(features)

    @listen_to('kyco/core.messages.openflow.new')
    def handle_new_openflow_message(self, event):
        """Handle a RawEvent and generate a KycoMessageIn event.

        Args:
            event (KycoRawOpenFlowMessage): RawOpenFlowMessage to be unpacked
        """
        log.debug('RawOpenFlowMessage received by RawOFMessage handler')

        # creates an empty OpenFlow Message based on the message_type defined
        # on the unpacked header.
        # TODO: Deal with openFlow version prior to message instantiation
        message = new_message_from_header(event.content['header'])
        binary_data = event.content['binary_data']

        # The unpack will happen only to those messages with body beyond header
        if binary_data and len(binary_data) > 0:
            message.unpack(binary_data)
        log.debug('RawOpenFlowMessage unpacked')

        name = message.header.message_type.name.lower()
        of_event = KycoEvent(name="kytos/of.core.messages.in.{}".format(name),
                             content={'message': message,
                                      'source': event.source})
        self.controller.buffers.msg_in.put(of_event)

    @listen_to('kytos/of.core.messages.in.ofpt_echo_request')
    def handle_echo_request(self, event):
        """Handle EchoRequest Event by Generating an EchoReply Answer

        Args:
            event (KycoMessageInEchoRequest): Received Event
        """
        log.debug("Echo Request message read")

        echo_request = event.content['message']
        echo_reply = EchoReply(xid=echo_request.header.xid)
        event_out = KycoEvent(name=('kytos/of.core.messages.out.'
                                    'ofpt_echo_reply'),
                              content={'message': echo_reply,
                                       'destination': event.source})
        self.controller.buffers.msg_out.put(event_out)

    @listen_to('kytos/of.core.messages.in.ofpt_hello')
    def handle_openflow_in_hello(self, event):
        """Handle a Hello MessageIn Event and sends a Hello to the client.

        Args:
            event (KycoMessageInHello): KycoMessageInHelloEvent
        """
        log.debug('Handling kytos/of.core.messages.ofpt_hello')

        # TODO: Evaluate the OpenFlow version that will be used...
        hello = Hello(xid=event.content['message'].header.xid)
{'fraction_non_alphanumeric': 0.08216619981325864, 'fraction_numerical': 0.0030086108517481066, 'mean_word_length': 2.3402633402633404, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7209613', 'n_tokens_mistral': 2663, 'n_tokens_neox': 2540, 'n_words': 693}