fix missing license headers
[osm/LW-UI.git] / projecthandler / osm_model.py
1 #
2 # Copyright 2018 EveryUP Srl
3 #
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
17 from __future__ import unicode_literals
18
19 import json
20 import os.path
21 import yaml
22 from lib.util import Util
23 import logging
24
25 from lib.osm.osmclient.client import Client
26
27 logging.basicConfig(level=logging.DEBUG)
28 log = logging.getLogger('OsmModel.py')
29
30
31 class OsmProject(object):
32 """Osm Project class
33 """
34
35 @staticmethod
36 def get_descriptors( type_descriptor):
37 """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
52 @staticmethod
53 def get_descriptor( descriptor_id, type_descriptor):
54 """Returns a specific descriptor"""
55 try:
56 client = Client()
57 if type_descriptor == 'nsd':
58 result = client.nsd_get(descriptor_id)
59
60 elif type_descriptor == 'vnfd':
61 result = client.vnfd_get(descriptor_id)
62
63 except Exception as e:
64 log.exception(e)
65 result = {}
66
67 return result
68
69 @staticmethod
70 def get_type():
71 return "osm"
72
73 def __str__(self):
74 return self.name
75
76 @staticmethod
77 def get_overview_data():
78 client = Client()
79 nsd = client.nsd_list()
80 vnfd = client.vnfd_list()
81 ns = client.ns_list()
82 vnf = client.vnf_list()
83 result = {
84 'owner': '-',
85 'name': '-',
86 'updated_date': '-',
87 'info': '-',
88 '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,
92 'vnf': len(vnf) if vnf else 0,
93 }
94
95 return result
96
97 @staticmethod
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
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
121 @staticmethod
122 def delete_descriptor(type_descriptor, descriptor_id):
123 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
140 @staticmethod
141 def edit_descriptor(type_descriptor, descriptor_id, new_data, data_type):
142 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
166 @staticmethod
167 def get_package_files_list(type_descriptor, descriptor_id):
168 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)
177
178 except Exception as e:
179 log.exception(e)
180 result = False
181
182 return result
183
184 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
205 @staticmethod
206 def create_ns(descriptor_type, descriptor_id, data_ns):
207 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
217 return result
218
219 @staticmethod
220 def download_pkg(descriptor_id, descriptor_type):
221 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
233 return result