/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2014-2022,  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 "route.hpp"

#include <ndn-cxx/util/string-helper.hpp>

namespace nfd::rib {

constexpr uint64_t PA_ROUTE_COST = 2048; // cost of route created by prefix announcement

static time::steady_clock::time_point
computeExpiration(const ndn::PrefixAnnouncement& ann)
{
  auto validityEnd = time::steady_clock::duration::max();
  if (ann.getValidityPeriod()) {
    auto now = time::system_clock::now();
    if (!ann.getValidityPeriod()->isValid(now)) {
      validityEnd = time::steady_clock::duration::zero();
    }
    else {
      validityEnd = ann.getValidityPeriod()->getPeriod().second - now;
    }
  }
  return time::steady_clock::now() +
    std::min(validityEnd, time::duration_cast<time::steady_clock::duration>(ann.getExpiration()));
}

Route::Route(const ndn::PrefixAnnouncement& ann, uint64_t faceId)
  : faceId(faceId)
  , origin(ndn::nfd::ROUTE_ORIGIN_PREFIXANN)
  , cost(PA_ROUTE_COST)
  , flags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT)
  , expires(computeExpiration(ann))
  , announcement(ann)
  , annExpires(*expires)
{
}

bool
operator==(const Route& lhs, const Route& rhs)
{
  return lhs.faceId == rhs.faceId &&
         lhs.origin == rhs.origin &&
         lhs.flags == rhs.flags &&
         lhs.cost == rhs.cost &&
         lhs.expires == rhs.expires &&
         lhs.announcement == rhs.announcement;
}

std::ostream&
operator<<(std::ostream& os, const Route& route)
{
  os << "Route("
     << "faceid: " << route.faceId
     << ", origin: " << route.origin
     << ", cost: " << route.cost
     << ", flags: " << ndn::AsHex{route.flags};
  if (route.expires) {
    os << ", expires in: " << time::duration_cast<time::milliseconds>(*route.expires - time::steady_clock::now());
  }
  else {
    os << ", never expires";
  }
  if (route.announcement) {
    os << ", announcement: (" << *route.announcement << ')';
  }
  os << ')';

  return os;
}

} // namespace nfd::rib
