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