From 9b53268341d1022688a431f5a01e38cb26b8f610 Mon Sep 17 00:00:00 2001 From: efiacor Date: Wed, 6 Nov 2019 11:08:54 +0000 Subject: Initial commit for PMSH Signed-off-by: efiacor Issue-ID: DCAEGEN2-1837 Change-Id: I3b7d7d379df68f0f5984a3c4e0faa9c8ca0bcdf8 --- components/pm-subscription-handler/.coveragerc | 43 ++++ components/pm-subscription-handler/.gitignore | 100 ++++++++ components/pm-subscription-handler/.gitreview | 4 + components/pm-subscription-handler/Changelog.md | 7 + components/pm-subscription-handler/Dockerfile | 55 ++++ components/pm-subscription-handler/LICENSE.txt | 20 ++ components/pm-subscription-handler/README.md | 47 ++++ .../pm-subscription-handler/mvn-phase-script.sh | 84 ++++++ .../pmsh_service/__init__.py | 21 ++ .../pmsh_service/mod/__init__.py | 21 ++ .../pmsh_service/mod/pmsh_logging.py | 285 +++++++++++++++++++++ .../pmsh_service/pmsh_service.py | 35 +++ components/pm-subscription-handler/pom.xml | 245 ++++++++++++++++++ .../pm-subscription-handler/requirements.txt | 9 + components/pm-subscription-handler/setup.py | 35 +++ .../pm-subscription-handler/tests/__init__.py | 21 ++ .../pm-subscription-handler/tests/test_logging.py | 28 ++ components/pm-subscription-handler/tox.ini | 41 +++ .../pm-subscription-handler/version.properties | 6 + 19 files changed, 1107 insertions(+) create mode 100644 components/pm-subscription-handler/.coveragerc create mode 100644 components/pm-subscription-handler/.gitignore create mode 100644 components/pm-subscription-handler/.gitreview create mode 100644 components/pm-subscription-handler/Changelog.md create mode 100644 components/pm-subscription-handler/Dockerfile create mode 100644 components/pm-subscription-handler/LICENSE.txt create mode 100644 components/pm-subscription-handler/README.md create mode 100755 components/pm-subscription-handler/mvn-phase-script.sh create mode 100644 components/pm-subscription-handler/pmsh_service/__init__.py create mode 100644 components/pm-subscription-handler/pmsh_service/mod/__init__.py create mode 100644 components/pm-subscription-handler/pmsh_service/mod/pmsh_logging.py create mode 100755 components/pm-subscription-handler/pmsh_service/pmsh_service.py create mode 100644 components/pm-subscription-handler/pom.xml create mode 100644 components/pm-subscription-handler/requirements.txt create mode 100644 components/pm-subscription-handler/setup.py create mode 100644 components/pm-subscription-handler/tests/__init__.py create mode 100644 components/pm-subscription-handler/tests/test_logging.py create mode 100644 components/pm-subscription-handler/tox.ini create mode 100644 components/pm-subscription-handler/version.properties diff --git a/components/pm-subscription-handler/.coveragerc b/components/pm-subscription-handler/.coveragerc new file mode 100644 index 00000000..d1c3205b --- /dev/null +++ b/components/pm-subscription-handler/.coveragerc @@ -0,0 +1,43 @@ +# ============LICENSE_START=================================================== +# Copyright (C) 2019 Nordix Foundation. +# ============================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END===================================================== + +# .coveragerc to control coverage.py +[run] +branch = True +cover_pylib = False +include = */pmsh_service/*.py + +[report] +# Regexes for lines to exclude from consideration +exclude_lines = + # Have to re-enable the standard pragma + pragma: no cover + + # Don't complain about missing debug-only code: + def __repr__ + if self\.debug + + # Don't complain if tests don't hit defensive assertion code: + raise AssertionError + raise NotImplementedError + + # Don't complain if non-runnable code isn't run: + if 0: + if __name__ == .__main__.: + +ignore_errors = True diff --git a/components/pm-subscription-handler/.gitignore b/components/pm-subscription-handler/.gitignore new file mode 100644 index 00000000..98ba212b --- /dev/null +++ b/components/pm-subscription-handler/.gitignore @@ -0,0 +1,100 @@ +mvn-phase-lib.sh +tox-local.ini +*.wgn +.pytest_cache/ +xunit-results.xml +.DS_Store +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +venv-tox/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# IPython Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# dotenv +.env + +# virtualenv +venv/ +ENV/ + +# Spyder project settings +.spyderproject + +# Rope project settings +.ropeproject + +# Test report +xunit-reports +coverage-reports diff --git a/components/pm-subscription-handler/.gitreview b/components/pm-subscription-handler/.gitreview new file mode 100644 index 00000000..4acf10dd --- /dev/null +++ b/components/pm-subscription-handler/.gitreview @@ -0,0 +1,4 @@ +[gerrit] +host=gerrit.onap.org +port=29418 +project=dcaegen2/services.git diff --git a/components/pm-subscription-handler/Changelog.md b/components/pm-subscription-handler/Changelog.md new file mode 100644 index 00000000..87c282ff --- /dev/null +++ b/components/pm-subscription-handler/Changelog.md @@ -0,0 +1,7 @@ +# Change Log +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/) +and this project adheres to [Semantic Versioning](http://semver.org/). + +## [1.0.0] - 11/4/2019 \ No newline at end of file diff --git a/components/pm-subscription-handler/Dockerfile b/components/pm-subscription-handler/Dockerfile new file mode 100644 index 00000000..e1dd16fe --- /dev/null +++ b/components/pm-subscription-handler/Dockerfile @@ -0,0 +1,55 @@ +# ============LICENSE_START=================================================== +# Copyright (C) 2019 Nordix Foundation. +# ============================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END===================================================== + +FROM python:3.7-slim-buster +MAINTAINER lego@est.tech + +ENV PMSHUSER=pmsh \ + APPDIR="/opt/app/pmsh" \ + # turn on file based EELF logging + PROD_LOGGING=1 \ + # set PATH & PYTHONPATH vars + PATH=/usr/local/lib/python3.7/bin:$PATH:$APPDIR/bin \ + PYTHONPATH=/usr/local/lib/python3.7/site-packages:./mod:./:$PYTHONPATH:$APPDIR/bin \ + REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt + +WORKDIR $APPDIR + + # add non root user & group +RUN addgroup --system $PMSHUSER && adduser --ingroup $PMSHUSER --system $PMSHUSER && \ + # create logs dir and change permissions + mkdir -p /var/log/ONAP/$PMSHUSER/logs && \ + chmod a+w /var/log/ONAP/$PMSHUSER/logs + +COPY setup.py ./ +COPY requirements.txt ./ +COPY ./pmsh_service ./bin/ + + # run the pip install +RUN pip install --upgrade pip && \ + pip install -r requirements.txt && \ + pip install -e . && \ + # change own & perms on entrypoint + chown -R $PMSHUSER:$PMSHUSER $APPDIR && \ + chmod 500 $APPDIR/bin/*.py + +# set to non root user +USER $PMSHUSER + +# run the app +ENTRYPOINT ["python", "./bin/pmsh_service.py"] \ No newline at end of file diff --git a/components/pm-subscription-handler/LICENSE.txt b/components/pm-subscription-handler/LICENSE.txt new file mode 100644 index 00000000..e7116cbd --- /dev/null +++ b/components/pm-subscription-handler/LICENSE.txt @@ -0,0 +1,20 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + diff --git a/components/pm-subscription-handler/README.md b/components/pm-subscription-handler/README.md new file mode 100644 index 00000000..661d71f6 --- /dev/null +++ b/components/pm-subscription-handler/README.md @@ -0,0 +1,47 @@ +# pm_subscription_handler + + ============LICENSE_START======================================================= + Copyright (C) 2019 Nordix Foundation. + ================================================================================ + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + SPDX-License-Identifier: Apache-2.0 + ============LICENSE_END========================================================= + + + +# Changelog +All changes are logged in Changelog.md + +# Overview + + + +# Usage + +# Assumptions +1. + + +# Development +## Version changes +Development changes require a version bump in several places. They are: +1. Changelod.md +2. version.properties +3. pom.xml +4. setup.py + +## Unit esting +You need `tox`; then just run: + + tox diff --git a/components/pm-subscription-handler/mvn-phase-script.sh b/components/pm-subscription-handler/mvn-phase-script.sh new file mode 100755 index 00000000..f51fa1ca --- /dev/null +++ b/components/pm-subscription-handler/mvn-phase-script.sh @@ -0,0 +1,84 @@ +#!/bin/bash +# ============LICENSE_START======================================================= +# Copyright (C) 2019 Nordix Foundation. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END========================================================= + +set -ex + +echo "running script: [$0] for module [$1] at stage [$2]" + +MVN_PROJECT_MODULEID="$1" +MVN_PHASE="$2" +PROJECT_ROOT=$(dirname $0) + + +echo "MVN_RELEASE_TAG is set to [$MVN_RELEASE_TAG]" +RELEASE_TAG=${MVN_RELEASE_TAG:-R6} +if [[ "$RELEASE_TAG" != "R1" ]]; then + RELEASE_TAGGED_DIR="${RELEASE_TAG}/" +else + RELEASE_TAGGED_DIR="" +fi + + +if [[ ! -f "${PROJECT_ROOT}/mvn-phase-lib.sh" ]]; then + if ! wget -O ${PROJECT_ROOT}/mvn-phase-lib.sh \ + "$MVN_RAWREPO_BASEURL_DOWNLOAD"/org.onap.dcaegen2.utils/${RELEASE_TAGGED_DIR}scripts/mvn-phase-lib.sh; then + echo "Fail to download mvn-phase-lib.sh" + exit 1 + fi +fi + +source ./mvn-phase-lib.sh + +# Customize the section below for each project +case ${MVN_PHASE} in +clean) + echo "==> clean phase script" + clean_templated_files + clean_tox_files + rm -rf ./venv-* ./*.wgn + ;; +generate-sources) + echo "==> generate-sources phase script" + ;; +compile) + echo "==> compile phase script" + ;; +test) + echo "==> test phase script" + run_tox_test + ;; +package) + echo "==> package phase script" + ;; +install) + echo "==> install phase script" + ;; +deploy) + echo "==> deploy phase script" + # below segments are example of how to deploy various artifacts + # copy the ones suitable for your repo + + # build docker image from Docker file (under root of repo) and push to registry + build_and_push_docker + ;; +*) + echo "==> unprocessed phase" + ;; +esac + diff --git a/components/pm-subscription-handler/pmsh_service/__init__.py b/components/pm-subscription-handler/pmsh_service/__init__.py new file mode 100644 index 00000000..d2f6f2fd --- /dev/null +++ b/components/pm-subscription-handler/pmsh_service/__init__.py @@ -0,0 +1,21 @@ +# ============LICENSE_START=================================================== +# Copyright (C) 2019 Nordix Foundation. +# ============================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END===================================================== + + +# empty __init__.py so that pytest can add correct path to coverage report, +# -- per pytest best practice guideline diff --git a/components/pm-subscription-handler/pmsh_service/mod/__init__.py b/components/pm-subscription-handler/pmsh_service/mod/__init__.py new file mode 100644 index 00000000..d2f6f2fd --- /dev/null +++ b/components/pm-subscription-handler/pmsh_service/mod/__init__.py @@ -0,0 +1,21 @@ +# ============LICENSE_START=================================================== +# Copyright (C) 2019 Nordix Foundation. +# ============================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END===================================================== + + +# empty __init__.py so that pytest can add correct path to coverage report, +# -- per pytest best practice guideline diff --git a/components/pm-subscription-handler/pmsh_service/mod/pmsh_logging.py b/components/pm-subscription-handler/pmsh_service/mod/pmsh_logging.py new file mode 100644 index 00000000..30c8db8e --- /dev/null +++ b/components/pm-subscription-handler/pmsh_service/mod/pmsh_logging.py @@ -0,0 +1,285 @@ +# ============LICENSE_START=================================================== +# Copyright (C) 2019 Nordix Foundation. +# ============================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END===================================================== + +import logging as log +from logging.handlers import RotatingFileHandler +from os import makedirs +import datetime + +# These loggers will be overwritten with EELF logging when running in Docker +_AUDIT_LOGGER = log.getLogger("defaultlogger") +_ERROR_LOGGER = log.getLogger("defaultlogger") +_METRICS_LOGGER = log.getLogger("defaultlogger") + +# Set up debug logger +_DEBUG_LOGGER = log.getLogger("defaultlogger") + + +def _create_logger(name, logfile): + """ + Create a RotatingFileHandler and a streamhandler for stdout + https://docs.python.org/3/library/logging.handlers.html + what's with the non-pythonic naming in these stdlib methods? Shameful. + """ + logger = log.getLogger(name) + file_handler = RotatingFileHandler(logfile, maxBytes=10000000, # 10 meg with one backup.., + backupCount=2) + formatter = log.Formatter("%(message)s") + file_handler.setFormatter(formatter) + logger.setLevel(log.DEBUG) + logger.addHandler(file_handler) + return logger + + +# Public + +def get_module_logger(mod_name): + """ + To use this, do logger = get_module_logger(__name__) + """ + logger = log.getLogger(mod_name) + handler = log.StreamHandler() + formatter = log.Formatter("%(asctime)s " + "[%(name)-12s] " + "%(levelname)-8s " + "%(message)s") + handler.setFormatter(formatter) + logger.addHandler(handler) + logger.setLevel(log.DEBUG) + return logger + + +def create_loggers(): + """ + Public method to set the global logger, launched from Run + This is *not* launched during unit testing, so unit tests do not + create/write log files + """ + makedirs("/var/log/ONAP/pmsh/logs", exist_ok=True) + + # create the audit log + aud_file = "/var/log/ONAP/pmsh/logs/audit.log" + open(aud_file, "a").close() # this is like "touch" + global _AUDIT_LOGGER + _AUDIT_LOGGER = _create_logger("pmsh_service_audit", aud_file) + + # create the error log + err_file = "/var/log/ONAP/pmsh/logs/error.log" + open(err_file, "a").close() # this is like "touch" + global _ERROR_LOGGER + _ERROR_LOGGER = _create_logger("pmsh_service_error", err_file) + + # create the metrics log + met_file = "/var/log/ONAP/pmsh/logs/metrics.log" + open(met_file, "a").close() # this is like "touch" + global _METRICS_LOGGER + _METRICS_LOGGER = _create_logger("pmsh_service_metrics", met_file) + + # create the debug log + debug_file = "/var/log/ONAP/pmsh/logs/debug.log" + open(debug_file, "a").close() # this is like "touch" + global _DEBUG_LOGGER + _DEBUG_LOGGER = _create_logger("pmsh_service_debug", debug_file) + + +def utc(): + """gets current time in utc""" + return datetime.datetime.utcnow() + + +def debug(msg="n/a"): + """ + This can be extended/modified to suit pmsh needs + """ + ets = utc() + + _DEBUG_LOGGER.info( + "{ets}|{msg}".format( + ets=ets.isoformat(), + msg=msg, + ) + ) + + +""" +!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +These loggers can be modified to suit the pmsh functionality +""" + + +def audit(raw_request, bts, xer, rcode, calling_mod, msg="n/a"): + """ + write an EELF error record per + 'https://wiki.onap.org/download/attachments/1015849/ + ONAP%20application%20logging%20guidelines.pdf?api=v2' + + %The audit fields implemented: + + 1 BeginTimestamp Implemented (bts) + 2 EndTimestamp Auto Injected when this is called + 3 RequestID Implemented (xer) + 5 threadId n/a + 7 serviceName Implemented (from Req) + 9 StatusCode Auto injected based on rcode + 10 ResponseCode Implemented (rcode) + 13 Category log level - all audit records are INFO. + 15 Server IP address Implemented (from Req) + 16 ElapsedTime Auto Injected (milliseconds) + 17 Server This is running in a Docker container so this is + not applicable, my HOSTNAME is always + "config_binding_service" + 18 ClientIPaddress Implemented (from Req) + 19 class name Implemented (mod), though docs say OOP, + I am using the python module here + 20 Unused ...implemented.... + 21-25 Custom n/a + 26 detailMessage Implemented (msg) + + Not implemented + 4 serviceInstanceID - ? + 6 physical/virtual server name (Optional) + 8 PartnerName - nothing in the request tells me this + 11 Response Description - the CBS follows standard HTTP + error codes so look them up + 12 instanceUUID - Optional + 14 Severity (Optional) + """ + ets = utc() + + _AUDIT_LOGGER.info( + "{bts}|{ets}|{xer}||n/a||{path}||{status}|{rcode}|||INFO||{servip}|{et}|" + "config_binding_service|{clientip}|{calling_mod}|||||||{msg}".format( + bts=bts.isoformat(), + ets=ets.isoformat(), + xer=xer, + rcode=rcode, + path=raw_request.path.split("/")[1], + status="COMPLETE" if rcode < 400 else "ERROR", + servip=raw_request.host.split(":")[0], + et=int((ets - bts).microseconds / 1000), + clientip=raw_request.remote_addr, + calling_mod=calling_mod, + msg=msg, + ) + ) + + +def error(raw_request, xer, severity, ecode, tgt_entity="n/a", + tgt_path="n/a", msg="n/a", adv_msg="n/a"): + """ + write an EELF error record per + 'https://wiki.onap.org/download/attachments/1015849/ + ONAP%20application%20logging%20guidelines.pdf?api=v2' + + the error fields implemented: + + 1 Timestamp Auto Injected when this is called + 2 RequestID Implemented (xer) + 3 ThreadID n/a + 4 ServiceName Implemented (from Req) + 6 TargetEntity Implemented (tgt_entity) + 7 TargetServiceName Implemented (tgt_path)/ + 8 ErrorCategory Implemented (severity) + 9. ErrorCode Implemented (ecode) + 10 ErrorDescription Implemented (msg) + 11. detailMessage Implemented (adv_msg) + + Not implemented: + 5 PartnerName - nothing in the request tells me this + """ + ets = utc() + + _ERROR_LOGGER.error( + "{ets}|{xer}|n/a|{path}||{tge}|{tgp}|{sev}|{ecode}|{msg}|{amsg}" + .format( + ets=ets, + xer=xer, + path=raw_request.path.split("/")[1], + tge=tgt_entity, + tgp=tgt_path, + sev=severity, + ecode=ecode, + msg=msg, + amsg=adv_msg, + ) + ) + + +def metrics(raw_request, bts, xer, target, target_path, rcode, + calling_mod, msg="n/a"): + """ + write an EELF error record per + 'https://wiki.onap.org/download/attachments/1015849/ + ONAP%20application%20logging%20guidelines.pdf?api=v2' + + %The metrics fields implemented: + + 1 BeginTimestamp Implemented (bts) + 2 EndTimestamp Auto Injected when this is called + 3 RequestID Implemented (xer) + 5 threadId n/a + 7 serviceName Implemented (from Req) + 9 TargetEntity Implemented (target) + 10 TargetServiceName Implemented (target_path) + 11 StatusCode Implemented (based on rcode) + 12 Response Code Implemented (rcode) + 15 Category log level all metrics records are INFO. + 17 Server IP address Implemented (from Req) + 18 ElapsedTime Auto Injected (milliseconds) + 19 Server This is running in a Docker container so this is + not applicable, my HOSTNAME is always + "config_binding_service" + 20 ClientIPaddress Implemented (from Req) + 21 class name Implemented (mod), though docs say OOP, + I am using the python module here + 22 Unused ...implemented.... + 24 TargetVirtualEntity n/a + 25-28 Custom n/a + 29 detailMessage Implemented (msg) + + Not implemented + 4 serviceInstanceID - ? + 6 physical/virtual server name (Optional) + 8 PartnerName - nothing in the request tells me this + 13 Response Description - the CBS follows standard HTTP error + codes so look them up + 14 instanceUUID - Optional + 16 Severity (Optional) + 23 ProcessKey - optional + """ + ets = utc() + + _METRICS_LOGGER.info( + "{bts}|{ets}|{xer}||n/a||{path}||{tge}|{tgp}|{status}|{rcode}|||INFO||{servip}|" + "{et}|config_binding_service|{clientip}|{calling_mod}|||n/a|||||{msg}" + .format( + bts=bts.isoformat(), + ets=ets.isoformat(), + xer=xer, + path=raw_request.path.split("/")[1], + tge=target, + tgp=target_path, + status="COMPLETE" if rcode < 400 else "ERROR", + rcode=rcode, + servip=raw_request.host.split(":")[0], + et=int((ets - bts).microseconds / 1000), + clientip=raw_request.remote_addr, + calling_mod=calling_mod, + msg=msg, + ) + ) diff --git a/components/pm-subscription-handler/pmsh_service/pmsh_service.py b/components/pm-subscription-handler/pmsh_service/pmsh_service.py new file mode 100755 index 00000000..6764c75f --- /dev/null +++ b/components/pm-subscription-handler/pmsh_service/pmsh_service.py @@ -0,0 +1,35 @@ +# ============LICENSE_START=================================================== +# Copyright (C) 2019 Nordix Foundation. +# ============================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END===================================================== + +import os +import time + +import mod.pmsh_logging as logger + + +def main(): + """Entrypoint""" + if "PROD_LOGGING" in os.environ: + logger.create_loggers() + while True: + time.sleep(30) + logger.debug("Ni! Ni! Ni!") + + +if __name__ == '__main__': + main() diff --git a/components/pm-subscription-handler/pom.xml b/components/pm-subscription-handler/pom.xml new file mode 100644 index 00000000..a4258060 --- /dev/null +++ b/components/pm-subscription-handler/pom.xml @@ -0,0 +1,245 @@ + + + + 4.0.0 + + + org.onap.oparent + oparent + 2.1.0 + + + org.onap.dcaegen2.services + pmsh + dcaegen2-services-pm-subscription-handler + 1.0.0-SNAPSHOT + + UTF-8 + . + xunit-results.xml + coverage.xml + xunit-results.xml + py + python + **/**.py + target/**,tests/**,setup.py,**/__init__.py + 1.2.1 + + + ${project.artifactId}-${project.version} + + + + + org.sonatype.plugins + nexus-staging-maven-plugin + + true + + + + org.apache.maven.plugins + maven-deploy-plugin + + + true + + + + org.apache.maven.plugins + maven-enforcer-plugin + + true + + + + + + org.apache.maven.plugins + maven-resources-plugin + + true + + + + + org.apache.maven.plugins + maven-compiler-plugin + + true + + + + + org.apache.maven.plugins + maven-jar-plugin + + + default-jar + + + + + + org.apache.maven.plugins + maven-install-plugin + + true + + + + + org.apache.maven.plugins + maven-surefire-plugin + + true + + + + org.codehaus.mojo + exec-maven-plugin + ${exec-mvn-plugin-version} + + ${project.basedir}/mvn-phase-script.sh + + + ${project.groupId} + ${project.artifactId} + ${project.version} + ${onap.nexus.url} + ${onap.nexus.rawrepo.baseurl.upload} + ${onap.nexus.rawrepo.baseurl.download} + ${onap.nexus.rawrepo.serverid} + ${onap.nexus.dockerregistry.daily} + ${onap.nexus.dockerregistry.daily} + ${onap.nexus.dockerregistry.release} + + + + + + + + + org.codehaus.mojo + exec-maven-plugin + ${exec-mvn-plugin-version} + + + clean phase script + clean + + exec + + + + __ + clean + + + + + generate-sources script + generate-sources + + exec + + + + __ + generate-sources + + + + + compile script + compile + + exec + + + + __ + compile + + + + + package script + package + + exec + + + + __ + package + + + + + test script + test + + exec + + + + __ + test + + + + + install script + install + + exec + + + + __ + install + + + + + deploy script + deploy + + exec + + + + __ + deploy + + + + + + + + diff --git a/components/pm-subscription-handler/requirements.txt b/components/pm-subscription-handler/requirements.txt new file mode 100644 index 00000000..a60bdc68 --- /dev/null +++ b/components/pm-subscription-handler/requirements.txt @@ -0,0 +1,9 @@ + # requirements.txt + # + # installs dependencies from ./setup.py, and the package itself, + # in editable mode + # -e . + + # (the -e above is optional). you could also just install the package + # normally with just the line below (after uncommenting) + . diff --git a/components/pm-subscription-handler/setup.py b/components/pm-subscription-handler/setup.py new file mode 100644 index 00000000..9012a04c --- /dev/null +++ b/components/pm-subscription-handler/setup.py @@ -0,0 +1,35 @@ +# ============LICENSE_START======================================================= +# Copyright (C) 2019 Nordix Foundation. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END========================================================= + +from setuptools import setup, find_packages + +setup( + name="pm_subscription_handler", + version="1.0.0", + packages=find_packages(), + author="lego@est.tech", + author_email="lego@est.tech", + license='Apache 2', + description="Service to handle PM subscriptions", + url="https://gerrit.onap.org/r/#/admin/projects/dcaegen2/services", + python_requires='>=3', + install_requires=[ + "requests==2.22.0", + "aiohttp==3.6.2", + "onappylog==1.0.9"], +) diff --git a/components/pm-subscription-handler/tests/__init__.py b/components/pm-subscription-handler/tests/__init__.py new file mode 100644 index 00000000..d2f6f2fd --- /dev/null +++ b/components/pm-subscription-handler/tests/__init__.py @@ -0,0 +1,21 @@ +# ============LICENSE_START=================================================== +# Copyright (C) 2019 Nordix Foundation. +# ============================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END===================================================== + + +# empty __init__.py so that pytest can add correct path to coverage report, +# -- per pytest best practice guideline diff --git a/components/pm-subscription-handler/tests/test_logging.py b/components/pm-subscription-handler/tests/test_logging.py new file mode 100644 index 00000000..790aa40c --- /dev/null +++ b/components/pm-subscription-handler/tests/test_logging.py @@ -0,0 +1,28 @@ +# ============LICENSE_START=================================================== +# Copyright (C) 2019 Nordix Foundation. +# ============================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END===================================================== + +import unittest + + +class MyTestCase(unittest.TestCase): + def test_something(self): + self.assertEqual(True, True) + + +if __name__ == '__main__': + unittest.main() diff --git a/components/pm-subscription-handler/tox.ini b/components/pm-subscription-handler/tox.ini new file mode 100644 index 00000000..fcbb647c --- /dev/null +++ b/components/pm-subscription-handler/tox.ini @@ -0,0 +1,41 @@ +# ============LICENSE_START======================================================= +# Copyright (C) 2019 Nordix Foundation. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END========================================================= + +[tox] +envlist = py36,py37,flake8 +skip_missing_interpreters = true + +[testenv] +deps= + -rrequirements.txt + pytest + coverage + pytest-cov +setenv = + PYTHONPATH={toxinidir}/pmsh_service:{toxinidir}/pmsh_service/mod:{toxinidir}/tests +commands= + pytest --junitxml xunit-results.xml --cov pmsh_service --cov-report xml --cov-report term tests --verbose --cov-fail-under=0 + +[testenv:flake8] +basepython = python3 +skip_install = true +deps = flake8 +commands = flake8 pmsh_service tests + +[flake8] +max-line-length=100 diff --git a/components/pm-subscription-handler/version.properties b/components/pm-subscription-handler/version.properties new file mode 100644 index 00000000..2ddebb39 --- /dev/null +++ b/components/pm-subscription-handler/version.properties @@ -0,0 +1,6 @@ +major=1 +minor=0 +patch=0 +base_version=${major}.${minor}.${patch} +release_version=${base_version} +snapshot_version=${base_version}-SNAPSHOT -- cgit 1.2.3-korg