-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathonsa
executable file
·268 lines (199 loc) · 10.6 KB
/
onsa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env python
import os
import sys
import uuid
import socket
import random
from twisted.python import log, usage
from twisted.internet import reactor, defer
from opennsa.cli import options, parser, commands, logobserver
CLI_DEFAULTS = '.opennsa-cli'
WSDL_DEFAULT_DIRECTORY = '/usr/local/share/nsi/wsdl'
REQUESTER_URL_BASE = 'http://%s:%i/NSI/services/ConnectionService'
HELP_MESSAGE = '%s: Try --help or <command> --help for usage details.'
def getHostname(dst_nsa):
"""
Figure out the hostname of this machine
Unfortunately socket.getfqdn() is not a reliable way of getting the actual
fqdn used for the destination we are trying to reach. The best way to do
that is to open a socket towards the destination and then request the fqdn.
"""
dsthost,dstport = dst_nsa.getHostPort()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((dsthost,dstport))
hostname = s.getsockname()[0]
s.close()
return hostname
@defer.inlineCallbacks
def doMain():
config = parser.Options()
try:
config.parseOptions()
except usage.UsageError, errortext:
print '%s: %s' % (sys.argv[0], errortext)
print HELP_MESSAGE % (sys.argv[0])
return
# User did not enter a command.
if not hasattr(config, "subOptions"):
print HELP_MESSAGE % (sys.argv[0])
return
# this import is slighly slow, so we delay it until parsing is successfull
from opennsa import setup, nsa
observer = logobserver.SimpleObserver(sys.stdout)
log.startLoggingWithObserver(observer.emit)
if config.subOptions[options.VERBOSE]:
observer.debug = True
if config.subOptions[options.DUMP_PAYLOAD]:
observer.dump_payload = True
# read defaults
defaults_file = config.subOptions[options.DEFAULTS_FILE] or os.path.join( os.path.expanduser('~'), CLI_DEFAULTS )
if os.path.exists(defaults_file):
defaults = options.readDefaults( open(defaults_file) )
else:
defaults = {}
log.msg('Defaults:', debug=True)
for k,v in defaults.items():
log.msg(' %s : %s' % (k,v), debug=True)
if config.subCommand in ['reserve', 'reserveprovision', 'provision', 'release', 'terminate', 'querysummary', 'querydetails']:
wsdl_dir = config.subOptions[options.WSDL_DIRECTORY] or defaults.get(options.WSDL_DIRECTORY) or WSDL_DEFAULT_DIRECTORY
topology_file = config.subOptions[options.TOPOLOGY_FILE] or defaults.get(options.TOPOLOGY_FILE)
network = config.subOptions[options.NETWORK] or defaults.get(options.NETWORK)
service_url = config.subOptions[options.SERVICE_URL] or defaults.get(options.SERVICE_URL)
requester_nsa = config.subOptions[options.REQUESTER] or defaults.get(options.REQUESTER) or 'OpenNSA-CLI'
provider_nsa = config.subOptions[options.PROVIDER] or defaults.get(options.PROVIDER)
connection_id = config.subOptions[options.CONNECTION_ID] or defaults.get(options.CONNECTION_ID)
global_id = config.subOptions[options.GLOBAL_ID] or defaults.get(options.GLOBAL_ID)
if topology_file and network:
from opennsa.topology import gole
topo, _ = gole.parseTopology( [ open(topology_file) ] )
provider_nsa = topo.getNetwork(network).nsa
elif service_url:
provider_nsa = nsa.NetworkServiceAgent(provider_nsa, service_url)
else:
raise usage.UsageError('Neither topology file+network or service URL defined')
host = config.subOptions[options.HOST] or defaults.get(options.HOST) or getHostname(provider_nsa)
port = config.subOptions[options.PORT] or defaults.get(options.PORT) or 7080
requester_url = REQUESTER_URL_BASE % (host, port)
client_nsa = nsa.NetworkServiceAgent(requester_nsa, requester_url)
log.msg("Requester URL: %s" % requester_url)
tls = config.subOptions[options.TLS] or defaults.get(options.TLS) or False
# setup ssl context
public_key = config.subOptions[options.CERTIFICATE] or defaults.get(options.CERTIFICATE)
private_key = config.subOptions[options.KEY] or defaults.get(options.KEY)
certificate_dir = config.subOptions[options.CERTIFICATE_DIR] or defaults.get(options.CERTIFICATE_DIR)
# verify cert is a flag, if it is set, it means it should be skipped
if config.subOptions[options.VERIFY_CERT]:
verify_cert = False
else:
verify_cert = defaults.get(options.VERIFY_CERT)
# if we don't get a value from defaults set it to true (default)
if verify_cert is None:
verify_cert = True
ctx_factory = None
if public_key or private_key or certificate_dir:
if public_key and private_key and certificate_dir:
from opennsa import ctxfactory
ctx_factory = ctxfactory.ContextFactory(private_key, public_key, certificate_dir, verify_cert)
elif tls:
if not public_key:
raise usage.UsageError('Cannot setup TLS. No public key defined')
if not private_key:
raise usage.UsageError('Cannot setup TLS. No private key defined')
if not certificate_dir:
raise usage.UsageError('Cannot setup TLS. No certificate directory defined')
else:
log.msg('Missing options for creating SSL/TLS context: Cannot create SSL/TLS context.')
if tls and not ctx_factory:
raise usage.UsageError('Options for TLS/SSL context not defined. Cannot setup TLS.')
client, factory = setup.createClient(host, port, wsdl_dir, tls, ctx_factory)
if tls:
iport = reactor.listenSSL(port, factory, ctx_factory)
else:
iport = reactor.listenTCP(port, factory)
# commands
if config.subCommand in ('reserve', 'reserveprovision'):
source_stp = config.subOptions[options.SOURCE_STP] or defaults.get(options.SOURCE_STP)
dest_stp = config.subOptions[options.DEST_STP] or defaults.get(options.DEST_STP)
if source_stp is None:
raise usage.UsageError('Source STP is not defined')
if dest_stp is None:
raise usage.UsageError('Dest STP is not defined')
start_time = config.subOptions[options.START_TIME] or defaults.get(options.START_TIME)
end_time = config.subOptions[options.END_TIME] or defaults.get(options.END_TIME)
if start_time is None:
raise usage.UsageError('Start time is not defined')
if end_time is None:
raise usage.UsageError('End time is not defined')
bandwidth = config.subOptions[options.BANDWIDTH] or defaults.get(options.BANDWIDTH)
if bandwidth is None:
raise usage.UsageError('Bandwidth is not defined')
bandwidth = config.subOptions[options.BANDWIDTH] or defaults.get(options.BANDWIDTH)
if connection_id is None:
connection_id = uuid.uuid1()
if global_id is None:
global_id = 'conn-%i' % random.randrange(1000,9999)
yield commands.reserve(client, client_nsa, provider_nsa, source_stp, dest_stp, start_time, end_time, bandwidth, connection_id, global_id)
if config.subCommand == 'reserveprovision':
yield commands.provision(client, client_nsa, provider_nsa, connection_id)
yield iport.stopListening()
elif config.subCommand == 'provision':
if connection_id is None:
raise usage.UsageError('Connection ID is not defined')
yield commands.provision(client, client_nsa, provider_nsa, connection_id)
yield iport.stopListening()
elif config.subCommand == 'release':
if connection_id is None:
raise usage.UsageError('Connection ID is not defined')
yield commands.release(client, client_nsa, provider_nsa, connection_id)
yield iport.stopListening()
elif config.subCommand == 'terminate':
if connection_id is None:
raise usage.UsageError('Connection ID is not defined')
yield commands.terminate(client, client_nsa, provider_nsa, connection_id)
yield iport.stopListening()
elif config.subCommand == 'querysummary':
connection_ids = [ connection_id ] if connection_id else None
global_ids = [ global_id ] if global_id else None
yield commands.querysummary(client, client_nsa, provider_nsa, connection_ids, global_ids)
yield iport.stopListening()
elif config.subCommand == 'querydetails':
connection_ids = [ connection_id ] if connection_id else None
global_ids = [ global_id ] if global_id else None
yield commands.querydetails(client, client_nsa, provider_nsa, connection_ids, global_ids)
yield iport.stopListening()
elif config.subCommand == 'path':
topology_file = config.subOptions[options.TOPOLOGY_FILE] or defaults.get(options.TOPOLOGY_FILE)
source_stp = config.subOptions[options.SOURCE_STP] or defaults.get(options.SOURCE_STP)
dest_stp = config.subOptions[options.DEST_STP] or defaults.get(options.DEST_STP)
commands.path(topology_file, source_stp, dest_stp)
elif config.subCommand == 'topology':
topology_file = config.subOptions[options.TOPOLOGY_FILE] or defaults.get(options.TOPOLOGY_FILE)
commands.topology(topology_file)
elif config.subCommand == 'topology-graph':
topology_file = config.subOptions[options.TOPOLOGY_FILE] or defaults.get(options.TOPOLOGY_FILE)
full_graph = config.subOptions[options.FULL_GRAPH] or False
commands.topologyGraph(topology_file, full_graph)
else:
print "Invalid subcommand specified"
print '%s: Try --help for usage details.' % (sys.argv[0])
return
def main():
def slightlyDelayedShutdown(_):
# this means that the reactor/kernel will have a bit of time
# to push off any replies/acks before shutdown
reactor.callLater(0.1, reactor.stop)
def printError(error):
if error.type == SystemExit:
return
elif error.type == usage.UsageError:
log.msg("Usage error: " + error.getErrorMessage())
else:
#print "Error: %s" % error.value
log.err(error)
d = defer.maybeDeferred(doMain)
d.addErrback(printError)
d.addBoth(slightlyDelayedShutdown)
return d
if __name__ == '__main__':
reactor.callWhenRunning(main)
reactor.run()