| 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 |
| 21 | import html_out as html |
| 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 | |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 27 | from authconn import AuthException |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 28 | from auth import Authenticator |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 29 | from engine import Engine, EngineException |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 30 | from validation import ValidationError |
| tierno | a8d6363 | 2018-05-10 13:12:32 +0200 | [diff] [blame] | 31 | from osm_common.dbbase import DbException |
| 32 | from osm_common.fsbase import FsException |
| 33 | from osm_common.msgbase import MsgException |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 34 | from http import HTTPStatus |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 35 | from codecs import getreader |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 36 | from os import environ, path |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 37 | |
| 38 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| tierno | dfe0957 | 2018-04-24 10:41:10 +0200 | [diff] [blame] | 39 | |
| 40 | # TODO consider to remove and provide version using the static version file |
| 41 | __version__ = "0.1.3" |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 42 | version_date = "Apr 2018" |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 43 | database_version = '1.0' |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 44 | auth_database_version = '1.0' |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 45 | |
| 46 | """ |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 47 | 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] | 48 | URL: /osm GET POST PUT DELETE PATCH |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 49 | /nsd/v1 |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 50 | /ns_descriptors_content O O |
| 51 | /<nsdInfoId> O O O O |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 52 | /ns_descriptors O5 O5 |
| 53 | /<nsdInfoId> O5 O5 5 |
| 54 | /nsd_content O5 O5 |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 55 | /nsd O |
| 56 | /artifacts[/<artifactPath>] O |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 57 | /pnf_descriptors 5 5 |
| 58 | /<pnfdInfoId> 5 5 5 |
| 59 | /pnfd_content 5 5 |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 60 | /subscriptions 5 5 |
| 61 | /<subscriptionId> 5 X |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 62 | |
| 63 | /vnfpkgm/v1 |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 64 | /vnf_packages_content O O |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 65 | /<vnfPkgId> O O |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 66 | /vnf_packages O5 O5 |
| 67 | /<vnfPkgId> O5 O5 5 |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 68 | /package_content O5 O5 |
| 69 | /upload_from_uri X |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 70 | /vnfd O5 |
| 71 | /artifacts[/<artifactPath>] O5 |
| 72 | /subscriptions X X |
| 73 | /<subscriptionId> X X |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 74 | |
| 75 | /nslcm/v1 |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 76 | /ns_instances_content O O |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 77 | /<nsInstanceId> O O |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 78 | /ns_instances 5 5 |
| tierno | 9569244 | 2018-05-24 18:05:28 +0200 | [diff] [blame] | 79 | /<nsInstanceId> O5 O5 |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 80 | instantiate O5 |
| 81 | terminate O5 |
| 82 | action O |
| 83 | scale O5 |
| 84 | heal 5 |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 85 | /ns_lcm_op_occs 5 5 |
| 86 | /<nsLcmOpOccId> 5 5 5 |
| 87 | TO BE COMPLETED 5 5 |
| tierno | f759d82 | 2018-06-11 18:54:54 +0200 | [diff] [blame] | 88 | /vnf_instances (also vnfrs for compatibility) O |
| 89 | /<vnfInstanceId> O |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 90 | /subscriptions 5 5 |
| 91 | /<subscriptionId> 5 X |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 92 | |
| tierno | cb83c94 | 2018-09-24 17:28:13 +0200 | [diff] [blame] | 93 | /pdu/v1 |
| 94 | /pdu_descriptor O O |
| 95 | /<id> O O O O |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 96 | |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 97 | /admin/v1 |
| 98 | /tokens O O |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 99 | /<id> O O |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 100 | /users O O |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 101 | /<id> O O O O |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 102 | /projects O O |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 103 | /<id> O O |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 104 | /vims_accounts (also vims for compatibility) O O |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 105 | /<id> O O O |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 106 | /sdns O O |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 107 | /<id> O O O |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 108 | |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 109 | /nst/v1 O O |
| 110 | /netslice_templates_content O O |
| 111 | /<nstInfoId> O O O O |
| 112 | /netslice_templates O O |
| 113 | /<nstInfoId> O O O |
| 114 | /nst_content O O |
| 115 | /nst O |
| 116 | /artifacts[/<artifactPath>] O |
| 117 | /subscriptions X X |
| 118 | /<subscriptionId> X X |
| 119 | |
| 120 | /nsilcm/v1 |
| 121 | /netslice_instances_content O O |
| 122 | /<SliceInstanceId> O O |
| 123 | /netslice_instances O O |
| 124 | /<SliceInstanceId> O O |
| 125 | instantiate O |
| 126 | terminate O |
| 127 | action O |
| 128 | /nsi_lcm_op_occs O O |
| 129 | /<nsiLcmOpOccId> O O O |
| 130 | /subscriptions X X |
| 131 | /<subscriptionId> X X |
| 132 | |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 133 | query string: |
| 134 | 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] | 135 | simpleFilterExpr := <attrName>["."<attrName>]*["."<op>]"="<value>[","<value>]* |
| 136 | filterExpr := <simpleFilterExpr>["&"<simpleFilterExpr>]* |
| 137 | op := "eq" | "neq" (or "ne") | "gt" | "lt" | "gte" | "lte" | "cont" | "ncont" |
| 138 | attrName := string |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 139 | For filtering inside array, it must select the element of the array, or add ANYINDEX to apply the filtering over any |
| 140 | item of the array, that is, pass if any item of the array pass the filter. |
| 141 | It allows both ne and neq for not equal |
| 142 | TODO: 4.3.3 Attribute selectors |
| 143 | all_fields, fields=x,y,.., exclude_default, exclude_fields=x,y,... |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 144 | (none) … same as “exclude_default” |
| 145 | all_fields … all attributes. |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 146 | fields=<list> … all attributes except all complex attributes with minimum cardinality of zero that are not |
| 147 | conditionally mandatory, and that are not provided in <list>. |
| 148 | exclude_fields=<list> … all attributes except those complex attributes with a minimum cardinality of zero that |
| 149 | are not conditionally mandatory, and that are provided in <list>. |
| 150 | exclude_default … all attributes except those complex attributes with a minimum cardinality of zero that are not |
| 151 | conditionally mandatory, and that are part of the "default exclude set" defined in the present specification for |
| 152 | the particular resource |
| 153 | exclude_default and include=<list> … all attributes except those complex attributes with a minimum cardinality |
| 154 | of zero that are not conditionally mandatory and that are part of the "default exclude set" defined in the |
| 155 | present specification for the particular resource, but that are not part of <list> |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 156 | Header field name Reference Example Descriptions |
| 157 | Accept IETF RFC 7231 [19] application/json Content-Types that are acceptable for the response. |
| 158 | This header field shall be present if the response is expected to have a non-empty message body. |
| 159 | Content-Type IETF RFC 7231 [19] application/json The MIME type of the body of the request. |
| 160 | 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] | 161 | Authorization IETF RFC 7235 [22] Bearer mF_9.B5f-4.1JqM The authorization token for the request. |
| 162 | Details are specified in clause 4.5.3. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 163 | Range IETF RFC 7233 [21] 1000-2000 Requested range of bytes from a file |
| 164 | Header field name Reference Example Descriptions |
| 165 | Content-Type IETF RFC 7231 [19] application/json The MIME type of the body of the response. |
| 166 | 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] | 167 | Location IETF RFC 7231 [19] http://www.example.com/vnflcm/v1/vnf_instances/123 Used in redirection, or when a |
| 168 | new resource has been created. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 169 | 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] | 170 | In the present document this header field is also used if the response status code is 202 and a new resource was |
| 171 | created. |
| 172 | WWW-Authenticate IETF RFC 7235 [22] Bearer realm="example" Challenge if the corresponding HTTP request has not |
| 173 | provided authorization, or error details if the corresponding HTTP request has provided an invalid authorization |
| 174 | token. |
| 175 | Accept-Ranges IETF RFC 7233 [21] bytes Used by the Server to signal whether or not it supports ranges for |
| 176 | certain resources. |
| 177 | Content-Range IETF RFC 7233 [21] bytes 21010-47021/ 47022 Signals the byte range that is contained in the |
| 178 | response, and the total length of the file. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 179 | 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] | 180 | """ |
| 181 | |
| 182 | |
| 183 | class NbiException(Exception): |
| 184 | |
| 185 | def __init__(self, message, http_code=HTTPStatus.METHOD_NOT_ALLOWED): |
| 186 | Exception.__init__(self, message) |
| 187 | self.http_code = http_code |
| 188 | |
| 189 | |
| 190 | class Server(object): |
| 191 | instance = 0 |
| 192 | # to decode bytes to str |
| 193 | reader = getreader("utf-8") |
| 194 | |
| 195 | def __init__(self): |
| 196 | self.instance += 1 |
| 197 | self.engine = Engine() |
| Eduardo Sousa | d1b525d | 2018-10-04 04:24:18 +0100 | [diff] [blame] | 198 | self.authenticator = Authenticator() |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 199 | self.valid_methods = { # contains allowed URL and methods |
| 200 | "admin": { |
| 201 | "v1": { |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 202 | "tokens": {"METHODS": ("GET", "POST", "DELETE"), |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 203 | "<ID>": {"METHODS": ("GET", "DELETE")} |
| 204 | }, |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 205 | "users": {"METHODS": ("GET", "POST"), |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 206 | "<ID>": {"METHODS": ("GET", "POST", "DELETE", "PATCH", "PUT")} |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 207 | }, |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 208 | "projects": {"METHODS": ("GET", "POST"), |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 209 | "<ID>": {"METHODS": ("GET", "DELETE")} |
| 210 | }, |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 211 | "vims": {"METHODS": ("GET", "POST"), |
| tierno | 7ae1011 | 2018-05-18 14:36:02 +0200 | [diff] [blame] | 212 | "<ID>": {"METHODS": ("GET", "DELETE", "PATCH", "PUT")} |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 213 | }, |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 214 | "vim_accounts": {"METHODS": ("GET", "POST"), |
| tierno | 7ae1011 | 2018-05-18 14:36:02 +0200 | [diff] [blame] | 215 | "<ID>": {"METHODS": ("GET", "DELETE", "PATCH", "PUT")} |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 216 | }, |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 217 | "sdns": {"METHODS": ("GET", "POST"), |
| tierno | 7ae1011 | 2018-05-18 14:36:02 +0200 | [diff] [blame] | 218 | "<ID>": {"METHODS": ("GET", "DELETE", "PATCH", "PUT")} |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 219 | }, |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 220 | } |
| 221 | }, |
| tierno | cb83c94 | 2018-09-24 17:28:13 +0200 | [diff] [blame] | 222 | "pdu": { |
| 223 | "v1": { |
| 224 | "pdu_descriptors": {"METHODS": ("GET", "POST"), |
| 225 | "<ID>": {"METHODS": ("GET", "POST", "DELETE", "PATCH", "PUT")} |
| 226 | }, |
| 227 | } |
| 228 | }, |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 229 | "nsd": { |
| 230 | "v1": { |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 231 | "ns_descriptors_content": {"METHODS": ("GET", "POST"), |
| 232 | "<ID>": {"METHODS": ("GET", "PUT", "DELETE")} |
| 233 | }, |
| 234 | "ns_descriptors": {"METHODS": ("GET", "POST"), |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 235 | "<ID>": {"METHODS": ("GET", "DELETE", "PATCH"), |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 236 | "nsd_content": {"METHODS": ("GET", "PUT")}, |
| 237 | "nsd": {"METHODS": "GET"}, # descriptor inside package |
| 238 | "artifacts": {"*": {"METHODS": "GET"}} |
| 239 | } |
| 240 | }, |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 241 | "pnf_descriptors": {"TODO": ("GET", "POST"), |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 242 | "<ID>": {"TODO": ("GET", "DELETE", "PATCH"), |
| 243 | "pnfd_content": {"TODO": ("GET", "PUT")} |
| 244 | } |
| 245 | }, |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 246 | "subscriptions": {"TODO": ("GET", "POST"), |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 247 | "<ID>": {"TODO": ("GET", "DELETE")} |
| 248 | }, |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 249 | } |
| 250 | }, |
| 251 | "vnfpkgm": { |
| 252 | "v1": { |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 253 | "vnf_packages_content": {"METHODS": ("GET", "POST"), |
| 254 | "<ID>": {"METHODS": ("GET", "PUT", "DELETE")} |
| 255 | }, |
| 256 | "vnf_packages": {"METHODS": ("GET", "POST"), |
| tierno | aae4dc4 | 2018-06-11 18:55:50 +0200 | [diff] [blame] | 257 | "<ID>": {"METHODS": ("GET", "DELETE", "PATCH"), # GET: vnfPkgInfo |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 258 | "package_content": {"METHODS": ("GET", "PUT"), # package |
| 259 | "upload_from_uri": {"TODO": "POST"} |
| 260 | }, |
| 261 | "vnfd": {"METHODS": "GET"}, # descriptor inside package |
| 262 | "artifacts": {"*": {"METHODS": "GET"}} |
| 263 | } |
| 264 | }, |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 265 | "subscriptions": {"TODO": ("GET", "POST"), |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 266 | "<ID>": {"TODO": ("GET", "DELETE")} |
| 267 | }, |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 268 | } |
| 269 | }, |
| 270 | "nslcm": { |
| 271 | "v1": { |
| 272 | "ns_instances_content": {"METHODS": ("GET", "POST"), |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 273 | "<ID>": {"METHODS": ("GET", "DELETE")} |
| 274 | }, |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 275 | "ns_instances": {"METHODS": ("GET", "POST"), |
| tierno | 9569244 | 2018-05-24 18:05:28 +0200 | [diff] [blame] | 276 | "<ID>": {"METHODS": ("GET", "DELETE"), |
| tierno | f759d82 | 2018-06-11 18:54:54 +0200 | [diff] [blame] | 277 | "scale": {"METHODS": "POST"}, |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 278 | "terminate": {"METHODS": "POST"}, |
| 279 | "instantiate": {"METHODS": "POST"}, |
| 280 | "action": {"METHODS": "POST"}, |
| 281 | } |
| 282 | }, |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 283 | "ns_lcm_op_occs": {"METHODS": "GET", |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 284 | "<ID>": {"METHODS": "GET"}, |
| 285 | }, |
| tierno | 0ffaa99 | 2018-05-09 13:21:56 +0200 | [diff] [blame] | 286 | "vnfrs": {"METHODS": ("GET"), |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 287 | "<ID>": {"METHODS": ("GET")} |
| 288 | }, |
| tierno | f759d82 | 2018-06-11 18:54:54 +0200 | [diff] [blame] | 289 | "vnf_instances": {"METHODS": ("GET"), |
| 290 | "<ID>": {"METHODS": ("GET")} |
| 291 | }, |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 292 | } |
| 293 | }, |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 294 | "nst": { |
| 295 | "v1": { |
| 296 | "netslice_templates_content": {"METHODS": ("GET", "POST"), |
| 297 | "<ID>": {"METHODS": ("GET", "PUT", "DELETE")} |
| 298 | }, |
| 299 | "netslice_templates": {"METHODS": ("GET", "POST"), |
| 300 | "<ID>": {"METHODS": ("GET", "DELETE"), "TODO": "PATCH", |
| 301 | "nst_content": {"METHODS": ("GET", "PUT")}, |
| 302 | "nst": {"METHODS": "GET"}, # descriptor inside package |
| 303 | "artifacts": {"*": {"METHODS": "GET"}} |
| 304 | } |
| 305 | }, |
| 306 | "subscriptions": {"TODO": ("GET", "POST"), |
| 307 | "<ID>": {"TODO": ("GET", "DELETE")} |
| 308 | }, |
| 309 | } |
| 310 | }, |
| 311 | "nsilcm": { |
| 312 | "v1": { |
| 313 | "netslice_instances_content": {"METHODS": ("GET", "POST"), |
| 314 | "<ID>": {"METHODS": ("GET", "DELETE")} |
| 315 | }, |
| 316 | "netslice_instances": {"METHODS": ("GET", "POST"), |
| 317 | "<ID>": {"METHODS": ("GET", "DELETE"), |
| 318 | "terminate": {"METHODS": "POST"}, |
| 319 | "instantiate": {"METHODS": "POST"}, |
| 320 | "action": {"METHODS": "POST"}, |
| 321 | } |
| 322 | }, |
| 323 | "nsi_lcm_op_occs": {"METHODS": "GET", |
| 324 | "<ID>": {"METHODS": "GET"}, |
| 325 | }, |
| 326 | } |
| 327 | }, |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 328 | } |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 329 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 330 | def _format_in(self, kwargs): |
| 331 | try: |
| 332 | indata = None |
| 333 | if cherrypy.request.body.length: |
| 334 | error_text = "Invalid input format " |
| 335 | |
| 336 | if "Content-Type" in cherrypy.request.headers: |
| 337 | if "application/json" in cherrypy.request.headers["Content-Type"]: |
| 338 | error_text = "Invalid json format " |
| 339 | indata = json.load(self.reader(cherrypy.request.body)) |
| gcalvino | de4adfe | 2018-10-30 11:46:09 +0100 | [diff] [blame] | 340 | cherrypy.request.headers.pop("Content-File-MD5", None) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 341 | elif "application/yaml" in cherrypy.request.headers["Content-Type"]: |
| 342 | error_text = "Invalid yaml format " |
| 343 | indata = yaml.load(cherrypy.request.body) |
| gcalvino | de4adfe | 2018-10-30 11:46:09 +0100 | [diff] [blame] | 344 | cherrypy.request.headers.pop("Content-File-MD5", None) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 345 | elif "application/binary" in cherrypy.request.headers["Content-Type"] or \ |
| 346 | "application/gzip" in cherrypy.request.headers["Content-Type"] or \ |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 347 | "application/zip" in cherrypy.request.headers["Content-Type"] or \ |
| 348 | "text/plain" in cherrypy.request.headers["Content-Type"]: |
| 349 | indata = cherrypy.request.body # .read() |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 350 | elif "multipart/form-data" in cherrypy.request.headers["Content-Type"]: |
| 351 | if "descriptor_file" in kwargs: |
| 352 | filecontent = kwargs.pop("descriptor_file") |
| 353 | if not filecontent.file: |
| 354 | raise NbiException("empty file or content", HTTPStatus.BAD_REQUEST) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 355 | indata = filecontent.file # .read() |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 356 | if filecontent.content_type.value: |
| 357 | cherrypy.request.headers["Content-Type"] = filecontent.content_type.value |
| 358 | else: |
| 359 | # raise cherrypy.HTTPError(HTTPStatus.Not_Acceptable, |
| 360 | # "Only 'Content-Type' of type 'application/json' or |
| 361 | # 'application/yaml' for input format are available") |
| 362 | error_text = "Invalid yaml format " |
| 363 | indata = yaml.load(cherrypy.request.body) |
| gcalvino | de4adfe | 2018-10-30 11:46:09 +0100 | [diff] [blame] | 364 | cherrypy.request.headers.pop("Content-File-MD5", None) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 365 | else: |
| 366 | error_text = "Invalid yaml format " |
| 367 | indata = yaml.load(cherrypy.request.body) |
| gcalvino | de4adfe | 2018-10-30 11:46:09 +0100 | [diff] [blame] | 368 | cherrypy.request.headers.pop("Content-File-MD5", None) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 369 | if not indata: |
| 370 | indata = {} |
| 371 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 372 | format_yaml = False |
| 373 | if cherrypy.request.headers.get("Query-String-Format") == "yaml": |
| 374 | format_yaml = True |
| 375 | |
| 376 | for k, v in kwargs.items(): |
| 377 | if isinstance(v, str): |
| 378 | if v == "": |
| 379 | kwargs[k] = None |
| 380 | elif format_yaml: |
| 381 | try: |
| 382 | kwargs[k] = yaml.load(v) |
| tierno | e128118 | 2018-05-22 12:24:36 +0200 | [diff] [blame] | 383 | except Exception: |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 384 | pass |
| 385 | elif k.endswith(".gt") or k.endswith(".lt") or k.endswith(".gte") or k.endswith(".lte"): |
| 386 | try: |
| 387 | kwargs[k] = int(v) |
| tierno | e128118 | 2018-05-22 12:24:36 +0200 | [diff] [blame] | 388 | except Exception: |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 389 | try: |
| 390 | kwargs[k] = float(v) |
| tierno | e128118 | 2018-05-22 12:24:36 +0200 | [diff] [blame] | 391 | except Exception: |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 392 | pass |
| 393 | elif v.find(",") > 0: |
| 394 | kwargs[k] = v.split(",") |
| 395 | elif isinstance(v, (list, tuple)): |
| 396 | for index in range(0, len(v)): |
| 397 | if v[index] == "": |
| 398 | v[index] = None |
| 399 | elif format_yaml: |
| 400 | try: |
| 401 | v[index] = yaml.load(v[index]) |
| tierno | e128118 | 2018-05-22 12:24:36 +0200 | [diff] [blame] | 402 | except Exception: |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 403 | pass |
| 404 | |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 405 | return indata |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 406 | except (ValueError, yaml.YAMLError) as exc: |
| 407 | raise NbiException(error_text + str(exc), HTTPStatus.BAD_REQUEST) |
| 408 | except KeyError as exc: |
| 409 | raise NbiException("Query string error: " + str(exc), HTTPStatus.BAD_REQUEST) |
| tierno | b92094f | 2018-05-11 13:44:22 +0200 | [diff] [blame] | 410 | except Exception as exc: |
| 411 | raise NbiException(error_text + str(exc), HTTPStatus.BAD_REQUEST) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 412 | |
| 413 | @staticmethod |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 414 | def _format_out(data, session=None, _format=None): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 415 | """ |
| 416 | 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] | 417 | :param data: response to be sent. Can be a dict, text or file |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 418 | :param session: |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 419 | :param _format: The format to be set as Content-Type ir data is a file |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 420 | :return: None |
| 421 | """ |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 422 | accept = cherrypy.request.headers.get("Accept") |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 423 | if data is None: |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 424 | if accept and "text/html" in accept: |
| 425 | return html.format(data, cherrypy.request, cherrypy.response, session) |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 426 | # cherrypy.response.status = HTTPStatus.NO_CONTENT.value |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 427 | return |
| 428 | elif hasattr(data, "read"): # file object |
| 429 | if _format: |
| 430 | cherrypy.response.headers["Content-Type"] = _format |
| 431 | elif "b" in data.mode: # binariy asssumig zip |
| 432 | cherrypy.response.headers["Content-Type"] = 'application/zip' |
| 433 | else: |
| 434 | cherrypy.response.headers["Content-Type"] = 'text/plain' |
| 435 | # TODO check that cherrypy close file. If not implement pending things to close per thread next |
| 436 | return data |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 437 | if accept: |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 438 | if "application/json" in accept: |
| 439 | cherrypy.response.headers["Content-Type"] = 'application/json; charset=utf-8' |
| 440 | a = json.dumps(data, indent=4) + "\n" |
| 441 | return a.encode("utf8") |
| 442 | elif "text/html" in accept: |
| 443 | return html.format(data, cherrypy.request, cherrypy.response, session) |
| 444 | |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 445 | elif "application/yaml" in accept or "*/*" in accept or "text/plain" in accept: |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 446 | pass |
| 447 | else: |
| 448 | raise cherrypy.HTTPError(HTTPStatus.NOT_ACCEPTABLE.value, |
| 449 | "Only 'Accept' of type 'application/json' or 'application/yaml' " |
| 450 | "for output format are available") |
| 451 | cherrypy.response.headers["Content-Type"] = 'application/yaml' |
| 452 | return yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False, tags=False, |
| 453 | encoding='utf-8', allow_unicode=True) # , canonical=True, default_style='"' |
| 454 | |
| 455 | @cherrypy.expose |
| 456 | def index(self, *args, **kwargs): |
| 457 | session = None |
| 458 | try: |
| 459 | if cherrypy.request.method == "GET": |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 460 | session = self.authenticator.authorize() |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 461 | outdata = "Index page" |
| 462 | else: |
| 463 | raise cherrypy.HTTPError(HTTPStatus.METHOD_NOT_ALLOWED.value, |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 464 | "Method {} not allowed for tokens".format(cherrypy.request.method)) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 465 | |
| 466 | return self._format_out(outdata, session) |
| 467 | |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 468 | except (EngineException, AuthException) as e: |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 469 | cherrypy.log("index Exception {}".format(e)) |
| 470 | cherrypy.response.status = e.http_code.value |
| 471 | return self._format_out("Welcome to OSM!", session) |
| 472 | |
| 473 | @cherrypy.expose |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 474 | def version(self, *args, **kwargs): |
| tierno | dfe0957 | 2018-04-24 10:41:10 +0200 | [diff] [blame] | 475 | # TODO consider to remove and provide version using the static version file |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 476 | global __version__, version_date |
| 477 | try: |
| 478 | if cherrypy.request.method != "GET": |
| 479 | raise NbiException("Only method GET is allowed", HTTPStatus.METHOD_NOT_ALLOWED) |
| 480 | elif args or kwargs: |
| 481 | raise NbiException("Invalid URL or query string for version", HTTPStatus.METHOD_NOT_ALLOWED) |
| 482 | return __version__ + " " + version_date |
| 483 | except NbiException as e: |
| 484 | cherrypy.response.status = e.http_code.value |
| 485 | problem_details = { |
| 486 | "code": e.http_code.name, |
| 487 | "status": e.http_code.value, |
| 488 | "detail": str(e), |
| 489 | } |
| 490 | return self._format_out(problem_details, None) |
| 491 | |
| 492 | @cherrypy.expose |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 493 | def token(self, method, token_id=None, kwargs=None): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 494 | session = None |
| 495 | # self.engine.load_dbase(cherrypy.request.app.config) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 496 | indata = self._format_in(kwargs) |
| 497 | if not isinstance(indata, dict): |
| 498 | raise NbiException("Expected application/yaml or application/json Content-Type", HTTPStatus.BAD_REQUEST) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 499 | try: |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 500 | if method == "GET": |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 501 | session = self.authenticator.authorize() |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 502 | if token_id: |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 503 | outdata = self.authenticator.get_token(session, token_id) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 504 | else: |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 505 | outdata = self.authenticator.get_token_list(session) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 506 | elif method == "POST": |
| 507 | try: |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 508 | session = self.authenticator.authorize() |
| tierno | e128118 | 2018-05-22 12:24:36 +0200 | [diff] [blame] | 509 | except Exception: |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 510 | session = None |
| 511 | if kwargs: |
| 512 | indata.update(kwargs) |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 513 | outdata = self.authenticator.new_token(session, indata, cherrypy.request.remote) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 514 | session = outdata |
| 515 | cherrypy.session['Authorization'] = outdata["_id"] |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 516 | self._set_location_header("admin", "v1", "tokens", outdata["_id"]) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 517 | # cherrypy.response.cookie["Authorization"] = outdata["id"] |
| 518 | # cherrypy.response.cookie["Authorization"]['expires'] = 3600 |
| 519 | elif method == "DELETE": |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 520 | if not token_id and "id" in kwargs: |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 521 | token_id = kwargs["id"] |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 522 | elif not token_id: |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 523 | session = self.authenticator.authorize() |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 524 | token_id = session["_id"] |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 525 | outdata = self.authenticator.del_token(token_id) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 526 | session = None |
| 527 | cherrypy.session['Authorization'] = "logout" |
| 528 | # cherrypy.response.cookie["Authorization"] = token_id |
| 529 | # cherrypy.response.cookie["Authorization"]['expires'] = 0 |
| 530 | else: |
| 531 | raise NbiException("Method {} not allowed for token".format(method), HTTPStatus.METHOD_NOT_ALLOWED) |
| 532 | return self._format_out(outdata, session) |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 533 | except (NbiException, EngineException, DbException, AuthException) as e: |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 534 | cherrypy.log("tokens Exception {}".format(e)) |
| 535 | cherrypy.response.status = e.http_code.value |
| 536 | problem_details = { |
| 537 | "code": e.http_code.name, |
| 538 | "status": e.http_code.value, |
| 539 | "detail": str(e), |
| 540 | } |
| 541 | return self._format_out(problem_details, session) |
| 542 | |
| 543 | @cherrypy.expose |
| 544 | def test(self, *args, **kwargs): |
| 545 | thread_info = None |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 546 | if args and args[0] == "help": |
| 547 | return "<html><pre>\ninit\nfile/<name> download file\ndb-clear/table\nprune\nlogin\nlogin2\n"\ |
| tierno | e128118 | 2018-05-22 12:24:36 +0200 | [diff] [blame] | 548 | "sleep/<time>\nmessage/topic\n</pre></html>" |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 549 | |
| 550 | elif args and args[0] == "init": |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 551 | try: |
| 552 | # self.engine.load_dbase(cherrypy.request.app.config) |
| 553 | self.engine.create_admin() |
| 554 | return "Done. User 'admin', password 'admin' created" |
| 555 | except Exception: |
| 556 | cherrypy.response.status = HTTPStatus.FORBIDDEN.value |
| 557 | return self._format_out("Database already initialized") |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 558 | elif args and args[0] == "file": |
| 559 | return cherrypy.lib.static.serve_file(cherrypy.tree.apps['/osm'].config["storage"]["path"] + "/" + args[1], |
| 560 | "text/plain", "attachment") |
| 561 | elif args and args[0] == "file2": |
| 562 | f_path = cherrypy.tree.apps['/osm'].config["storage"]["path"] + "/" + args[1] |
| 563 | f = open(f_path, "r") |
| 564 | cherrypy.response.headers["Content-type"] = "text/plain" |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 565 | return f |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 566 | |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 567 | elif len(args) == 2 and args[0] == "db-clear": |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 568 | return self.engine.db.del_list(args[1], kwargs) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 569 | elif args and args[0] == "prune": |
| 570 | return self.engine.prune() |
| 571 | elif args and args[0] == "login": |
| 572 | if not cherrypy.request.headers.get("Authorization"): |
| 573 | cherrypy.response.headers["WWW-Authenticate"] = 'Basic realm="Access to OSM site", charset="UTF-8"' |
| 574 | cherrypy.response.status = HTTPStatus.UNAUTHORIZED.value |
| 575 | elif args and args[0] == "login2": |
| 576 | if not cherrypy.request.headers.get("Authorization"): |
| 577 | cherrypy.response.headers["WWW-Authenticate"] = 'Bearer realm="Access to OSM site"' |
| 578 | cherrypy.response.status = HTTPStatus.UNAUTHORIZED.value |
| 579 | elif args and args[0] == "sleep": |
| 580 | sleep_time = 5 |
| 581 | try: |
| 582 | sleep_time = int(args[1]) |
| 583 | except Exception: |
| 584 | cherrypy.response.status = HTTPStatus.FORBIDDEN.value |
| 585 | return self._format_out("Database already initialized") |
| 586 | thread_info = cherrypy.thread_data |
| 587 | print(thread_info) |
| 588 | time.sleep(sleep_time) |
| 589 | # thread_info |
| 590 | elif len(args) >= 2 and args[0] == "message": |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 591 | main_topic = args[1] |
| 592 | return_text = "<html><pre>{} ->\n".format(main_topic) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 593 | try: |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 594 | if cherrypy.request.method == 'POST': |
| 595 | to_send = yaml.load(cherrypy.request.body) |
| 596 | for k, v in to_send.items(): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 597 | self.engine.msg.write(main_topic, k, v) |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 598 | return_text += " {}: {}\n".format(k, v) |
| 599 | elif cherrypy.request.method == 'GET': |
| 600 | for k, v in kwargs.items(): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 601 | self.engine.msg.write(main_topic, k, yaml.load(v)) |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 602 | return_text += " {}: {}\n".format(k, yaml.load(v)) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 603 | except Exception as e: |
| tierno | 55945e7 | 2018-04-06 16:40:27 +0200 | [diff] [blame] | 604 | return_text += "Error: " + str(e) |
| 605 | return_text += "</pre></html>\n" |
| 606 | return return_text |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 607 | |
| 608 | return_text = ( |
| 609 | "<html><pre>\nheaders:\n args: {}\n".format(args) + |
| 610 | " kwargs: {}\n".format(kwargs) + |
| 611 | " headers: {}\n".format(cherrypy.request.headers) + |
| 612 | " path_info: {}\n".format(cherrypy.request.path_info) + |
| 613 | " query_string: {}\n".format(cherrypy.request.query_string) + |
| 614 | " session: {}\n".format(cherrypy.session) + |
| 615 | " cookie: {}\n".format(cherrypy.request.cookie) + |
| 616 | " method: {}\n".format(cherrypy.request.method) + |
| 617 | " session: {}\n".format(cherrypy.session.get('fieldname')) + |
| 618 | " body:\n") |
| 619 | return_text += " length: {}\n".format(cherrypy.request.body.length) |
| 620 | if cherrypy.request.body.length: |
| 621 | return_text += " content: {}\n".format( |
| 622 | str(cherrypy.request.body.read(int(cherrypy.request.headers.get('Content-Length', 0))))) |
| 623 | if thread_info: |
| 624 | return_text += "thread: {}\n".format(thread_info) |
| 625 | return_text += "</pre></html>" |
| 626 | return return_text |
| 627 | |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 628 | def _check_valid_url_method(self, method, *args): |
| 629 | if len(args) < 3: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 630 | raise NbiException("URL must contain at least 'main_topic/version/topic'", HTTPStatus.METHOD_NOT_ALLOWED) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 631 | |
| 632 | reference = self.valid_methods |
| 633 | for arg in args: |
| 634 | if arg is None: |
| 635 | break |
| 636 | if not isinstance(reference, dict): |
| 637 | raise NbiException("URL contains unexpected extra items '{}'".format(arg), |
| 638 | HTTPStatus.METHOD_NOT_ALLOWED) |
| 639 | |
| 640 | if arg in reference: |
| 641 | reference = reference[arg] |
| 642 | elif "<ID>" in reference: |
| 643 | reference = reference["<ID>"] |
| 644 | elif "*" in reference: |
| 645 | reference = reference["*"] |
| 646 | break |
| 647 | else: |
| 648 | raise NbiException("Unexpected URL item {}".format(arg), HTTPStatus.METHOD_NOT_ALLOWED) |
| 649 | if "TODO" in reference and method in reference["TODO"]: |
| 650 | raise NbiException("Method {} not supported yet for this URL".format(method), HTTPStatus.NOT_IMPLEMENTED) |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 651 | elif "METHODS" in reference and method not in reference["METHODS"]: |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 652 | raise NbiException("Method {} not supported for this URL".format(method), HTTPStatus.METHOD_NOT_ALLOWED) |
| 653 | return |
| 654 | |
| 655 | @staticmethod |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 656 | def _set_location_header(main_topic, version, topic, id): |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 657 | """ |
| 658 | 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] | 659 | :param main_topic: |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 660 | :param version: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 661 | :param topic: |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 662 | :param id: |
| 663 | :return: None |
| 664 | """ |
| 665 | # Use cherrypy.request.base for absoluted path and make use of request.header HOST just in case behind aNAT |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 666 | cherrypy.response.headers["Location"] = "/osm/{}/{}/{}/{}".format(main_topic, version, topic, id) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 667 | return |
| 668 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 669 | @cherrypy.expose |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 670 | def default(self, main_topic=None, version=None, topic=None, _id=None, item=None, *args, **kwargs): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 671 | session = None |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 672 | outdata = None |
| 673 | _format = None |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 674 | method = "DONE" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 675 | engine_topic = None |
| tierno | db9dc58 | 2018-06-20 17:27:29 +0200 | [diff] [blame] | 676 | rollback = [] |
| 677 | session = None |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 678 | try: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 679 | if not main_topic or not version or not topic: |
| 680 | raise NbiException("URL must contain at least 'main_topic/version/topic'", |
| 681 | HTTPStatus.METHOD_NOT_ALLOWED) |
| tierno | cd65be3 | 2018-11-16 16:34:49 +0100 | [diff] [blame] | 682 | if main_topic not in ("admin", "vnfpkgm", "nsd", "nslcm", "pdu", "nst", "nsilcm"): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 683 | raise NbiException("URL main_topic '{}' not supported".format(main_topic), |
| 684 | HTTPStatus.METHOD_NOT_ALLOWED) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 685 | if version != 'v1': |
| 686 | raise NbiException("URL version '{}' not supported".format(version), HTTPStatus.METHOD_NOT_ALLOWED) |
| 687 | |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 688 | if kwargs and "METHOD" in kwargs and kwargs["METHOD"] in ("PUT", "POST", "DELETE", "GET", "PATCH"): |
| 689 | method = kwargs.pop("METHOD") |
| 690 | else: |
| 691 | method = cherrypy.request.method |
| tierno | b92094f | 2018-05-11 13:44:22 +0200 | [diff] [blame] | 692 | if kwargs and "FORCE" in kwargs: |
| 693 | force = kwargs.pop("FORCE") |
| 694 | else: |
| 695 | force = False |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 696 | self._check_valid_url_method(method, main_topic, version, topic, _id, item, *args) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 697 | if main_topic == "admin" and topic == "tokens": |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 698 | return self.token(method, _id, kwargs) |
| 699 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 700 | # self.engine.load_dbase(cherrypy.request.app.config) |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 701 | session = self.authenticator.authorize() |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 702 | indata = self._format_in(kwargs) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 703 | engine_topic = topic |
| 704 | if topic == "subscriptions": |
| 705 | engine_topic = main_topic + "_" + topic |
| 706 | if item: |
| 707 | engine_topic = item |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 708 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 709 | if main_topic == "nsd": |
| 710 | engine_topic = "nsds" |
| 711 | elif main_topic == "vnfpkgm": |
| 712 | engine_topic = "vnfds" |
| 713 | elif main_topic == "nslcm": |
| 714 | engine_topic = "nsrs" |
| 715 | if topic == "ns_lcm_op_occs": |
| 716 | engine_topic = "nslcmops" |
| 717 | if topic == "vnfrs" or topic == "vnf_instances": |
| 718 | engine_topic = "vnfrs" |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 719 | elif main_topic == "nst": |
| 720 | engine_topic = "nsts" |
| 721 | elif main_topic == "nsilcm": |
| 722 | engine_topic = "nsis" |
| 723 | if topic == "nsi_lcm_op_occs": |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 724 | engine_topic = "nsilcmops" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 725 | elif main_topic == "pdu": |
| 726 | engine_topic = "pdus" |
| 727 | if engine_topic == "vims": # TODO this is for backward compatibility, it will remove in the future |
| 728 | engine_topic = "vim_accounts" |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 729 | |
| 730 | if method == "GET": |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 731 | if item in ("nsd_content", "package_content", "artifacts", "vnfd", "nsd", "nst", "nst_content"): |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 732 | if item in ("vnfd", "nsd", "nst"): |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 733 | path = "$DESCRIPTOR" |
| 734 | elif args: |
| 735 | path = args |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 736 | elif item == "artifacts": |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 737 | path = () |
| 738 | else: |
| 739 | path = None |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 740 | file, _format = self.engine.get_file(session, engine_topic, _id, path, |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 741 | cherrypy.request.headers.get("Accept")) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 742 | outdata = file |
| 743 | elif not _id: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 744 | outdata = self.engine.get_item_list(session, engine_topic, kwargs) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 745 | else: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 746 | outdata = self.engine.get_item(session, engine_topic, _id) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 747 | elif method == "POST": |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 748 | if topic in ("ns_descriptors_content", "vnf_packages_content", "netslice_templates_content"): |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 749 | _id = cherrypy.request.headers.get("Transaction-Id") |
| 750 | if not _id: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 751 | _id = self.engine.new_item(rollback, session, engine_topic, {}, None, cherrypy.request.headers, |
| tierno | b92094f | 2018-05-11 13:44:22 +0200 | [diff] [blame] | 752 | force=force) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 753 | completed = self.engine.upload_content(session, engine_topic, _id, indata, kwargs, |
| 754 | cherrypy.request.headers, force=force) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 755 | if completed: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 756 | self._set_location_header(main_topic, version, topic, _id) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 757 | else: |
| 758 | cherrypy.response.headers["Transaction-Id"] = _id |
| 759 | outdata = {"id": _id} |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 760 | elif topic == "ns_instances_content": |
| 761 | # creates NSR |
| 762 | _id = self.engine.new_item(rollback, session, engine_topic, indata, kwargs, force=force) |
| 763 | # creates nslcmop |
| 764 | indata["lcmOperationType"] = "instantiate" |
| 765 | indata["nsInstanceId"] = _id |
| 766 | self.engine.new_item(rollback, session, "nslcmops", indata, None) |
| 767 | self._set_location_header(main_topic, version, topic, _id) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 768 | outdata = {"id": _id} |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 769 | elif topic == "ns_instances" and item: |
| 770 | indata["lcmOperationType"] = item |
| 771 | indata["nsInstanceId"] = _id |
| 772 | _id = self.engine.new_item(rollback, session, "nslcmops", indata, kwargs) |
| 773 | self._set_location_header(main_topic, version, "ns_lcm_op_occs", _id) |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 774 | outdata = {"id": _id} |
| 775 | cherrypy.response.status = HTTPStatus.ACCEPTED.value |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 776 | elif topic == "netslice_instances_content": |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 777 | # creates NetSlice_Instance_record (NSIR) |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 778 | _id = self.engine.new_item(rollback, session, engine_topic, indata, kwargs, force=force) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 779 | self._set_location_header(main_topic, version, topic, _id) |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 780 | indata["lcmOperationType"] = "instantiate" |
| 781 | indata["nsiInstanceId"] = _id |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 782 | self.engine.new_item(rollback, session, "nsilcmops", indata, kwargs) |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 783 | outdata = {"id": _id} |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 784 | |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 785 | elif topic == "netslice_instances" and item: |
| 786 | indata["lcmOperationType"] = item |
| 787 | indata["nsiInstanceId"] = _id |
| 788 | _id = self.engine.new_item(rollback, session, "nsilcmops", indata, kwargs) |
| 789 | self._set_location_header(main_topic, version, "nsi_lcm_op_occs", _id) |
| 790 | outdata = {"id": _id} |
| 791 | cherrypy.response.status = HTTPStatus.ACCEPTED.value |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 792 | else: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 793 | _id = self.engine.new_item(rollback, session, engine_topic, indata, kwargs, |
| 794 | cherrypy.request.headers, force=force) |
| 795 | self._set_location_header(main_topic, version, topic, _id) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 796 | outdata = {"id": _id} |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 797 | # TODO form NsdInfo when topic in ("ns_descriptors", "vnf_packages") |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 798 | cherrypy.response.status = HTTPStatus.CREATED.value |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 799 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 800 | elif method == "DELETE": |
| 801 | if not _id: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 802 | outdata = self.engine.del_item_list(session, engine_topic, kwargs) |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 803 | cherrypy.response.status = HTTPStatus.OK.value |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 804 | else: # len(args) > 1 |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 805 | if topic == "ns_instances_content" and not force: |
| 806 | nslcmop_desc = { |
| 807 | "lcmOperationType": "terminate", |
| 808 | "nsInstanceId": _id, |
| 809 | "autoremove": True |
| 810 | } |
| 811 | opp_id = self.engine.new_item(rollback, session, "nslcmops", nslcmop_desc, None) |
| tierno | 3ace63c | 2018-05-03 17:51:43 +0200 | [diff] [blame] | 812 | outdata = {"_id": opp_id} |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 813 | cherrypy.response.status = HTTPStatus.ACCEPTED.value |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 814 | elif topic == "netslice_instances_content" and not force: |
| 815 | nsilcmop_desc = { |
| 816 | "lcmOperationType": "terminate", |
| 817 | "nsiInstanceId": _id, |
| 818 | "autoremove": True |
| 819 | } |
| 820 | opp_id = self.engine.new_item(rollback, session, "nsilcmops", nsilcmop_desc, None) |
| 821 | outdata = {"_id": opp_id} |
| 822 | cherrypy.response.status = HTTPStatus.ACCEPTED.value |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 823 | else: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 824 | self.engine.del_item(session, engine_topic, _id, force) |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 825 | cherrypy.response.status = HTTPStatus.NO_CONTENT.value |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 826 | if engine_topic in ("vim_accounts", "sdns"): |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 827 | cherrypy.response.status = HTTPStatus.ACCEPTED.value |
| 828 | |
| tierno | 7ae1011 | 2018-05-18 14:36:02 +0200 | [diff] [blame] | 829 | elif method in ("PUT", "PATCH"): |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 830 | outdata = None |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 831 | if not indata and not kwargs: |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 832 | raise NbiException("Nothing to update. Provide payload and/or query string", |
| 833 | HTTPStatus.BAD_REQUEST) |
| garciadeblas | 9750c5a | 2018-10-15 16:20:35 +0200 | [diff] [blame] | 834 | if item in ("nsd_content", "package_content", "nst_content") and method == "PUT": |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 835 | completed = self.engine.upload_content(session, engine_topic, _id, indata, kwargs, |
| 836 | cherrypy.request.headers, force=force) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 837 | if not completed: |
| 838 | cherrypy.response.headers["Transaction-Id"] = id |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 839 | else: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 840 | self.engine.edit_item(session, engine_topic, _id, indata, kwargs, force=force) |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 841 | cherrypy.response.status = HTTPStatus.NO_CONTENT.value |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 842 | else: |
| 843 | raise NbiException("Method {} not allowed".format(method), HTTPStatus.METHOD_NOT_ALLOWED) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 844 | return self._format_out(outdata, session, _format) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 845 | except Exception as e: |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 846 | if isinstance(e, (NbiException, EngineException, DbException, FsException, MsgException, AuthException, |
| 847 | ValidationError)): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 848 | http_code_value = cherrypy.response.status = e.http_code.value |
| 849 | http_code_name = e.http_code.name |
| 850 | cherrypy.log("Exception {}".format(e)) |
| 851 | else: |
| 852 | http_code_value = cherrypy.response.status = HTTPStatus.BAD_REQUEST.value # INTERNAL_SERVER_ERROR |
| Felipe Vicens | e36ab85 | 2018-11-23 14:12:09 +0100 | [diff] [blame] | 853 | cherrypy.log("CRITICAL: Exception {}".format(e), traceback=True) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 854 | http_code_name = HTTPStatus.BAD_REQUEST.name |
| tierno | 3ace63c | 2018-05-03 17:51:43 +0200 | [diff] [blame] | 855 | if hasattr(outdata, "close"): # is an open file |
| 856 | outdata.close() |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 857 | error_text = str(e) |
| 858 | rollback.reverse() |
| tierno | db9dc58 | 2018-06-20 17:27:29 +0200 | [diff] [blame] | 859 | for rollback_item in rollback: |
| tierno | 3ace63c | 2018-05-03 17:51:43 +0200 | [diff] [blame] | 860 | try: |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 861 | if rollback_item.get("operation") == "set": |
| 862 | self.engine.db.set_one(rollback_item["topic"], {"_id": rollback_item["_id"]}, |
| 863 | rollback_item["content"], fail_on_empty=False) |
| 864 | else: |
| 865 | self.engine.del_item(**rollback_item, session=session, force=True) |
| tierno | 3ace63c | 2018-05-03 17:51:43 +0200 | [diff] [blame] | 866 | except Exception as e2: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 867 | rollback_error_text = "Rollback Exception {}: {}".format(rollback_item, e2) |
| 868 | cherrypy.log(rollback_error_text) |
| 869 | error_text += ". " + rollback_error_text |
| 870 | # if isinstance(e, MsgException): |
| 871 | # error_text = "{} has been '{}' but other modules cannot be informed because an error on bus".format( |
| 872 | # engine_topic[:-1], method, error_text) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 873 | problem_details = { |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 874 | "code": http_code_name, |
| 875 | "status": http_code_value, |
| 876 | "detail": error_text, |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 877 | } |
| 878 | return self._format_out(problem_details, session) |
| 879 | # raise cherrypy.HTTPError(e.http_code.value, str(e)) |
| 880 | |
| 881 | |
| 882 | # def validate_password(realm, username, password): |
| 883 | # cherrypy.log("realm "+ str(realm)) |
| 884 | # if username == "admin" and password == "admin": |
| 885 | # return True |
| 886 | # return False |
| 887 | |
| 888 | |
| 889 | def _start_service(): |
| 890 | """ |
| 891 | Callback function called when cherrypy.engine starts |
| 892 | Override configuration with env variables |
| 893 | Set database, storage, message configuration |
| 894 | Init database with admin/admin user password |
| 895 | """ |
| 896 | cherrypy.log.error("Starting osm_nbi") |
| 897 | # update general cherrypy configuration |
| 898 | update_dict = {} |
| 899 | |
| 900 | engine_config = cherrypy.tree.apps['/osm'].config |
| 901 | for k, v in environ.items(): |
| 902 | if not k.startswith("OSMNBI_"): |
| 903 | continue |
| tierno | e128118 | 2018-05-22 12:24:36 +0200 | [diff] [blame] | 904 | k1, _, k2 = k[7:].lower().partition("_") |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 905 | if not k2: |
| 906 | continue |
| 907 | try: |
| 908 | # update static configuration |
| 909 | if k == 'OSMNBI_STATIC_DIR': |
| 910 | engine_config["/static"]['tools.staticdir.dir'] = v |
| 911 | engine_config["/static"]['tools.staticdir.on'] = True |
| 912 | elif k == 'OSMNBI_SOCKET_PORT' or k == 'OSMNBI_SERVER_PORT': |
| 913 | update_dict['server.socket_port'] = int(v) |
| 914 | elif k == 'OSMNBI_SOCKET_HOST' or k == 'OSMNBI_SERVER_HOST': |
| 915 | update_dict['server.socket_host'] = v |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 916 | elif k1 in ("server", "test", "auth", "log"): |
| 917 | update_dict[k1 + '.' + k2] = v |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 918 | elif k1 in ("message", "database", "storage", "authentication"): |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 919 | # k2 = k2.replace('_', '.') |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 920 | if k2 in ("port", "db_port"): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 921 | engine_config[k1][k2] = int(v) |
| 922 | else: |
| 923 | engine_config[k1][k2] = v |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 924 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 925 | except ValueError as e: |
| 926 | cherrypy.log.error("Ignoring environ '{}': " + str(e)) |
| 927 | except Exception as e: |
| 928 | cherrypy.log.warn("skipping environ '{}' on exception '{}'".format(k, e)) |
| 929 | |
| 930 | if update_dict: |
| 931 | cherrypy.config.update(update_dict) |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 932 | engine_config["global"].update(update_dict) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 933 | |
| 934 | # logging cherrypy |
| 935 | log_format_simple = "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s" |
| 936 | log_formatter_simple = logging.Formatter(log_format_simple, datefmt='%Y-%m-%dT%H:%M:%S') |
| 937 | logger_server = logging.getLogger("cherrypy.error") |
| 938 | logger_access = logging.getLogger("cherrypy.access") |
| 939 | logger_cherry = logging.getLogger("cherrypy") |
| 940 | logger_nbi = logging.getLogger("nbi") |
| 941 | |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 942 | if "log.file" in engine_config["global"]: |
| 943 | file_handler = logging.handlers.RotatingFileHandler(engine_config["global"]["log.file"], |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 944 | maxBytes=100e6, backupCount=9, delay=0) |
| 945 | file_handler.setFormatter(log_formatter_simple) |
| 946 | logger_cherry.addHandler(file_handler) |
| 947 | logger_nbi.addHandler(file_handler) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 948 | # log always to standard output |
| 949 | for format_, logger in {"nbi.server %(filename)s:%(lineno)s": logger_server, |
| 950 | "nbi.access %(filename)s:%(lineno)s": logger_access, |
| 951 | "%(name)s %(filename)s:%(lineno)s": logger_nbi |
| 952 | }.items(): |
| 953 | log_format_cherry = "%(asctime)s %(levelname)s {} %(message)s".format(format_) |
| 954 | log_formatter_cherry = logging.Formatter(log_format_cherry, datefmt='%Y-%m-%dT%H:%M:%S') |
| 955 | str_handler = logging.StreamHandler() |
| 956 | str_handler.setFormatter(log_formatter_cherry) |
| 957 | logger.addHandler(str_handler) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 958 | |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 959 | if engine_config["global"].get("log.level"): |
| 960 | logger_cherry.setLevel(engine_config["global"]["log.level"]) |
| 961 | logger_nbi.setLevel(engine_config["global"]["log.level"]) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 962 | |
| 963 | # logging other modules |
| 964 | for k1, logname in {"message": "nbi.msg", "database": "nbi.db", "storage": "nbi.fs"}.items(): |
| 965 | engine_config[k1]["logger_name"] = logname |
| 966 | logger_module = logging.getLogger(logname) |
| 967 | if "logfile" in engine_config[k1]: |
| 968 | file_handler = logging.handlers.RotatingFileHandler(engine_config[k1]["logfile"], |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 969 | maxBytes=100e6, backupCount=9, delay=0) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 970 | file_handler.setFormatter(log_formatter_simple) |
| 971 | logger_module.addHandler(file_handler) |
| 972 | if "loglevel" in engine_config[k1]: |
| 973 | logger_module.setLevel(engine_config[k1]["loglevel"]) |
| 974 | # TODO add more entries, e.g.: storage |
| 975 | cherrypy.tree.apps['/osm'].root.engine.start(engine_config) |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 976 | cherrypy.tree.apps['/osm'].root.authenticator.start(engine_config) |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 977 | cherrypy.tree.apps['/osm'].root.engine.init_db(target_version=database_version) |
| 978 | cherrypy.tree.apps['/osm'].root.authenticator.init_db(target_version=auth_database_version) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 979 | # getenv('OSMOPENMANO_TENANT', None) |
| 980 | |
| 981 | |
| 982 | def _stop_service(): |
| 983 | """ |
| 984 | Callback function called when cherrypy.engine stops |
| 985 | TODO: Ending database connections. |
| 986 | """ |
| 987 | cherrypy.tree.apps['/osm'].root.engine.stop() |
| 988 | cherrypy.log.error("Stopping osm_nbi") |
| 989 | |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 990 | |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 991 | def nbi(config_file): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 992 | # conf = { |
| 993 | # '/': { |
| 994 | # #'request.dispatch': cherrypy.dispatch.MethodDispatcher(), |
| 995 | # 'tools.sessions.on': True, |
| 996 | # 'tools.response_headers.on': True, |
| 997 | # # 'tools.response_headers.headers': [('Content-Type', 'text/plain')], |
| 998 | # } |
| 999 | # } |
| 1000 | # cherrypy.Server.ssl_module = 'builtin' |
| 1001 | # cherrypy.Server.ssl_certificate = "http/cert.pem" |
| 1002 | # cherrypy.Server.ssl_private_key = "http/privkey.pem" |
| 1003 | # cherrypy.Server.thread_pool = 10 |
| 1004 | # cherrypy.config.update({'Server.socket_port': config["port"], 'Server.socket_host': config["host"]}) |
| 1005 | |
| 1006 | # cherrypy.config.update({'tools.auth_basic.on': True, |
| 1007 | # 'tools.auth_basic.realm': 'localhost', |
| 1008 | # 'tools.auth_basic.checkpassword': validate_password}) |
| 1009 | cherrypy.engine.subscribe('start', _start_service) |
| 1010 | cherrypy.engine.subscribe('stop', _stop_service) |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 1011 | cherrypy.quickstart(Server(), '/osm', config_file) |
| 1012 | |
| 1013 | |
| 1014 | def usage(): |
| 1015 | print("""Usage: {} [options] |
| 1016 | -c|--config [configuration_file]: loads the configuration file (default: ./nbi.cfg) |
| 1017 | -h|--help: shows this help |
| 1018 | """.format(sys.argv[0])) |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 1019 | # --log-socket-host HOST: send logs to this host") |
| 1020 | # --log-socket-port PORT: send logs using this port (default: 9022)") |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1021 | |
| 1022 | |
| 1023 | if __name__ == '__main__': |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 1024 | try: |
| 1025 | # load parameters and configuration |
| 1026 | opts, args = getopt.getopt(sys.argv[1:], "hvc:", ["config=", "help"]) |
| 1027 | # TODO add "log-socket-host=", "log-socket-port=", "log-file=" |
| 1028 | config_file = None |
| 1029 | for o, a in opts: |
| 1030 | if o in ("-h", "--help"): |
| 1031 | usage() |
| 1032 | sys.exit() |
| 1033 | elif o in ("-c", "--config"): |
| 1034 | config_file = a |
| 1035 | # elif o == "--log-socket-port": |
| 1036 | # log_socket_port = a |
| 1037 | # elif o == "--log-socket-host": |
| 1038 | # log_socket_host = a |
| 1039 | # elif o == "--log-file": |
| 1040 | # log_file = a |
| 1041 | else: |
| 1042 | assert False, "Unhandled option" |
| 1043 | if config_file: |
| 1044 | if not path.isfile(config_file): |
| 1045 | print("configuration file '{}' that not exist".format(config_file), file=sys.stderr) |
| 1046 | exit(1) |
| 1047 | else: |
| 1048 | for config_file in (__file__[:__file__.rfind(".")] + ".cfg", "./nbi.cfg", "/etc/osm/nbi.cfg"): |
| 1049 | if path.isfile(config_file): |
| 1050 | break |
| 1051 | else: |
| 1052 | print("No configuration file 'nbi.cfg' found neither at local folder nor at /etc/osm/", file=sys.stderr) |
| 1053 | exit(1) |
| 1054 | nbi(config_file) |
| 1055 | except getopt.GetoptError as e: |
| 1056 | print(str(e), file=sys.stderr) |
| 1057 | # usage() |
| 1058 | exit(1) |