-
Notifications
You must be signed in to change notification settings - Fork 1
/
APIOpenmano.py
286 lines (230 loc) · 7.05 KB
/
APIOpenmano.py
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import argparse
import requests
from ConfigParser import SafeConfigParser
from requests import post, delete
parser = SafeConfigParser()
parser.read('upr.conf')
tenant=parser.get('mano','tenant')
remoteIp=parser.get('mano','ipMano')
remotePort=parser.get('mano','portMano')
pathScenario=parser.get('mano','pathScenario')
urlbase='http://'+remoteIp+':'+remotePort+'/openmano/'
pathYaml=pathScenario+'/TVD/'
pathNed= pathScenario+'/NED/'
def _print_verbose(mano_response, verbose_level=0):
content = mano_response.json()
result = 0 if mano_response.status_code==200 else mano_response.status_code
if type(content)!=dict or len(content)!=1:
#print "Non expected format output"
#print str(content)
return result
val=content.values()[0]
if type(val)==str:
#print val
return result
elif type(val) == list:
content_list = val
elif type(val)==dict:
content_list = [val]
else:
#print "Non expected dict/list format output"
#print str(content)
return result
#print content_list
if verbose_level==None:
verbose_level=0
if verbose_level >= 3:
print yaml.safe_dump(content, indent=4, default_flow_style=False)
return result
id=''
if mano_response.status_code == 200:
for content in content_list:
myoutput = "%s %s" %(content['uuid'].ljust(38),content['name'].ljust(20))
id= content['uuid'].ljust(38)
if verbose_level >=1:
myoutput += " " + content['created_at'].ljust(20)
if verbose_level >=2:
new_line='\n'
if 'type' in content and content['type']!=None:
myoutput += new_line + " Type: " + content['type'].ljust(29)
new_line=''
if 'type' in content and content['type']!=None:
myoutput += new_line + " Type: " + content['type'].ljust(29)
new_line=''
if 'description' in content and content['description']!=None:
myoutput += new_line + " Description: " + content['description'].ljust(20)
print myoutput
else:
print content['error']['description']
#return id
return result
def scenario_create(scenario):
d=(open(scenario,'rb'))
dataset=d.read()
d.close()
#print dataset
headers_req = {'content-type': 'application/yaml'}
URLrequest = "http://%s:%s/openmano/%s/scenarios" %(remoteIp, remotePort, tenant)
mano_response=requests.post(URLrequest, headers = headers_req, data=dataset)
r = _print_verbose(mano_response)
#print r
return r
def parseAG(nameXML, nameScenario, nameDoc=None):
import os
from requests import get
if nameDoc is None:
nameDoc = nameScenario+'.yaml'
from xml.dom import minidom
try:
ag = minidom.parse(nameXML)
PSAlist = ag.getElementsByTagName('PSA')
except:
print 'format error xml'
return
string1="""description: Complex network scenario consisting of """
string1b=""" PSAS connected
topology:
nodes:
PC: # vnf/net name in the scenario
graph:
x: 0
y: 142
type: VNF # VNF, network, external_network (if it is a datacenter network)
VNF model: linux # VNF name as introduced in OPENMANO DB
MOVIL: # vnf/net name in the scenario
graph:
x: 120
y: 142
type: VNF # VNF, network, external_network (if it is a datacenter network)
VNF model: linux # VNF name as introduced in OPENMANO DB
"""
modelVFN2="""
type: VNF
VNF model: """
string2="""
bridge1:
graph:
x: 217
y: 264
type: network
model: bridge_net
default:
graph:
x: 16
y: 464
type: external_network # Datacenter net
model: default
connections:
"""
tam=len(PSAlist)
f=open(nameDoc,'a')
f.write('\n---\nname: '+nameScenario+'\n')
f.write(string1+str(tam)+string1b)
lim=500
lim1=lim/tam
vy=lim1/2
vx=570
for s in PSAlist:
nombre1=s.attributes['name'].value
f.write(' '+nombre1+':\n')
f.write(' graph:\n')
f.write(' x: '+str(vx)+'\n')
f.write(' y: '+str(vy))
f.write(modelVFN2)
print urlbase+tenant+'/vnfs/'+str(nombre1)
r=get (urlbase+tenant+'/vnfs/'+str(nombre1))
print r
t='<Response [200]>'
if str(r) == t:
f.write(nombre1+'\n')
else:
f.write ('PSA \n')
vy=vy+lim1
f.write(string2)
a=len(PSAlist)
for i in range(1, a):
f.write(' dataconn'+str(i)+':\n nodes: \n - '+PSAlist[i-1].attributes['name'].value+': xe1\n - '+PSAlist[i].attributes['name'].value+': xe0\n')
f.write(' bridgeconn1:\n nodes: \n - bridge1: null\n - PC: eth0\n - MOVIL: eth0\n - '+PSAlist[0].attributes['name'].value+': xe0\n ')
b=a-1
f.write('mngmt-net:\n nodes: \n - default: null\n - '+PSAlist[b].attributes['name'].value+': xe1\n')
f.close()
os.remove(nameXML)
return
def parseNed(nameXML, nameScenario, nameDOC=None):
import os
from requests import delete
if nameDOC is None:
nameDOC = pathNed+'yamls/'+nameScenario+'.yaml'
f=open(nameXML, 'r')
g=f.read()
f.close()
G=g.split('\n')
tam=len(G)
if tam==1:
try:
delete (str(urlbase+tenant+'/scenarios/'+nameScenario))
except:
pass
try:
os.remove(nameXML)
except:
pass
return
else:
pass
string1="""description: Complex network scenario consisting of """
string1b=""" TVD connected
topology:
nodes:
PC: # vnf/net name in the scenario
graph:
x: 1
y: 294
type: VNF # VNF, network, external_network (if it is a$
VNF model: linux # VNF name as introduced in OPENMANO DB
"""
string2="""
bridge_net:
graph:
x: 166
y: 323
type: network
model: bridge_net
default:
graph:
x: 16
y: 464
type: external_network # Datacenter net
model: default
connections:
"""
modelTVD="""
type: VNF
VNF model: TVD
"""
f=open(nameDOC,'w')
f.write('')
f.close()
f=open(nameDOC,'a')
f.write('\n---\nname: '+nameScenario+'\n')
f.write(string1+str(tam-1)+string1b)
lim=700
lim1=lim/tam
vy=lim1/2
vx=550
for i in range(1, tam):
f.write(G[i-1]+':\n')
f.write(' graph:\n')
f.write(' x: '+str(vx)+'\n')
f.write(' y: '+str(vy))
f.write(modelTVD)
vy=vy+lim1
a=0
f.write(string2)
for i in range(1, tam):
f.write(' connection '+str(a)+':\n type: link \n '+' nodes: \n - '+G[i-1]+': xe0\n - '+'bridge_net: "0"\n')
a=a+1
f.write(' connection '+str(a)+':\n type : link \n '+' nodes: \n - '+G[i-1]+': xe1\n - '+'bridge_net: "0"\n')
a=a+1
f.write(' connection '+str(a)+':\n type : link \n '+' nodes: \n - PC: eth0\n - '+'bridge_net: "0"\n')
return