| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| tierno | d125caf | 2018-11-22 16:05:54 +0000 | [diff] [blame] | 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 "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 | # implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 17 | import cherrypy |
| 18 | import time |
| 19 | import json |
| 20 | import yaml |
| tierno | 9c63011 | 2019-08-29 14:21:41 +0000 | [diff] [blame] | 21 | import osm_nbi.html_out as html |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 22 | import logging |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 23 | import logging.handlers |
| 24 | import getopt |
| 25 | import sys |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 26 | |
| tierno | 9c63011 | 2019-08-29 14:21:41 +0000 | [diff] [blame] | 27 | from osm_nbi.authconn import AuthException, AuthconnException |
| 28 | from osm_nbi.auth import Authenticator |
| 29 | from osm_nbi.engine import Engine, EngineException |
| 30 | from osm_nbi.subscriptions import SubscriptionThread |
| 31 | from osm_nbi.validation import ValidationError |
| tierno | a8d6363 | 2018-05-10 13:12:32 +0200 | [diff] [blame] | 32 | from osm_common.dbbase import DbException |
| 33 | from osm_common.fsbase import FsException |
| 34 | from osm_common.msgbase import MsgException |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 35 | from http import HTTPStatus |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 36 | from codecs import getreader |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 37 | from os import environ, path |
| tierno | b2e48bd | 2020-02-04 15:47:18 +0000 | [diff] [blame] | 38 | from osm_nbi import version as nbi_version, version_date as nbi_version_date |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 39 | |
| 40 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| tierno | dfe0957 | 2018-04-24 10:41:10 +0200 | [diff] [blame] | 41 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 42 | __version__ = "0.1.3" # file version, not NBI version |
| tierno | 9c63011 | 2019-08-29 14:21:41 +0000 | [diff] [blame] | 43 | version_date = "Aug 2019" |
| 44 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 45 | database_version = "1.2" |
| 46 | auth_database_version = "1.0" |
| 47 | nbi_server = None # instance of Server class |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 48 | subscription_thread = None # instance of SubscriptionThread class |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 49 | |
| 50 | """ |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 51 | North Bound Interface (O: OSM specific; 5,X: SOL005 not implemented yet; O5: SOL005 implemented) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 52 | URL: /osm GET POST PUT DELETE PATCH |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 53 | /nsd/v1 |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 54 | /ns_descriptors_content O O |
| 55 | /<nsdInfoId> O O O O |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 56 | /ns_descriptors O5 O5 |
| 57 | /<nsdInfoId> O5 O5 5 |
| 58 | /nsd_content O5 O5 |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 59 | /nsd O |
| 60 | /artifacts[/<artifactPath>] O |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 61 | /pnf_descriptors 5 5 |
| 62 | /<pnfdInfoId> 5 5 5 |
| 63 | /pnfd_content 5 5 |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 64 | /subscriptions 5 5 |
| 65 | /<subscriptionId> 5 X |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 66 | |
| 67 | /vnfpkgm/v1 |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 68 | /vnf_packages_content O O |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 69 | /<vnfPkgId> O O |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 70 | /vnf_packages O5 O5 |
| 71 | /<vnfPkgId> O5 O5 5 |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 72 | /package_content O5 O5 |
| 73 | /upload_from_uri X |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 74 | /vnfd O5 |
| 75 | /artifacts[/<artifactPath>] O5 |
| 76 | /subscriptions X X |
| 77 | /<subscriptionId> X X |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 78 | |
| 79 | /nslcm/v1 |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 80 | /ns_instances_content O O |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 81 | /<nsInstanceId> O O |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 82 | /ns_instances 5 5 |
| tierno | 9569244 | 2018-05-24 18:05:28 +0200 | [diff] [blame] | 83 | /<nsInstanceId> O5 O5 |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 84 | instantiate O5 |
| 85 | terminate O5 |
| 86 | action O |
| 87 | scale O5 |
| elumalai | 8e3806c | 2022-04-28 17:26:24 +0530 | [diff] [blame] | 88 | migrate O |
| aticig | 544a2ae | 2022-04-05 09:00:17 +0300 | [diff] [blame] | 89 | update 05 |
| garciadeblas | 0964edf | 2022-02-11 00:43:44 +0100 | [diff] [blame] | 90 | heal O5 |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 91 | /ns_lcm_op_occs 5 5 |
| 92 | /<nsLcmOpOccId> 5 5 5 |
| 93 | TO BE COMPLETED 5 5 |
| tierno | f759d82 | 2018-06-11 18:54:54 +0200 | [diff] [blame] | 94 | /vnf_instances (also vnfrs for compatibility) O |
| 95 | /<vnfInstanceId> O |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 96 | /subscriptions 5 5 |
| 97 | /<subscriptionId> 5 X |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 98 | |
| tierno | cb83c94 | 2018-09-24 17:28:13 +0200 | [diff] [blame] | 99 | /pdu/v1 |
| tierno | 032916c | 2019-03-22 13:27:12 +0000 | [diff] [blame] | 100 | /pdu_descriptors O O |
| tierno | cb83c94 | 2018-09-24 17:28:13 +0200 | [diff] [blame] | 101 | /<id> O O O O |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 102 | |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 103 | /admin/v1 |
| 104 | /tokens O O |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 105 | /<id> O O |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 106 | /users O O |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 107 | /<id> O O O O |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 108 | /projects O O |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 109 | /<id> O O |
| tierno | 55ba2e6 | 2018-12-11 17:22:22 +0000 | [diff] [blame] | 110 | /vim_accounts (also vims for compatibility) O O |
| 111 | /<id> O O O |
| 112 | /wim_accounts O O |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 113 | /<id> O O O |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 114 | /sdns O O |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 115 | /<id> O O O |
| delacruzramo | fe598fe | 2019-10-23 18:25:11 +0200 | [diff] [blame] | 116 | /k8sclusters O O |
| 117 | /<id> O O O |
| 118 | /k8srepos O O |
| 119 | /<id> O O |
| Felipe Vicens | b66b041 | 2020-05-06 10:11:00 +0200 | [diff] [blame] | 120 | /osmrepos O O |
| 121 | /<id> O O |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 122 | |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 123 | /nst/v1 O O |
| 124 | /netslice_templates_content O O |
| 125 | /<nstInfoId> O O O O |
| 126 | /netslice_templates O O |
| 127 | /<nstInfoId> O O O |
| 128 | /nst_content O O |
| 129 | /nst O |
| 130 | /artifacts[/<artifactPath>] O |
| 131 | /subscriptions X X |
| 132 | /<subscriptionId> X X |
| 133 | |
| 134 | /nsilcm/v1 |
| 135 | /netslice_instances_content O O |
| 136 | /<SliceInstanceId> O O |
| 137 | /netslice_instances O O |
| 138 | /<SliceInstanceId> O O |
| 139 | instantiate O |
| 140 | terminate O |
| 141 | action O |
| 142 | /nsi_lcm_op_occs O O |
| 143 | /<nsiLcmOpOccId> O O O |
| 144 | /subscriptions X X |
| 145 | /<subscriptionId> X X |
| 146 | |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 147 | query string: |
| 148 | Follows SOL005 section 4.3.2 It contains extra METHOD to override http method, FORCE to force. |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 149 | simpleFilterExpr := <attrName>["."<attrName>]*["."<op>]"="<value>[","<value>]* |
| 150 | filterExpr := <simpleFilterExpr>["&"<simpleFilterExpr>]* |
| 151 | op := "eq" | "neq" (or "ne") | "gt" | "lt" | "gte" | "lte" | "cont" | "ncont" |
| 152 | attrName := string |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 153 | For filtering inside array, it must select the element of the array, or add ANYINDEX to apply the filtering over any |
| 154 | item of the array, that is, pass if any item of the array pass the filter. |
| 155 | It allows both ne and neq for not equal |
| 156 | TODO: 4.3.3 Attribute selectors |
| 157 | all_fields, fields=x,y,.., exclude_default, exclude_fields=x,y,... |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 158 | (none) … same as “exclude_default” |
| 159 | all_fields … all attributes. |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 160 | fields=<list> … all attributes except all complex attributes with minimum cardinality of zero that are not |
| 161 | conditionally mandatory, and that are not provided in <list>. |
| 162 | exclude_fields=<list> … all attributes except those complex attributes with a minimum cardinality of zero that |
| 163 | are not conditionally mandatory, and that are provided in <list>. |
| 164 | exclude_default … all attributes except those complex attributes with a minimum cardinality of zero that are not |
| 165 | conditionally mandatory, and that are part of the "default exclude set" defined in the present specification for |
| 166 | the particular resource |
| 167 | exclude_default and include=<list> … all attributes except those complex attributes with a minimum cardinality |
| 168 | of zero that are not conditionally mandatory and that are part of the "default exclude set" defined in the |
| 169 | present specification for the particular resource, but that are not part of <list> |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 170 | Additionally it admits some administrator values: |
| 171 | FORCE: To force operations skipping dependency checkings |
| 172 | ADMIN: To act as an administrator or a different project |
| 173 | PUBLIC: To get public descriptors or set a descriptor as public |
| 174 | SET_PROJECT: To make a descriptor available for other project |
| beierlm | bc5a524 | 2022-05-17 21:25:29 -0400 | [diff] [blame] | 175 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 176 | Header field name Reference Example Descriptions |
| 177 | Accept IETF RFC 7231 [19] application/json Content-Types that are acceptable for the response. |
| 178 | This header field shall be present if the response is expected to have a non-empty message body. |
| 179 | Content-Type IETF RFC 7231 [19] application/json The MIME type of the body of the request. |
| 180 | This header field shall be present if the request has a non-empty message body. |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 181 | Authorization IETF RFC 7235 [22] Bearer mF_9.B5f-4.1JqM The authorization token for the request. |
| 182 | Details are specified in clause 4.5.3. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 183 | Range IETF RFC 7233 [21] 1000-2000 Requested range of bytes from a file |
| 184 | Header field name Reference Example Descriptions |
| 185 | Content-Type IETF RFC 7231 [19] application/json The MIME type of the body of the response. |
| 186 | This header field shall be present if the response has a non-empty message body. |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 187 | Location IETF RFC 7231 [19] http://www.example.com/vnflcm/v1/vnf_instances/123 Used in redirection, or when a |
| 188 | new resource has been created. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 189 | This header field shall be present if the response status code is 201 or 3xx. |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 190 | In the present document this header field is also used if the response status code is 202 and a new resource was |
| 191 | created. |
| 192 | WWW-Authenticate IETF RFC 7235 [22] Bearer realm="example" Challenge if the corresponding HTTP request has not |
| 193 | provided authorization, or error details if the corresponding HTTP request has provided an invalid authorization |
| 194 | token. |
| 195 | Accept-Ranges IETF RFC 7233 [21] bytes Used by the Server to signal whether or not it supports ranges for |
| 196 | certain resources. |
| 197 | Content-Range IETF RFC 7233 [21] bytes 21010-47021/ 47022 Signals the byte range that is contained in the |
| 198 | response, and the total length of the file. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 199 | Retry-After IETF RFC 7231 [19] Fri, 31 Dec 1999 23:59:59 GMT |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 200 | """ |
| 201 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 202 | valid_query_string = ("ADMIN", "SET_PROJECT", "FORCE", "PUBLIC") |
| 203 | # ^ Contains possible administrative query string words: |
| 204 | # ADMIN=True(by default)|Project|Project-list: See all elements, or elements of a project |
| 205 | # (not owned by my session project). |
| 206 | # PUBLIC=True(by default)|False: See/hide public elements. Set/Unset a topic to be public |
| 207 | # FORCE=True(by default)|False: Force edition/deletion operations |
| 208 | # SET_PROJECT=Project|Project-list: Add/Delete the topic to the projects portfolio |
| 209 | |
| 210 | valid_url_methods = { |
| 211 | # contains allowed URL and methods, and the role_permission name |
| 212 | "admin": { |
| 213 | "v1": { |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 214 | "tokens": { |
| 215 | "METHODS": ("GET", "POST", "DELETE"), |
| 216 | "ROLE_PERMISSION": "tokens:", |
| 217 | "<ID>": {"METHODS": ("GET", "DELETE"), "ROLE_PERMISSION": "tokens:id:"}, |
| 218 | }, |
| 219 | "users": { |
| 220 | "METHODS": ("GET", "POST"), |
| 221 | "ROLE_PERMISSION": "users:", |
| 222 | "<ID>": { |
| 223 | "METHODS": ("GET", "DELETE", "PATCH"), |
| 224 | "ROLE_PERMISSION": "users:id:", |
| 225 | }, |
| 226 | }, |
| 227 | "projects": { |
| 228 | "METHODS": ("GET", "POST"), |
| 229 | "ROLE_PERMISSION": "projects:", |
| 230 | "<ID>": { |
| 231 | "METHODS": ("GET", "DELETE", "PATCH"), |
| 232 | "ROLE_PERMISSION": "projects:id:", |
| 233 | }, |
| 234 | }, |
| 235 | "roles": { |
| 236 | "METHODS": ("GET", "POST"), |
| 237 | "ROLE_PERMISSION": "roles:", |
| 238 | "<ID>": { |
| 239 | "METHODS": ("GET", "DELETE", "PATCH"), |
| 240 | "ROLE_PERMISSION": "roles:id:", |
| 241 | }, |
| 242 | }, |
| 243 | "vims": { |
| 244 | "METHODS": ("GET", "POST"), |
| 245 | "ROLE_PERMISSION": "vims:", |
| 246 | "<ID>": { |
| 247 | "METHODS": ("GET", "DELETE", "PATCH"), |
| 248 | "ROLE_PERMISSION": "vims:id:", |
| 249 | }, |
| 250 | }, |
| 251 | "vim_accounts": { |
| 252 | "METHODS": ("GET", "POST"), |
| 253 | "ROLE_PERMISSION": "vim_accounts:", |
| 254 | "<ID>": { |
| 255 | "METHODS": ("GET", "DELETE", "PATCH"), |
| 256 | "ROLE_PERMISSION": "vim_accounts:id:", |
| 257 | }, |
| 258 | }, |
| 259 | "wim_accounts": { |
| 260 | "METHODS": ("GET", "POST"), |
| 261 | "ROLE_PERMISSION": "wim_accounts:", |
| 262 | "<ID>": { |
| 263 | "METHODS": ("GET", "DELETE", "PATCH"), |
| 264 | "ROLE_PERMISSION": "wim_accounts:id:", |
| 265 | }, |
| 266 | }, |
| 267 | "sdns": { |
| 268 | "METHODS": ("GET", "POST"), |
| 269 | "ROLE_PERMISSION": "sdn_controllers:", |
| 270 | "<ID>": { |
| 271 | "METHODS": ("GET", "DELETE", "PATCH"), |
| 272 | "ROLE_PERMISSION": "sdn_controllers:id:", |
| 273 | }, |
| 274 | }, |
| 275 | "k8sclusters": { |
| 276 | "METHODS": ("GET", "POST"), |
| 277 | "ROLE_PERMISSION": "k8sclusters:", |
| 278 | "<ID>": { |
| 279 | "METHODS": ("GET", "DELETE", "PATCH"), |
| 280 | "ROLE_PERMISSION": "k8sclusters:id:", |
| 281 | }, |
| 282 | }, |
| 283 | "vca": { |
| 284 | "METHODS": ("GET", "POST"), |
| 285 | "ROLE_PERMISSION": "vca:", |
| 286 | "<ID>": { |
| 287 | "METHODS": ("GET", "DELETE", "PATCH"), |
| 288 | "ROLE_PERMISSION": "vca:id:", |
| 289 | }, |
| 290 | }, |
| 291 | "k8srepos": { |
| 292 | "METHODS": ("GET", "POST"), |
| 293 | "ROLE_PERMISSION": "k8srepos:", |
| 294 | "<ID>": { |
| 295 | "METHODS": ("GET", "DELETE"), |
| 296 | "ROLE_PERMISSION": "k8srepos:id:", |
| 297 | }, |
| 298 | }, |
| 299 | "osmrepos": { |
| 300 | "METHODS": ("GET", "POST"), |
| 301 | "ROLE_PERMISSION": "osmrepos:", |
| 302 | "<ID>": { |
| 303 | "METHODS": ("GET", "DELETE", "PATCH"), |
| 304 | "ROLE_PERMISSION": "osmrepos:id:", |
| 305 | }, |
| 306 | }, |
| 307 | "domains": { |
| 308 | "METHODS": ("GET",), |
| 309 | "ROLE_PERMISSION": "domains:", |
| 310 | }, |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 311 | } |
| 312 | }, |
| 313 | "pdu": { |
| 314 | "v1": { |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 315 | "pdu_descriptors": { |
| 316 | "METHODS": ("GET", "POST"), |
| 317 | "ROLE_PERMISSION": "pduds:", |
| 318 | "<ID>": { |
| 319 | "METHODS": ("GET", "POST", "DELETE", "PATCH", "PUT"), |
| 320 | "ROLE_PERMISSION": "pduds:id:", |
| 321 | }, |
| 322 | }, |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 323 | } |
| 324 | }, |
| 325 | "nsd": { |
| 326 | "v1": { |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 327 | "ns_descriptors_content": { |
| 328 | "METHODS": ("GET", "POST"), |
| 329 | "ROLE_PERMISSION": "nsds:", |
| 330 | "<ID>": { |
| 331 | "METHODS": ("GET", "PUT", "DELETE"), |
| 332 | "ROLE_PERMISSION": "nsds:id:", |
| 333 | }, |
| 334 | }, |
| 335 | "ns_descriptors": { |
| 336 | "METHODS": ("GET", "POST"), |
| 337 | "ROLE_PERMISSION": "nsds:", |
| 338 | "<ID>": { |
| 339 | "METHODS": ("GET", "DELETE", "PATCH"), |
| 340 | "ROLE_PERMISSION": "nsds:id:", |
| 341 | "nsd_content": { |
| 342 | "METHODS": ("GET", "PUT"), |
| 343 | "ROLE_PERMISSION": "nsds:id:content:", |
| 344 | }, |
| 345 | "nsd": { |
| 346 | "METHODS": ("GET",), # descriptor inside package |
| 347 | "ROLE_PERMISSION": "nsds:id:content:", |
| 348 | }, |
| 349 | "artifacts": { |
| 350 | "METHODS": ("GET",), |
| 351 | "ROLE_PERMISSION": "nsds:id:nsd_artifact:", |
| 352 | "*": None, |
| 353 | }, |
| 354 | }, |
| 355 | }, |
| 356 | "pnf_descriptors": { |
| 357 | "TODO": ("GET", "POST"), |
| 358 | "<ID>": { |
| 359 | "TODO": ("GET", "DELETE", "PATCH"), |
| 360 | "pnfd_content": {"TODO": ("GET", "PUT")}, |
| 361 | }, |
| 362 | }, |
| 363 | "subscriptions": { |
| 364 | "TODO": ("GET", "POST"), |
| 365 | "<ID>": {"TODO": ("GET", "DELETE")}, |
| 366 | }, |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 367 | } |
| 368 | }, |
| 369 | "vnfpkgm": { |
| 370 | "v1": { |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 371 | "vnf_packages_content": { |
| 372 | "METHODS": ("GET", "POST"), |
| 373 | "ROLE_PERMISSION": "vnfds:", |
| 374 | "<ID>": { |
| 375 | "METHODS": ("GET", "PUT", "DELETE"), |
| 376 | "ROLE_PERMISSION": "vnfds:id:", |
| 377 | }, |
| 378 | }, |
| 379 | "vnf_packages": { |
| 380 | "METHODS": ("GET", "POST"), |
| 381 | "ROLE_PERMISSION": "vnfds:", |
| 382 | "<ID>": { |
| 383 | "METHODS": ("GET", "DELETE", "PATCH"), # GET: vnfPkgInfo |
| 384 | "ROLE_PERMISSION": "vnfds:id:", |
| 385 | "package_content": { |
| 386 | "METHODS": ("GET", "PUT"), # package |
| 387 | "ROLE_PERMISSION": "vnfds:id:", |
| 388 | "upload_from_uri": { |
| 389 | "METHODS": (), |
| 390 | "TODO": ("POST",), |
| 391 | "ROLE_PERMISSION": "vnfds:id:upload:", |
| 392 | }, |
| 393 | }, |
| 394 | "vnfd": { |
| 395 | "METHODS": ("GET",), # descriptor inside package |
| 396 | "ROLE_PERMISSION": "vnfds:id:content:", |
| 397 | }, |
| 398 | "artifacts": { |
| 399 | "METHODS": ("GET",), |
| 400 | "ROLE_PERMISSION": "vnfds:id:vnfd_artifact:", |
| 401 | "*": None, |
| 402 | }, |
| 403 | "action": { |
| 404 | "METHODS": ("POST",), |
| 405 | "ROLE_PERMISSION": "vnfds:id:action:", |
| 406 | }, |
| 407 | }, |
| 408 | }, |
| 409 | "subscriptions": { |
| 410 | "TODO": ("GET", "POST"), |
| 411 | "<ID>": {"TODO": ("GET", "DELETE")}, |
| 412 | }, |
| 413 | "vnfpkg_op_occs": { |
| 414 | "METHODS": ("GET",), |
| 415 | "ROLE_PERMISSION": "vnfds:vnfpkgops:", |
| 416 | "<ID>": {"METHODS": ("GET",), "ROLE_PERMISSION": "vnfds:vnfpkgops:id:"}, |
| 417 | }, |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 418 | } |
| 419 | }, |
| 420 | "nslcm": { |
| 421 | "v1": { |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 422 | "ns_instances_content": { |
| 423 | "METHODS": ("GET", "POST"), |
| 424 | "ROLE_PERMISSION": "ns_instances:", |
| 425 | "<ID>": { |
| 426 | "METHODS": ("GET", "DELETE"), |
| 427 | "ROLE_PERMISSION": "ns_instances:id:", |
| 428 | }, |
| 429 | }, |
| 430 | "ns_instances": { |
| 431 | "METHODS": ("GET", "POST"), |
| 432 | "ROLE_PERMISSION": "ns_instances:", |
| 433 | "<ID>": { |
| 434 | "METHODS": ("GET", "DELETE"), |
| 435 | "ROLE_PERMISSION": "ns_instances:id:", |
| garciadeblas | 0964edf | 2022-02-11 00:43:44 +0100 | [diff] [blame] | 436 | "heal": { |
| 437 | "METHODS": ("POST",), |
| 438 | "ROLE_PERMISSION": "ns_instances:id:heal:", |
| 439 | }, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 440 | "scale": { |
| 441 | "METHODS": ("POST",), |
| 442 | "ROLE_PERMISSION": "ns_instances:id:scale:", |
| 443 | }, |
| 444 | "terminate": { |
| 445 | "METHODS": ("POST",), |
| 446 | "ROLE_PERMISSION": "ns_instances:id:terminate:", |
| 447 | }, |
| 448 | "instantiate": { |
| 449 | "METHODS": ("POST",), |
| 450 | "ROLE_PERMISSION": "ns_instances:id:instantiate:", |
| 451 | }, |
| elumalai | 8e3806c | 2022-04-28 17:26:24 +0530 | [diff] [blame] | 452 | "migrate": { |
| 453 | "METHODS": ("POST",), |
| 454 | "ROLE_PERMISSION": "ns_instances:id:migrate:", |
| 455 | }, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 456 | "action": { |
| 457 | "METHODS": ("POST",), |
| 458 | "ROLE_PERMISSION": "ns_instances:id:action:", |
| 459 | }, |
| aticig | 544a2ae | 2022-04-05 09:00:17 +0300 | [diff] [blame] | 460 | "update": { |
| 461 | "METHODS": ("POST",), |
| 462 | "ROLE_PERMISSION": "ns_instances:id:update:", |
| 463 | }, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 464 | }, |
| 465 | }, |
| 466 | "ns_lcm_op_occs": { |
| 467 | "METHODS": ("GET",), |
| 468 | "ROLE_PERMISSION": "ns_instances:opps:", |
| 469 | "<ID>": { |
| 470 | "METHODS": ("GET",), |
| 471 | "ROLE_PERMISSION": "ns_instances:opps:id:", |
| 472 | }, |
| 473 | }, |
| 474 | "vnfrs": { |
| 475 | "METHODS": ("GET",), |
| 476 | "ROLE_PERMISSION": "vnf_instances:", |
| 477 | "<ID>": {"METHODS": ("GET",), "ROLE_PERMISSION": "vnf_instances:id:"}, |
| 478 | }, |
| 479 | "vnf_instances": { |
| 480 | "METHODS": ("GET",), |
| 481 | "ROLE_PERMISSION": "vnf_instances:", |
| 482 | "<ID>": {"METHODS": ("GET",), "ROLE_PERMISSION": "vnf_instances:id:"}, |
| 483 | }, |
| 484 | "subscriptions": { |
| 485 | "METHODS": ("GET", "POST"), |
| 486 | "ROLE_PERMISSION": "ns_subscriptions:", |
| 487 | "<ID>": { |
| 488 | "METHODS": ("GET", "DELETE"), |
| 489 | "ROLE_PERMISSION": "ns_subscriptions:id:", |
| 490 | }, |
| 491 | }, |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 492 | } |
| 493 | }, |
| almagia | e47b913 | 2022-05-17 14:12:22 +0200 | [diff] [blame] | 494 | "vnflcm": { |
| 495 | "v1": { |
| 496 | "vnf_instances": {"METHODS": ("GET", "POST"), |
| 497 | "ROLE_PERMISSION": "vnflcm_instances:", |
| 498 | "<ID>": {"METHODS": ("GET", "DELETE"), |
| 499 | "ROLE_PERMISSION": "vnflcm_instances:id:", |
| 500 | "scale": {"METHODS": ("POST",), |
| 501 | "ROLE_PERMISSION": "vnflcm_instances:id:scale:" |
| 502 | }, |
| 503 | "terminate": {"METHODS": ("POST",), |
| 504 | "ROLE_PERMISSION": "vnflcm_instances:id:terminate:" |
| 505 | }, |
| 506 | "instantiate": {"METHODS": ("POST",), |
| 507 | "ROLE_PERMISSION": "vnflcm_instances:id:instantiate:" |
| 508 | }, |
| 509 | } |
| 510 | }, |
| 511 | "vnf_lcm_op_occs": {"METHODS": ("GET",), |
| 512 | "ROLE_PERMISSION": "vnf_instances:opps:", |
| 513 | "<ID>": {"METHODS": ("GET",), |
| 514 | "ROLE_PERMISSION": "vnf_instances:opps:id:" |
| 515 | }, |
| 516 | }, |
| selvi.j | f100459 | 2022-04-29 05:42:35 +0000 | [diff] [blame] | 517 | "subscriptions": {"METHODS": ("GET", "POST"), |
| 518 | "ROLE_PERMISSION": "vnflcm_subscriptions:", |
| 519 | "<ID>": {"METHODS": ("GET", "DELETE"), |
| 520 | "ROLE_PERMISSION": "vnflcm_subscriptions:id:" |
| 521 | } |
| 522 | }, |
| almagia | e47b913 | 2022-05-17 14:12:22 +0200 | [diff] [blame] | 523 | } |
| 524 | }, |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 525 | "nst": { |
| 526 | "v1": { |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 527 | "netslice_templates_content": { |
| 528 | "METHODS": ("GET", "POST"), |
| 529 | "ROLE_PERMISSION": "slice_templates:", |
| 530 | "<ID>": { |
| 531 | "METHODS": ("GET", "PUT", "DELETE"), |
| 532 | "ROLE_PERMISSION": "slice_templates:id:", |
| 533 | }, |
| 534 | }, |
| 535 | "netslice_templates": { |
| 536 | "METHODS": ("GET", "POST"), |
| 537 | "ROLE_PERMISSION": "slice_templates:", |
| 538 | "<ID>": { |
| 539 | "METHODS": ("GET", "DELETE"), |
| 540 | "TODO": ("PATCH",), |
| 541 | "ROLE_PERMISSION": "slice_templates:id:", |
| 542 | "nst_content": { |
| 543 | "METHODS": ("GET", "PUT"), |
| 544 | "ROLE_PERMISSION": "slice_templates:id:content:", |
| 545 | }, |
| 546 | "nst": { |
| 547 | "METHODS": ("GET",), # descriptor inside package |
| 548 | "ROLE_PERMISSION": "slice_templates:id:content:", |
| 549 | }, |
| 550 | "artifacts": { |
| 551 | "METHODS": ("GET",), |
| 552 | "ROLE_PERMISSION": "slice_templates:id:content:", |
| 553 | "*": None, |
| 554 | }, |
| 555 | }, |
| 556 | }, |
| 557 | "subscriptions": { |
| 558 | "TODO": ("GET", "POST"), |
| 559 | "<ID>": {"TODO": ("GET", "DELETE")}, |
| 560 | }, |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 561 | } |
| 562 | }, |
| 563 | "nsilcm": { |
| 564 | "v1": { |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 565 | "netslice_instances_content": { |
| 566 | "METHODS": ("GET", "POST"), |
| 567 | "ROLE_PERMISSION": "slice_instances:", |
| 568 | "<ID>": { |
| 569 | "METHODS": ("GET", "DELETE"), |
| 570 | "ROLE_PERMISSION": "slice_instances:id:", |
| 571 | }, |
| 572 | }, |
| 573 | "netslice_instances": { |
| 574 | "METHODS": ("GET", "POST"), |
| 575 | "ROLE_PERMISSION": "slice_instances:", |
| 576 | "<ID>": { |
| 577 | "METHODS": ("GET", "DELETE"), |
| 578 | "ROLE_PERMISSION": "slice_instances:id:", |
| 579 | "terminate": { |
| 580 | "METHODS": ("POST",), |
| 581 | "ROLE_PERMISSION": "slice_instances:id:terminate:", |
| 582 | }, |
| 583 | "instantiate": { |
| 584 | "METHODS": ("POST",), |
| 585 | "ROLE_PERMISSION": "slice_instances:id:instantiate:", |
| 586 | }, |
| 587 | "action": { |
| 588 | "METHODS": ("POST",), |
| 589 | "ROLE_PERMISSION": "slice_instances:id:action:", |
| 590 | }, |
| 591 | }, |
| 592 | }, |
| 593 | "nsi_lcm_op_occs": { |
| 594 | "METHODS": ("GET",), |
| 595 | "ROLE_PERMISSION": "slice_instances:opps:", |
| 596 | "<ID>": { |
| 597 | "METHODS": ("GET",), |
| 598 | "ROLE_PERMISSION": "slice_instances:opps:id:", |
| 599 | }, |
| 600 | }, |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 601 | } |
| 602 | }, |
| 603 | "nspm": { |
| 604 | "v1": { |
| 605 | "pm_jobs": { |
| 606 | "<ID>": { |
| 607 | "reports": { |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 608 | "<ID>": { |
| 609 | "METHODS": ("GET",), |
| 610 | "ROLE_PERMISSION": "reports:id:", |
| 611 | } |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 612 | } |
| 613 | }, |
| 614 | }, |
| 615 | }, |
| 616 | }, |
| Atul Agarwal | b6480fc | 2021-03-18 08:19:32 +0000 | [diff] [blame] | 617 | "nsfm": { |
| 618 | "v1": { |
| 619 | "alarms": {"METHODS": ("GET", "PATCH"), |
| 620 | "ROLE_PERMISSION": "alarms:", |
| 621 | "<ID>": {"METHODS": ("GET", "PATCH"), |
| 622 | "ROLE_PERMISSION": "alarms:id:", |
| 623 | }, |
| 624 | } |
| 625 | }, |
| 626 | }, |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 629 | |
| 630 | class NbiException(Exception): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 631 | def __init__(self, message, http_code=HTTPStatus.METHOD_NOT_ALLOWED): |
| 632 | Exception.__init__(self, message) |
| 633 | self.http_code = http_code |
| 634 | |
| 635 | |
| 636 | class Server(object): |
| 637 | instance = 0 |
| 638 | # to decode bytes to str |
| 639 | reader = getreader("utf-8") |
| 640 | |
| 641 | def __init__(self): |
| 642 | self.instance += 1 |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 643 | self.authenticator = Authenticator(valid_url_methods, valid_query_string) |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 644 | self.engine = Engine(self.authenticator) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 645 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 646 | def _format_in(self, kwargs): |
| 647 | try: |
| 648 | indata = None |
| 649 | if cherrypy.request.body.length: |
| 650 | error_text = "Invalid input format " |
| 651 | |
| 652 | if "Content-Type" in cherrypy.request.headers: |
| 653 | if "application/json" in cherrypy.request.headers["Content-Type"]: |
| 654 | error_text = "Invalid json format " |
| 655 | indata = json.load(self.reader(cherrypy.request.body)) |
| gcalvino | de4adfe | 2018-10-30 11:46:09 +0100 | [diff] [blame] | 656 | cherrypy.request.headers.pop("Content-File-MD5", None) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 657 | elif "application/yaml" in cherrypy.request.headers["Content-Type"]: |
| 658 | error_text = "Invalid yaml format " |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 659 | indata = yaml.load( |
| 660 | cherrypy.request.body, Loader=yaml.SafeLoader |
| 661 | ) |
| gcalvino | de4adfe | 2018-10-30 11:46:09 +0100 | [diff] [blame] | 662 | cherrypy.request.headers.pop("Content-File-MD5", None) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 663 | elif ( |
| 664 | "application/binary" in cherrypy.request.headers["Content-Type"] |
| 665 | or "application/gzip" |
| 666 | in cherrypy.request.headers["Content-Type"] |
| 667 | or "application/zip" in cherrypy.request.headers["Content-Type"] |
| 668 | or "text/plain" in cherrypy.request.headers["Content-Type"] |
| 669 | ): |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 670 | indata = cherrypy.request.body # .read() |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 671 | elif ( |
| 672 | "multipart/form-data" |
| 673 | in cherrypy.request.headers["Content-Type"] |
| 674 | ): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 675 | if "descriptor_file" in kwargs: |
| 676 | filecontent = kwargs.pop("descriptor_file") |
| 677 | if not filecontent.file: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 678 | raise NbiException( |
| 679 | "empty file or content", HTTPStatus.BAD_REQUEST |
| 680 | ) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 681 | indata = filecontent.file # .read() |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 682 | if filecontent.content_type.value: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 683 | cherrypy.request.headers[ |
| 684 | "Content-Type" |
| 685 | ] = filecontent.content_type.value |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 686 | else: |
| 687 | # raise cherrypy.HTTPError(HTTPStatus.Not_Acceptable, |
| 688 | # "Only 'Content-Type' of type 'application/json' or |
| 689 | # 'application/yaml' for input format are available") |
| 690 | error_text = "Invalid yaml format " |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 691 | indata = yaml.load( |
| 692 | cherrypy.request.body, Loader=yaml.SafeLoader |
| 693 | ) |
| gcalvino | de4adfe | 2018-10-30 11:46:09 +0100 | [diff] [blame] | 694 | cherrypy.request.headers.pop("Content-File-MD5", None) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 695 | else: |
| 696 | error_text = "Invalid yaml format " |
| delacruzramo | b19cadc | 2019-10-08 10:18:02 +0200 | [diff] [blame] | 697 | indata = yaml.load(cherrypy.request.body, Loader=yaml.SafeLoader) |
| gcalvino | de4adfe | 2018-10-30 11:46:09 +0100 | [diff] [blame] | 698 | cherrypy.request.headers.pop("Content-File-MD5", None) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 699 | if not indata: |
| 700 | indata = {} |
| 701 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 702 | format_yaml = False |
| 703 | if cherrypy.request.headers.get("Query-String-Format") == "yaml": |
| 704 | format_yaml = True |
| 705 | |
| 706 | for k, v in kwargs.items(): |
| 707 | if isinstance(v, str): |
| 708 | if v == "": |
| 709 | kwargs[k] = None |
| 710 | elif format_yaml: |
| 711 | try: |
| delacruzramo | b19cadc | 2019-10-08 10:18:02 +0200 | [diff] [blame] | 712 | kwargs[k] = yaml.load(v, Loader=yaml.SafeLoader) |
| tierno | e128118 | 2018-05-22 12:24:36 +0200 | [diff] [blame] | 713 | except Exception: |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 714 | pass |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 715 | elif ( |
| 716 | k.endswith(".gt") |
| 717 | or k.endswith(".lt") |
| 718 | or k.endswith(".gte") |
| 719 | or k.endswith(".lte") |
| 720 | ): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 721 | try: |
| 722 | kwargs[k] = int(v) |
| tierno | e128118 | 2018-05-22 12:24:36 +0200 | [diff] [blame] | 723 | except Exception: |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 724 | try: |
| 725 | kwargs[k] = float(v) |
| tierno | e128118 | 2018-05-22 12:24:36 +0200 | [diff] [blame] | 726 | except Exception: |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 727 | pass |
| 728 | elif v.find(",") > 0: |
| 729 | kwargs[k] = v.split(",") |
| 730 | elif isinstance(v, (list, tuple)): |
| 731 | for index in range(0, len(v)): |
| 732 | if v[index] == "": |
| 733 | v[index] = None |
| 734 | elif format_yaml: |
| 735 | try: |
| delacruzramo | b19cadc | 2019-10-08 10:18:02 +0200 | [diff] [blame] | 736 | v[index] = yaml.load(v[index], Loader=yaml.SafeLoader) |
| tierno | e128118 | 2018-05-22 12:24:36 +0200 | [diff] [blame] | 737 | except Exception: |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 738 | pass |
| 739 | |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 740 | return indata |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 741 | except (ValueError, yaml.YAMLError) as exc: |
| 742 | raise NbiException(error_text + str(exc), HTTPStatus.BAD_REQUEST) |
| 743 | except KeyError as exc: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 744 | raise NbiException( |
| 745 | "Query string error: " + str(exc), HTTPStatus.BAD_REQUEST |
| 746 | ) |
| tierno | b92094f | 2018-05-11 13:44:22 +0200 | [diff] [blame] | 747 | except Exception as exc: |
| 748 | raise NbiException(error_text + str(exc), HTTPStatus.BAD_REQUEST) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 749 | |
| 750 | @staticmethod |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 751 | def _format_out(data, token_info=None, _format=None): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 752 | """ |
| 753 | return string of dictionary data according to requested json, yaml, xml. By default json |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 754 | :param data: response to be sent. Can be a dict, text or file |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 755 | :param token_info: Contains among other username and project |
| tierno | 5792d7d | 2019-08-30 15:37:12 +0000 | [diff] [blame] | 756 | :param _format: The format to be set as Content-Type if data is a file |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 757 | :return: None |
| 758 | """ |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 759 | accept = cherrypy.request.headers.get("Accept") |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 760 | if data is None: |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 761 | if accept and "text/html" in accept: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 762 | return html.format( |
| 763 | data, cherrypy.request, cherrypy.response, token_info |
| 764 | ) |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 765 | # cherrypy.response.status = HTTPStatus.NO_CONTENT.value |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 766 | return |
| 767 | elif hasattr(data, "read"): # file object |
| 768 | if _format: |
| 769 | cherrypy.response.headers["Content-Type"] = _format |
| 770 | elif "b" in data.mode: # binariy asssumig zip |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 771 | cherrypy.response.headers["Content-Type"] = "application/zip" |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 772 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 773 | cherrypy.response.headers["Content-Type"] = "text/plain" |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 774 | # TODO check that cherrypy close file. If not implement pending things to close per thread next |
| 775 | return data |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 776 | if accept: |
| Frank Bryden | 02e700c | 2020-06-03 13:34:16 +0000 | [diff] [blame] | 777 | if "text/html" in accept: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 778 | return html.format( |
| 779 | data, cherrypy.request, cherrypy.response, token_info |
| 780 | ) |
| Frank Bryden | b5422da | 2020-08-10 11:44:11 +0000 | [diff] [blame] | 781 | elif "application/yaml" in accept or "*/*" in accept: |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 782 | pass |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 783 | elif "application/json" in accept or ( |
| 784 | cherrypy.response.status and cherrypy.response.status >= 300 |
| 785 | ): |
| 786 | cherrypy.response.headers[ |
| 787 | "Content-Type" |
| 788 | ] = "application/json; charset=utf-8" |
| Frank Bryden | 02e700c | 2020-06-03 13:34:16 +0000 | [diff] [blame] | 789 | a = json.dumps(data, indent=4) + "\n" |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 790 | return a.encode("utf8") |
| 791 | cherrypy.response.headers["Content-Type"] = "application/yaml" |
| 792 | return yaml.safe_dump( |
| 793 | data, |
| 794 | explicit_start=True, |
| 795 | indent=4, |
| 796 | default_flow_style=False, |
| 797 | tags=False, |
| 798 | encoding="utf-8", |
| 799 | allow_unicode=True, |
| 800 | ) # , canonical=True, default_style='"' |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 801 | |
| 802 | @cherrypy.expose |
| 803 | def index(self, *args, **kwargs): |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 804 | token_info = None |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 805 | try: |
| 806 | if cherrypy.request.method == "GET": |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 807 | token_info = self.authenticator.authorize() |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 808 | outdata = token_info # Home page |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 809 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 810 | raise cherrypy.HTTPError( |
| 811 | HTTPStatus.METHOD_NOT_ALLOWED.value, |
| 812 | "Method {} not allowed for tokens".format(cherrypy.request.method), |
| 813 | ) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 814 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 815 | return self._format_out(outdata, token_info) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 816 | |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 817 | except (EngineException, AuthException) as e: |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 818 | # cherrypy.log("index Exception {}".format(e)) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 819 | cherrypy.response.status = e.http_code.value |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 820 | return self._format_out("Welcome to OSM!", token_info) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 821 | |
| 822 | @cherrypy.expose |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 823 | def version(self, *args, **kwargs): |
| tierno | dfe0957 | 2018-04-24 10:41:10 +0200 | [diff] [blame] | 824 | # TODO consider to remove and provide version using the static version file |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 825 | try: |
| 826 | if cherrypy.request.method != "GET": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 827 | raise NbiException( |
| 828 | "Only method GET is allowed", HTTPStatus.METHOD_NOT_ALLOWED |
| 829 | ) |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 830 | elif args or kwargs: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 831 | raise NbiException( |
| 832 | "Invalid URL or query string for version", |
| 833 | HTTPStatus.METHOD_NOT_ALLOWED, |
| 834 | ) |
| tierno | 9c63011 | 2019-08-29 14:21:41 +0000 | [diff] [blame] | 835 | # TODO include version of other modules, pick up from some kafka admin message |
| tierno | 5792d7d | 2019-08-30 15:37:12 +0000 | [diff] [blame] | 836 | osm_nbi_version = {"version": nbi_version, "date": nbi_version_date} |
| 837 | return self._format_out(osm_nbi_version) |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 838 | except NbiException as e: |
| 839 | cherrypy.response.status = e.http_code.value |
| 840 | problem_details = { |
| 841 | "code": e.http_code.name, |
| 842 | "status": e.http_code.value, |
| 843 | "detail": str(e), |
| 844 | } |
| 845 | return self._format_out(problem_details, None) |
| 846 | |
| tierno | 12eac3c | 2020-03-19 23:22:08 +0000 | [diff] [blame] | 847 | def domain(self): |
| 848 | try: |
| 849 | domains = { |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 850 | "user_domain_name": cherrypy.tree.apps["/osm"] |
| 851 | .config["authentication"] |
| 852 | .get("user_domain_name"), |
| 853 | "project_domain_name": cherrypy.tree.apps["/osm"] |
| 854 | .config["authentication"] |
| 855 | .get("project_domain_name"), |
| 856 | } |
| tierno | 12eac3c | 2020-03-19 23:22:08 +0000 | [diff] [blame] | 857 | return self._format_out(domains) |
| 858 | except NbiException as e: |
| 859 | cherrypy.response.status = e.http_code.value |
| 860 | problem_details = { |
| 861 | "code": e.http_code.name, |
| 862 | "status": e.http_code.value, |
| 863 | "detail": str(e), |
| 864 | } |
| 865 | return self._format_out(problem_details, None) |
| 866 | |
| tierno | a503570 | 2019-07-29 08:54:42 +0000 | [diff] [blame] | 867 | @staticmethod |
| 868 | def _format_login(token_info): |
| 869 | """ |
| 870 | Changes cherrypy.request.login to include username/project_name;session so that cherrypy access log will |
| 871 | log this information |
| 872 | :param token_info: Dictionary with token content |
| 873 | :return: None |
| 874 | """ |
| 875 | cherrypy.request.login = token_info.get("username", "-") |
| 876 | if token_info.get("project_name"): |
| 877 | cherrypy.request.login += "/" + token_info["project_name"] |
| 878 | if token_info.get("id"): |
| 879 | cherrypy.request.login += ";session=" + token_info["id"][0:12] |
| 880 | |
| Atul Agarwal | b6480fc | 2021-03-18 08:19:32 +0000 | [diff] [blame] | 881 | # NS Fault Management |
| 882 | @cherrypy.expose |
| 883 | def nsfm(self, version=None, topic=None, uuid=None, project_name=None, ns_id=None, *args, **kwargs): |
| 884 | if topic == 'alarms': |
| 885 | try: |
| 886 | method = cherrypy.request.method |
| 887 | role_permission = self._check_valid_url_method(method, "nsfm", version, topic, None, None, *args) |
| 888 | query_string_operations = self._extract_query_string_operations(kwargs, method) |
| 889 | |
| 890 | self.authenticator.authorize(role_permission, query_string_operations, None) |
| 891 | |
| 892 | # to handle get request |
| 893 | if cherrypy.request.method == 'GET': |
| 894 | # if request is on basis of uuid |
| 895 | if uuid and uuid != 'None': |
| 896 | try: |
| 897 | alarm = self.engine.db.get_one("alarms", {"uuid": uuid}) |
| 898 | alarm_action = self.engine.db.get_one("alarms_action", {"uuid": uuid}) |
| 899 | alarm.update(alarm_action) |
| 900 | vnf = self.engine.db.get_one("vnfrs", {"nsr-id-ref": alarm["tags"]["ns_id"]}) |
| 901 | alarm["vnf-id"] = vnf["_id"] |
| 902 | return self._format_out(str(alarm)) |
| 903 | except Exception: |
| 904 | return self._format_out("Please provide valid alarm uuid") |
| 905 | elif ns_id and ns_id != 'None': |
| 906 | # if request is on basis of ns_id |
| 907 | try: |
| 908 | alarms = self.engine.db.get_list("alarms", {"tags.ns_id": ns_id}) |
| 909 | for alarm in alarms: |
| 910 | alarm_action = self.engine.db.get_one("alarms_action", {"uuid": alarm['uuid']}) |
| 911 | alarm.update(alarm_action) |
| 912 | return self._format_out(str(alarms)) |
| 913 | except Exception: |
| 914 | return self._format_out("Please provide valid ns id") |
| 915 | else: |
| 916 | # to return only alarm which are related to given project |
| 917 | project = self.engine.db.get_one("projects", {"name": project_name}) |
| 918 | project_id = project.get('_id') |
| 919 | ns_list = self.engine.db.get_list("nsrs", {"_admin.projects_read": project_id}) |
| 920 | ns_ids = [] |
| 921 | for ns in ns_list: |
| 922 | ns_ids.append(ns.get("_id")) |
| 923 | alarms = self.engine.db.get_list("alarms") |
| 924 | alarm_list = [alarm for alarm in alarms if alarm["tags"]["ns_id"] in ns_ids] |
| 925 | for alrm in alarm_list: |
| 926 | action = self.engine.db.get_one("alarms_action", {"uuid": alrm.get("uuid")}) |
| 927 | alrm.update(action) |
| 928 | return self._format_out(str(alarm_list)) |
| 929 | # to handle patch request for alarm update |
| 930 | elif cherrypy.request.method == 'PATCH': |
| 931 | data = yaml.load(cherrypy.request.body, Loader=yaml.SafeLoader) |
| 932 | try: |
| 933 | # check if uuid is valid |
| 934 | self.engine.db.get_one("alarms", {"uuid": data.get("uuid")}) |
| 935 | except Exception: |
| 936 | return self._format_out("Please provide valid alarm uuid.") |
| 937 | if data.get("is_enable") is not None: |
| 938 | if data.get("is_enable"): |
| 939 | alarm_status = 'ok' |
| 940 | else: |
| 941 | alarm_status = 'disabled' |
| 942 | self.engine.db.set_one("alarms", {"uuid": data.get("uuid")}, |
| 943 | {"alarm_status": alarm_status}) |
| 944 | else: |
| 945 | self.engine.db.set_one("alarms", {"uuid": data.get("uuid")}, |
| 946 | {"threshold": data.get("threshold")}) |
| 947 | return self._format_out("Alarm updated") |
| 948 | except Exception as e: |
| 949 | cherrypy.response.status = e.http_code.value |
| 950 | if isinstance(e, (NbiException, EngineException, DbException, FsException, MsgException, AuthException, |
| 951 | ValidationError, AuthconnException)): |
| 952 | http_code_value = cherrypy.response.status = e.http_code.value |
| 953 | http_code_name = e.http_code.name |
| 954 | cherrypy.log("Exception {}".format(e)) |
| 955 | else: |
| 956 | http_code_value = cherrypy.response.status = HTTPStatus.BAD_REQUEST.value # INTERNAL_SERVER_ERROR |
| 957 | cherrypy.log("CRITICAL: Exception {}".format(e), traceback=True) |
| 958 | http_code_name = HTTPStatus.BAD_REQUEST.name |
| 959 | problem_details = { |
| 960 | "code": http_code_name, |
| 961 | "status": http_code_value, |
| 962 | "detail": str(e), |
| 963 | } |
| 964 | return self._format_out(problem_details) |
| 965 | |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 966 | @cherrypy.expose |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 967 | def token(self, method, token_id=None, kwargs=None): |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 968 | token_info = None |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 969 | # self.engine.load_dbase(cherrypy.request.app.config) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 970 | indata = self._format_in(kwargs) |
| 971 | if not isinstance(indata, dict): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 972 | raise NbiException( |
| 973 | "Expected application/yaml or application/json Content-Type", |
| 974 | HTTPStatus.BAD_REQUEST, |
| 975 | ) |
| tierno | a503570 | 2019-07-29 08:54:42 +0000 | [diff] [blame] | 976 | |
| 977 | if method == "GET": |
| 978 | token_info = self.authenticator.authorize() |
| 979 | # for logging |
| 980 | self._format_login(token_info) |
| 981 | if token_id: |
| 982 | outdata = self.authenticator.get_token(token_info, token_id) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 983 | else: |
| tierno | a503570 | 2019-07-29 08:54:42 +0000 | [diff] [blame] | 984 | outdata = self.authenticator.get_token_list(token_info) |
| 985 | elif method == "POST": |
| 986 | try: |
| 987 | token_info = self.authenticator.authorize() |
| 988 | except Exception: |
| 989 | token_info = None |
| 990 | if kwargs: |
| 991 | indata.update(kwargs) |
| 992 | # This is needed to log the user when authentication fails |
| 993 | cherrypy.request.login = "{}".format(indata.get("username", "-")) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 994 | outdata = token_info = self.authenticator.new_token( |
| 995 | token_info, indata, cherrypy.request.remote |
| 996 | ) |
| 997 | cherrypy.session["Authorization"] = outdata["_id"] |
| tierno | a503570 | 2019-07-29 08:54:42 +0000 | [diff] [blame] | 998 | self._set_location_header("admin", "v1", "tokens", outdata["_id"]) |
| 999 | # for logging |
| 1000 | self._format_login(token_info) |
| selvi.j | a9a1fc8 | 2022-04-04 06:54:30 +0000 | [diff] [blame] | 1001 | # password expiry check |
| 1002 | if self.authenticator.check_password_expiry(outdata): |
| 1003 | outdata = {"id": outdata["id"], |
| 1004 | "message": "change_password", |
| 1005 | "user_id": outdata["user_id"] |
| 1006 | } |
| tierno | a503570 | 2019-07-29 08:54:42 +0000 | [diff] [blame] | 1007 | # cherrypy.response.cookie["Authorization"] = outdata["id"] |
| 1008 | # cherrypy.response.cookie["Authorization"]['expires'] = 3600 |
| 1009 | elif method == "DELETE": |
| 1010 | if not token_id and "id" in kwargs: |
| 1011 | token_id = kwargs["id"] |
| 1012 | elif not token_id: |
| 1013 | token_info = self.authenticator.authorize() |
| 1014 | # for logging |
| 1015 | self._format_login(token_info) |
| 1016 | token_id = token_info["_id"] |
| 1017 | outdata = self.authenticator.del_token(token_id) |
| 1018 | token_info = None |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1019 | cherrypy.session["Authorization"] = "logout" |
| tierno | a503570 | 2019-07-29 08:54:42 +0000 | [diff] [blame] | 1020 | # cherrypy.response.cookie["Authorization"] = token_id |
| 1021 | # cherrypy.response.cookie["Authorization"]['expires'] = 0 |
| 1022 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1023 | raise NbiException( |
| 1024 | "Method {} not allowed for token".format(method), |
| 1025 | HTTPStatus.METHOD_NOT_ALLOWED, |
| 1026 | ) |
| tierno | a503570 | 2019-07-29 08:54:42 +0000 | [diff] [blame] | 1027 | return self._format_out(outdata, token_info) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1028 | |
| 1029 | @cherrypy.expose |
| 1030 | def test(self, *args, **kwargs): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1031 | if not cherrypy.config.get("server.enable_test") or ( |
| 1032 | isinstance(cherrypy.config["server.enable_test"], str) |
| 1033 | and cherrypy.config["server.enable_test"].lower() == "false" |
| 1034 | ): |
| tierno | 4836bac | 2020-01-15 14:41:48 +0000 | [diff] [blame] | 1035 | cherrypy.response.status = HTTPStatus.METHOD_NOT_ALLOWED.value |
| 1036 | return "test URL is disabled" |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1037 | thread_info = None |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1038 | if args and args[0] == "help": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1039 | return ( |
| 1040 | "<html><pre>\ninit\nfile/<name> download file\ndb-clear/table\nfs-clear[/folder]\nlogin\nlogin2\n" |
| 1041 | "sleep/<time>\nmessage/topic\n</pre></html>" |
| 1042 | ) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1043 | |
| 1044 | elif args and args[0] == "init": |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1045 | try: |
| 1046 | # self.engine.load_dbase(cherrypy.request.app.config) |
| 1047 | self.engine.create_admin() |
| 1048 | return "Done. User 'admin', password 'admin' created" |
| 1049 | except Exception: |
| 1050 | cherrypy.response.status = HTTPStatus.FORBIDDEN.value |
| 1051 | return self._format_out("Database already initialized") |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1052 | elif args and args[0] == "file": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1053 | return cherrypy.lib.static.serve_file( |
| 1054 | cherrypy.tree.apps["/osm"].config["storage"]["path"] + "/" + args[1], |
| 1055 | "text/plain", |
| 1056 | "attachment", |
| 1057 | ) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1058 | elif args and args[0] == "file2": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1059 | f_path = ( |
| 1060 | cherrypy.tree.apps["/osm"].config["storage"]["path"] + "/" + args[1] |
| 1061 | ) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1062 | f = open(f_path, "r") |
| 1063 | cherrypy.response.headers["Content-type"] = "text/plain" |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1064 | return f |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 1065 | |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1066 | elif len(args) == 2 and args[0] == "db-clear": |
| tierno | f717cbe | 2018-12-03 16:35:42 +0000 | [diff] [blame] | 1067 | deleted_info = self.engine.db.del_list(args[1], kwargs) |
| 1068 | return "{} {} deleted\n".format(deleted_info["deleted"], args[1]) |
| 1069 | elif len(args) and args[0] == "fs-clear": |
| 1070 | if len(args) >= 2: |
| 1071 | folders = (args[1],) |
| 1072 | else: |
| 1073 | folders = self.engine.fs.dir_ls(".") |
| 1074 | for folder in folders: |
| 1075 | self.engine.fs.file_delete(folder) |
| 1076 | return ",".join(folders) + " folders deleted\n" |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1077 | elif args and args[0] == "login": |
| 1078 | if not cherrypy.request.headers.get("Authorization"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1079 | cherrypy.response.headers[ |
| 1080 | "WWW-Authenticate" |
| 1081 | ] = 'Basic realm="Access to OSM site", charset="UTF-8"' |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1082 | cherrypy.response.status = HTTPStatus.UNAUTHORIZED.value |
| 1083 | elif args and args[0] == "login2": |
| 1084 | if not cherrypy.request.headers.get("Authorization"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1085 | cherrypy.response.headers[ |
| 1086 | "WWW-Authenticate" |
| 1087 | ] = 'Bearer realm="Access to OSM site"' |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1088 | cherrypy.response.status = HTTPStatus.UNAUTHORIZED.value |
| 1089 | elif args and args[0] == "sleep": |
| 1090 | sleep_time = 5 |
| 1091 | try: |
| 1092 | sleep_time = int(args[1]) |
| 1093 | except Exception: |
| 1094 | cherrypy.response.status = HTTPStatus.FORBIDDEN.value |
| 1095 | return self._format_out("Database already initialized") |
| 1096 | thread_info = cherrypy.thread_data |
| 1097 | print(thread_info) |
| 1098 | time.sleep(sleep_time) |
| 1099 | # thread_info |
| 1100 | elif len(args) >= 2 and args[0] == "message": |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1101 | main_topic = args[1] |
| 1102 | return_text = "<html><pre>{} ->\n".format(main_topic) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1103 | try: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1104 | if cherrypy.request.method == "POST": |
| delacruzramo | b19cadc | 2019-10-08 10:18:02 +0200 | [diff] [blame] | 1105 | to_send = yaml.load(cherrypy.request.body, Loader=yaml.SafeLoader) |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 1106 | for k, v in to_send.items(): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1107 | self.engine.msg.write(main_topic, k, v) |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 1108 | return_text += " {}: {}\n".format(k, v) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1109 | elif cherrypy.request.method == "GET": |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 1110 | for k, v in kwargs.items(): |
| tierno | f1509b2 | 2020-05-12 14:32:37 +0000 | [diff] [blame] | 1111 | v_dict = yaml.load(v, Loader=yaml.SafeLoader) |
| 1112 | self.engine.msg.write(main_topic, k, v_dict) |
| 1113 | return_text += " {}: {}\n".format(k, v_dict) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1114 | except Exception as e: |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 1115 | return_text += "Error: " + str(e) |
| 1116 | return_text += "</pre></html>\n" |
| 1117 | return return_text |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1118 | |
| 1119 | return_text = ( |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1120 | "<html><pre>\nheaders:\n args: {}\n".format(args) |
| 1121 | + " kwargs: {}\n".format(kwargs) |
| 1122 | + " headers: {}\n".format(cherrypy.request.headers) |
| 1123 | + " path_info: {}\n".format(cherrypy.request.path_info) |
| 1124 | + " query_string: {}\n".format(cherrypy.request.query_string) |
| 1125 | + " session: {}\n".format(cherrypy.session) |
| 1126 | + " cookie: {}\n".format(cherrypy.request.cookie) |
| 1127 | + " method: {}\n".format(cherrypy.request.method) |
| 1128 | + " session: {}\n".format(cherrypy.session.get("fieldname")) |
| 1129 | + " body:\n" |
| 1130 | ) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1131 | return_text += " length: {}\n".format(cherrypy.request.body.length) |
| 1132 | if cherrypy.request.body.length: |
| 1133 | return_text += " content: {}\n".format( |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1134 | str( |
| 1135 | cherrypy.request.body.read( |
| 1136 | int(cherrypy.request.headers.get("Content-Length", 0)) |
| 1137 | ) |
| 1138 | ) |
| 1139 | ) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1140 | if thread_info: |
| 1141 | return_text += "thread: {}\n".format(thread_info) |
| 1142 | return_text += "</pre></html>" |
| 1143 | return return_text |
| 1144 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 1145 | @staticmethod |
| 1146 | def _check_valid_url_method(method, *args): |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1147 | if len(args) < 3: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1148 | raise NbiException( |
| 1149 | "URL must contain at least 'main_topic/version/topic'", |
| 1150 | HTTPStatus.METHOD_NOT_ALLOWED, |
| 1151 | ) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1152 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 1153 | reference = valid_url_methods |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1154 | for arg in args: |
| 1155 | if arg is None: |
| 1156 | break |
| 1157 | if not isinstance(reference, dict): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1158 | raise NbiException( |
| 1159 | "URL contains unexpected extra items '{}'".format(arg), |
| 1160 | HTTPStatus.METHOD_NOT_ALLOWED, |
| 1161 | ) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1162 | |
| 1163 | if arg in reference: |
| 1164 | reference = reference[arg] |
| 1165 | elif "<ID>" in reference: |
| 1166 | reference = reference["<ID>"] |
| 1167 | elif "*" in reference: |
| tierno | 74b5358 | 2020-06-18 10:52:37 +0000 | [diff] [blame] | 1168 | # if there is content |
| 1169 | if reference["*"]: |
| 1170 | reference = reference["*"] |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1171 | break |
| 1172 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1173 | raise NbiException( |
| 1174 | "Unexpected URL item {}".format(arg), HTTPStatus.METHOD_NOT_ALLOWED |
| 1175 | ) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1176 | if "TODO" in reference and method in reference["TODO"]: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1177 | raise NbiException( |
| 1178 | "Method {} not supported yet for this URL".format(method), |
| 1179 | HTTPStatus.NOT_IMPLEMENTED, |
| 1180 | ) |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 1181 | elif "METHODS" in reference and method not in reference["METHODS"]: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1182 | raise NbiException( |
| 1183 | "Method {} not supported for this URL".format(method), |
| 1184 | HTTPStatus.METHOD_NOT_ALLOWED, |
| 1185 | ) |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 1186 | return reference["ROLE_PERMISSION"] + method.lower() |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1187 | |
| 1188 | @staticmethod |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1189 | def _set_location_header(main_topic, version, topic, id): |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1190 | """ |
| 1191 | Insert response header Location with the URL of created item base on URL params |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1192 | :param main_topic: |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1193 | :param version: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1194 | :param topic: |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1195 | :param id: |
| 1196 | :return: None |
| 1197 | """ |
| 1198 | # Use cherrypy.request.base for absoluted path and make use of request.header HOST just in case behind aNAT |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1199 | cherrypy.response.headers["Location"] = "/osm/{}/{}/{}/{}".format( |
| 1200 | main_topic, version, topic, id |
| 1201 | ) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1202 | return |
| 1203 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1204 | @staticmethod |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 1205 | def _extract_query_string_operations(kwargs, method): |
| 1206 | """ |
| 1207 | |
| 1208 | :param kwargs: |
| 1209 | :return: |
| 1210 | """ |
| 1211 | query_string_operations = [] |
| 1212 | if kwargs: |
| 1213 | for qs in ("FORCE", "PUBLIC", "ADMIN", "SET_PROJECT"): |
| 1214 | if qs in kwargs and kwargs[qs].lower() != "false": |
| 1215 | query_string_operations.append(qs.lower() + ":" + method.lower()) |
| 1216 | return query_string_operations |
| 1217 | |
| 1218 | @staticmethod |
| 1219 | def _manage_admin_query(token_info, kwargs, method, _id): |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1220 | """ |
| 1221 | Processes the administrator query inputs (if any) of FORCE, ADMIN, PUBLIC, SET_PROJECT |
| 1222 | Check that users has rights to use them and returs the admin_query |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 1223 | :param token_info: token_info rights obtained by token |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1224 | :param kwargs: query string input. |
| 1225 | :param method: http method: GET, POSST, PUT, ... |
| 1226 | :param _id: |
| 1227 | :return: admin_query dictionary with keys: |
| 1228 | public: True, False or None |
| 1229 | force: True or False |
| 1230 | project_id: tuple with projects used for accessing an element |
| 1231 | set_project: tuple with projects that a created element will belong to |
| 1232 | method: show, list, delete, write |
| 1233 | """ |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1234 | admin_query = { |
| 1235 | "force": False, |
| 1236 | "project_id": (token_info["project_id"],), |
| 1237 | "username": token_info["username"], |
| 1238 | "admin": token_info["admin"], |
| 1239 | "public": None, |
| 1240 | "allow_show_user_project_role": token_info["allow_show_user_project_role"], |
| 1241 | } |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1242 | if kwargs: |
| 1243 | # FORCE |
| 1244 | if "FORCE" in kwargs: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1245 | if ( |
| 1246 | kwargs["FORCE"].lower() != "false" |
| 1247 | ): # if None or True set force to True |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1248 | admin_query["force"] = True |
| 1249 | del kwargs["FORCE"] |
| 1250 | # PUBLIC |
| 1251 | if "PUBLIC" in kwargs: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1252 | if ( |
| 1253 | kwargs["PUBLIC"].lower() != "false" |
| 1254 | ): # if None or True set public to True |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1255 | admin_query["public"] = True |
| 1256 | else: |
| 1257 | admin_query["public"] = False |
| 1258 | del kwargs["PUBLIC"] |
| 1259 | # ADMIN |
| 1260 | if "ADMIN" in kwargs: |
| 1261 | behave_as = kwargs.pop("ADMIN") |
| 1262 | if behave_as.lower() != "false": |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 1263 | if not token_info["admin"]: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1264 | raise NbiException( |
| 1265 | "Only admin projects can use 'ADMIN' query string", |
| 1266 | HTTPStatus.UNAUTHORIZED, |
| 1267 | ) |
| 1268 | if ( |
| 1269 | not behave_as or behave_as.lower() == "true" |
| 1270 | ): # convert True, None to empty list |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1271 | admin_query["project_id"] = () |
| 1272 | elif isinstance(behave_as, (list, tuple)): |
| 1273 | admin_query["project_id"] = behave_as |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1274 | else: # isinstance(behave_as, str) |
| 1275 | admin_query["project_id"] = (behave_as,) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1276 | if "SET_PROJECT" in kwargs: |
| 1277 | set_project = kwargs.pop("SET_PROJECT") |
| 1278 | if not set_project: |
| 1279 | admin_query["set_project"] = list(admin_query["project_id"]) |
| 1280 | else: |
| 1281 | if isinstance(set_project, str): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1282 | set_project = (set_project,) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1283 | if admin_query["project_id"]: |
| 1284 | for p in set_project: |
| 1285 | if p not in admin_query["project_id"]: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1286 | raise NbiException( |
| 1287 | "Unauthorized for 'SET_PROJECT={p}'. Try with 'ADMIN=True' or " |
| 1288 | "'ADMIN='{p}'".format(p=p), |
| 1289 | HTTPStatus.UNAUTHORIZED, |
| 1290 | ) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1291 | admin_query["set_project"] = set_project |
| 1292 | |
| 1293 | # PROJECT_READ |
| 1294 | # if "PROJECT_READ" in kwargs: |
| 1295 | # admin_query["project"] = kwargs.pop("project") |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 1296 | # if admin_query["project"] == token_info["project_id"]: |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1297 | if method == "GET": |
| 1298 | if _id: |
| 1299 | admin_query["method"] = "show" |
| 1300 | else: |
| 1301 | admin_query["method"] = "list" |
| 1302 | elif method == "DELETE": |
| 1303 | admin_query["method"] = "delete" |
| 1304 | else: |
| 1305 | admin_query["method"] = "write" |
| 1306 | return admin_query |
| 1307 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1308 | @cherrypy.expose |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1309 | def default( |
| 1310 | self, |
| 1311 | main_topic=None, |
| 1312 | version=None, |
| 1313 | topic=None, |
| 1314 | _id=None, |
| 1315 | item=None, |
| 1316 | *args, |
| 1317 | **kwargs |
| 1318 | ): |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 1319 | token_info = None |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1320 | outdata = None |
| 1321 | _format = None |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 1322 | method = "DONE" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1323 | engine_topic = None |
| tierno | db9dc58 | 2018-06-20 17:27:29 +0200 | [diff] [blame] | 1324 | rollback = [] |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 1325 | engine_session = None |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1326 | try: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1327 | if not main_topic or not version or not topic: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1328 | raise NbiException( |
| 1329 | "URL must contain at least 'main_topic/version/topic'", |
| 1330 | HTTPStatus.METHOD_NOT_ALLOWED, |
| 1331 | ) |
| 1332 | if main_topic not in ( |
| 1333 | "admin", |
| 1334 | "vnfpkgm", |
| 1335 | "nsd", |
| 1336 | "nslcm", |
| 1337 | "pdu", |
| 1338 | "nst", |
| 1339 | "nsilcm", |
| 1340 | "nspm", |
| almagia | e47b913 | 2022-05-17 14:12:22 +0200 | [diff] [blame] | 1341 | "vnflcm", |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1342 | ): |
| 1343 | raise NbiException( |
| 1344 | "URL main_topic '{}' not supported".format(main_topic), |
| 1345 | HTTPStatus.METHOD_NOT_ALLOWED, |
| 1346 | ) |
| 1347 | if version != "v1": |
| 1348 | raise NbiException( |
| 1349 | "URL version '{}' not supported".format(version), |
| 1350 | HTTPStatus.METHOD_NOT_ALLOWED, |
| 1351 | ) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1352 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1353 | if ( |
| 1354 | kwargs |
| 1355 | and "METHOD" in kwargs |
| 1356 | and kwargs["METHOD"] in ("PUT", "POST", "DELETE", "GET", "PATCH") |
| 1357 | ): |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1358 | method = kwargs.pop("METHOD") |
| 1359 | else: |
| 1360 | method = cherrypy.request.method |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1361 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1362 | role_permission = self._check_valid_url_method( |
| 1363 | method, main_topic, version, topic, _id, item, *args |
| 1364 | ) |
| 1365 | query_string_operations = self._extract_query_string_operations( |
| 1366 | kwargs, method |
| 1367 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1368 | if main_topic == "admin" and topic == "tokens": |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1369 | return self.token(method, _id, kwargs) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1370 | token_info = self.authenticator.authorize( |
| 1371 | role_permission, query_string_operations, _id |
| 1372 | ) |
| tierno | 12eac3c | 2020-03-19 23:22:08 +0000 | [diff] [blame] | 1373 | if main_topic == "admin" and topic == "domains": |
| 1374 | return self.domain() |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 1375 | engine_session = self._manage_admin_query(token_info, kwargs, method, _id) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1376 | indata = self._format_in(kwargs) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1377 | engine_topic = topic |
| preethika.p | 329b818 | 2020-04-22 12:25:39 +0530 | [diff] [blame] | 1378 | |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 1379 | if item and topic != "pm_jobs": |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1380 | engine_topic = item |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1381 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1382 | if main_topic == "nsd": |
| 1383 | engine_topic = "nsds" |
| 1384 | elif main_topic == "vnfpkgm": |
| 1385 | engine_topic = "vnfds" |
| delacruzramo | 271d200 | 2019-12-02 21:00:37 +0100 | [diff] [blame] | 1386 | if topic == "vnfpkg_op_occs": |
| 1387 | engine_topic = "vnfpkgops" |
| 1388 | if topic == "vnf_packages" and item == "action": |
| 1389 | engine_topic = "vnfpkgops" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1390 | elif main_topic == "nslcm": |
| 1391 | engine_topic = "nsrs" |
| 1392 | if topic == "ns_lcm_op_occs": |
| 1393 | engine_topic = "nslcmops" |
| 1394 | if topic == "vnfrs" or topic == "vnf_instances": |
| 1395 | engine_topic = "vnfrs" |
| almagia | e47b913 | 2022-05-17 14:12:22 +0200 | [diff] [blame] | 1396 | elif main_topic == "vnflcm": |
| 1397 | if topic == "vnf_lcm_op_occs": |
| 1398 | engine_topic = "vnflcmops" |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 1399 | elif main_topic == "nst": |
| 1400 | engine_topic = "nsts" |
| 1401 | elif main_topic == "nsilcm": |
| 1402 | engine_topic = "nsis" |
| 1403 | if topic == "nsi_lcm_op_occs": |
| delacruzramo | c061f56 | 2019-04-05 11:00:02 +0200 | [diff] [blame] | 1404 | engine_topic = "nsilcmops" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1405 | elif main_topic == "pdu": |
| 1406 | engine_topic = "pdus" |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1407 | if ( |
| 1408 | engine_topic == "vims" |
| 1409 | ): # TODO this is for backward compatibility, it will be removed in the future |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1410 | engine_topic = "vim_accounts" |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1411 | |
| preethika.p | 329b818 | 2020-04-22 12:25:39 +0530 | [diff] [blame] | 1412 | if topic == "subscriptions": |
| 1413 | engine_topic = main_topic + "_" + topic |
| 1414 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1415 | if method == "GET": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1416 | if item in ( |
| 1417 | "nsd_content", |
| 1418 | "package_content", |
| 1419 | "artifacts", |
| 1420 | "vnfd", |
| 1421 | "nsd", |
| 1422 | "nst", |
| 1423 | "nst_content", |
| 1424 | ): |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 1425 | if item in ("vnfd", "nsd", "nst"): |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1426 | path = "$DESCRIPTOR" |
| 1427 | elif args: |
| 1428 | path = args |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1429 | elif item == "artifacts": |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1430 | path = () |
| 1431 | else: |
| 1432 | path = None |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1433 | file, _format = self.engine.get_file( |
| 1434 | engine_session, |
| 1435 | engine_topic, |
| 1436 | _id, |
| 1437 | path, |
| 1438 | cherrypy.request.headers.get("Accept"), |
| 1439 | ) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1440 | outdata = file |
| 1441 | elif not _id: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1442 | outdata = self.engine.get_item_list( |
| 1443 | engine_session, engine_topic, kwargs, api_req=True |
| 1444 | ) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1445 | else: |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 1446 | if item == "reports": |
| 1447 | # TODO check that project_id (_id in this context) has permissions |
| 1448 | _id = args[0] |
| K Sai Kiran | 5758955 | 2021-01-27 21:38:34 +0530 | [diff] [blame] | 1449 | filter_q = None |
| 1450 | if "vcaStatusRefresh" in kwargs: |
| 1451 | filter_q = {"vcaStatusRefresh": kwargs["vcaStatusRefresh"]} |
| 1452 | outdata = self.engine.get_item(engine_session, engine_topic, _id, filter_q, True) |
| delacruzramo | 271d200 | 2019-12-02 21:00:37 +0100 | [diff] [blame] | 1453 | |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1454 | elif method == "POST": |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 1455 | cherrypy.response.status = HTTPStatus.CREATED.value |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1456 | if topic in ( |
| 1457 | "ns_descriptors_content", |
| 1458 | "vnf_packages_content", |
| 1459 | "netslice_templates_content", |
| 1460 | ): |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1461 | _id = cherrypy.request.headers.get("Transaction-Id") |
| 1462 | if not _id: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1463 | _id, _ = self.engine.new_item( |
| 1464 | rollback, |
| 1465 | engine_session, |
| 1466 | engine_topic, |
| 1467 | {}, |
| 1468 | None, |
| 1469 | cherrypy.request.headers, |
| 1470 | ) |
| 1471 | completed = self.engine.upload_content( |
| 1472 | engine_session, |
| 1473 | engine_topic, |
| 1474 | _id, |
| 1475 | indata, |
| 1476 | kwargs, |
| 1477 | cherrypy.request.headers, |
| 1478 | ) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1479 | if completed: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1480 | self._set_location_header(main_topic, version, topic, _id) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1481 | else: |
| 1482 | cherrypy.response.headers["Transaction-Id"] = _id |
| 1483 | outdata = {"id": _id} |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1484 | elif topic == "ns_instances_content": |
| 1485 | # creates NSR |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1486 | _id, _ = self.engine.new_item( |
| 1487 | rollback, engine_session, engine_topic, indata, kwargs |
| 1488 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1489 | # creates nslcmop |
| 1490 | indata["lcmOperationType"] = "instantiate" |
| 1491 | indata["nsInstanceId"] = _id |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1492 | nslcmop_id, _ = self.engine.new_item( |
| 1493 | rollback, engine_session, "nslcmops", indata, None |
| 1494 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1495 | self._set_location_header(main_topic, version, topic, _id) |
| kuuse | 078f55e | 2019-05-16 19:24:21 +0200 | [diff] [blame] | 1496 | outdata = {"id": _id, "nslcmop_id": nslcmop_id} |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1497 | elif topic == "ns_instances" and item: |
| 1498 | indata["lcmOperationType"] = item |
| 1499 | indata["nsInstanceId"] = _id |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1500 | _id, _ = self.engine.new_item( |
| 1501 | rollback, engine_session, "nslcmops", indata, kwargs |
| 1502 | ) |
| 1503 | self._set_location_header( |
| 1504 | main_topic, version, "ns_lcm_op_occs", _id |
| 1505 | ) |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 1506 | outdata = {"id": _id} |
| 1507 | cherrypy.response.status = HTTPStatus.ACCEPTED.value |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 1508 | elif topic == "netslice_instances_content": |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 1509 | # creates NetSlice_Instance_record (NSIR) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1510 | _id, _ = self.engine.new_item( |
| 1511 | rollback, engine_session, engine_topic, indata, kwargs |
| 1512 | ) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 1513 | self._set_location_header(main_topic, version, topic, _id) |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 1514 | indata["lcmOperationType"] = "instantiate" |
| Felipe Vicens | 126af57 | 2019-06-05 19:13:04 +0200 | [diff] [blame] | 1515 | indata["netsliceInstanceId"] = _id |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1516 | nsilcmop_id, _ = self.engine.new_item( |
| 1517 | rollback, engine_session, "nsilcmops", indata, kwargs |
| 1518 | ) |
| kuuse | 078f55e | 2019-05-16 19:24:21 +0200 | [diff] [blame] | 1519 | outdata = {"id": _id, "nsilcmop_id": nsilcmop_id} |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 1520 | elif topic == "netslice_instances" and item: |
| 1521 | indata["lcmOperationType"] = item |
| Felipe Vicens | 126af57 | 2019-06-05 19:13:04 +0200 | [diff] [blame] | 1522 | indata["netsliceInstanceId"] = _id |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1523 | _id, _ = self.engine.new_item( |
| 1524 | rollback, engine_session, "nsilcmops", indata, kwargs |
| 1525 | ) |
| 1526 | self._set_location_header( |
| 1527 | main_topic, version, "nsi_lcm_op_occs", _id |
| 1528 | ) |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 1529 | outdata = {"id": _id} |
| 1530 | cherrypy.response.status = HTTPStatus.ACCEPTED.value |
| delacruzramo | 271d200 | 2019-12-02 21:00:37 +0100 | [diff] [blame] | 1531 | elif topic == "vnf_packages" and item == "action": |
| 1532 | indata["lcmOperationType"] = item |
| 1533 | indata["vnfPkgId"] = _id |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1534 | _id, _ = self.engine.new_item( |
| 1535 | rollback, engine_session, "vnfpkgops", indata, kwargs |
| 1536 | ) |
| 1537 | self._set_location_header( |
| 1538 | main_topic, version, "vnfpkg_op_occs", _id |
| 1539 | ) |
| delacruzramo | 271d200 | 2019-12-02 21:00:37 +0100 | [diff] [blame] | 1540 | outdata = {"id": _id} |
| 1541 | cherrypy.response.status = HTTPStatus.ACCEPTED.value |
| preethika.p | 329b818 | 2020-04-22 12:25:39 +0530 | [diff] [blame] | 1542 | elif topic == "subscriptions": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1543 | _id, _ = self.engine.new_item( |
| 1544 | rollback, engine_session, engine_topic, indata, kwargs |
| 1545 | ) |
| preethika.p | 329b818 | 2020-04-22 12:25:39 +0530 | [diff] [blame] | 1546 | self._set_location_header(main_topic, version, topic, _id) |
| 1547 | link = {} |
| 1548 | link["self"] = cherrypy.response.headers["Location"] |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1549 | outdata = { |
| 1550 | "id": _id, |
| 1551 | "filter": indata["filter"], |
| 1552 | "callbackUri": indata["CallbackUri"], |
| 1553 | "_links": link, |
| 1554 | } |
| preethika.p | 329b818 | 2020-04-22 12:25:39 +0530 | [diff] [blame] | 1555 | cherrypy.response.status = HTTPStatus.CREATED.value |
| almagia | e47b913 | 2022-05-17 14:12:22 +0200 | [diff] [blame] | 1556 | elif topic == "vnf_instances" and item: |
| 1557 | indata["lcmOperationType"] = item |
| 1558 | indata["vnfInstanceId"] = _id |
| 1559 | _id, _ = self.engine.new_item(rollback, engine_session, "vnflcmops", indata, kwargs) |
| 1560 | self._set_location_header(main_topic, version, "vnf_lcm_op_occs", _id) |
| 1561 | outdata = {"id": _id} |
| 1562 | cherrypy.response.status = HTTPStatus.ACCEPTED.value |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1563 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1564 | _id, op_id = self.engine.new_item( |
| 1565 | rollback, |
| 1566 | engine_session, |
| 1567 | engine_topic, |
| 1568 | indata, |
| 1569 | kwargs, |
| 1570 | cherrypy.request.headers, |
| 1571 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1572 | self._set_location_header(main_topic, version, topic, _id) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1573 | outdata = {"id": _id} |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 1574 | if op_id: |
| 1575 | outdata["op_id"] = op_id |
| 1576 | cherrypy.response.status = HTTPStatus.ACCEPTED.value |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1577 | # TODO form NsdInfo when topic in ("ns_descriptors", "vnf_packages") |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 1578 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1579 | elif method == "DELETE": |
| 1580 | if not _id: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1581 | outdata = self.engine.del_item_list( |
| 1582 | engine_session, engine_topic, kwargs |
| 1583 | ) |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 1584 | cherrypy.response.status = HTTPStatus.OK.value |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1585 | else: # len(args) > 1 |
| tierno | 2257743 | 2020-04-08 15:16:57 +0000 | [diff] [blame] | 1586 | # for NS NSI generate an operation |
| 1587 | op_id = None |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 1588 | if topic == "ns_instances_content" and not engine_session["force"]: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1589 | nslcmop_desc = { |
| 1590 | "lcmOperationType": "terminate", |
| 1591 | "nsInstanceId": _id, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1592 | "autoremove": True, |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1593 | } |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1594 | op_id, _ = self.engine.new_item( |
| 1595 | rollback, engine_session, "nslcmops", nslcmop_desc, kwargs |
| 1596 | ) |
| tierno | 2257743 | 2020-04-08 15:16:57 +0000 | [diff] [blame] | 1597 | if op_id: |
| 1598 | outdata = {"_id": op_id} |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1599 | elif ( |
| 1600 | topic == "netslice_instances_content" |
| 1601 | and not engine_session["force"] |
| 1602 | ): |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 1603 | nsilcmop_desc = { |
| 1604 | "lcmOperationType": "terminate", |
| Felipe Vicens | 126af57 | 2019-06-05 19:13:04 +0200 | [diff] [blame] | 1605 | "netsliceInstanceId": _id, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1606 | "autoremove": True, |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 1607 | } |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1608 | op_id, _ = self.engine.new_item( |
| 1609 | rollback, engine_session, "nsilcmops", nsilcmop_desc, None |
| 1610 | ) |
| tierno | 2257743 | 2020-04-08 15:16:57 +0000 | [diff] [blame] | 1611 | if op_id: |
| 1612 | outdata = {"_id": op_id} |
| 1613 | # if there is not any deletion in process, delete |
| 1614 | if not op_id: |
| 1615 | op_id = self.engine.del_item(engine_session, engine_topic, _id) |
| 1616 | if op_id: |
| 1617 | outdata = {"op_id": op_id} |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1618 | cherrypy.response.status = ( |
| 1619 | HTTPStatus.ACCEPTED.value |
| 1620 | if op_id |
| 1621 | else HTTPStatus.NO_CONTENT.value |
| 1622 | ) |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 1623 | |
| tierno | 7ae1011 | 2018-05-18 14:36:02 +0200 | [diff] [blame] | 1624 | elif method in ("PUT", "PATCH"): |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 1625 | op_id = None |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 1626 | if not indata and not kwargs and not engine_session.get("set_project"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1627 | raise NbiException( |
| 1628 | "Nothing to update. Provide payload and/or query string", |
| 1629 | HTTPStatus.BAD_REQUEST, |
| 1630 | ) |
| 1631 | if ( |
| 1632 | item in ("nsd_content", "package_content", "nst_content") |
| 1633 | and method == "PUT" |
| 1634 | ): |
| 1635 | completed = self.engine.upload_content( |
| 1636 | engine_session, |
| 1637 | engine_topic, |
| 1638 | _id, |
| 1639 | indata, |
| 1640 | kwargs, |
| 1641 | cherrypy.request.headers, |
| 1642 | ) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1643 | if not completed: |
| 1644 | cherrypy.response.headers["Transaction-Id"] = id |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 1645 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1646 | op_id = self.engine.edit_item( |
| 1647 | engine_session, engine_topic, _id, indata, kwargs |
| 1648 | ) |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 1649 | |
| 1650 | if op_id: |
| 1651 | cherrypy.response.status = HTTPStatus.ACCEPTED.value |
| 1652 | outdata = {"op_id": op_id} |
| 1653 | else: |
| 1654 | cherrypy.response.status = HTTPStatus.NO_CONTENT.value |
| 1655 | outdata = None |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1656 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1657 | raise NbiException( |
| 1658 | "Method {} not allowed".format(method), |
| 1659 | HTTPStatus.METHOD_NOT_ALLOWED, |
| 1660 | ) |
| tierno | a6bb45d | 2019-06-14 09:45:39 +0000 | [diff] [blame] | 1661 | |
| 1662 | # if Role information changes, it is needed to reload the information of roles |
| 1663 | if topic == "roles" and method != "GET": |
| 1664 | self.authenticator.load_operation_to_allowed_roles() |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 1665 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1666 | if ( |
| 1667 | topic == "projects" |
| 1668 | and method == "DELETE" |
| 1669 | or topic in ["users", "roles"] |
| 1670 | and method in ["PUT", "PATCH", "DELETE"] |
| 1671 | ): |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 1672 | self.authenticator.remove_token_from_cache() |
| 1673 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 1674 | return self._format_out(outdata, token_info, _format) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1675 | except Exception as e: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1676 | if isinstance( |
| 1677 | e, |
| 1678 | ( |
| 1679 | NbiException, |
| 1680 | EngineException, |
| 1681 | DbException, |
| 1682 | FsException, |
| 1683 | MsgException, |
| 1684 | AuthException, |
| 1685 | ValidationError, |
| 1686 | AuthconnException, |
| 1687 | ), |
| 1688 | ): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1689 | http_code_value = cherrypy.response.status = e.http_code.value |
| 1690 | http_code_name = e.http_code.name |
| 1691 | cherrypy.log("Exception {}".format(e)) |
| 1692 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1693 | http_code_value = ( |
| 1694 | cherrypy.response.status |
| 1695 | ) = HTTPStatus.BAD_REQUEST.value # INTERNAL_SERVER_ERROR |
| Felipe Vicens | e36ab85 | 2018-11-23 14:12:09 +0100 | [diff] [blame] | 1696 | cherrypy.log("CRITICAL: Exception {}".format(e), traceback=True) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1697 | http_code_name = HTTPStatus.BAD_REQUEST.name |
| tierno | 3ace63c | 2018-05-03 17:51:43 +0200 | [diff] [blame] | 1698 | if hasattr(outdata, "close"): # is an open file |
| 1699 | outdata.close() |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1700 | error_text = str(e) |
| 1701 | rollback.reverse() |
| tierno | db9dc58 | 2018-06-20 17:27:29 +0200 | [diff] [blame] | 1702 | for rollback_item in rollback: |
| tierno | 3ace63c | 2018-05-03 17:51:43 +0200 | [diff] [blame] | 1703 | try: |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 1704 | if rollback_item.get("operation") == "set": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1705 | self.engine.db.set_one( |
| 1706 | rollback_item["topic"], |
| 1707 | {"_id": rollback_item["_id"]}, |
| 1708 | rollback_item["content"], |
| 1709 | fail_on_empty=False, |
| 1710 | ) |
| preethika.p | 329b818 | 2020-04-22 12:25:39 +0530 | [diff] [blame] | 1711 | elif rollback_item.get("operation") == "del_list": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1712 | self.engine.db.del_list( |
| 1713 | rollback_item["topic"], |
| 1714 | rollback_item["filter"], |
| 1715 | fail_on_empty=False, |
| 1716 | ) |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 1717 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1718 | self.engine.db.del_one( |
| 1719 | rollback_item["topic"], |
| 1720 | {"_id": rollback_item["_id"]}, |
| 1721 | fail_on_empty=False, |
| 1722 | ) |
| tierno | 3ace63c | 2018-05-03 17:51:43 +0200 | [diff] [blame] | 1723 | except Exception as e2: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1724 | rollback_error_text = "Rollback Exception {}: {}".format( |
| 1725 | rollback_item, e2 |
| 1726 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1727 | cherrypy.log(rollback_error_text) |
| 1728 | error_text += ". " + rollback_error_text |
| 1729 | # if isinstance(e, MsgException): |
| 1730 | # error_text = "{} has been '{}' but other modules cannot be informed because an error on bus".format( |
| 1731 | # engine_topic[:-1], method, error_text) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1732 | problem_details = { |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1733 | "code": http_code_name, |
| 1734 | "status": http_code_value, |
| 1735 | "detail": error_text, |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1736 | } |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 1737 | return self._format_out(problem_details, token_info) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1738 | # raise cherrypy.HTTPError(e.http_code.value, str(e)) |
| tierno | a503570 | 2019-07-29 08:54:42 +0000 | [diff] [blame] | 1739 | finally: |
| 1740 | if token_info: |
| 1741 | self._format_login(token_info) |
| 1742 | if method in ("PUT", "PATCH", "POST") and isinstance(outdata, dict): |
| 1743 | for logging_id in ("id", "op_id", "nsilcmop_id", "nslcmop_id"): |
| 1744 | if outdata.get(logging_id): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1745 | cherrypy.request.login += ";{}={}".format( |
| 1746 | logging_id, outdata[logging_id][:36] |
| 1747 | ) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1748 | |
| 1749 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1750 | def _start_service(): |
| 1751 | """ |
| 1752 | Callback function called when cherrypy.engine starts |
| 1753 | Override configuration with env variables |
| 1754 | Set database, storage, message configuration |
| 1755 | Init database with admin/admin user password |
| 1756 | """ |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 1757 | global nbi_server |
| 1758 | global subscription_thread |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1759 | cherrypy.log.error("Starting osm_nbi") |
| 1760 | # update general cherrypy configuration |
| 1761 | update_dict = {} |
| 1762 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1763 | engine_config = cherrypy.tree.apps["/osm"].config |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1764 | for k, v in environ.items(): |
| 1765 | if not k.startswith("OSMNBI_"): |
| 1766 | continue |
| tierno | e128118 | 2018-05-22 12:24:36 +0200 | [diff] [blame] | 1767 | k1, _, k2 = k[7:].lower().partition("_") |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1768 | if not k2: |
| 1769 | continue |
| 1770 | try: |
| 1771 | # update static configuration |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1772 | if k == "OSMNBI_STATIC_DIR": |
| 1773 | engine_config["/static"]["tools.staticdir.dir"] = v |
| 1774 | engine_config["/static"]["tools.staticdir.on"] = True |
| 1775 | elif k == "OSMNBI_SOCKET_PORT" or k == "OSMNBI_SERVER_PORT": |
| 1776 | update_dict["server.socket_port"] = int(v) |
| 1777 | elif k == "OSMNBI_SOCKET_HOST" or k == "OSMNBI_SERVER_HOST": |
| 1778 | update_dict["server.socket_host"] = v |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 1779 | elif k1 in ("server", "test", "auth", "log"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1780 | update_dict[k1 + "." + k2] = v |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 1781 | elif k1 in ("message", "database", "storage", "authentication"): |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 1782 | # k2 = k2.replace('_', '.') |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 1783 | if k2 in ("port", "db_port"): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1784 | engine_config[k1][k2] = int(v) |
| 1785 | else: |
| 1786 | engine_config[k1][k2] = v |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 1787 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1788 | except ValueError as e: |
| 1789 | cherrypy.log.error("Ignoring environ '{}': " + str(e)) |
| 1790 | except Exception as e: |
| 1791 | cherrypy.log.warn("skipping environ '{}' on exception '{}'".format(k, e)) |
| 1792 | |
| 1793 | if update_dict: |
| 1794 | cherrypy.config.update(update_dict) |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 1795 | engine_config["global"].update(update_dict) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1796 | |
| 1797 | # logging cherrypy |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1798 | log_format_simple = ( |
| 1799 | "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s" |
| 1800 | ) |
| 1801 | log_formatter_simple = logging.Formatter( |
| 1802 | log_format_simple, datefmt="%Y-%m-%dT%H:%M:%S" |
| 1803 | ) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1804 | logger_server = logging.getLogger("cherrypy.error") |
| 1805 | logger_access = logging.getLogger("cherrypy.access") |
| 1806 | logger_cherry = logging.getLogger("cherrypy") |
| 1807 | logger_nbi = logging.getLogger("nbi") |
| 1808 | |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 1809 | if "log.file" in engine_config["global"]: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1810 | file_handler = logging.handlers.RotatingFileHandler( |
| 1811 | engine_config["global"]["log.file"], maxBytes=100e6, backupCount=9, delay=0 |
| 1812 | ) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1813 | file_handler.setFormatter(log_formatter_simple) |
| 1814 | logger_cherry.addHandler(file_handler) |
| 1815 | logger_nbi.addHandler(file_handler) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1816 | # log always to standard output |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1817 | for format_, logger in { |
| 1818 | "nbi.server %(filename)s:%(lineno)s": logger_server, |
| 1819 | "nbi.access %(filename)s:%(lineno)s": logger_access, |
| 1820 | "%(name)s %(filename)s:%(lineno)s": logger_nbi, |
| 1821 | }.items(): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1822 | log_format_cherry = "%(asctime)s %(levelname)s {} %(message)s".format(format_) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1823 | log_formatter_cherry = logging.Formatter( |
| 1824 | log_format_cherry, datefmt="%Y-%m-%dT%H:%M:%S" |
| 1825 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1826 | str_handler = logging.StreamHandler() |
| 1827 | str_handler.setFormatter(log_formatter_cherry) |
| 1828 | logger.addHandler(str_handler) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1829 | |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 1830 | if engine_config["global"].get("log.level"): |
| 1831 | logger_cherry.setLevel(engine_config["global"]["log.level"]) |
| 1832 | logger_nbi.setLevel(engine_config["global"]["log.level"]) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1833 | |
| 1834 | # logging other modules |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1835 | for k1, logname in { |
| 1836 | "message": "nbi.msg", |
| 1837 | "database": "nbi.db", |
| 1838 | "storage": "nbi.fs", |
| 1839 | }.items(): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1840 | engine_config[k1]["logger_name"] = logname |
| 1841 | logger_module = logging.getLogger(logname) |
| 1842 | if "logfile" in engine_config[k1]: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1843 | file_handler = logging.handlers.RotatingFileHandler( |
| 1844 | engine_config[k1]["logfile"], maxBytes=100e6, backupCount=9, delay=0 |
| 1845 | ) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1846 | file_handler.setFormatter(log_formatter_simple) |
| 1847 | logger_module.addHandler(file_handler) |
| 1848 | if "loglevel" in engine_config[k1]: |
| 1849 | logger_module.setLevel(engine_config[k1]["loglevel"]) |
| 1850 | # TODO add more entries, e.g.: storage |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1851 | cherrypy.tree.apps["/osm"].root.engine.start(engine_config) |
| 1852 | cherrypy.tree.apps["/osm"].root.authenticator.start(engine_config) |
| 1853 | cherrypy.tree.apps["/osm"].root.engine.init_db(target_version=database_version) |
| 1854 | cherrypy.tree.apps["/osm"].root.authenticator.init_db( |
| 1855 | target_version=auth_database_version |
| 1856 | ) |
| tierno | bee508e | 2019-01-21 11:21:49 +0000 | [diff] [blame] | 1857 | |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 1858 | # start subscriptions thread: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1859 | subscription_thread = SubscriptionThread( |
| 1860 | config=engine_config, engine=nbi_server.engine |
| 1861 | ) |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 1862 | subscription_thread.start() |
| 1863 | # Do not capture except SubscriptionException |
| 1864 | |
| tierno | b2e48bd | 2020-02-04 15:47:18 +0000 | [diff] [blame] | 1865 | backend = engine_config["authentication"]["backend"] |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1866 | cherrypy.log.error( |
| 1867 | "Starting OSM NBI Version '{} {}' with '{}' authentication backend".format( |
| 1868 | nbi_version, nbi_version_date, backend |
| 1869 | ) |
| 1870 | ) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1871 | |
| 1872 | |
| 1873 | def _stop_service(): |
| 1874 | """ |
| 1875 | Callback function called when cherrypy.engine stops |
| 1876 | TODO: Ending database connections. |
| 1877 | """ |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 1878 | global subscription_thread |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1879 | if subscription_thread: |
| 1880 | subscription_thread.terminate() |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 1881 | subscription_thread = None |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1882 | cherrypy.tree.apps["/osm"].root.engine.stop() |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1883 | cherrypy.log.error("Stopping osm_nbi") |
| 1884 | |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 1885 | |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 1886 | def nbi(config_file): |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 1887 | global nbi_server |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1888 | # conf = { |
| 1889 | # '/': { |
| 1890 | # #'request.dispatch': cherrypy.dispatch.MethodDispatcher(), |
| 1891 | # 'tools.sessions.on': True, |
| 1892 | # 'tools.response_headers.on': True, |
| 1893 | # # 'tools.response_headers.headers': [('Content-Type', 'text/plain')], |
| 1894 | # } |
| 1895 | # } |
| 1896 | # cherrypy.Server.ssl_module = 'builtin' |
| 1897 | # cherrypy.Server.ssl_certificate = "http/cert.pem" |
| 1898 | # cherrypy.Server.ssl_private_key = "http/privkey.pem" |
| 1899 | # cherrypy.Server.thread_pool = 10 |
| 1900 | # cherrypy.config.update({'Server.socket_port': config["port"], 'Server.socket_host': config["host"]}) |
| 1901 | |
| 1902 | # cherrypy.config.update({'tools.auth_basic.on': True, |
| 1903 | # 'tools.auth_basic.realm': 'localhost', |
| 1904 | # 'tools.auth_basic.checkpassword': validate_password}) |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 1905 | nbi_server = Server() |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1906 | cherrypy.engine.subscribe("start", _start_service) |
| 1907 | cherrypy.engine.subscribe("stop", _stop_service) |
| 1908 | cherrypy.quickstart(nbi_server, "/osm", config_file) |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 1909 | |
| 1910 | |
| 1911 | def usage(): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1912 | print( |
| 1913 | """Usage: {} [options] |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 1914 | -c|--config [configuration_file]: loads the configuration file (default: ./nbi.cfg) |
| 1915 | -h|--help: shows this help |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1916 | """.format( |
| 1917 | sys.argv[0] |
| 1918 | ) |
| 1919 | ) |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 1920 | # --log-socket-host HOST: send logs to this host") |
| 1921 | # --log-socket-port PORT: send logs using this port (default: 9022)") |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1922 | |
| 1923 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1924 | if __name__ == "__main__": |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 1925 | try: |
| 1926 | # load parameters and configuration |
| 1927 | opts, args = getopt.getopt(sys.argv[1:], "hvc:", ["config=", "help"]) |
| 1928 | # TODO add "log-socket-host=", "log-socket-port=", "log-file=" |
| 1929 | config_file = None |
| 1930 | for o, a in opts: |
| 1931 | if o in ("-h", "--help"): |
| 1932 | usage() |
| 1933 | sys.exit() |
| 1934 | elif o in ("-c", "--config"): |
| 1935 | config_file = a |
| 1936 | # elif o == "--log-socket-port": |
| 1937 | # log_socket_port = a |
| 1938 | # elif o == "--log-socket-host": |
| 1939 | # log_socket_host = a |
| 1940 | # elif o == "--log-file": |
| 1941 | # log_file = a |
| 1942 | else: |
| 1943 | assert False, "Unhandled option" |
| 1944 | if config_file: |
| 1945 | if not path.isfile(config_file): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1946 | print( |
| 1947 | "configuration file '{}' that not exist".format(config_file), |
| 1948 | file=sys.stderr, |
| 1949 | ) |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 1950 | exit(1) |
| 1951 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1952 | for config_file in ( |
| 1953 | __file__[: __file__.rfind(".")] + ".cfg", |
| 1954 | "./nbi.cfg", |
| 1955 | "/etc/osm/nbi.cfg", |
| 1956 | ): |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 1957 | if path.isfile(config_file): |
| 1958 | break |
| 1959 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1960 | print( |
| 1961 | "No configuration file 'nbi.cfg' found neither at local folder nor at /etc/osm/", |
| 1962 | file=sys.stderr, |
| 1963 | ) |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 1964 | exit(1) |
| 1965 | nbi(config_file) |
| 1966 | except getopt.GetoptError as e: |
| 1967 | print(str(e), file=sys.stderr) |
| 1968 | # usage() |
| 1969 | exit(1) |