blob: 718c08d79d08295030a8e3ebfe3068abb0e27746 [file] [log] [blame]
lombardoffb37bca2018-05-03 16:20:04 +02001#
lombardofr099364f2018-06-12 11:21:02 +02002# Copyright 2018 EveryUP Srl
lombardoffb37bca2018-05-03 16:20:04 +02003#
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
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
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.
15#
16
17from __future__ import unicode_literals
18
lombardoffb37bca2018-05-03 16:20:04 +020019import json
20import os.path
21import yaml
22from lib.util import Util
23import logging
lombardoffb37bca2018-05-03 16:20:04 +020024
lombardoffb37bca2018-05-03 16:20:04 +020025from lib.osm.osmclient.client import Client
26
27logging.basicConfig(level=logging.DEBUG)
28log = logging.getLogger('OsmModel.py')
29
30
lombardofr099364f2018-06-12 11:21:02 +020031class OsmProject(object):
lombardoffb37bca2018-05-03 16:20:04 +020032 """Osm Project class
lombardoffb37bca2018-05-03 16:20:04 +020033 """
34
lombardofr099364f2018-06-12 11:21:02 +020035 @staticmethod
36 def get_descriptors( type_descriptor):
lombardoffb37bca2018-05-03 16:20:04 +020037 """Returns all descriptors of a given type"""
38 log.debug("Get %s descriptors", type_descriptor)
39 try:
40 client = Client()
41 if type_descriptor == 'nsd':
42 result = client.nsd_list()
43
44 elif type_descriptor == 'vnfd':
45 result = client.vnfd_list()
46
47 except Exception as e:
48 log.exception(e)
49 result = {}
50 return result
51
lombardofr099364f2018-06-12 11:21:02 +020052 @staticmethod
53 def get_descriptor( descriptor_id, type_descriptor):
lombardoffb37bca2018-05-03 16:20:04 +020054 """Returns a specific descriptor"""
55 try:
56 client = Client()
57 if type_descriptor == 'nsd':
58 result = client.nsd_get(descriptor_id)
lombardofr55d48e72019-06-21 19:35:14 +020059
lombardoffb37bca2018-05-03 16:20:04 +020060 elif type_descriptor == 'vnfd':
61 result = client.vnfd_get(descriptor_id)
62
lombardoffb37bca2018-05-03 16:20:04 +020063 except Exception as e:
64 log.exception(e)
65 result = {}
66
67 return result
68
lombardofr099364f2018-06-12 11:21:02 +020069 @staticmethod
70 def get_type():
lombardoffb37bca2018-05-03 16:20:04 +020071 return "osm"
72
73 def __str__(self):
74 return self.name
75
lombardofr099364f2018-06-12 11:21:02 +020076 @staticmethod
77 def get_overview_data():
lombardoffb37bca2018-05-03 16:20:04 +020078 client = Client()
79 nsd = client.nsd_list()
80 vnfd = client.vnfd_list()
81 ns = client.ns_list()
lombardofe5476c02018-05-28 18:34:32 +020082 vnf = client.vnf_list()
lombardoffb37bca2018-05-03 16:20:04 +020083 result = {
lombardofr099364f2018-06-12 11:21:02 +020084 'owner': '-',
85 'name': '-',
86 'updated_date': '-',
87 'info': '-',
lombardoffb37bca2018-05-03 16:20:04 +020088 'type': 'osm',
89 'nsd': len(nsd) if nsd else 0,
90 'vnfd': len(vnfd) if vnfd else 0,
91 'ns': len(ns) if ns else 0,
lombardofe5476c02018-05-28 18:34:32 +020092 'vnf': len(vnf) if vnf else 0,
lombardoffb37bca2018-05-03 16:20:04 +020093 }
94
95 return result
96
lombardofr099364f2018-06-12 11:21:02 +020097 @staticmethod
98 def create_descriptor(descriptor_name, type_descriptor, new_data, data_type, file_uploaded):
lombardoffb37bca2018-05-03 16:20:04 +020099 """Creates a descriptor of a given type from a json or yaml representation
100
101 Returns the descriptor id or False
102 """
103 log.debug('Create descriptor')
104
105 try:
106 client = Client()
107 if type_descriptor == 'nsd':
108 result = client.nsd_onboard(file_uploaded)
109 elif type_descriptor == 'vnfd':
110 result = client.vnfd_onboard(file_uploaded)
111
112 else:
113 log.debug('Create descriptor: Unknown data type')
114 return False
115
116 except Exception as e:
117 log.exception(e)
118 result = False
119 return result
120
lombardofr099364f2018-06-12 11:21:02 +0200121 @staticmethod
122 def delete_descriptor(type_descriptor, descriptor_id):
lombardoffb37bca2018-05-03 16:20:04 +0200123 log.debug('Delete descriptor')
124 try:
125 client = Client()
126 if type_descriptor == 'nsd':
127 result = client.nsd_delete(descriptor_id)
128 elif type_descriptor == 'vnfd':
129 result = client.vnfd_delete(descriptor_id)
130
131 else:
132 log.debug('Create descriptor: Unknown data type')
133 return False
134
135 except Exception as e:
136 log.exception(e)
137 result = False
138 return result
139
lombardofr099364f2018-06-12 11:21:02 +0200140 @staticmethod
141 def edit_descriptor(type_descriptor, descriptor_id, new_data, data_type):
lombardoffb37bca2018-05-03 16:20:04 +0200142 log.debug("Edit descriptor")
143 try:
144 client = Client()
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)
157
158 else:
159 log.debug('Create descriptor: Unknown data type')
160 return False
161 except Exception as e:
162 log.exception(e)
163 result = False
164 return result
165
lombardofr099364f2018-06-12 11:21:02 +0200166 @staticmethod
167 def get_package_files_list(type_descriptor, descriptor_id):
lombardoffb37bca2018-05-03 16:20:04 +0200168 try:
169 client = Client()
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)
174 else:
175 return False
176 result = yaml.load(result)
lombardofr55d48e72019-06-21 19:35:14 +0200177
lombardoffb37bca2018-05-03 16:20:04 +0200178 except Exception as e:
179 log.exception(e)
180 result = False
lombardofr55d48e72019-06-21 19:35:14 +0200181
lombardoffb37bca2018-05-03 16:20:04 +0200182 return result
183
lombardoffb37bca2018-05-03 16:20:04 +0200184 def get_add_element(self, request):
185 result = False
186
187 return result
188
189 def get_remove_element(self, request):
190 result = False
191
192 return result
193
194 def get_add_link(self, request):
195
196 result = False
197
198 return result
199
200 def get_remove_link(self, request):
201 result = False
202
203 return result
204
lombardofr099364f2018-06-12 11:21:02 +0200205 @staticmethod
206 def create_ns(descriptor_type, descriptor_id, data_ns):
lombardoffb37bca2018-05-03 16:20:04 +0200207 try:
208 client = Client()
209 if descriptor_type == 'nsd':
210 result = client.ns_create( data_ns)
211 else:
212 return False
213
214 except Exception as e:
215 log.exception(e)
216 result = False
lombardoffb37bca2018-05-03 16:20:04 +0200217 return result
218
lombardofr099364f2018-06-12 11:21:02 +0200219 @staticmethod
220 def download_pkg(descriptor_id, descriptor_type):
lombardoffb37bca2018-05-03 16:20:04 +0200221 try:
222 client = Client()
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)
227 else:
228 return False
229
230 except Exception as e:
231 log.exception(e)
232 result = False
lombardofr099364f2018-06-12 11:21:02 +0200233 return result