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