<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <glog/logging.h>

#include "cnetlink.h"
#include "nbi_impl.h"
#include "tap_manager.h"

#include "netlink/ctapdev.h"
#include "utils/utils.h"

namespace basebox {

nbi_impl::nbi_impl(std::shared_ptr<cnetlink> nl,
                   std::shared_ptr<tap_manager> tap_man)
    : nl(nl), tap_man(tap_man) {
  nl->set_tapmanager(tap_man);
  nl->start();
}

nbi_impl::~nbi_impl() { nl->stop(); }

void nbi_impl::resend_state() noexcept { nl->resend_state(); }

void nbi_impl::register_switch(switch_interface *swi) noexcept {
  this->swi = swi;
  nl->register_switch(swi);
}

void nbi_impl::port_notification(
    std::deque<port_notification_data> &notifications) noexcept {

  for (auto &&ntfy : notifications) {
    switch (ntfy.ev) {
    case PORT_EVENT_MODIFY:
      switch (get_port_type(ntfy.port_id)) {
      case nbi::port_type_physical:
        LOG(INFO) << __FUNCTION__ << ": port state changed, continuing";
        break;
      default:
        LOG(ERROR) << __FUNCTION__ << ": unknown port";
        break;
      }
      break;
    case PORT_EVENT_ADD:
      switch (get_port_type(ntfy.port_id)) {
      case nbi::port_type_physical:
        if (tap_man->create_tapdev(ntfy.port_id, ntfy.name, *this) == -EEXIST) {
          LOG(INFO) << __FUNCTION__ << ": port already exists";
          break;
        }
        break;
      case nbi::port_type_vxlan:
        // XXX TODO notify this?
        LOG(INFO) << __FUNCTION__ << ": port_type_vxlan added";
        break;
      default:
        LOG(ERROR) << __FUNCTION__ << ": unknown port";
        break;
      }
      break;
    case PORT_EVENT_DEL:
      switch (get_port_type(ntfy.port_id)) {
      case nbi::port_type_physical:
        tap_man->destroy_tapdev(ntfy.port_id, ntfy.name);
        break;
      case nbi::port_type_vxlan:
        // XXX TODO notify this?
        LOG(INFO) << __FUNCTION__ << ": port_type_vxlan removed";
        break;
      default:
        LOG(ERROR) << __FUNCTION__ << ": unknown port";
        break;
      }
      break;<commit_msg>Revert "cnetlink: avoid changing interface state on modify"<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <glog/logging.h>

#include "cnetlink.h"
#include "nbi_impl.h"
#include "tap_manager.h"

#include "netlink/ctapdev.h"
#include "utils/utils.h"

namespace basebox {

nbi_impl::nbi_impl(std::shared_ptr<cnetlink> nl,
                   std::shared_ptr<tap_manager> tap_man)
    : nl(nl), tap_man(tap_man) {
  nl->set_tapmanager(tap_man);
  nl->start();
}

nbi_impl::~nbi_impl() { nl->stop(); }

void nbi_impl::resend_state() noexcept { nl->resend_state(); }

void nbi_impl::register_switch(switch_interface *swi) noexcept {
  this->swi = swi;
  nl->register_switch(swi);
}

void nbi_impl::port_notification(
    std::deque<port_notification_data> &notifications) noexcept {

  for (auto &&ntfy : notifications) {
    switch (ntfy.ev) {
    case PORT_EVENT_MODIFY:
      switch (get_port_type(ntfy.port_id)) {
      case nbi::port_type_physical:
        tap_man->change_port_status(ntfy.name, ntfy.status);
        break;
      default:
        LOG(ERROR) << __FUNCTION__ << ": unknown port";
        break;
      }
      break;
    case PORT_EVENT_ADD:
      switch (get_port_type(ntfy.port_id)) {
      case nbi::port_type_physical:
        if (tap_man->create_tapdev(ntfy.port_id, ntfy.name, *this) == -EEXIST) {
          LOG(INFO) << __FUNCTION__ << ": port already exists";
          break;
        }
        tap_man->change_port_status(ntfy.name, ntfy.status);
        break;
      case nbi::port_type_vxlan:
        // XXX TODO notify this?
        LOG(INFO) << __FUNCTION__ << ": port_type_vxlan added";
        break;
      default:
        LOG(ERROR) << __FUNCTION__ << ": unknown port";
        break;
      }
      break;
    case PORT_EVENT_DEL:
      switch (get_port_type(ntfy.port_id)) {
      case nbi::port_type_physical:
        tap_man->destroy_tapdev(ntfy.port_id, ntfy.name);
        break;
      case nbi::port_type_vxlan:
        // XXX TODO notify this?
        LOG(INFO) << __FUNCTION__ << ": port_type_vxlan removed";
        break;
      default:
        LOG(ERROR) << __FUNCTION__ << ": unknown port";
        break;
      }
      break;
{'fraction_non_alphanumeric': 0.13029315960912052, 'fraction_numerical': 0.001737242128121607, 'mean_word_length': 2.816072908036454, 'pattern_counts': {'":': 13, '<': 63, '<?xml version=': 0, '>': 29, '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': '6055422', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1589, 'n_words': 332}