2 # Copyright 2018 EveryUP Srl
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
22 from lib
.util
import Util
25 from lib
.osm
.osmclient
.client
import Client
27 logging
.basicConfig(level
=logging
.DEBUG
)
28 log
= logging
.getLogger('OsmModel.py')
31 class OsmProject(object):
36 def get_descriptors( type_descriptor
):
37 """Returns all descriptors of a given type"""
38 log
.debug("Get %s descriptors", type_descriptor
)
41 if type_descriptor
== 'nsd':
42 result
= client
.nsd_list()
44 elif type_descriptor
== 'vnfd':
45 result
= client
.vnfd_list()
47 except Exception as e
:
53 def get_descriptor( descriptor_id
, type_descriptor
):
54 """Returns a specific descriptor"""
57 if type_descriptor
== 'nsd':
58 result
= client
.nsd_get(descriptor_id
)
60 elif type_descriptor
== 'vnfd':
61 result
= client
.vnfd_get(descriptor_id
)
64 except Exception as e
:
78 def get_overview_data():
80 nsd
= client
.nsd_list()
81 vnfd
= client
.vnfd_list()
83 vnf
= client
.vnf_list()
90 'nsd': len(nsd
) if nsd
else 0,
91 'vnfd': len(vnfd
) if vnfd
else 0,
92 'ns': len(ns
) if ns
else 0,
93 'vnf': len(vnf
) if vnf
else 0,
99 def create_descriptor(descriptor_name
, type_descriptor
, new_data
, data_type
, file_uploaded
):
100 """Creates a descriptor of a given type from a json or yaml representation
102 Returns the descriptor id or False
104 log
.debug('Create descriptor')
108 if type_descriptor
== 'nsd':
109 result
= client
.nsd_onboard(file_uploaded
)
110 elif type_descriptor
== 'vnfd':
111 result
= client
.vnfd_onboard(file_uploaded
)
114 log
.debug('Create descriptor: Unknown data type')
117 except Exception as e
:
123 def delete_descriptor(type_descriptor
, descriptor_id
):
124 log
.debug('Delete descriptor')
127 if type_descriptor
== 'nsd':
128 result
= client
.nsd_delete(descriptor_id
)
129 elif type_descriptor
== 'vnfd':
130 result
= client
.vnfd_delete(descriptor_id
)
133 log
.debug('Create descriptor: Unknown data type')
136 except Exception as e
:
142 def edit_descriptor(type_descriptor
, descriptor_id
, new_data
, data_type
):
143 log
.debug("Edit descriptor")
146 if type_descriptor
== 'nsd':
147 if data_type
== 'yaml':
148 new_data
= yaml
.load(new_data
)
149 elif data_type
== 'json':
150 new_data
= json
.loads(new_data
)
151 result
= client
.nsd_update(descriptor_id
, new_data
)
152 elif type_descriptor
== 'vnfd':
153 if data_type
== 'yaml':
154 new_data
= yaml
.load(new_data
)
155 elif data_type
== 'json':
156 new_data
= json
.loads(new_data
)
157 result
= client
.vnfd_update(descriptor_id
, new_data
)
160 log
.debug('Create descriptor: Unknown data type')
162 except Exception as e
:
168 def get_package_files_list(type_descriptor
, descriptor_id
):
171 if type_descriptor
== 'nsd':
172 result
= client
.nsd_artifacts(descriptor_id
)
173 elif type_descriptor
== 'vnfd':
174 result
= client
.vnf_packages_artifacts(descriptor_id
)
177 result
= yaml
.load(result
)
179 except Exception as e
:
185 def get_add_element(self
, request
):
190 def get_remove_element(self
, request
):
195 def get_add_link(self
, request
):
201 def get_remove_link(self
, request
):
207 def create_ns(descriptor_type
, descriptor_id
, data_ns
):
210 if descriptor_type
== 'nsd':
211 result
= client
.ns_create( data_ns
)
215 except Exception as e
:
222 def download_pkg(descriptor_id
, descriptor_type
):
225 if descriptor_type
== 'nsd':
226 result
= client
.get_nsd_pkg(descriptor_id
)
227 elif descriptor_type
== 'vnfd':
228 result
= client
.get_vnfd_pkg(descriptor_id
)
232 except Exception as e
: