2 # Copyright 2017 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 from __future__
import unicode_literals
23 from lib
.util
import Util
25 from projecthandler
.models
import ProjectStateless
27 from lib
.osm
.osm_parser
import OsmParser
28 from lib
.osm
.osm_rdcl_graph
import OsmRdclGraph
29 from lib
.osm
.osmclient
.client
import Client
31 logging
.basicConfig(level
=logging
.DEBUG
)
32 log
= logging
.getLogger('OsmModel.py')
35 PATH_TO_SCHEMAS
= 'lib/osm/schemas/'
36 PATH_TO_DESCRIPTORS_TEMPLATES
= 'lib/osm/descriptor_template'
37 DESCRIPTOR_TEMPLATE_SUFFIX
= '.json'
38 GRAPH_MODEL_FULL_NAME
= 'lib/TopologyModels/osm/osm.yaml'
39 EXAMPLES_FOLDER
= 'usecases/OSM/'
42 class OsmProject(ProjectStateless
):
44 The data model has the following descriptors:
45 # descrtiptor list in comment #
49 def get_descriptors(self
, type_descriptor
):
50 """Returns all descriptors of a given type"""
51 log
.debug("Get %s descriptors", type_descriptor
)
54 if type_descriptor
== 'nsd':
55 result
= client
.nsd_list()
57 elif type_descriptor
== 'vnfd':
58 result
= client
.vnfd_list()
60 except Exception as e
:
65 def get_descriptor(self
, descriptor_id
, type_descriptor
):
66 """Returns a specific descriptor"""
69 if type_descriptor
== 'nsd':
70 result
= client
.nsd_get(descriptor_id
)
72 elif type_descriptor
== 'vnfd':
73 result
= client
.vnfd_get(descriptor_id
)
76 except Exception as e
:
83 def data_project_from_files(cls
, request
):
86 for my_key
in request
.FILES
.keys():
87 file_dict
[my_key
] = request
.FILES
.getlist(my_key
)
91 data_project
= OsmParser
.importprojectfiles(file_dict
)
96 def data_project_from_example(cls
, request
):
97 osm_id
= request
.POST
.get('example-osm-id', '')
98 data_project
= OsmParser
.importprojectdir(EXAMPLES_FOLDER
+ osm_id
+ '/JSON', 'yaml')
102 def get_example_list(cls
):
103 """Returns a list of directories, in each directory there is a project osm"""
105 path
= EXAMPLES_FOLDER
106 dirs
= [d
for d
in os
.listdir(path
) if os
.path
.isdir(os
.path
.join(path
, d
))]
110 def get_new_descriptor(cls
, descriptor_type
, request_id
):
112 json_template
= cls
.get_descriptor_template(descriptor_type
)
117 def get_descriptor_template(cls
, type_descriptor
):
118 """Returns a descriptor template for a given descriptor type"""
121 schema
= Util
.loadjsonfile(os
.path
.join(PATH_TO_DESCRIPTORS_TEMPLATES
, type_descriptor
+ DESCRIPTOR_TEMPLATE_SUFFIX
))
123 except Exception as e
:
128 def get_clone_descriptor(cls
, descriptor
, type_descriptor
, new_descriptor_id
):
129 new_descriptor
= copy
.deepcopy(descriptor
)
131 return new_descriptor
139 def get_overview_data(self
):
140 current_data
= json
.loads(self
.data_project
)
142 nsd
= client
.nsd_list()
143 vnfd
= client
.vnfd_list()
144 ns
= client
.ns_list()
146 'owner': self
.owner
.__str
__(),
148 'updated_date': self
.updated_date
.strftime('%Y-%m-%d %H:%M'),
151 'nsd': len(nsd
) if nsd
else 0,
152 'vnfd': len(vnfd
) if vnfd
else 0,
153 'ns': len(ns
) if ns
else 0,
154 'validated': self
.validated
159 def get_graph_data_json_topology(self
, descriptor_id
):
160 rdcl_graph
= OsmRdclGraph()
161 project
= self
.get_dataproject()
162 topology
= rdcl_graph
.build_graph_from_project(project
,
163 model
=self
.get_graph_model(GRAPH_MODEL_FULL_NAME
))
164 return json
.dumps(topology
)
166 def create_descriptor(self
, descriptor_name
, type_descriptor
, new_data
, data_type
, file_uploaded
):
167 """Creates a descriptor of a given type from a json or yaml representation
169 Returns the descriptor id or False
171 log
.debug('Create descriptor')
175 if type_descriptor
== 'nsd':
176 result
= client
.nsd_onboard(file_uploaded
)
177 elif type_descriptor
== 'vnfd':
178 result
= client
.vnfd_onboard(file_uploaded
)
181 log
.debug('Create descriptor: Unknown data type')
184 except Exception as e
:
189 def delete_descriptor(self
, type_descriptor
, descriptor_id
):
190 log
.debug('Delete descriptor')
193 if type_descriptor
== 'nsd':
194 result
= client
.nsd_delete(descriptor_id
)
195 elif type_descriptor
== 'vnfd':
196 result
= client
.vnfd_delete(descriptor_id
)
199 log
.debug('Create descriptor: Unknown data type')
202 except Exception as e
:
207 def edit_descriptor(self
, type_descriptor
, descriptor_id
, new_data
, data_type
):
208 log
.debug("Edit descriptor")
211 if type_descriptor
== 'nsd':
212 if data_type
== 'yaml':
213 new_data
= yaml
.load(new_data
)
214 elif data_type
== 'json':
215 new_data
= json
.loads(new_data
)
216 result
= client
.nsd_update(descriptor_id
, new_data
)
217 elif type_descriptor
== 'vnfd':
218 if data_type
== 'yaml':
219 new_data
= yaml
.load(new_data
)
220 elif data_type
== 'json':
221 new_data
= json
.loads(new_data
)
222 result
= client
.vnfd_update(descriptor_id
, new_data
)
225 log
.debug('Create descriptor: Unknown data type')
227 except Exception as e
:
232 def get_package_files_list(self
, type_descriptor
, descriptor_id
):
235 if type_descriptor
== 'nsd':
236 result
= client
.nsd_artifacts(descriptor_id
)
237 elif type_descriptor
== 'vnfd':
238 result
= client
.vnf_packages_artifacts(descriptor_id
)
241 result
= yaml
.load(result
)
243 except Exception as e
:
249 def set_validated(self
, value
):
250 self
.validated
= True if value
is not None and value
== True else False
252 def get_add_element(self
, request
):
257 def get_remove_element(self
, request
):
262 def get_add_link(self
, request
):
268 def get_remove_link(self
, request
):
273 def create_ns(self
, descriptor_type
, descriptor_id
, data_ns
):
276 if descriptor_type
== 'nsd':
277 result
= client
.ns_create( data_ns
)
281 except Exception as e
:
287 def download_pkg(self
, project
, descriptor_id
, descriptor_type
):
290 if descriptor_type
== 'nsd':
291 result
= client
.get_nsd_pkg(descriptor_id
)
292 elif descriptor_type
== 'vnfd':
293 result
= client
.get_vnfd_pkg(descriptor_id
)
297 except Exception as e
:
303 def get_available_nodes(self
, args
):
304 """Returns all available node """
305 log
.debug('get_available_nodes')
308 #current_data = json.loads(self.data_project)
309 model_graph
= self
.get_graph_model(GRAPH_MODEL_FULL_NAME
)
310 for node
in model_graph
['layer'][args
['layer']]['nodes']:
314 "category_name": model_graph
['nodes'][node
]['label'],
322 result
.append(current_data
)
324 #result = current_data[type_descriptor][descriptor_id]
325 except Exception as e
: