-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresources.py
350 lines (312 loc) · 13.1 KB
/
resources.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# @author: Aymen Frikha, netvirt
# Ressources.py
# 23.07.2013
# # IF IN NEUTRON
from neutron.openstack.common import log as logging
LOG = logging.getLogger(__name__)
# Else
# from neutron.plugins.map.fake_environment import _
# from neutron.plugins.map.fake_environment import log
#
# LOG = log()
import re
import os
from neutron.plugins.map.DB_Structures import GenericService, Rule, Device, \
ServiceInstance, OSreference
from config import cfg
from neutron.plugins.map.map_utils import NoDeviceReprException, \
NoOSRefReprException, NoRuleReprException, NoServiceInstanceReprException, \
NoServiceReprException, TopologyLoadingException
class ResourceManager(object):
def __init__(self):
self.network_path = cfg.CONF.map.network_path
self.service_path = cfg.CONF.map.service_path
self.instance_service_path = cfg.CONF.map.network_service_path
self.rules_path = cfg.CONF.map.service_rules_path
self.os_ref_path = cfg.CONF.map.os_references
self.scenario_file = cfg.CONF.map.scenar_file
self._update_files()
def _update_files(self):
self.network_files = [file for file in os.listdir(self.network_path) if
not file.endswith('~')]
self.service_files = [file for file in os.listdir(self.service_path) if
not file.endswith('~')]
self.instance_service_files = [file for file in
os.listdir(self.instance_service_path) if
not file.endswith('~')]
self.rule_files = [file for file in os.listdir(self.rules_path) if
not file.endswith('~')]
self.os_ref_files = [file for file in os.listdir(self.os_ref_path) if
not file.endswith('~')]
''' Manage device configurations:
' get_all_device_config(): List[device_config]
' get_device_config(device_name): device_config
' set_device_config(device_config): None
' delete_device_config(device_name): None
'''
def get_device_config(self, device_name):
try:
xml = open(os.path.join(self.network_path, device_name + '.xml')).read()
device = Device.CreateFromDocument(xml)
return device
except IOError as e:
print(e)
LOG.error(_("Problem during the loading of '%s'"),
device_name + '.xml')
raise NoDeviceReprException(device_name)
def get_all_device_config(self):
"""
:param self:
:return: devices: List[device_config]
"""
self._update_files()
devices = []
for device_name_xml in self.network_files:
devices.append(
self.get_device_config(device_name_xml.split('.')[0]))
return devices
def device_known(self, device_name):
self._update_files()
return device_name + '.xml' in self.network_files
def set_device_config(self, device):
cli_file = self.network_path + device.name + '.xml'
try:
file_writer = open(cli_file, 'w') # new StreamWriter
if file_writer is None:
LOG.error("Cannot open file %s for writing!", cli_file)
return
print(device.toDOM().toprettyxml())
file_writer.write(device.toDOM().toprettyxml())
file_writer.close()
except IOError as ioe:
LOG.error(("Unable to write device %s configuration in file %s"),
device.name, cli_file)
LOG.error(("%r"), ioe)
def delete_device_config(self, device_name):
self._update_files()
try:
os.remove(self.network_path + device_name + '.xml')
except OSError as ose:
LOG.error("Unable to remove device %s configuration",
device_name)
LOG.error("%r", ose)
''' Manage generic services:\n'
' get_all_gen_services(): List[gen_service]\n'
' get_gen_service(gen_service_name): gen_service\n'
' set_gen_service(gen_service): None\n '
' delete_gen_service(gen_service_name)
'''
def get_gen_service(self, gen_service_name):
try:
xml = open(os.path.join(self.service_path, gen_service_name + '.xml')).read()
service = GenericService.CreateFromDocument(xml)
return service
except IOError as e:
print(e)
LOG.error(_("Problem during the loading of '%s'"),
gen_service_name + '.xml')
raise NoServiceReprException(gen_service_name)
def get_all_gen_services(self):
self._update_files()
services = []
for service in self.service_files:
generic_service = self.get_gen_service(service.split('.')[0])
services.append(generic_service.name)
return services
def set_gen_service(self, gen_service):
cli_file = self.service_path + gen_service.name + '.xml'
try:
file_writer = open(cli_file, 'w') # new StreamWriter
if file_writer is None:
LOG.error("Cannot open file %s for writing!", cli_file)
return
file_writer.write(gen_service.toDOM().toprettyxml())
file_writer.close()
except IOError as ioe:
LOG.error("Unable to write service %s configuration in file %s",
gen_service.name, cli_file)
LOG.error("%r", ioe)
def delete_gen_service(self, gen_service_name):
self._update_files()
try:
os.remove(self.service_path + gen_service_name + '.xml')
except OSError as ose:
LOG.error("Unable to remove service %s configuration",
gen_service_name)
LOG.error("%r", ose)
def gen_service_known(self, service_name):
self._update_files()
return service_name + '.xml' in self.service_files
''' Manage instance services:\n'
' get_all_inst_services(Type): List[inst_service]\n'
' get_all_inst_services(): Dict{Type:List[inst_service]}\n'
' get_inst_service(inst_service_name): inst_service\n '
' set_inst_service(inst_service): None
' delete_inst_service(inst_service_name)
'''
def get_inst_service(self, inst_service_name):
try:
xml = open(
self.instance_service_path + inst_service_name + '.xml').read()
inst_service = ServiceInstance.CreateFromDocument(xml)
return inst_service
except IOError as e:
print(e)
LOG.error(_("Problem during the loading of '%s'"),
inst_service_name + '.xml')
raise NoServiceInstanceReprException(inst_service_name)
def get_all_inst_services(self, instance_of=None):
self._update_files()
inst_services = {}
for inst_service_name in self.instance_service_files:
instance_service = self.get_inst_service(
inst_service_name.split('.')[0])
service_ref = instance_service.InstanceOf.href
if service_ref in inst_services:
inst_services[service_ref].append(instance_service)
else:
inst_services[service_ref] = []
inst_services[service_ref].append(instance_service)
if instance_of is not None:
return inst_services[instance_of]
return inst_services
def set_inst_service(self, inst_service):
cli_file = self.instance_service_path + inst_service.name + '.xml'
try:
file_writer = open(cli_file, 'w') # new StreamWriter
if file_writer is None:
LOG.error("Cannot open file %s for writing!", cli_file)
return
file_writer.write(inst_service.toDOM().toprettyxml())
file_writer.close()
except IOError as ioe:
LOG.error(
"Unable to write instance service %s configuration in file %s",
inst_service.name, cli_file)
LOG.error("%r", ioe)
def delete_inst_service(self, inst_service_name):
self._update_files()
try:
os.remove(self.instance_service_path + inst_service_name + '.xml')
except OSError as ose:
LOG.error("Unable to remove instance service %s configuration",
inst_service_name)
LOG.error("%r", ose)
def inst_service_known(self, inst_service_name):
self._update_files()
return inst_service_name + '.xml' in self.instance_service_files
''' Manage services rules:\n'
' get_all_rules(Type): List[rules]
' get_all_rules(): Dict{Type: List[rules]}
' get_rule(rule_name): rule
' set_rule(rule): None
' delete_rule(rule_name): None
'''
def get_rule(self, rule_name):
try:
xml = open(os.path.join(self.rules_path, rule_name + '.xml')).read()
inst_service = Rule.CreateFromDocument(xml)
return inst_service
except IOError as e:
print(e)
LOG.error(_("Problem during the loading of '%s'"),
rule_name + '.xml')
raise NoRuleReprException(rule_name)
def get_all_rules(self, service_type=None):
# TODO implement extraction with service_type
self._update_files()
rules = []
for rule_name in self.rule_files:
rules.append(self.get_rule(rule_name.split('.')[0]))
return rules
def set_rule(self, rule):
cli_file = self.rules_path + rule.name + '.xml'
try:
file_writer = open(cli_file, 'w') # new StreamWriter
if file_writer is None:
LOG.error("Cannot open file %s for writing!", cli_file)
return
file_writer.write(rule.toDOM().toprettyxml())
file_writer.close()
except IOError as ioe:
LOG.error("Unable to write rule %s configuration in file %s",
rule.name, cli_file)
LOG.error("%r", ioe)
def rule_known(self, rule_name):
self._update_files()
return rule_name + '.xml' in self.rule_files
def delete_rule(self, rule_name):
self._update_files()
try:
os.remove(self.rules_path + rule_name + '.xml')
except OSError as ose:
LOG.error("Unable to remove rule %s configuration",
rule_name)
LOG.error("%r", ose)
''' Manage OS references:\n'
' get_conf_references(type): conf_references
' get_all_conf_references(): Dict{Type: conf_references}
' set_conf_references(conf_references): None
'''
def get_conf_references(self, type):
try:
xml = open(os.path.join(self.os_ref_path, type + '.xml')).read()
os_ref = OSreference.CreateFromDocument(xml)
return os_ref
except IOError as e:
print(e)
LOG.error(_("Problem during the loading of '%s'"), type + '.xml')
raise NoOSRefReprException(type)
def get_all_conf_references(self):
self._update_files()
os_references = {}
for ref_name in self.os_ref_files:
os_references[ref_name] = self.get_conf_references(
ref_name.split('.')[0])
return os_references
def set_conf_references(self, os_ref):
cli_file = self.os_ref_path + os_ref.name + '.xml'
try:
file_writer = open(cli_file, 'w') # new StreamWriter
if file_writer is None:
LOG.error("Cannot open file %s for writing!", cli_file)
return
file_writer.write(os_ref.toDOM().toprettyxml())
file_writer.close()
except IOError as ioe:
LOG.error("Unable to write references %s configuration in file %s",
os_ref.name, cli_file)
LOG.error("%r", ioe)
def conf_reference_known(self, type):
self._update_files()
return type + '.xml' in self.os_ref_files
def delete_conf_reference(self, type):
self._update_files()
try:
os.remove(self.os_ref_path + type + '.xml')
except OSError as ose:
LOG.error(
"Unable to remove configuration reference %s configuration",
type)
LOG.error("%r", ose)
''' Extract physical infrastructure:
' get_infrastructure_matrix(): List[List[String]]
'''
def get_infrastructure_matrix(self):
LOG.info(_('loading: %s'), self.scenario_file)
matrix = []
try:
topo_db = open(self.scenario_file)
except:
LOG.error(_("Problem during the loading from database %r"),
self.scenario_file)
raise TopologyLoadingException(self.scenario_file)
for line in topo_db:
matrix.append(line.replace(' ', '').replace('\n', '').split(';'))
LOG.info(_('Infrastructure matrix has been successfully loaded : %s'),
matrix)
topo_db.close()
return matrix