blob: 1775797cc94163b98e6f5f139bdaa5228178ddb6 [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)
59 print result
60 elif type_descriptor == 'vnfd':
61 result = client.vnfd_get(descriptor_id)
62
63 print result
64 except Exception as e:
65 log.exception(e)
66 result = {}
67
68 return result
69
lombardofr099364f2018-06-12 11:21:02 +020070 @staticmethod
71 def get_type():
lombardoffb37bca2018-05-03 16:20:04 +020072 return "osm"
73
74 def __str__(self):
75 return self.name
76
lombardofr099364f2018-06-12 11:21:02 +020077 @staticmethod
78 def get_overview_data():
lombardoffb37bca2018-05-03 16:20:04 +020079 client = Client()
80 nsd = client.nsd_list()
81 vnfd = client.vnfd_list()
82 ns = client.ns_list()
lombardofe5476c02018-05-28 18:34:32 +020083 vnf = client.vnf_list()
lombardoffb37bca2018-05-03 16:20:04 +020084 result = {
lombardofr099364f2018-06-12 11:21:02 +020085 'owner': '-',
86 'name': '-',
87 'updated_date': '-',
88 'info': '-',
lombardoffb37bca2018-05-03 16:20:04 +020089 'type': 'osm',
90 'nsd': len(nsd) if nsd else 0,
91 'vnfd': len(vnfd) if vnfd else 0,
92 'ns': len(ns) if ns else 0,
lombardofe5476c02018-05-28 18:34:32 +020093 'vnf': len(vnf) if vnf else 0,
lombardoffb37bca2018-05-03 16:20:04 +020094 }
95
96 return result
97
lombardofr099364f2018-06-12 11:21:02 +020098 @staticmethod
99 def create_descriptor(descriptor_name, type_descriptor, new_data, data_type, file_uploaded):
lombardoffb37bca2018-05-03 16:20:04 +0200100 """Creates a descriptor of a given type from a json or yaml representation
101
102 Returns the descriptor id or False
103 """
104 log.debug('Create descriptor')
105
106 try:
107 client = Client()
108 if type_descriptor == 'nsd':
109 result = client.nsd_onboard(file_uploaded)
110 elif type_descriptor == 'vnfd':
111 result = client.vnfd_onboard(file_uploaded)
112
113 else:
114 log.debug('Create descriptor: Unknown data type')
115 return False
116
117 except Exception as e:
118 log.exception(e)
119 result = False
120 return result
121
lombardofr099364f2018-06-12 11:21:02 +0200122 @staticmethod
123 def delete_descriptor(type_descriptor, descriptor_id):
lombardoffb37bca2018-05-03 16:20:04 +0200124 log.debug('Delete descriptor')
125 try:
126 client = Client()
127 if type_descriptor == 'nsd':
128 result = client.nsd_delete(descriptor_id)
129 elif type_descriptor == 'vnfd':
130 result = client.vnfd_delete(descriptor_id)
131
132 else:
133 log.debug('Create descriptor: Unknown data type')
134 return False
135
136 except Exception as e:
137 log.exception(e)
138 result = False
139 return result
140
lombardofr099364f2018-06-12 11:21:02 +0200141 @staticmethod
142 def edit_descriptor(type_descriptor, descriptor_id, new_data, data_type):
lombardoffb37bca2018-05-03 16:20:04 +0200143 log.debug("Edit descriptor")
144 try:
145 client = Client()
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)
158
159 else:
160 log.debug('Create descriptor: Unknown data type')
161 return False
162 except Exception as e:
163 log.exception(e)
164 result = False
165 return result
166
lombardofr099364f2018-06-12 11:21:02 +0200167 @staticmethod
168 def get_package_files_list(type_descriptor, descriptor_id):
lombardoffb37bca2018-05-03 16:20:04 +0200169 try:
170 client = Client()
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)
175 else:
176 return False
177 result = yaml.load(result)
178 print result
179 except Exception as e:
180 log.exception(e)
181 result = False
182 print result
183 return result
184
lombardoffb37bca2018-05-03 16:20:04 +0200185 def get_add_element(self, request):
186 result = False
187
188 return result
189
190 def get_remove_element(self, request):
191 result = False
192
193 return result
194
195 def get_add_link(self, request):
196
197 result = False
198
199 return result
200
201 def get_remove_link(self, request):
202 result = False
203
204 return result
205
lombardofr099364f2018-06-12 11:21:02 +0200206 @staticmethod
207 def create_ns(descriptor_type, descriptor_id, data_ns):
lombardoffb37bca2018-05-03 16:20:04 +0200208 try:
209 client = Client()
210 if descriptor_type == 'nsd':
211 result = client.ns_create( data_ns)
212 else:
213 return False
214
215 except Exception as e:
216 log.exception(e)
217 result = False
218 print result
219 return result
220
lombardofr099364f2018-06-12 11:21:02 +0200221 @staticmethod
222 def download_pkg(descriptor_id, descriptor_type):
lombardoffb37bca2018-05-03 16:20:04 +0200223 try:
224 client = Client()
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)
229 else:
230 return False
231
232 except Exception as e:
233 log.exception(e)
234 result = False
235 print result
lombardofr099364f2018-06-12 11:21:02 +0200236 return result