blob: b7d060cde6c1aa6edcc45d1fc00763a1dbb3b3a6 [file] [log] [blame]
Ashlesh Gawande6c86e302019-09-17 22:27:05 -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,
Ashlesh Gawande6c86e302019-09-17 22:27:05 -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
24import sys
25from os.path import isfile
26from subprocess import call
dulalsaurab0ed77722020-09-24 22:32:58 +000027from six.moves.urllib.parse import quote
28
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050029from mininet.cli import CLI
Alexander Laneea2d5d62019-10-04 16:48:52 -050030from mn_wifi.cli import CLI as CLI_wifi
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050031
32sshbase = ['ssh', '-q', '-t', '-i/home/mininet/.ssh/id_rsa']
33scpbase = ['scp', '-i', '/home/mininet/.ssh/id_rsa']
34devnull = open('/dev/null', 'w')
35
dulalsaurab0ed77722020-09-24 22:32:58 +000036def getSafeName(namePrefix):
37 """
38 Check if the prefix/string is safe to use with ndn commands or not.
39 return safe prefix.
40 :param namePrefix: name of the prefix
41 """
42 # remove redundant "/"es, multiple "/"es are an invalid representation for empty name component
43 # https://named-data.net/doc/NDN-packet-spec/current/changelog.html#version-0-3
44 namePrefix= "/" + ("/".join(filter(None, namePrefix.split("/"))))
45 return quote(namePrefix, safe='/')
46
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050047def ssh(login, cmd):
48 rcmd = sshbase + [login, cmd]
49 call(rcmd, stdout=devnull, stderr=devnull)
50
51def scp(*args):
52 tmp = []
53 for arg in args:
54 tmp.append(arg)
55 rcmd = scpbase + tmp
56 call(rcmd, stdout=devnull, stderr=devnull)
57
58def copyExistentFile(node, fileList, destination):
59 for f in fileList:
60 if isfile(f):
61 node.cmd('cp {} {}'.format(f, destination))
62 break
63 if not isfile(destination):
64 fileName = destination.split('/')[-1]
65 raise IOError('{} not found in expected directory.'.format(fileName))
66
67def popenGetEnv(node, envDict=None):
68 env = {}
69 homeDir = node.params['params']['homeDir']
70 printenv = node.popen('printenv'.split(), cwd=homeDir).communicate()[0].decode('utf-8')
71 for var in printenv.split('\n'):
72 if var == '':
73 break
74 p = var.split('=')
75 env[p[0]] = p[1]
76 env['HOME'] = homeDir
77
78 if envDict is not None:
79 for key, value in envDict.items():
80 env[key] = str(value)
81
82 return env
83
84def getPopen(host, cmd, envDict=None, **params):
85 return host.popen(cmd, cwd=host.params['params']['homeDir'],
86 env=popenGetEnv(host, envDict), **params)
87
88class MiniNDNCLI(CLI):
89 prompt = 'mini-ndn> '
90 def __init__(self, mininet, stdin=sys.stdin, script=None):
91 CLI.__init__(self, mininet, stdin, script)
Alexander Laneea2d5d62019-10-04 16:48:52 -050092
93class MiniNDNWifiCLI(CLI_wifi):
94 prompt = 'mini-ndn-wifi> '
95 def __init__(self, mininet, stdin=sys.stdin, script=None):
dulalsaurab0ed77722020-09-24 22:32:58 +000096 CLI_wifi.__init__(self, mininet, stdin, script)