Add metrics API
Defines MetricsData, MetricsReport and MetricsManager classes
so client xApps can generate and send reports via RMR.
Issue-ID: RIC-381
Signed-off-by: Lott, Christopher (cl778h) <cl778h@att.com>
Change-Id: I46890db2e9fedd007412cb480f592d059687ccf8
diff --git a/tests/fixtures/test_local.rt b/tests/fixtures/test_local.rt
index f9d14ce..b821a51 100644
--- a/tests/fixtures/test_local.rt
+++ b/tests/fixtures/test_local.rt
@@ -1,12 +1,13 @@
# do NOT use localhost, seems unresolved on jenkins VMs
-# first 3 lines (port 4564) are used for xapp frame tests
-# next 4 lines (port 3563-3564) are used in the rmr submodule
+# first 4 lines (ports 4564, 4569) are used for xapp frame tests
+# last 4 lines (port 3563, 3564) are used in the rmr submodule
newrt|start
-mse| 60000 | -1 | 127.0.0.1:4564
-mse| 60001 | -1 | 127.0.0.1:4564
-mse| 100 | -1 | 127.0.0.1:4564
-mse| 0 | -1 | 127.0.0.1:3563
+mse| 60000 | -1 | 127.0.0.1:4564
+mse| 60001 | -1 | 127.0.0.1:4564
+mse| 100 | -1 | 127.0.0.1:4564
+mse| 120 | -1 | 127.0.0.1:4569
+mse| 0 | -1 | 127.0.0.1:3563
mse| 46656 | 777 | 127.0.0.1:3563
-mse| 1 | -1 | 127.0.0.1:3564
-mse| 2 | -1 | 127.0.0.1:3564
+mse| 1 | -1 | 127.0.0.1:3564
+mse| 2 | -1 | 127.0.0.1:3564
newrt|end
diff --git a/tests/test_alarm.py b/tests/test_alarm.py
index 0cde0ce..df8b2d4 100644
--- a/tests/test_alarm.py
+++ b/tests/test_alarm.py
@@ -47,6 +47,7 @@
test alarm module teardown
"""
rmr.rmr_close(MRC_SEND)
+ rmr.rmr_close(MRC_RCV)
def test_alarm_set_get(monkeypatch):
diff --git a/tests/test_metric.py b/tests/test_metric.py
new file mode 100644
index 0000000..a40c978
--- /dev/null
+++ b/tests/test_metric.py
@@ -0,0 +1,115 @@
+# =================================================================================2
+# Copyright (c) 2020 AT&T Intellectual Property.
+# Copyright (c) 2020 Nokia
+#
+# 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.
+# ==================================================================================
+import json
+import pytest
+import time
+from mdclogpy import Logger
+from ricxappframe.metric import metric
+from ricxappframe.metric.exceptions import EmptyReport
+from ricxappframe.metric.metric import MetricData, MetricsReport, MetricsManager
+from ricxappframe.rmr import rmr
+
+mdc_logger = Logger(name=__name__)
+MRC_SEND = None
+MRC_RCV = None
+SIZE = 256
+
+
+def setup_module():
+ """
+ test metric module setup
+ """
+ global MRC_SEND
+ MRC_SEND = rmr.rmr_init(b"4568", rmr.RMR_MAX_RCV_BYTES, 0x00)
+ while rmr.rmr_ready(MRC_SEND) == 0:
+ time.sleep(1)
+
+ global MRC_RCV
+ MRC_RCV = rmr.rmr_init(b"4569", rmr.RMR_MAX_RCV_BYTES, 0x00)
+ while rmr.rmr_ready(MRC_RCV) == 0:
+ time.sleep(1)
+
+ # let all the RMR output appear
+ time.sleep(2)
+ mdc_logger.debug("RMR instances initialized")
+
+
+def teardown_module():
+ """
+ test metric module teardown
+ """
+ mdc_logger.debug("Closing RMR instances")
+ rmr.rmr_close(MRC_SEND)
+ rmr.rmr_close(MRC_RCV)
+
+
+def test_metric_set_get(monkeypatch):
+ mdc_logger.debug("Testing set functions")
+ md = MetricData("id", "value", "type")
+ assert md[metric.KEY_DATA_ID] == "id"
+ assert md[metric.KEY_DATA_VALUE] == "value"
+ assert md[metric.KEY_DATA_TYPE] == "type"
+
+ mr = MetricsReport("reporter", "generator", [md])
+ assert mr[metric.KEY_REPORTER] == "reporter"
+ assert mr[metric.KEY_GENERATOR] == "generator"
+ assert len(mr[metric.KEY_DATA]) == 1
+ assert mr[metric.KEY_DATA][0] == md
+
+ mgr = metric.MetricsManager(MRC_SEND, "reporter", "generator")
+ assert mgr is not None
+ assert mgr.reporter == "reporter"
+ assert mgr.generator == "generator"
+
+ mr = MetricsReport("empty", "empty", [])
+ with pytest.raises(EmptyReport):
+ mgr.send_report(mr)
+
+
+def _receive_metric_rpt(rptr: str):
+ """
+ delays briefly, receives a message, checks the message type and reporter
+ """
+ time.sleep(0.5)
+ sbuf_rcv = rmr.rmr_alloc_msg(MRC_RCV, SIZE)
+ sbuf_rcv = rmr.rmr_torcv_msg(MRC_RCV, sbuf_rcv, 2000)
+ rcv_summary = rmr.message_summary(sbuf_rcv)
+ mdc_logger.debug("Receive result is {}".format(rcv_summary[rmr.RMR_MS_MSG_STATE]))
+ assert rcv_summary[rmr.RMR_MS_MSG_STATE] == rmr.RMR_OK
+ assert rcv_summary[rmr.RMR_MS_MSG_TYPE] == metric.RIC_METRICS
+ # parse JSON
+ data = json.loads(rcv_summary[rmr.RMR_MS_PAYLOAD].decode())
+ mdc_logger.debug("Received reporter {}".format(data[metric.KEY_REPORTER]))
+ assert data[metric.KEY_REPORTER] == rptr
+
+
+def test_metrics_manager():
+ """
+ test send functions and ensure a message arrives
+ """
+ mdc_logger.debug("Testing metrics-send functions")
+ rptr = "metr"
+
+ mgr = MetricsManager(MRC_SEND, rptr, "generator")
+ assert mgr is not None
+
+ md = MetricData("id", "value", "type")
+ assert md is not None
+
+ success = mgr.send_metrics([md])
+ assert success
+ _receive_metric_rpt(rptr)