/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2014-2025,  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&&... args) { changeConfig(std::forward<decltype(args)>(args)...); });
  registerCommandHandler<ndn::nfd::CsEraseCommand>("erase",
    [this] (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 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 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
