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