import yaml
import html_out as html
import logging
+import logging.handlers
+import getopt
+import sys
from engine import Engine, EngineException
-from dbbase import DbException
-from fsbase import FsException
+from osm_common.dbbase import DbException
+from osm_common.fsbase import FsException
+from osm_common.msgbase import MsgException
from base64 import standard_b64decode
#from os import getenv
from http import HTTPStatus
#from http.client import responses as http_responses
from codecs import getreader
-from os import environ
+from os import environ, path
__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
-__version__ = "0.2"
-version_date = "Mar 2018"
+
+# TODO consider to remove and provide version using the static version file
+__version__ = "0.1.3"
+version_date = "Apr 2018"
+database_version = '1.0'
"""
North Bound Interface (O: OSM specific; 5,X: SOL005 not implemented yet; O5: SOL005 implemented)
/<subscriptionId> 5 X
/vnfpkgm/v1
+ /vnf_packages_content O O
+ /<vnfPkgId> O O
/vnf_packages O5 O5
/<vnfPkgId> O5 O5 5
/package_content O5 O5
/<nsInstanceId> O O
/ns_instances 5 5
/<nsInstanceId> 5 5
- TO BE COMPLETED
+ instantiate O5
+ terminate O5
+ action O
+ scale O5
+ heal 5
/ns_lcm_op_occs 5 5
/<nsLcmOpOccId> 5 5 5
TO BE COMPLETED 5 5
+ /vnfrs O
+ /<vnfrId> O
/subscriptions 5 5
/<subscriptionId> 5 X
/admin/v1
/<id> O O
/projects O O
/<id> O O
+ /vims_accounts (also vims for compatibility) O O
+ /<id> O O O
+ /sdns O O
+ /<id> O O O
query string.
<attrName>[.<attrName>...]*[.<op>]=<value>[,<value>...]&...
self.valid_methods = { # contains allowed URL and methods
"admin": {
"v1": {
- "tokens": { "METHODS": ("GET", "POST", "DELETE"),
+ "tokens": {"METHODS": ("GET", "POST", "DELETE"),
"<ID>": { "METHODS": ("GET", "DELETE")}
},
- "users": { "METHODS": ("GET", "POST"),
+ "users": {"METHODS": ("GET", "POST"),
"<ID>": {"METHODS": ("GET", "POST", "DELETE")}
},
- "projects": { "METHODS": ("GET", "POST"),
- "<ID>": {"METHODS": ("GET", "POST", "DELETE")}
+ "projects": {"METHODS": ("GET", "POST"),
+ "<ID>": {"METHODS": ("GET", "DELETE")}
+ },
+ "vims": {"METHODS": ("GET", "POST"),
+ "<ID>": {"METHODS": ("GET", "DELETE")}
+ },
+ "vim_accounts": {"METHODS": ("GET", "POST"),
+ "<ID>": {"METHODS": ("GET", "DELETE", "PATCH")}
+ },
+ "sdns": {"METHODS": ("GET", "POST"),
+ "<ID>": {"METHODS": ("GET", "DELETE", "PATCH")}
},
}
},
"<ID>": {"METHODS": ("GET", "PUT", "DELETE")}
},
"ns_descriptors": { "METHODS": ("GET", "POST"),
- "<ID>": { "METHODS": ("GET", "DELETE"), "TODO": "PATCH",
+ "<ID>": {"METHODS": ("GET", "DELETE"), "TODO": "PATCH",
"nsd_content": { "METHODS": ("GET", "PUT")},
"nsd": {"METHODS": "GET"}, # descriptor inside package
"artifacts": {"*": {"METHODS": "GET"}}
"ns_instances_content": {"METHODS": ("GET", "POST"),
"<ID>": {"METHODS": ("GET", "DELETE")}
},
- "ns_instances": {"TODO": ("GET", "POST"),
- "<ID>": {"TODO": ("GET", "DELETE")}
- }
+ "ns_instances": {"METHODS": ("GET", "POST"),
+ "<ID>": {"TODO": ("GET", "DELETE"),
+ "scale": {"TODO": "POST"},
+ "terminate": {"METHODS": "POST"},
+ "instantiate": {"METHODS": "POST"},
+ "action": {"METHODS": "POST"},
+ }
+ },
+ "ns_lcm_op_occs": {"METHODS": "GET",
+ "<ID>": {"METHODS": "GET"},
+ },
+ "vnfrs": {"METHODS": ("GET"),
+ "<ID>": {"METHODS": ("GET")}
+ },
}
},
}
raise NbiException(error_text + str(exc), HTTPStatus.BAD_REQUEST)
except KeyError as exc:
raise NbiException("Query string error: " + str(exc), HTTPStatus.BAD_REQUEST)
+ except Exception as exc:
+ raise NbiException(error_text + str(exc), HTTPStatus.BAD_REQUEST)
@staticmethod
def _format_out(data, session=None, _format=None):
:param _format: The format to be set as Content-Type ir data is a file
:return: None
"""
+ accept = cherrypy.request.headers.get("Accept")
if data is None:
- cherrypy.response.status = HTTPStatus.NO_CONTENT.value
+ if accept and "text/html" in accept:
+ return html.format(data, cherrypy.request, cherrypy.response, session)
+ # cherrypy.response.status = HTTPStatus.NO_CONTENT.value
return
elif hasattr(data, "read"): # file object
if _format:
cherrypy.response.headers["Content-Type"] = 'text/plain'
# TODO check that cherrypy close file. If not implement pending things to close per thread next
return data
- if "Accept" in cherrypy.request.headers:
- accept = cherrypy.request.headers["Accept"]
+ if accept:
if "application/json" in accept:
cherrypy.response.headers["Content-Type"] = 'application/json; charset=utf-8'
a = json.dumps(data, indent=4) + "\n"
cherrypy.response.status = e.http_code.value
return self._format_out("Welcome to OSM!", session)
+ @cherrypy.expose
+ def version(self, *args, **kwargs):
+ # TODO consider to remove and provide version using the static version file
+ global __version__, version_date
+ try:
+ if cherrypy.request.method != "GET":
+ raise NbiException("Only method GET is allowed", HTTPStatus.METHOD_NOT_ALLOWED)
+ elif args or kwargs:
+ raise NbiException("Invalid URL or query string for version", HTTPStatus.METHOD_NOT_ALLOWED)
+ return __version__ + " " + version_date
+ except NbiException as e:
+ cherrypy.response.status = e.http_code.value
+ problem_details = {
+ "code": e.http_code.name,
+ "status": e.http_code.value,
+ "detail": str(e),
+ }
+ return self._format_out(problem_details, None)
+
@cherrypy.expose
def token(self, method, token_id=None, kwargs=None):
session = None
outdata = self.engine.new_token(session, indata, cherrypy.request.remote)
session = outdata
cherrypy.session['Authorization'] = outdata["_id"]
+ self._set_location_header("admin", "v1", "tokens", outdata["_id"])
# cherrypy.response.cookie["Authorization"] = outdata["id"]
# cherrypy.response.cookie["Authorization"]['expires'] = 3600
elif method == "DELETE":
session = self._authorization()
token_id = session["_id"]
outdata = self.engine.del_token(token_id)
+ oudata = None
session = None
cherrypy.session['Authorization'] = "logout"
# cherrypy.response.cookie["Authorization"] = token_id
}
return self._format_out(problem_details, session)
- @cherrypy.expose
- def test2(self, args0=None, args1=None, args2=None, args3=None, *args, **kwargs):
- return_text = (
- "<html><pre>\n{} {} {} {} {} {} \n".format(args0, args1, args2, args3, args, kwargs))
- return_text += "</pre></html>"
- return return_text
-
@cherrypy.expose
def test(self, *args, **kwargs):
thread_info = None
if args and args[0] == "help":
return "<html><pre>\ninit\nfile/<name> download file\ndb-clear/table\nprune\nlogin\nlogin2\n"\
- "sleep/<time>\n</pre></html>"
+ "sleep/<time>\nmessage/topic\n</pre></html>"
elif args and args[0] == "init":
try:
f_path = cherrypy.tree.apps['/osm'].config["storage"]["path"] + "/" + args[1]
f = open(f_path, "r")
cherrypy.response.headers["Content-type"] = "text/plain"
-
return f
+
elif len(args) == 2 and args[0] == "db-clear":
return self.engine.del_item_list({"project_id": "admin"}, args[1], {})
elif args and args[0] == "prune":
# thread_info
elif len(args) >= 2 and args[0] == "message":
topic = args[1]
+ return_text = "<html><pre>{} ->\n".format(topic)
try:
- for k, v in kwargs.items():
- self.engine.msg.write(topic, k, yaml.load(v))
- return "ok"
+ if cherrypy.request.method == 'POST':
+ to_send = yaml.load(cherrypy.request.body)
+ for k, v in to_send.items():
+ self.engine.msg.write(topic, k, v)
+ return_text += " {}: {}\n".format(k, v)
+ elif cherrypy.request.method == 'GET':
+ for k, v in kwargs.items():
+ self.engine.msg.write(topic, k, yaml.load(v))
+ return_text += " {}: {}\n".format(k, yaml.load(v))
except Exception as e:
- return "Error: " + format(e)
+ return_text += "Error: " + str(e)
+ return_text += "</pre></html>\n"
+ return return_text
return_text = (
"<html><pre>\nheaders:\n args: {}\n".format(args) +
session = None
outdata = None
_format = None
+ method = "DONE"
+ engine_item = None
+ rollback = None
try:
if not topic or not version or not item:
raise NbiException("URL must contain at least 'topic/version/item'", HTTPStatus.METHOD_NOT_ALLOWED)
method = kwargs.pop("METHOD")
else:
method = cherrypy.request.method
+ if kwargs and "FORCE" in kwargs:
+ force = kwargs.pop("FORCE")
+ else:
+ force = False
self._check_valid_url_method(method, topic, version, item, _id, item2, *args)
engine_item = "vnfds"
elif topic == "nslcm":
engine_item = "nsrs"
+ if item == "ns_lcm_op_occs":
+ engine_item = "nslcmops"
+ if item == "vnfrs":
+ engine_item = "vnfrs"
+ if engine_item == "vims": # TODO this is for backward compatibility, it will remove in the future
+ engine_item = "vim_accounts"
if method == "GET":
if item2 in ("nsd_content", "package_content", "artifacts", "vnfd", "nsd"):
if item in ("ns_descriptors_content", "vnf_packages_content"):
_id = cherrypy.request.headers.get("Transaction-Id")
if not _id:
- _id = self.engine.new_item(session, engine_item, {}, None, cherrypy.request.headers)
+ _id = self.engine.new_item(session, engine_item, {}, None, cherrypy.request.headers,
+ force=force)
+ rollback = {"session": session, "item": engine_item, "_id": _id, "force": True}
completed = self.engine.upload_content(session, engine_item, _id, indata, kwargs, cherrypy.request.headers)
if completed:
self._set_location_header(topic, version, item, _id)
else:
cherrypy.response.headers["Transaction-Id"] = _id
outdata = {"id": _id}
- elif item in ("ns_descriptors", "vnf_packages"):
- _id = self.engine.new_item(session, engine_item, indata, kwargs, cherrypy.request.headers)
+ elif item == "ns_instances_content":
+ _id = self.engine.new_item(session, engine_item, indata, kwargs, force=force)
+ rollback = {"session": session, "item": engine_item, "_id": _id, "force": True}
+ self.engine.ns_operation(session, _id, "instantiate", {}, None)
self._set_location_header(topic, version, item, _id)
- #TODO form NsdInfo
outdata = {"id": _id}
+ elif item == "ns_instances" and item2:
+ _id = self.engine.ns_operation(session, _id, item2, indata, kwargs)
+ self._set_location_header(topic, version, "ns_lcm_op_occs", _id)
+ outdata = {"id": _id}
+ cherrypy.response.status = HTTPStatus.ACCEPTED.value
else:
- _id = self.engine.new_item(session, engine_item, indata, kwargs, cherrypy.request.headers)
+ _id = self.engine.new_item(session, engine_item, indata, kwargs, cherrypy.request.headers,
+ force=force)
self._set_location_header(topic, version, item, _id)
outdata = {"id": _id}
+ # TODO form NsdInfo when item in ("ns_descriptors", "vnf_packages")
cherrypy.response.status = HTTPStatus.CREATED.value
+
elif method == "DELETE":
if not _id:
outdata = self.engine.del_item_list(session, engine_item, kwargs)
+ cherrypy.response.status = HTTPStatus.OK.value
else: # len(args) > 1
- outdata = self.engine.del_item(session, engine_item, _id)
- if item in ("ns_descriptors", "vnf_packages"): # SOL005
- outdata = None
+ if item == "ns_instances_content":
+ opp_id = self.engine.ns_operation(session, _id, "terminate", {"autoremove": True}, None)
+ outdata = {"_id": opp_id}
+ cherrypy.response.status = HTTPStatus.ACCEPTED.value
+ else:
+ self.engine.del_item(session, engine_item, _id, force)
+ cherrypy.response.status = HTTPStatus.NO_CONTENT.value
+ if engine_item in ("vim_accounts", "sdns"):
+ cherrypy.response.status = HTTPStatus.ACCEPTED.value
+
elif method == "PUT":
if not indata and not kwargs:
raise NbiException("Nothing to update. Provide payload and/or query string",
completed = self.engine.upload_content(session, engine_item, _id, indata, kwargs, cherrypy.request.headers)
if not completed:
cherrypy.response.headers["Transaction-Id"] = id
+ cherrypy.response.status = HTTPStatus.NO_CONTENT.value
outdata = None
else:
- outdata = {"id": self.engine.edit_item(session, engine_item, args[1], indata, kwargs)}
+ outdata = {"id": self.engine.edit_item(session, engine_item, _id, indata, kwargs, force=force)}
+ elif method == "PATCH":
+ if not indata and not kwargs:
+ raise NbiException("Nothing to update. Provide payload and/or query string",
+ HTTPStatus.BAD_REQUEST)
+ outdata = {"id": self.engine.edit_item(session, engine_item, _id, indata, kwargs, force=force)}
else:
raise NbiException("Method {} not allowed".format(method), HTTPStatus.METHOD_NOT_ALLOWED)
return self._format_out(outdata, session, _format)
- except (NbiException, EngineException, DbException, FsException) as e:
- if hasattr(outdata, "close"): # is an open file
- outdata.close()
+ except (NbiException, EngineException, DbException, FsException, MsgException) as e:
cherrypy.log("Exception {}".format(e))
cherrypy.response.status = e.http_code.value
+ if hasattr(outdata, "close"): # is an open file
+ outdata.close()
+ if rollback:
+ try:
+ self.engine.del_item(**rollback)
+ except Exception as e2:
+ cherrypy.log("Rollback Exception {}: {}".format(rollback, e2))
+ error_text = str(e)
+ if isinstance(e, MsgException):
+ error_text = "{} has been '{}' but other modules cannot be informed because an error on bus".format(
+ engine_item[:-1], method, error_text)
problem_details = {
"code": e.http_code.name,
"status": e.http_code.value,
update_dict['server.socket_port'] = int(v)
elif k == 'OSMNBI_SOCKET_HOST' or k == 'OSMNBI_SERVER_HOST':
update_dict['server.socket_host'] = v
- elif k1 == "server":
- update_dict['server' + k2] = v
- # TODO add more entries
+ elif k1 in ("server", "test", "auth", "log"):
+ update_dict[k1 + '.' + k2] = v
elif k1 in ("message", "database", "storage"):
+ # k2 = k2.replace('_', '.')
if k2 == "port":
engine_config[k1][k2] = int(v)
else:
if update_dict:
cherrypy.config.update(update_dict)
+ engine_config["global"].update(update_dict)
# logging cherrypy
log_format_simple = "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s"
logger_cherry = logging.getLogger("cherrypy")
logger_nbi = logging.getLogger("nbi")
- if "logfile" in engine_config["global"]:
- file_handler = logging.handlers.RotatingFileHandler(engine_config["global"]["logfile"],
+ if "log.file" in engine_config["global"]:
+ file_handler = logging.handlers.RotatingFileHandler(engine_config["global"]["log.file"],
maxBytes=100e6, backupCount=9, delay=0)
file_handler.setFormatter(log_formatter_simple)
logger_cherry.addHandler(file_handler)
str_handler.setFormatter(log_formatter_cherry)
logger.addHandler(str_handler)
- if engine_config["global"].get("loglevel"):
- logger_cherry.setLevel(engine_config["global"]["loglevel"])
- logger_nbi.setLevel(engine_config["global"]["loglevel"])
+ if engine_config["global"].get("log.level"):
+ logger_cherry.setLevel(engine_config["global"]["log.level"])
+ logger_nbi.setLevel(engine_config["global"]["log.level"])
# logging other modules
for k1, logname in {"message": "nbi.msg", "database": "nbi.db", "storage": "nbi.fs"}.items():
# TODO add more entries, e.g.: storage
cherrypy.tree.apps['/osm'].root.engine.start(engine_config)
try:
- cherrypy.tree.apps['/osm'].root.engine.create_admin()
+ cherrypy.tree.apps['/osm'].root.engine.init_db(target_version=database_version)
except EngineException:
pass
# getenv('OSMOPENMANO_TENANT', None)
cherrypy.tree.apps['/osm'].root.engine.stop()
cherrypy.log.error("Stopping osm_nbi")
-def nbi():
+def nbi(config_file):
# conf = {
# '/': {
# #'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
# 'tools.auth_basic.checkpassword': validate_password})
cherrypy.engine.subscribe('start', _start_service)
cherrypy.engine.subscribe('stop', _stop_service)
- cherrypy.quickstart(Server(), '/osm', "nbi.cfg")
+ cherrypy.quickstart(Server(), '/osm', config_file)
+
+
+def usage():
+ print("""Usage: {} [options]
+ -c|--config [configuration_file]: loads the configuration file (default: ./nbi.cfg)
+ -h|--help: shows this help
+ """.format(sys.argv[0]))
+ # --log-socket-host HOST: send logs to this host")
+ # --log-socket-port PORT: send logs using this port (default: 9022)")
if __name__ == '__main__':
- nbi()
+ try:
+ # load parameters and configuration
+ opts, args = getopt.getopt(sys.argv[1:], "hvc:", ["config=", "help"])
+ # TODO add "log-socket-host=", "log-socket-port=", "log-file="
+ config_file = None
+ for o, a in opts:
+ if o in ("-h", "--help"):
+ usage()
+ sys.exit()
+ elif o in ("-c", "--config"):
+ config_file = a
+ # elif o == "--log-socket-port":
+ # log_socket_port = a
+ # elif o == "--log-socket-host":
+ # log_socket_host = a
+ # elif o == "--log-file":
+ # log_file = a
+ else:
+ assert False, "Unhandled option"
+ if config_file:
+ if not path.isfile(config_file):
+ print("configuration file '{}' that not exist".format(config_file), file=sys.stderr)
+ exit(1)
+ else:
+ for config_file in (__file__[:__file__.rfind(".")] + ".cfg", "./nbi.cfg", "/etc/osm/nbi.cfg"):
+ if path.isfile(config_file):
+ break
+ else:
+ print("No configuration file 'nbi.cfg' found neither at local folder nor at /etc/osm/", file=sys.stderr)
+ exit(1)
+ nbi(config_file)
+ except getopt.GetoptError as e:
+ print(str(e), file=sys.stderr)
+ # usage()
+ exit(1)