Revert "Integrate NBI and Prometheus"

This reverts commit 38f5d5834d610e011b3608fe8cc34b927775204a.

Reason for revert: Grafana-k8s requires Juju 3.1 and we are not ready
to change Juju versions at this time

Change-Id: I90dbb6e1c2921289ecaabfa636878763f981c3e1
Signed-off-by: Mark Beierl <mark.beierl@canonical.com>
diff --git a/installers/charm/osm-nbi/src/charm.py b/installers/charm/osm-nbi/src/charm.py
index 4a0bbf1..b19beae 100755
--- a/installers/charm/osm-nbi/src/charm.py
+++ b/installers/charm/osm-nbi/src/charm.py
@@ -29,7 +29,6 @@
 
 import logging
 from typing import Any, Dict
-from urllib.parse import urlparse
 
 from charms.data_platform_libs.v0.data_interfaces import DatabaseRequires
 from charms.kafka_k8s.v0.kafka import KafkaEvents, KafkaRequires
@@ -49,7 +48,7 @@
 from ops.main import main
 from ops.model import ActiveStatus, Container
 
-from legacy_interfaces import KeystoneClient
+from legacy_interfaces import KeystoneClient, PrometheusClient
 
 HOSTPATHS = [
     HostPath(
@@ -87,6 +86,7 @@
         self.mongodb_client = DatabaseRequires(
             self, "mongodb", database_name="osm", extra_user_roles="admin"
         )
+        self.prometheus_client = PrometheusClient(self, "prometheus")
         self.keystone_client = KeystoneClient(self, "keystone")
         self._observe_charm_events()
         self.container: Container = self.unit.get_container("nbi")
@@ -129,7 +129,6 @@
     def _on_update_status(self, _=None) -> None:
         """Handler for the update-status event."""
         try:
-            self._validate_config()
             self._check_relations()
             if self.debug_mode.started:
                 return
@@ -185,12 +184,13 @@
             self.on["kafka"].relation_broken: self._on_required_relation_broken,
             self.mongodb_client.on.database_created: self._on_config_changed,
             self.on["mongodb"].relation_broken: self._on_required_relation_broken,
-            self.on["keystone"].relation_changed: self._on_config_changed,
-            self.on["keystone"].relation_broken: self._on_required_relation_broken,
             # Action events
             self.on.get_debug_mode_information_action: self._on_get_debug_mode_information_action,
             self.on.nbi_relation_joined: self._update_nbi_relation,
         }
+        for relation in [self.on[rel_name] for rel_name in ["prometheus", "keystone"]]:
+            event_handler_mapping[relation.relation_changed] = self._on_config_changed
+            event_handler_mapping[relation.relation_broken] = self._on_required_relation_broken
 
         for event, handler in event_handler_mapping.items():
             self.framework.observe(event, handler)
@@ -208,14 +208,6 @@
             CharmError: if charm configuration is invalid.
         """
         logger.debug("validating charm config")
-        url = self.config.get("prometheus-url")
-        if not url:
-            raise CharmError("need prometheus-url config")
-        if not self._is_valid_url(url):
-            raise CharmError(f"Invalid value for prometheus-url config: '{url}'")
-
-    def _is_valid_url(self, url) -> bool:
-        return urlparse(url).hostname is not None
 
     def _check_relations(self) -> None:
         """Validate charm relations.
@@ -230,6 +222,8 @@
             missing_relations.append("kafka")
         if not self._is_database_available():
             missing_relations.append("mongodb")
+        if self.prometheus_client.is_missing_data_in_app():
+            missing_relations.append("prometheus")
         if self.keystone_client.is_missing_data_in_app():
             missing_relations.append("keystone")
 
@@ -259,7 +253,6 @@
 
     def _get_layer(self) -> Dict[str, Any]:
         """Get layer for Pebble."""
-        prometheus = urlparse(self.config["prometheus-url"])
         return {
             "summary": "nbi layer",
             "description": "pebble config layer for nbi",
@@ -290,8 +283,8 @@
                         "OSMNBI_STORAGE_COLLECTION": "files",
                         "OSMNBI_STORAGE_URI": self._get_mongodb_uri(),
                         # Prometheus configuration
-                        "OSMNBI_PROMETHEUS_HOST": prometheus.hostname,
-                        "OSMNBI_PROMETHEUS_PORT": prometheus.port if prometheus.port else "",
+                        "OSMNBI_PROMETHEUS_HOST": self.prometheus_client.hostname,
+                        "OSMNBI_PROMETHEUS_PORT": self.prometheus_client.port,
                         # Log configuration
                         "OSMNBI_LOG_LEVEL": self.config["log-level"],
                         # Authentication environments
diff --git a/installers/charm/osm-nbi/src/legacy_interfaces.py b/installers/charm/osm-nbi/src/legacy_interfaces.py
index 4750cff..5deb3f5 100644
--- a/installers/charm/osm-nbi/src/legacy_interfaces.py
+++ b/installers/charm/osm-nbi/src/legacy_interfaces.py
@@ -141,3 +141,65 @@
     @property
     def admin_project_name(self):
         return self.get_data_from_app("admin_project_name")
+
+
+class MongoClient(BaseRelationClient):
+    """Requires side of a Mongo Endpoint"""
+
+    mandatory_fields_mapping = {
+        "reactive": ["connection_string"],
+        "ops": ["replica_set_uri", "replica_set_name"],
+    }
+
+    def __init__(self, charm: ops.charm.CharmBase, relation_name: str):
+        super().__init__(charm, relation_name, mandatory_fields=[])
+
+    @property
+    def connection_string(self):
+        if self.is_opts():
+            replica_set_uri = self.get_data_from_unit("replica_set_uri")
+            replica_set_name = self.get_data_from_unit("replica_set_name")
+            return f"{replica_set_uri}?replicaSet={replica_set_name}"
+        else:
+            return self.get_data_from_unit("connection_string")
+
+    def is_opts(self):
+        return not self.is_missing_data_in_unit_ops()
+
+    def is_missing_data_in_unit(self):
+        return self.is_missing_data_in_unit_ops() and self.is_missing_data_in_unit_reactive()
+
+    def is_missing_data_in_unit_ops(self):
+        return not all(
+            [self.get_data_from_unit(field) for field in self.mandatory_fields_mapping["ops"]]
+        )
+
+    def is_missing_data_in_unit_reactive(self):
+        return not all(
+            [self.get_data_from_unit(field) for field in self.mandatory_fields_mapping["reactive"]]
+        )
+
+
+class PrometheusClient(BaseRelationClient):
+    """Requires side of a Prometheus Endpoint"""
+
+    mandatory_fields = ["hostname", "port"]
+
+    def __init__(self, charm: ops.charm.CharmBase, relation_name: str):
+        super().__init__(charm, relation_name, self.mandatory_fields)
+
+    @property
+    def hostname(self):
+        return self.get_data_from_app("hostname")
+
+    @property
+    def port(self):
+        return self.get_data_from_app("port")
+
+    @property
+    def user(self):
+        return self.get_data_from_app("user")
+
+    @property
+    def password(self):
+        return self.get_data_from_app("password")