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