| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 1 | # |
| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 2 | # Copyright 2018 EveryUP Srl |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 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 | |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 19 | import json |
| 20 | import os.path |
| 21 | import yaml |
| 22 | from lib.util import Util |
| 23 | import logging |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 24 | |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 25 | from lib.osm.osmclient.client import Client |
| 26 | |
| 27 | logging.basicConfig(level=logging.DEBUG) |
| 28 | log = logging.getLogger('OsmModel.py') |
| 29 | |
| 30 | |
| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 31 | class OsmProject(object): |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 32 | """Osm Project class |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 33 | """ |
| 34 | |
| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 35 | @staticmethod |
| 36 | def get_descriptors( type_descriptor): |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 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 | |
| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 52 | @staticmethod |
| 53 | def get_descriptor( descriptor_id, type_descriptor): |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 54 | """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 | |
| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 70 | @staticmethod |
| 71 | def get_type(): |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 72 | return "osm" |
| 73 | |
| 74 | def __str__(self): |
| 75 | return self.name |
| 76 | |
| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 77 | @staticmethod |
| 78 | def get_overview_data(): |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 79 | client = Client() |
| 80 | nsd = client.nsd_list() |
| 81 | vnfd = client.vnfd_list() |
| 82 | ns = client.ns_list() |
| lombardof | e5476c0 | 2018-05-28 18:34:32 +0200 | [diff] [blame] | 83 | vnf = client.vnf_list() |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 84 | result = { |
| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 85 | 'owner': '-', |
| 86 | 'name': '-', |
| 87 | 'updated_date': '-', |
| 88 | 'info': '-', |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 89 | '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, |
| lombardof | e5476c0 | 2018-05-28 18:34:32 +0200 | [diff] [blame] | 93 | 'vnf': len(vnf) if vnf else 0, |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | return result |
| 97 | |
| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 98 | @staticmethod |
| 99 | def create_descriptor(descriptor_name, type_descriptor, new_data, data_type, file_uploaded): |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 100 | """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 | |
| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 122 | @staticmethod |
| 123 | def delete_descriptor(type_descriptor, descriptor_id): |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 124 | 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 | |
| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 141 | @staticmethod |
| 142 | def edit_descriptor(type_descriptor, descriptor_id, new_data, data_type): |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 143 | 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 | |
| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 167 | @staticmethod |
| 168 | def get_package_files_list(type_descriptor, descriptor_id): |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 169 | 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 | |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 185 | 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 | |
| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 206 | @staticmethod |
| 207 | def create_ns(descriptor_type, descriptor_id, data_ns): |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 208 | 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 | |
| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 221 | @staticmethod |
| 222 | def download_pkg(descriptor_id, descriptor_type): |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 223 | 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 |
| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 236 | return result |