/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2014-2024,  Regents of the University of California,
 *                           Arizona Board of Regents,
 *                           Colorado State University,
 *                           University Pierre & Marie Curie, Sorbonne University,
 *                           Washington University in St. Louis,
 *                           Beijing Institute of Technology,
 *                           The University of Memphis.
 *
 * This file is part of NFD (Named Data Networking Forwarding Daemon).
 * See AUTHORS.md for complete list of NFD authors and contributors.
 *
 * NFD is free software: you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "cs-manager.hpp"
#include "fw/forwarder-counters.hpp"
#include "table/cs.hpp"

#include <ndn-cxx/mgmt/nfd/cs-info.hpp>

#include <limits>

namespace nfd {

CsManager::CsManager(Cs& cs, const ForwarderCounters& fwCounters,
                     Dispatcher& dispatcher, CommandAuthenticator& authenticator)
  : ManagerBase("cs", dispatcher, authenticator)
  , m_cs(cs)
  , m_fwCounters(fwCounters)
{
  registerCommandHandler<ndn::nfd::CsConfigCommand>("config",
    [this] (auto&&, auto&&, auto&&, auto&&... args) { changeConfig(std::forward<decltype(args)>(args)...); });
  registerCommandHandler<ndn::nfd::CsEraseCommand>("erase",
    [this] (auto&&, auto&&, auto&&, auto&&... args) { erase(std::forward<decltype(args)>(args)...); });
  registerStatusDatasetHandler("info",
    [this] (auto&&, auto&&, auto&&... args) { serveInfo(std::forward<decltype(args)>(args)...); });
}

void
CsManager::changeConfig(const ControlParameters& parameters,
                        const ndn::mgmt::CommandContinuation& done)
{
  using ndn::nfd::CsFlagBit;

  if (parameters.hasCapacity()) {
    m_cs.setLimit(parameters.getCapacity());
  }

  if (parameters.hasFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT)) {
    m_cs.enableAdmit(parameters.getFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT));
  }

  if (parameters.hasFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE)) {
    m_cs.enableServe(parameters.getFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE));
  }

  ControlParameters body;
  body.setCapacity(m_cs.getLimit());
  body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT, m_cs.shouldAdmit(), false);
  body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE, m_cs.shouldServe(), false);
  done(ControlResponse(200, "OK").setBody(body.wireEncode()));
}

void
CsManager::erase(const ControlParameters& parameters,
                 const ndn::mgmt::CommandContinuation& done)
{
  size_t count = parameters.hasCount() ?
                 parameters.getCount() :
                 std::numeric_limits<size_t>::max();
  m_cs.erase(parameters.getName(), std::min(count, ERASE_LIMIT),
    [=] (size_t nErased) {
      ControlParameters body;
      body.setName(parameters.getName());
      body.setCount(nErased);
      if (nErased == ERASE_LIMIT && count > ERASE_LIMIT) {
        m_cs.find(Interest(parameters.getName()).setCanBePrefix(true),
          [=] (const Interest&, const Data&) mutable {
            body.setCapacity(ERASE_LIMIT);
            done(ControlResponse(200, "OK").setBody(body.wireEncode()));
          },
          [=] (const Interest&) {
            done(ControlResponse(200, "OK").setBody(body.wireEncode()));
          });
      }
      else {
        done(ControlResponse(200, "OK").setBody(body.wireEncode()));
      }
    });
}

void
CsManager::serveInfo(ndn::mgmt::StatusDatasetContext& context) const
{
  ndn::nfd::CsInfo info;
  info.setCapacity(m_cs.getLimit());
  info.setEnableAdmit(m_cs.shouldAdmit());
  info.setEnableServe(m_cs.shouldServe());
  info.setNEntries(m_cs.size());
  info.setNHits(m_fwCounters.nCsHits);
  info.setNMisses(m_fwCounters.nCsMisses);

  context.append(info.wireEncode());
  context.end();
}

} // namespace nfd
