blob: 9e1e184b6df6ee49d4fc13768cf40d772ec637fc [file] [log] [blame]
Alexander Lane6f7a64f2018-05-17 15:01:14 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
Saurab Dulal576a4192020-08-25 00:55:22 -05003# Copyright (C) 2015-2020, The University of Memphis,
Alexander Lane6f7a64f2018-05-17 15:01:14 -05004# Arizona Board of Regents,
5# Regents of the University of California.
6#
7# This file is part of Mini-NDN.
8# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
9#
10# Mini-NDN is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# Mini-NDN is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with Mini-NDN, e.g., in COPYING.md file.
22# If not, see <http://www.gnu.org/licenses/>.
23
Saurab Dulal8ae870a2018-07-31 05:17:49 +000024from mininet.log import debug
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050025from minindn.minindn import Minindn
Saurab Dulal8ae870a2018-07-31 05:17:49 +000026
27SLEEP_TIME = 0.2
Alexander Lane6f7a64f2018-05-17 15:01:14 -050028
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050029class Nfdc(object):
30 STRATEGY_ASF = 'asf'
31 STRATEGY_BEST_ROUTE = 'best-route'
32 STRATEGY_MULTICAST = 'multicast'
33 STRATEGY_NCC = 'ncc'
34 PROTOCOL_UDP = 'udp'
35 PROTOCOL_TCP = 'tcp'
36 PROTOCOL_ETHER = 'ether'
Alexander Lane6f7a64f2018-05-17 15:01:14 -050037
38 @staticmethod
Alex Lane1d3c0a82021-07-22 17:28:16 -050039 def registerRoute(node, namePrefix, remoteNode, protocol=PROTOCOL_UDP, origin=255,
Ashlesh Gawande6651a742019-01-03 18:13:06 -060040 cost=0, inheritFlag=True, captureFlag=False, expirationInMillis=None):
Alex Lane1d3c0a82021-07-22 17:28:16 -050041 cmd = ""
42 if remoteNode.isdigit() and not protocol == "fd":
43 cmd = ('nfdc route add {} {} origin {} cost {} {}{}{}').format(
44 namePrefix,
45 remoteNode,
46 origin,
47 cost,
48 'no-inherit ' if not inheritFlag else '',
49 'capture ' if captureFlag else '',
50 'expires {}'.format(expirationInMillis) if expirationInMillis else ''
51 )
52 else:
53 cmd = ('nfdc route add {} {}://{} origin {} cost {} {}{}{}').format(
54 namePrefix,
55 protocol,
56 remoteNode,
57 origin,
58 cost,
59 'no-inherit ' if not inheritFlag else '',
60 'capture ' if captureFlag else '',
61 'expires {}'.format(expirationInMillis) if expirationInMillis else ''
62 )
dulalsaurab2b899532018-10-25 18:02:15 +000063 debug(node.cmd(cmd))
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050064 Minindn.sleep(SLEEP_TIME)
Alexander Lane6f7a64f2018-05-17 15:01:14 -050065
66 @staticmethod
Alex Lane1d3c0a82021-07-22 17:28:16 -050067 def unregisterRoute(node, namePrefix, remoteNode, origin=255):
68 cmd = ""
69 if remoteNode.isdigit() and not protocol == "fd":
70 cmd = 'nfdc route remove {} {} {}'.format(namePrefix, remoteNode, origin)
71 else:
72 cmd = 'nfdc route remove {} {} {}'.format(namePrefix, remoteNode, origin)
dulalsaurab0dcdb322018-08-15 20:39:07 +000073 debug(node.cmd(cmd))
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050074 Minindn.sleep(SLEEP_TIME)
Alexander Lane6f7a64f2018-05-17 15:01:14 -050075
76 @staticmethod
Varun Patilc69041f2022-05-18 14:17:54 -070077 def createFace(node, remoteNodeAddress, protocol='udp', isPermanent=False, allowExisting=True):
Alex Lane1d3c0a82021-07-22 17:28:16 -050078 '''Create face in node's NFD instance. Returns FaceID of created face or -1 if failed.'''
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050079 cmd = ('nfdc face create {}://{} {}'.format(
Alexander Lane6f7a64f2018-05-17 15:01:14 -050080 protocol,
Ashlesh Gawande6651a742019-01-03 18:13:06 -060081 remoteNodeAddress,
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050082 'permanent' if isPermanent else 'persistent'
Alexander Lane6f7a64f2018-05-17 15:01:14 -050083 ))
Alex Lane1d3c0a82021-07-22 17:28:16 -050084 output = node.cmd(cmd)
85 debug(output)
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050086 Minindn.sleep(SLEEP_TIME)
Varun Patilc69041f2022-05-18 14:17:54 -070087 if "face-created" in output or (allowExisting and "face-exists" in output):
88 faceID = output.split(" ")[1][3:]
89 return faceID
90 return -1
Alexander Lane6f7a64f2018-05-17 15:01:14 -050091
92 @staticmethod
Alex Lane1d3c0a82021-07-22 17:28:16 -050093 def destroyFace(node, remoteNode, protocol='udp'):
94 if remoteNode.isdigit() and not protocol == "fd":
95 debug(node.cmd('nfdc face destroy {}'.format(protocol, remoteNode)))
96 else:
97 debug(node.cmd('nfdc face destroy {}://{}'.format(protocol, remoteNode)))
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050098 Minindn.sleep(SLEEP_TIME)
Alexander Lane6f7a64f2018-05-17 15:01:14 -050099
100 @staticmethod
101 def setStrategy(node, namePrefix, strategy):
Ashlesh Gawande6c86e302019-09-17 22:27:05 -0500102 cmd = 'nfdc strategy set {} ndn:/localhost/nfd/strategy/{}'.format(namePrefix, strategy)
dulalsaurab2b899532018-10-25 18:02:15 +0000103 debug(node.cmd(cmd))
Ashlesh Gawande6c86e302019-09-17 22:27:05 -0500104 Minindn.sleep(SLEEP_TIME)
Alexander Lane6f7a64f2018-05-17 15:01:14 -0500105
106 @staticmethod
107 def unsetStrategy(node, namePrefix):
dulalsaurab2b899532018-10-25 18:02:15 +0000108 debug(node.cmd("nfdc strategy unset {}".format(namePrefix)))
Ashlesh Gawande6c86e302019-09-17 22:27:05 -0500109 Minindn.sleep(SLEEP_TIME)
Alex Lane1d3c0a82021-07-22 17:28:16 -0500110
111 @staticmethod
112 def getFaceId(node, remoteNodeAddress, localEndpoint=None, protocol="udp", portNum="6363"):
113 '''Returns the faceId for a remote node based on FaceURI, or -1 if a face is not found'''
114 #Should this be cached or is the hit not worth it?
115 local = ""
116 if localEndpoint:
117 local = " local {}".format(localEndpoint)
118 output = node.cmd("nfdc face list remote {}://{}:{}{}".format(protocol, remoteNodeAddress, portNum, local))
119 debug(output)
120 Minindn.sleep(SLEEP_TIME)
121 # This is fragile but we don't have that many better options
122 if "faceid=" not in output:
123 return -1
124 faceId = output.split(" ")[0][7:]
125 return faceId