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
)
63 except Exception as e
:
77 def get_overview_data():
79 nsd
= client
.nsd_list()
80 vnfd
= client
.vnfd_list()
82 vnf
= client
.vnf_list()
89 'nsd': len(nsd
) if nsd
else 0,
90 'vnfd': len(vnfd
) if vnfd
else 0,
91 'ns': len(ns
) if ns
else 0,
92 'vnf': len(vnf
) if vnf
else 0,
98 def create_descriptor(descriptor_name
, type_descriptor
, new_data
, data_type
, file_uploaded
):
99 """Creates a descriptor of a given type from a json or yaml representation
101 Returns the descriptor id or False
103 log
.debug('Create descriptor')
107 if type_descriptor
== 'nsd':
108 result
= client
.nsd_onboard(file_uploaded
)
109 elif type_descriptor
== 'vnfd':
110 result
= client
.vnfd_onboard(file_uploaded
)
113 log
.debug('Create descriptor: Unknown data type')
116 except Exception as e
:
122 def delete_descriptor(type_descriptor
, descriptor_id
):
123 log
.debug('Delete descriptor')
126 if type_descriptor
== 'nsd':
127 result
= client
.nsd_delete(descriptor_id
)
128 elif type_descriptor
== 'vnfd':
129 result
= client
.vnfd_delete(descriptor_id
)
132 log
.debug('Create descriptor: Unknown data type')
135 except Exception as e
:
141 def edit_descriptor(type_descriptor
, descriptor_id
, new_data
, data_type
):
142 log
.debug("Edit descriptor")
145 if type_descriptor
== 'nsd':
146 if data_type
== 'yaml':
147 new_data
= yaml
.load(new_data
)
148 elif data_type
== 'json':
149 new_data
= json
.loads(new_data
)
150 result
= client
.nsd_update(descriptor_id
, new_data
)
151 elif type_descriptor
== 'vnfd':
152 if data_type
== 'yaml':
153 new_data
= yaml
.load(new_data
)
154 elif data_type
== 'json':
155 new_data
= json
.loads(new_data
)
156 result
= client
.vnfd_update(descriptor_id
, new_data
)
159 log
.debug('Create descriptor: Unknown data type')
161 except Exception as e
:
167 def get_package_files_list(type_descriptor
, descriptor_id
):
170 if type_descriptor
== 'nsd':
171 result
= client
.nsd_artifacts(descriptor_id
)
172 elif type_descriptor
== 'vnfd':
173 result
= client
.vnf_packages_artifacts(descriptor_id
)
176 result
= yaml
.load(result
)
178 except Exception as e
:
184 def get_add_element(self
, request
):
189 def get_remove_element(self
, request
):
194 def get_add_link(self
, request
):
200 def get_remove_link(self
, request
):
206 def create_ns(descriptor_type
, descriptor_id
, data_ns
):
209 if descriptor_type
== 'nsd':
210 result
= client
.ns_create( data_ns
)
214 except Exception as e
:
220 def download_pkg(descriptor_id
, descriptor_type
):
223 if descriptor_type
== 'nsd':
224 result
= client
.get_nsd_pkg(descriptor_id
)
225 elif descriptor_type
== 'vnfd':
226 result
= client
.get_vnfd_pkg(descriptor_id
)
230 except Exception as e
: