identity-container.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2023 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
23 #include "ndn-cxx/security/pib/impl/identity-impl.hpp"
25 #include "ndn-cxx/util/logger.hpp"
26 
27 namespace ndn::security::pib {
28 
29 NDN_LOG_INIT(ndn.security.IdentityContainer);
30 
31 bool
32 IdentityContainer::const_iterator::equals(const const_iterator& other) const noexcept
33 {
34  bool isThisEnd = m_container == nullptr || m_it == m_container->m_identityNames.end();
35  bool isOtherEnd = other.m_container == nullptr || other.m_it == other.m_container->m_identityNames.end();
36  if (isThisEnd)
37  return isOtherEnd;
38  return !isOtherEnd && m_container->m_pib == other.m_container->m_pib && m_it == other.m_it;
39 }
40 
41 IdentityContainer::IdentityContainer(shared_ptr<PibImpl> pibImpl)
42  : m_pib(std::move(pibImpl))
43 {
44  BOOST_ASSERT(m_pib != nullptr);
45  m_identityNames = m_pib->getIdentities();
46 }
47 
48 IdentityContainer::const_iterator
49 IdentityContainer::find(const Name& identity) const
50 {
51  return {m_identityNames.find(identity), *this};
52 }
53 
55 IdentityContainer::add(const Name& identityName)
56 {
57  bool didInsert = m_identityNames.insert(identityName).second;
58  if (!didInsert) {
59  // identity already exists
60  return get(identityName);
61  }
62 
63  NDN_LOG_DEBUG("Adding " << identityName);
64  m_pib->addIdentity(identityName);
65  auto ret = m_identities.emplace(identityName,
66  std::make_shared<IdentityImpl>(identityName, m_pib));
67  // consistency check
68  BOOST_ASSERT(ret.second);
69 
70  return Identity(ret.first->second);
71 }
72 
73 void
74 IdentityContainer::remove(const Name& identityName)
75 {
76  if (m_identityNames.erase(identityName) > 0) {
77  NDN_LOG_DEBUG("Removing " << identityName);
78  m_identities.erase(identityName);
79  }
80  else {
81  // consistency check
82  BOOST_ASSERT(m_identities.find(identityName) == m_identities.end());
83  }
84  m_pib->removeIdentity(identityName);
85 }
86 
88 IdentityContainer::get(const Name& identityName) const
89 {
90  if (auto it = m_identities.find(identityName); it != m_identities.end()) {
91  return Identity(it->second);
92  }
93 
94  // check that the identity exists in the backend
95  if (!m_pib->hasIdentity(identityName)) {
96  NDN_THROW(Pib::Error("Identity `" + identityName.toUri() + "` does not exist"));
97  }
98 
99  auto id = std::make_shared<IdentityImpl>(identityName, m_pib);
100  m_identities[identityName] = id;
101  return Identity(id);
102 }
103 
104 void
105 IdentityContainer::reset()
106 {
107  NDN_LOG_DEBUG("Reloading");
108  m_identities.clear();
109  m_identityNames = m_pib->getIdentities();
110 }
111 
112 bool
113 IdentityContainer::isConsistent() const
114 {
115  return m_identityNames == m_pib->getIdentities();
116 }
117 
118 } // namespace ndn::security::pib
Represents an absolute name.
Definition: name.hpp:45
void toUri(std::ostream &os, name::UriFormat format=name::UriFormat::DEFAULT) const
Write URI representation of the name to the output stream.
Definition: name.cpp:324
Frontend handle for an identity in the PIB.
Definition: identity.hpp:44
Represents a semantic error.
Definition: pib.hpp:56
#define NDN_THROW(e)
Definition: exception.hpp:56
#define NDN_LOG_DEBUG(expression)
Log at DEBUG level.
Definition: logger.hpp:260
#define NDN_LOG_INIT(name)
Define a non-member log module.
Definition: logger.hpp:169
Contains the ndn-cxx security framework.
Definition: data.cpp:25