A Discrete-Event Network Simulator
API
qkd-connection-register.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2020 DOTFEESA www.tk.etf.unsa.ba
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Emir Dervisevic <emir.dervisevic@etf.unsa.ba>
19  * Miralem Mehic <miralem.mehic@ieee.org>
20  */
21 #include "ns3/log.h"
22 #include "ns3/address.h"
23 #include "ns3/node.h"
24 #include "ns3/nstime.h"
25 #include "ns3/simulator.h"
26 #include "ns3/uinteger.h"
27 #include "ns3/trace-source-accessor.h"
28 #include <iostream>
29 #include <fstream>
30 #include <string>
31 
32 #include "ns3/qkd-connection-register.h"
33 
34 namespace ns3 {
35 
36 NS_LOG_COMPONENT_DEFINE ("QKDConnectionRegister");
37 
38 NS_OBJECT_ENSURE_REGISTERED (QKDConnectionRegister);
39 
40 TypeId
42 {
43  static TypeId tid = TypeId ("ns3::QKDConnectionRegister")
44  .SetParent<Object> ()
45  .SetGroupName ("QKDConnectionRegister")
46  .AddConstructor<QKDConnectionRegister> ()
47  ;
48  return tid;
49 }
50 
51 TypeId
53 {
54  return GetTypeId ();
55 }
56 
58 
59 bool
61 {
62  NS_LOG_FUNCTION(this << rt.GetId () );
63 
64  std::pair<std::map<UUID, QKDApplicationEntry>::iterator, bool> result = m_applications.insert (
65  std::make_pair (rt.GetId (), rt)
66  );
67 
68 
69  for (std::map<UUID, QKDApplicationEntry>::iterator i3 = m_applications.begin (); i3 != m_applications.end (); ++i3)
70  {
71  NS_LOG_FUNCTION(this
72  << "aaaaaa"
73  << i3->second.GetId()
74  << i3->second.GetSrcSaeId()
75  << i3->second.GetDestinationSaeId()
76  << i3->second.GetType()
77  );
78  }
79 
80  return result.second;
81 }
82 
83 bool
85 {
86  NS_LOG_FUNCTION(this << "saving keyAssociationId:" << rt.GetId().string());
87  std::pair<std::map<uint32_t, QKDKeyAssociationLinkEntry>::iterator, bool> result = m_keyAssociations.insert (
88  std::make_pair (rt.GetDestinationNodeId (),rt)
89  );
90  return result.second;
91 }
92 
93 bool
95 {
96  if (m_applications.erase (dstSaeId) != 0) return true;
97  return false;
98 }
99 
100 bool
102 {
103  if (m_keyAssociations.erase (dstSaeId) != 0) return true;
104  return false;
105 }
106 
107 bool
109  UUID id,
111 ){
112  NS_LOG_FUNCTION(this << id << m_applications.size() );
113  if (m_applications.empty ()) return false;
114 
115  std::map<UUID, QKDApplicationEntry>::const_iterator i = m_applications.find (id);
116  if (i != m_applications.end ()) {
117  rt = i->second;
118  return true;
119  }else{
120 
121  for (std::map<UUID, QKDApplicationEntry>::iterator i3 = m_applications.begin (); i3 != m_applications.end (); ++i3)
122  {
123  if(
124  i3->second.GetSrcSaeId() == id || i3->second.GetDestinationSaeId() == id
125  ){
126  rt = i3->second;
127  return true;
128  }
129 
130  NS_LOG_FUNCTION(this
131  << "bbbbbb"
132  << i3->second.GetId()
133  << i3->second.GetSrcSaeId()
134  << i3->second.GetDestinationSaeId()
135  << i3->second.GetType()
136  );
137  }
138  }
139  return false;
140 
141 }
142 
143 bool
145  UUID srcSaeId,
146  UUID dstSaeId,
149 ){
150  NS_LOG_FUNCTION(this << srcSaeId << dstSaeId << type << m_applications.size() );
151  if (m_applications.empty ()) return false;
152 
153  for (std::map<UUID, QKDApplicationEntry>::iterator i3 = m_applications.begin (); i3 != m_applications.end (); ++i3)
154  {
155  if(
156  (
157  (i3->second.GetSrcSaeId() == srcSaeId && i3->second.GetDestinationSaeId() == dstSaeId) ||
158  (i3->second.GetSrcSaeId() == dstSaeId && i3->second.GetDestinationSaeId() == srcSaeId)
159  ) && i3->second.GetType() == type
160  ){
161  rt = i3->second;
162  return true;
163  }
164 
165  NS_LOG_FUNCTION(this
166  << "bbbbbb"
167  << i3->second.GetId()
168  << i3->second.GetSrcSaeId()
169  << i3->second.GetDestinationSaeId()
170  << i3->second.GetType()
171  );
172  }
173 
174  return false;
175 }
176 
177 
178 
179 bool
181  UUID keyAssociationId,
183 ){
184  NS_LOG_FUNCTION(this << keyAssociationId );
185 
186  if (m_keyAssociations.empty ()) return false;
187  for (std::map<uint32_t, QKDKeyAssociationLinkEntry>::iterator i = m_keyAssociations.begin (); i != m_keyAssociations.end (); ++i)
188  {
189  if(i->second.GetId() == keyAssociationId){
190  rt = i->second;
191  return true;
192  }
193  }
194  return false;
195 }
196 
197 bool
199  uint32_t srcNodeId,
200  uint32_t dstNodeId,
202 ){
203  NS_LOG_FUNCTION(this << srcNodeId << dstNodeId);
204 
205  if (m_keyAssociations.empty ()) return false;
206 
207  for (std::map<uint32_t, QKDKeyAssociationLinkEntry>::iterator i = m_keyAssociations.begin (); i != m_keyAssociations.end (); ++i)
208  {
209  if(
210  (i->second.GetSourceNodeId() == srcNodeId && i->second.GetDestinationNodeId() == dstNodeId) ||
211  (i->second.GetSourceNodeId() == dstNodeId && i->second.GetDestinationNodeId() == srcNodeId)
212  ){
213  rt = i->second;
214  return true;
215  }
216  }
217  return false;
218 }
219 
220 bool
222  UUID appId,
224 ){
225  NS_LOG_FUNCTION(this << appId << m_keyAssociations.size() );
227 
228  if (m_keyAssociations.empty ()) return false;
229  std::map<double, QKDKeyAssociationLinkEntry> connectedLinks;
230  for (std::map<uint32_t, QKDKeyAssociationLinkEntry>::iterator i = m_keyAssociations.begin (); i != m_keyAssociations.end (); ++i)
231  {
232  if( i->second.CheckSAEApplicationExists(appId) ) {
233  connectedLinks.insert(
234  std::make_pair(i->second.GetEffectiveSKR(), i->second)
235  );
236  rt = i->second;
237  }
238  }
239 
240  //@toDo: one qkd application can be connected to multiple key association entries
241  if(connectedLinks.size()){
242  uint32_t counter = 1;
243  uint32_t random = rand() % connectedLinks.size();
244  for (std::map<double, QKDKeyAssociationLinkEntry>::iterator i = connectedLinks.begin (); i != connectedLinks.end (); ++i)
245  {
246  if(counter == random){
247  rt = i->second;
248  return true;
249  }
250  counter++;
251  }
252  }
253  return false;
254 }
255 
256 bool
258  UUID srcSaeId,
259  UUID dstSaeId,
260  std::string type,
261  uint32_t priority,
263 ){
264  NS_LOG_FUNCTION(this << srcSaeId << dstSaeId << type << priority << m_keyAssociations.size() );
266 
267  if (m_keyAssociations.empty ()) return false;
268  std::map<double, QKDKeyAssociationLinkEntry> connectedLinks;
269  for (std::map<uint32_t, QKDKeyAssociationLinkEntry>::iterator i = m_keyAssociations.begin (); i != m_keyAssociations.end (); ++i)
270  {
271  NS_LOG_FUNCTION(this << "Assigned keyAssociationId:" << i->second.GetId().string());
272  rt = i->second;
273  return true;
274  }
275  return false;
276 }
277 
278 bool
281 ){
282  NS_LOG_FUNCTION(this << rt.GetId() );
284 
285  if (m_keyAssociations.empty ()) return false;
286  for (std::map<uint32_t, QKDKeyAssociationLinkEntry>::iterator i = m_keyAssociations.begin (); i != m_keyAssociations.end (); ++i)
287  {
288  if(i->second.GetId() == rt.GetId()){
289  i->second = rt;
290  return true;
291  }
292  }
293  return false;
294 }
295 
296 void
298 {
299  NS_LOG_FUNCTION(this << m_keyAssociations.size() );
300 
301  for (std::map<uint32_t, QKDKeyAssociationLinkEntry>::iterator i = m_keyAssociations.begin (); i != m_keyAssociations.end (); ++i)
302  {
303  i->second.PrintSAEApplications();
304  }
305 }
306 
307 void
309 
310  NS_LOG_FUNCTION(this << keyAssociationId << appId);
311 
312  bool keyAssociationFound = false;
313  for (std::map<uint32_t, QKDKeyAssociationLinkEntry>::iterator i = m_keyAssociations.begin (); i != m_keyAssociations.end (); ++i)
314  {
315  if(i->second.GetId() == keyAssociationId){
316  i->second.UpdateQKDApplications(appId);
317  i->second.PrintRegistryInfo();
318  i->second.PrintSAEApplications();
319  keyAssociationFound = true;
320  }
321  }
322  if(!keyAssociationFound){
323  NS_LOG_FUNCTION(this << "No key association found with id " << keyAssociationId);
324  }
325 }
326 
327 
328 /*
329 void
330 QKDConnectionRegister::GetListOfAllEntries (std::map<uint32_t, QKDConnectionRegisterEntry> & allRoutes)
331 {
332  for (std::map<uint32_t, QKDConnectionRegisterEntry>::iterator i = m_locationEntites.begin (); i != m_locationEntites.end (); ++i)
333  {
334  allRoutes.insert (std::make_pair (i->first,i->second));
335  }
336 }
337 
338 void
339 QKDConnectionRegister::GetListOfDestinationWithNextHop (uint32_t nextHop,
340  std::map<uint32_t, QKDConnectionRegisterEntry> & unreachable)
341 {
342  unreachable.clear ();
343  for (std::map<uint32_t, QKDConnectionRegisterEntry>::const_iterator i = m_locationEntites.begin (); i
344  != m_locationEntites.end (); ++i)
345  {
346  if (i->second.GetNextHop () == nextHop)
347  {
348  unreachable.insert (std::make_pair (i->first,i->second));
349  }
350  }
351 }
352 */
353 
354 
355 } // namespace ns3
A base class which provides memory management and object aggregation.
Definition: object.h:88
Introspection did not find any typical Config paths.
ConnectionType
The connection types.
UUID GetId()
Get the application identifier.
QKDConnectionRegister is a class used to keep details about distant QKD links and applications.
virtual TypeId GetInstanceTypeId(void) const
Get the type ID for the instance.
static TypeId GetTypeId(void)
Get the type ID.
bool LookupKeyAssociationByApplicationId(UUID saeId, QKDKeyAssociationLinkEntry &rt)
Lookup the key association entry with the application identifier (UUID).
void PrintListOfAllKeyAssociations()
Print the key association list.
bool AddApplicationEntry(QKDApplicationEntry &r)
Add the QKD application entry if it doesn't yet exist in the table.
bool LookupKeyAssociationByDestinationNodeId(uint32_t srcNodeId, uint32_t dstNodeId, QKDKeyAssociationLinkEntry &rt)
Lookup the key association entry with the source and destination identifiers.
std::map< UUID, QKDApplicationEntry > m_applications
The list of applications.
bool SaveKeyAssociation(QKDKeyAssociationLinkEntry &rt)
Store the key association entry.
bool LookupKeyAssociationById(UUID keyAssociationId, QKDKeyAssociationLinkEntry &rt)
Lookup the key association entry with destination node identifier.
bool LookupApplication(UUID dstSaeId, QKDApplicationEntry &rt)
Lookup the application entry with the destination (receiver) application identifier (UUID).
std::map< uint32_t, QKDKeyAssociationLinkEntry > m_keyAssociations
The list of key associations.
bool DeleteKeyAssociationLinkEntry(uint32_t dst)
Delete the key association table entry with a given destination, if it exists.
bool AssignKeyAssociation(UUID srcSaeId, UUID dstSaeId, std::string type, uint32_t priority, QKDKeyAssociationLinkEntry &rt)
Assign the key association.
bool DeleteApplicationEntry(UUID dst)
Delete the application entry, if it exists.
void UpdateQKDApplications(UUID keyAssociationId, UUID saeId)
Update the key association list of QKD applications.
bool LookupApplicationBySaeIDsAndType(UUID srcSaeId, UUID dstSaeId, QKDApplicationEntry::ConnectionType type, QKDApplicationEntry &rt)
Lookup the application entry with the application identifiers and the connection type.
bool AddKeyAssociationEntry(QKDKeyAssociationLinkEntry &r)
Add the key association entry if it doesn't yet exist in the table.
Introspection did not find any typical Config paths.
UUID GetId()
uint32_t GetDestinationNodeId()
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
Universally unique identifier (UUID)
Definition: uuid.h:35
std::string string() const
Get string from the current UUID in format "00000000-0000-0000-0000-000000000000".
Definition: uuid.cc:74
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Every class exported by the ns3 library is enclosed in the ns3 namespace.